开始使用免费开始使用

使用提取的对话事实

现在来应用带结构化输出的 LLM 吧!您在上一个练习中创建的 llm_with_output 仍然可用,且该 LLM 的提示模板已为您提供。

本练习是课程的一部分

使用 LangChain 和 Neo4j 的 Graph RAG

查看课程

练习说明

  • 将提供的聊天提示模板通过管道传递给带结构化输出的 LLM(llm_with_output)。
  • 在给定的用户与会话 ID 上调用该链,并传入历史记录(history)中的 messages。

交互式实操练习

通过完成这段示例代码来试试这个练习。

chain = ChatPromptTemplate.from_messages([
    SystemMessagePromptTemplate.from_template("""
    	You are talking to {user}, the user of the application. 
        Any facts in the first person relate to {user}.
        The Session ID is {session_id}.
    	Extract the facts from the conversation and return them in the format of object, subject, predicate.
    """),
    # Pipe the chat prompt template into the LLM with structured output
    MessagesPlaceholder(variable_name="history")
]) | ____

# Invoke the chain, passing it the messages from the history
chain.invoke({"user": USER, "session_id": SESSION_ID, "history": ____})
编辑并运行代码