用记忆组织聊天机器人输出

使用 LangChain 设计 Agentic 系统

Dilini K. Sumanapala, PhD

Founder & AI Engineer, Genverv, Ltd.

流式输出多个工具的结果

    完整图形工作流。

 

  • 使用多工具打印输出
      - Agent: [工具 1:答案]
      - Agent: [工具 2:答案]
    
  • 打印用户查询并启用记忆
      - User: [查询]
      - Agent: [工具 1:答案]
    
      - User: [追问]
      - Agent: [工具 1:追问答案]
    
使用 LangChain 设计 Agentic 系统

流式输出多个工具的结果

from langchain_core.messages import AIMessage, HumanMessage


config = {"configurable": {"thread_id": "1"}}
# 使用用户查询创建输入消息 def multi_tool_output(query):
inputs = {"messages": [HumanMessage(content=query)]}
# 从聊天机器人应用流式获取消息和元数据 for msg, metadata in app.stream(inputs, config, stream_mode="messages"):
# 若消息有内容且不是来自人类,则打印 if msg.content and not isinstance(msg, HumanMessage): print(msg.content, end="", flush=True)
print("\n")
使用 LangChain 设计 Agentic 系统

使用多工具进行测试

检查动态工具分配
multi_tool_output("Is `Stella won no wallets` a palindrome?")

multi_tool_output("What happened on April 12th, 1955?")
短语 'Stella won no wallets' 是回文。

是的,短语 "Stella won no wallets" 是回文。
1955 年 4 月 12 日在科学与技术领域具有多重意义。这一天,乔纳斯·索尔克博士开发的首个成功的小儿麻痹症疫苗向公众公布。这一疫苗是抗击脊髓灰质炎的重大突破,该疾病曾引发广泛恐慌与多次暴发,尤其影响儿童。此公告成为公共卫生的转折点,并促成了广泛的疫苗接种运动,最终几乎消灭了脊灰……
使用 LangChain 设计 Agentic 系统

使用多工具处理追问

# 在每次交互中先打印用户查询
def user_agent_multiturn(queries):
    for query in queries:
        print(f"User: {query}")


# 流式处理与查询对应的消息,排除元数据 print("Agent: " + "".join(msg.content for msg, metadata in app.stream( {"messages": [HumanMessage(content=query)]}, config, stream_mode="messages")
# 过滤掉人类消息,仅打印代理消息 if msg.content and not isinstance(msg, HumanMessage)) + "\n")
queries = ["What happened on the 12 April 1961?", "What about 10 December 1948?", "Is `Mr. Owl ate my metal worm?` a palindrome?", "What about 'palladium stadium?'"] user_agent_multiturn(queries)
使用 LangChain 设计 Agentic 系统

完整对话输出

  • 历史事件
User: 1961 年 4 月 12 日发生了什么?
Agent: 1961 年 4 月 12 日,苏联宇航员尤里·加加林乘坐"东方一号"成为首位进入太空的人……
User: 那 1963 年 11 月 22 日呢?

Agent: 1948 年 12 月 10 日……《世界人权宣言》(UDHR)发表。
  • 回文检查

User: "Mr. Owl ate my metal worm?" 是回文吗?
Agent: 词语或短语 'Mr. Owl ate my metal worm?' 是回文……
User: 那 "palladium stadium" 呢?

Agent: 不是,短语 `palladium stadium` 不是回文……
使用 LangChain 设计 Agentic 系统

Passons à la pratique !

使用 LangChain 设计 Agentic 系统

Preparing Video For Download...