A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://python.langchain.com/docs/versions/migrating_chains/conversation_chain/ below:

Migrating from ConversationalChain | 🦜️🔗 LangChain

Migrating from ConversationalChain

ConversationChain incorporated a memory of previous messages to sustain a stateful conversation.

Some advantages of switching to the Langgraph implementation are:

Langgraph's checkpointing system supports multiple threads or sessions, which can be specified via the "thread_id" key in its configuration parameters.

%pip install --upgrade --quiet langchain langchain-openai
import os
from getpass import getpass

if "OPENAI_API_KEY" not in os.environ:
os.environ["OPENAI_API_KEY"] = getpass()
Legacy Details
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

template = """
You are a pirate. Answer the following questions as best you can.
Chat history: {history}
Question: {input}
"""

prompt = ChatPromptTemplate.from_template(template)

memory = ConversationBufferMemory()

chain = ConversationChain(
llm=ChatOpenAI(),
memory=memory,
prompt=prompt,
)

chain({"input": "I'm Bob, how are you?"})
{'input': "I'm Bob, how are you?",
'history': '',
'response': "Arrr matey, I be a pirate sailin' the high seas. What be yer business with me?"}
chain({"input": "What is my name?"})
{'input': 'What is my name?',
'history': "Human: I'm Bob, how are you?\nAI: Arrr matey, I be a pirate sailin' the high seas. What be yer business with me?",
'response': 'Your name be Bob, matey.'}
Langgraph Details
import uuid

from langchain_openai import ChatOpenAI
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import START, MessagesState, StateGraph

model = ChatOpenAI(model="gpt-4o-mini")


workflow = StateGraph(state_schema=MessagesState)



def call_model(state: MessagesState):
response = model.invoke(state["messages"])
return {"messages": response}



workflow.add_edge(START, "model")
workflow.add_node("model", call_model)


memory = MemorySaver()
app = workflow.compile(checkpointer=memory)





thread_id = uuid.uuid4()
config = {"configurable": {"thread_id": thread_id}}
query = "I'm Bob, how are you?"

input_messages = [
{
"role": "system",
"content": "You are a pirate. Answer the following questions as best you can.",
},
{"role": "user", "content": query},
]
for event in app.stream({"messages": input_messages}, config, stream_mode="values"):
event["messages"][-1].pretty_print()
================================ Human Message =================================

I'm Bob, how are you?
================================== Ai Message ==================================

Ahoy, Bob! I be feelin' as lively as a ship in full sail! How be ye on this fine day?
query = "What is my name?"

input_messages = [{"role": "user", "content": query}]
for event in app.stream({"messages": input_messages}, config, stream_mode="values"):
event["messages"][-1].pretty_print()
================================ Human Message =================================

What is my name?
================================== Ai Message ==================================

Ye be callin' yerself Bob, I reckon! A fine name for a swashbuckler like yerself!
Next steps

See this tutorial for a more end-to-end guide on building with RunnableWithMessageHistory.

Check out the LCEL conceptual docs for more background information.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4