会話履歴から事実を抽出する
それでは会話履歴をさらに強化しましょう。会話履歴から事実を構造化して抽出するための pydantic クラスを定義します。
これは、次の最終演習で実際に抽出処理を行うための下地づくりになります。
必要な pydantic クラスはすでにインポート済みで、llm も定義済みです。
この演習はコースの一部です
Graph RAG with LangChain and Neo4j
演習の手順
- 会話から事実を抽出するための
pydantic製ConversationFactクラスを定義します。object、subject、relationship、session_idの各フィールドを、与えられた説明に合うように含めてください。 ConversationFactオブジェクトのリストを作成するためのpydantic製ConversationFactsクラスを定義します。- 提供された
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.____(____)