Skip to content

Instantly share code, notes, and snippets.

@cassc
Created June 13, 2024 07:15
Show Gist options
  • Save cassc/bebb2ae441e739c4c411c0e9a3c755d8 to your computer and use it in GitHub Desktop.
Save cassc/bebb2ae441e739c4c411c0e9a3c755d8 to your computer and use it in GitHub Desktop.
Langchain ChatGPT converstation with memory
from langchain_core.globals import set_debug
from langchain_openai import ChatOpenAI
from langchain_community.callbacks import get_openai_callback
from langchain.chains import ConversationChain
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory, ConversationSummaryBufferMemory
set_debug(False)
model_name = 'gpt-4o'
llm = ChatOpenAI(model=model_name, temperature=0.03)
conversation = ConversationChain(
llm=llm,
verbose=True,
memory=ConversationBufferMemory(), # NOTE: memory is just concatenation previous user message and responses in the new conversation.
)
for _ in range(3):
with get_openai_callback() as cb:
output = conversation.invoke("give me some random integers between 1 to 20, inclusive, exclluding any number you've already mentioned")
print(output)
print(f"Total Tokens: {cb.total_tokens}")
print(f"Total Cost (USD): ${cb.total_cost}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment