開始使用免費開始

從對話歷程中擷取事實

現在來升級你的對話歷程吧!你將定義 pydantic 類別,用結構化的方式從對話歷程中擷取事實。

這會為下一個、也是本課程最後一個練習打好基礎,屆時你會實際執行擷取。

必要的 pydantic 類別已經匯入,llm 也已經定義好了。

本練習屬於課程

使用 LangChain 與 Neo4j 的 Graph RAG

檢視課程

練習說明

  • 定義一個 pydanticConversationFact 類別,用於從對話中擷取事實;包含 objectsubjectrelationshipsession_id 欄位,並與提供的描述相符。
  • 定義一個 pydanticConversationFacts 類別,用於建立 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.____(____) 
編輯並執行程式碼