从会话历史中抽取事实
现在让您的会话历史更上一层楼!您将定义用于以结构化方式从会话历史中抽取事实的 pydantic 类。
这会为下一道也是本课程最后一道练习打下基础,届时您将实际执行抽取。
所需的 pydantic 类已经导入,并且已定义了一个 llm。
本练习是课程的一部分
使用 LangChain 和 Neo4j 的 Graph RAG
练习说明
- 定义一个
pydantic的ConversationFact类,用于从会话中抽取事实;包含与说明相匹配的字段object、subject、relationship和session_id。 - 定义一个
pydantic的ConversationFacts类,用于创建ConversationFact对象列表。 - 将结构化输出格式绑定到提供的
llm。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Define the ConversationFact with the correct fields
class ____(____):
"""
A class that holds the facts from a conversation in a format of object, subject, predicate.
For example, if the conversation includes the fact that the user likes ice creamthe facts would be:
- object: "Adam"
- subject: "ice cream"
- relationship: "LIKES"
The class also includes a session ID to identify the conversation.
"""
____: str = Field(description="The session ID of the conversation.")
____: str = Field(description="The object of the fact. For example, 'Adam' ")
____: str = Field(description="The subject of the fact. For example, 'Ice cream'")
____: str = Field(description="The relationship between the object and the subject. This should be a single word verb in upper case. For example, 'LIKES' or 'OWNS'")
# Define a ConversationFacts class for creating lists of ConversationFact objects
class ____(____):
"""A class that holds a list of ConversationFact objects."""
facts: list[____] = Field(description="A list of ConversationFact objects.")
# Bind the output to the llm provided
llm_with_output = llm.____(____)