Bắt đầu ngayBắt đầu miễn phí

Trích xuất sự kiện từ lịch sử hội thoại

Giờ là lúc nâng cấp lịch sử hội thoại của bạn! Bạn sẽ định nghĩa các lớp pydantic để trích xuất các sự kiện từ lịch sử hội thoại theo cách có cấu trúc.

Điều này sẽ đặt nền móng cho bài tập tiếp theo và cũng là bài cuối của khóa học, nơi bạn sẽ thực hiện việc trích xuất thực tế.

Các lớp pydantic cần thiết đã được import, và một llm cũng đã được định nghĩa.

Bài tập này là một phần của khóa học

Graph RAG với LangChain và Neo4j

Xem khóa học

Hướng dẫn bài tập

  • Định nghĩa lớp pydantic ConversationFact để trích xuất sự kiện từ một cuộc hội thoại; bao gồm các trường object, subject, relationship, và session_id khớp với các mô tả đã cung cấp.
  • Định nghĩa lớp pydantic ConversationFacts để tạo danh sách các đối tượng ConversationFact.
  • Gắn định dạng đầu ra có cấu trúc vào llm đã cho.

Bài tập tương tác thực hành trực tiếp

Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.

# 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.____(____) 
Chỉnh sửa và Chạy Mã