Generare risposte del chatbot

Progettare sistemi agentici con LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

Streaming degli eventi del grafo

   

  • Trasmetti eventi in tempo reale

  • Ogni evento è una fase del workflow

  • Tieni traccia di risposte e chiamate agli strumenti

  • Monitora l'avanzamento del chatbot

User: Where is the Congo?

Agent: Controllo alcuni dettagli per te...
Agent: AIMessage([Tool Call to Google Maps] Searching for information on "the Congo")
Agent: The Democratic Republic of Congo is a country in Central Africa.
Progettare sistemi agentici con LangChain

Streaming delle risposte LLM

# Definisci una funzione per eseguire il chatbot in base all'input utente
def stream_graph_updates(user_input: str):


# Avvia lo streaming degli eventi dal grafo con l'input dell'utente for event in graph.stream({"messages": [("user", user_input)]}):
# Recupera e stampa le risposte dei nodi del chatbot for value in event.values(): print("Agent:", value["messages"])
# Definisci la query dell'utente ed esegui il chatbot user_query = "Who is Mary Shelley?" stream_graph_updates(user_query)
Progettare sistemi agentici con LangChain

Streaming delle risposte LLM

   Agent: [AIMessage(content='Mary Shelley (1797-1851) was an English 
   novelist...

...best known for her groundbreaking work in the Gothic genre, particularly for her novel "Frankenstein; or, The Modern Prometheus," published in 1818. This novel is often considered one of the earliest examples of science fiction and explores themes of creation, responsibility, and the nature of humanity through the story of Victor Frankenstein, a scientist who creates a sentient creature in an unorthodox experiment...',
response_metadata={'finish_reason': 'stop', 'model_name': 'gpt-4o-mini-...')]
Progettare sistemi agentici con LangChain

LLM e allucinazioni

Agent: [AIMessage(content='Judith 
Love Cohen was an American aerospace 
engineer, and worked on various 
space missions, including the Apollo 
program... Cohen was also the mother 
of actor and writer Adam Cohen, 
who has spoken about her influence on 
his life and career. 
additional_kwargs={}, 
response_metadata={'finish_reason': 
'stop', 'model_name': 
'gpt-4o-mini-...)

   

Esempio di allucinazione

 

  • Il figlio famoso di Judith Love Cohen è Jack Black, non "Adam Cohen".
Progettare sistemi agentici con LangChain

Generare un diagramma LangGraph

# Importa i moduli per il diagramma del chatbot
from IPython.display import 
Image, display


# Prova a generare e visualizzare # il diagramma del grafo try: display(Image(graph.get_graph() .draw_mermaid_png()))
# Restituisci un'eccezione se necessario except Exception: print("Dipendenze aggiuntive richieste.")

Diagramma di base del chatbot LangGraph.

Progettare sistemi agentici con LangChain

Esercitiamoci!

Progettare sistemi agentici con LangChain

Preparing Video For Download...