生成聊天机器人回复

使用 LangChain 设计 Agentic 系统

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

流式图事件

   

  • 实时流式事件

  • 每个事件为一条工作流步骤

  • 跟踪回复与工具调用

  • 跟踪聊天机器人进度

User: Where is the Congo?

Agent: 我来为您查一些细节...
Agent: AIMessage([调用工具至 Google Maps] 正在搜索 "the Congo"的信息)
Agent: 刚果民主共和国位于 中非。
使用 LangChain 设计 Agentic 系统

流式传输 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)
使用 LangChain 设计 Agentic 系统

流式传输 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-...')]
使用 LangChain 设计 Agentic 系统

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"。
使用 LangChain 设计 Agentic 系统

生成 LangGraph 图示

# 导入用于聊天机器人图表的模块
from IPython.display import 
Image, display


# 尝试生成并显示 # 图表 try: display(Image(graph.get_graph() .draw_mermaid_png()))
# 如有需要,返回异常 except Exception: print("需要额外依赖项。")

基础聊天机器人 LangGraph 图示。

使用 LangChain 设计 Agentic 系统

Passons à la pratique !

使用 LangChain 设计 Agentic 系统

Preparing Video For Download...