Extraer hechos de historiales de conversación
¡Hora de mejorar tu historial de conversación! Definirás clases de pydantic para extraer hechos de historiales de conversación de forma estructurada.
Esto sentará las bases para el siguiente y último ejercicio del curso, donde realizarás la extracción realmente.
Las clases necesarias de pydantic ya se han importado y ya se ha definido un llm.
Este ejercicio forma parte del curso
Graph RAG con LangChain y Neo4j
Instrucciones del ejercicio
- Define una clase
ConversationFactdepydanticpara extraer hechos de una conversación; incluye los camposobject,subject,relationshipysession_idque coincidan con las descripciones proporcionadas. - Define una clase
ConversationFactsdepydanticpara crear listas de objetosConversationFact. - Vincula el formato de salida estructurada al
llmproporcionado.
ejercicio interactivo práctico
Prueba este ejercicio completando este código de ejemplo.
# 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.____(____)