En versiones de langchain >= 1.0, se puede replicar este comportamiento con los graph (workflow de nodos) de langgraph
1- Agregamos las importaciones necesarias
import uuid
from pydantic import BaseModel, Field
from typing import Optional
from langchain_openai import ChatOpenAI
from langchain_core.messages import SystemMessage, HumanMessage, RemoveMessage
from langgraph.graph import MessagesState, StateGraph, START, END
from langgraph.checkpoint.memory import MemorySaver
from langgraph.store.memory import InMemoryStore
from langgraph.store.base import BaseStore
from langchain_core.runnables import RunnableConfig
2- Definimos un schema de pydantic que representara un data-contract cuyos atributos son las entidades que queremos preservar del usuario.
# This entity schema for structured output allows LLM return data in a specific format.
# Read docs: https://docs.langchain.com/oss/python/langchain/structured-output
class ExctractedUserEntities(BaseModel):
direction: Optional[str] = Field(None, description="The customer's phyisical adress")
phone_number: Optional[str] = Field(None, description="The customer's phone number")
waranty: Optional[str] = Field(None, description="The machine's warranty number")
sold_date: Optional[str] = Field(None, description="The date of purchase of machine by the customer")
employee_name: Optional[str] = Field(None, description="The name of the employee who attend served them'")
3- Inicializar instancias de LLM y extraction LLM
# model for chatbot answers and questions
llm = ChatOpenAI(
model="gpt-4o-mini",
temperature=0.2,
max_retries=3,
max_tokens=1000
)
# model extractor will returns outputs formatted to match the given schema.
# if `schema` is a pydantic class, then the model output will be a pydantic instance of that class
extractor_llm = llm.with_structured_output(schema=ExctractedUserEntities)
4- Definimos una function-node la cual sera un paso en nuestro workflow para extraer entidades desde cada mensaje enviado por el usuario.
# extraction node finds entities in messages and saves to store.
def extract_user_entities_node(state: MessagesState, config: RunnableConfig, *, store: BaseStore):
""" This node will finds entities in messages
and saves to store.
Args:
state: state of the current conversation, contains 'messages' history.
config: Configuration of the conversation, contains conversation metadata.
store: where the entities will be stored in this conversation, langgraph looks for the store registered in it's workflow and passes directly.
"""
user_id = config["configurable"].get("user_id", "default_user")
# Check if store exists
if store is None:
raise ValueError("Store is not initialized.")
# Analize only the last user message for new entities
last_message = state["messages"][-1].content
# invoke the extrator_llm with the last human message
extracted = extractor_llm.invoke(f"Extract information from this message {last_message}")
# if found entities, then save on the store under entities
if extracted:
data = {k: v for k, v in extracted.model_dump().items() if v is not None}
for key, value in data.items():
store.put(("entities", user_id), key, value)
# returns empty dict because store updates are side-effect
return {}
5- Definimos un function-node la cual sera otro paso en nuestro workflow para comunicarnos con el modelo en nuestro question/answering
def call_model_node(state: MessagesState, config: RunnableConfig, *, store: BaseStore):
""" This node will define the system prompt and
handle question/answering between user and LLM
Args:
state: state of the current conversation, contains 'messages' history.
config: Configuration of the conversation, contains conversation metadata.
store: where the entities will be stored in this conversation, langgraph looks for the store registered in it's workflow and passes directly.
"""
user_id = config["configurable"].get("user_id", "default_user")
# Search and retrieve all entities for this user
stored_items = store.search(("entities", user_id))
entities_str = "\n".join([f"- {item.key}: {item.value}" for item in stored_items])
if not entities_str:
entities_str = "There is not personalizated information yet."
system_instruction = f"""
You are a sales assistant for a photography studio.
You are only designed to (1) try to resolve issues with downloading edited photos that are pending delivery to customers, and if the customer is unable to do so, (2) add the shots (photos) that the customer chose to the editors' task queue.
If the customer cannot find the photos on the download page, then ask if they want to send the shots back for editing, which will take one week. If the customer wants to send the shots back for editing, they must leave their cell phone number and address.
You also have access to personalized information provided by the human in the Context section below.
You are here to help, always with the spark and character of someone born in Medellin, Colombia.
It is essential that you ask for the date of their photo session, their invoice number, and who attended to them. Always ask if the customer's problem or question has been resolved.
Context:
{entities_str}"""
# invoke the LLM with whole context with our system prompt with entities if found and the complete messages history
messages = [system_instruction] + state["messages"]
response = llm.invoke(messages)
return {"messages": [response]}
6- Creamos nuestro workflow the langgraph al cual enlazaremos con los nodos definidos arriba.
# Build and compile the graph
# store to handle entities memories
store = InMemoryStore()
# checkpointer to save each interaction between user and LLM
checkpointer = MemorySaver()
# init the workflow
workflow = StateGraph(MessagesState)
# Add our two defined nodes to the workflow
workflow.add_node("extract", extract_user_entities_node)
workflow.add_node("assistant", call_model_node)
# Define steps order of the workflow
# init -> extract -> assistant -> end
workflow.add_edge(START, "extract")
workflow.add_edge("extract", "assistant")
workflow.add_edge("assistant", END)
# Compile with checkpointer for history and store for entities
app = workflow.compile(checkpointer=checkpointer, store=store)
7- Definimos la metadata necesaria para nuestra conversacion
# The user_id ensures the address/phone persists across different conversations
config = {
"configurable": {
"thread_id": "x1s343",
"user_id": "user_001"
}
}
8- desde este punto podemos enviar mensajes a nuestro chatbot assistente, repetiremos el mismo codigo con diferentes mensajes para ir viendo las respuestas y asi reformular nuestro siguiente mensaje como mejor nos convenga
msg_1 = HumanMessage(content="Hi, My name is Evert Escalante, I would like support, I cant find my photos on your company url to download them")
for chunk in app.stream({"messages": [msg_1]}, config, stream_mode="values"):
if chunk["messages"]:
print(f"Assistant: {chunk['messages'][-1].content}\n")
mensaje 2:
msg_2 = HumanMessage(content="it was on january 27th, invoice_number is 23332334 and Yurley Munioz attends me")
for chunk in app.stream({"messages": [msg_2]}, config, stream_mode="values"):
if chunk["messages"]:
print(f"Assistant: {chunk['messages'][-1].content}\n")
Si quieren ver las entidades guardadas
all_entities = store.search(("entities", "user_001"))
for item in all_entities:
# item.key is the entity name (e.g., 'direccion')
# item.value is the stored value
print(f"{item.key}: {item.value}")