I have just started with langgraph and I am facing very basic issue.
I am following the “Build a basic chatbot” page in the Getting started section (link: 1. Build a basic chatbot
It seems the add_messages annotation is not working for me. The AI is not recognizing the context.
My code,
import os
from langchain_openai import AzureChatOpenAI
from dotenv import load_dotenv
_ = load_dotenv()
model = AzureChatOpenAI(
azure_endpoint=os.environ['AZURE_OPENAI_ENDPOINT'],
azure_deployment=os.environ['AZURE_OPENAI_DEPLOYMENT_NAME'],
openai_api_version=os.environ['AZURE_OPENAI_API_VERSION'],
)
from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
class State(TypedDict):
messages: Annotated[list, add_messages]
graph_builder = StateGraph(State)
def chatbot(state: State) -> State:
print('state', state)
updated_state = {'messages': [model.invoke(state['messages'])]}
print('updated_state', updated_state)
return updated_state
graph_builder.add_node('chatbot', chatbot)
graph_builder.add_edge(START, 'chatbot')
graph_builder.add_edge('chatbot', END)
graph = graph_builder.compile()
def stream_graph_updates(user_input: str):
for event in graph.stream({'messages': [{'role': 'user', 'content': user_input}]}):
for value in event.values():
print('Assistant:', value['messages'][-1].content)
while True:
try:
user_input = input('User: ')
if user_input.lower() in ['quit', 'exit', 'q']:
print('Goodbye!');
break
stream_graph_updates(user_input)
except:
user_input = 'What do yoy know about LangGraph?'
print('User: ' + user_input)
stream_graph_updates(user_input)
break
Here is the interaction,
User: What is 2+2?
state {'messages': [HumanMessage(content='What is 2+2?', additional_kwargs={}, response_metadata={}, id='20178aec-155f-4081-b505-3ec5545ec17f')]}
updated_state {'messages': [AIMessage(content='2 + 2 equals **4**! 😊', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 11, 'prompt_tokens': 14, 'total_tokens': 25, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-11-20', 'system_fingerprint': 'fp_ee1d74bde0', 'id': 'chatcmpl-BvjSCXYRhPQJ9jl5ylNS5O9GDLv0n', 'service_tier': None, 'prompt_filter_results': [{'prompt_index': 0, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'jailbreak': {'filtered': False, 'detected': False}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}], 'finish_reason': 'stop', 'logprobs': None, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'protected_material_code': {'filtered': False, 'detected': False}, 'protected_material_text': {'filtered': False, 'detected': False}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}, id='run--c33d943d-f9e8-4c07-bab0-fd951cf61ceb-0', usage_metadata={'input_tokens': 14, 'output_tokens': 11, 'total_tokens': 25, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})]}
Assistant: 2 + 2 equals **4**! 😊
User: What if I use subtraction in my earlier message?
state {'messages': [HumanMessage(content='What if I use subtraction in my earlier message?', additional_kwargs={}, response_metadata={}, id='19dcbb5b-3d72-4a59-b98a-8b4db07be424')]}
updated_state {'messages': [AIMessage(content='It seems like you\'re asking what would happen if you use subtraction in a message or a question. Could you clarify what you mean by "my earlier message"? Are you referring to a specific calculation or context? If you\'re thinking about using subtraction to express or solve something, feel free to elaborate so I can assist you further!', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 65, 'prompt_tokens': 17, 'total_tokens': 82, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-11-20', 'system_fingerprint': 'fp_ee1d74bde0', 'id': 'chatcmpl-BvjSeh3Rn4XwkeHp1jbBkFyJp8NFR', 'service_tier': None, 'prompt_filter_results': [{'prompt_index': 0, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'jailbreak': {'filtered': False, 'detected': False}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}], 'finish_reason': 'stop', 'logprobs': None, 'content_filter_results': {'hate': {'filtered': False, 'severity': 'safe'}, 'protected_material_code': {'filtered': False, 'detected': False}, 'protected_material_text': {'filtered': False, 'detected': False}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}, id='run--3be5e449-9fe4-4ad1-b940-9db6ff1cef07-0', usage_metadata={'input_tokens': 17, 'output_tokens': 65, 'total_tokens': 82, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})]}
Assistant: It seems like you're asking what would happen if you use subtraction in a message or a question. Could you clarify what you mean by "my earlier message"? Are you referring to a specific calculation or context? If you're thinking about using subtraction to express or solve something, feel free to elaborate so I can assist you further!
User: q
Goodbye!
My python version,
Python 3.11.2
Showing portion of pip list command,
langchain 0.3.26
langchain-community 0.3.27
langchain-core 0.3.68
langchain-groq 0.2.4
langchain-openai 0.3.28
langchain-text-splitters 0.3.8
langgraph 0.5.3
langgraph-api 0.2.98
langgraph-checkpoint 2.1.0
langgraph-cli 0.3.5
langgraph-prebuilt 0.5.2
langgraph-runtime-inmem 0.6.0
langgraph-sdk 0.1.72
langsmith 0.4.8
Does it have to do with my model with older version in the .env file?
AZURE_OPENAI_API_VERSION=2024-12-01-preview