產生聊天機器人回應

Designing Agentic Systems with LangChain

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

串流圖事件

   

  • 即時串流事件

  • 每個事件是工作流程步驟

  • 追蹤回應與工具呼叫

  • 追蹤聊天機器人進度

User: Where is the Congo?

Agent: Let me check some details for you...
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.
Designing Agentic Systems with LangChain

串流 LLM 回應

# 根據使用者輸入執行聊天機器人的函式
def stream_graph_updates(user_input: str):


# 以使用者輸入開始從圖中串流事件 for event in graph.stream({"messages": [("user", user_input)]}):
# 取得並印出聊天機器人節點的回應 for value in event.values(): print("Agent:", value["messages"])
# 定義使用者查詢並執行聊天機器人 user_query = "Who is Mary Shelley?" stream_graph_updates(user_query)
Designing Agentic Systems with LangChain

串流 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-...')]
Designing Agentic Systems with LangChain

LLM 與幻覺

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-...)

   

幻覺示例

 

  • Judith Love Cohen 的知名兒子是 Jack Black,而非「Adam Cohen」。
Designing Agentic Systems with LangChain

產生 LangGraph 圖表

# 匯入聊天機器人圖表所需模組
from IPython.display import 
Image, display


# 嘗試產生並顯示 # 圖形圖表 try: display(Image(graph.get_graph() .draw_mermaid_png()))
# 必要時回傳例外 except Exception: print("Additional dependencies required.")

基本聊天機器人 LangGraph 圖。

Designing Agentic Systems with LangChain

一起來練習吧!

Designing Agentic Systems with LangChain

Preparing Video For Download...