id
stringlengths
14
16
text
stringlengths
29
2.73k
source
stringlengths
49
117
288269394a4e-1
session_id = str(uuid4()) # This is a unique identifier for the user # Load your OpenAI key from a .env file from dotenv import load_dotenv load_dotenv() True Initialize the Zep Chat Message History Class and initialize the Agent# ddg = DuckDuckGoSearchRun() tools = [ddg] # Set up Zep Chat History zep_chat_history = ZepChatMessageHistory( session_id=session_id, url=ZEP_API_URL, ) # Use a standard ConversationBufferMemory to encapsulate the Zep chat history memory = ConversationBufferMemory( memory_key="chat_history", chat_memory=zep_chat_history ) # Initialize the agent llm = OpenAI(temperature=0) agent_chain = initialize_agent( tools, llm, agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=memory, ) Add some history data# # Preload some messages into the memory. The default message window is 12 messages. We want to push beyond this to demonstrate auto-summarization. test_history = [ {"role": "human", "content": "Who was Octavia Butler?"}, { "role": "ai", "content": ( "Octavia Estelle Butler (June 22, 1947 – February 24, 2006) was an American" " science fiction author." ), }, {"role": "human", "content": "Which books of hers were made into movies?"}, { "role": "ai", "content": ( "The most well-known adaptation of Octavia Butler's work is the FX series"
https://python.langchain.com/en/latest/modules/memory/examples/zep_memory.html
288269394a4e-2
"The most well-known adaptation of Octavia Butler's work is the FX series" " Kindred, based on her novel of the same name." ), }, {"role": "human", "content": "Who were her contemporaries?"}, { "role": "ai", "content": ( "Octavia Butler's contemporaries included Ursula K. Le Guin, Samuel R." " Delany, and Joanna Russ." ), }, {"role": "human", "content": "What awards did she win?"}, { "role": "ai", "content": ( "Octavia Butler won the Hugo Award, the Nebula Award, and the MacArthur" " Fellowship." ), }, { "role": "human", "content": "Which other women sci-fi writers might I want to read?", }, { "role": "ai", "content": "You might want to read Ursula K. Le Guin or Joanna Russ.", }, { "role": "human", "content": ( "Write a short synopsis of Butler's book, Parable of the Sower. What is it" " about?" ), }, { "role": "ai", "content": ( "Parable of the Sower is a science fiction novel by Octavia Butler," " published in 1993. It follows the story of Lauren Olamina, a young woman" " living in a dystopian future where society has collapsed due to" " environmental disasters, poverty, and violence." ), }, ] for msg in test_history:
https://python.langchain.com/en/latest/modules/memory/examples/zep_memory.html
288269394a4e-3
), }, ] for msg in test_history: zep_chat_history.append( HumanMessage(content=msg["content"]) if msg["role"] == "human" else AIMessage(content=msg["content"]) ) Run the agent# Doing so will automatically add the input and response to the Zep memory. agent_chain.run( input="WWhat is the book's relevance to the challenges facing contemporary society?" ) > Entering new AgentExecutor chain... Thought: Do I need to use a tool? No AI: Parable of the Sower is a prescient novel that speaks to the challenges facing contemporary society, such as climate change, economic inequality, and the rise of authoritarianism. It is a cautionary tale that warns of the dangers of ignoring these issues and the importance of taking action to address them. > Finished chain. 'Parable of the Sower is a prescient novel that speaks to the challenges facing contemporary society, such as climate change, economic inequality, and the rise of authoritarianism. It is a cautionary tale that warns of the dangers of ignoring these issues and the importance of taking action to address them.' Inspect the Zep memory# Note the summary, and that the history has been enriched with token counts, UUIDs, and timestamps. Summaries are biased towards the most recent messages. def print_messages(messages): for m in messages: print(m.to_dict()) print(zep_chat_history.zep_summary) print("\n") print_messages(zep_chat_history.zep_messages) The conversation is about Octavia Butler. The AI describes her as an American science fiction author and mentions the FX series Kindred as a well-known adaptation of her work. The human then asks about her contemporaries, and the AI lists
https://python.langchain.com/en/latest/modules/memory/examples/zep_memory.html
288269394a4e-4
Ursula K. Le Guin, Samuel R. Delany, and Joanna Russ. {'role': 'human', 'content': 'What awards did she win?', 'uuid': '9fa75c3c-edae-41e3-b9bc-9fcf16b523c9', 'created_at': '2023-05-25T15:09:41.91662Z', 'token_count': 8} {'role': 'ai', 'content': 'Octavia Butler won the Hugo Award, the Nebula Award, and the MacArthur Fellowship.', 'uuid': 'def4636c-32cb-49ed-b671-32035a034712', 'created_at': '2023-05-25T15:09:41.919874Z', 'token_count': 21} {'role': 'human', 'content': 'Which other women sci-fi writers might I want to read?', 'uuid': '6e87bd4a-bc23-451e-ae36-05a140415270', 'created_at': '2023-05-25T15:09:41.923771Z', 'token_count': 14} {'role': 'ai', 'content': 'You might want to read Ursula K. Le Guin or Joanna Russ.', 'uuid': 'f65d8dde-9ee8-4983-9da6-ba789b7e8aa4', 'created_at': '2023-05-25T15:09:41.935254Z', 'token_count': 18}
https://python.langchain.com/en/latest/modules/memory/examples/zep_memory.html
288269394a4e-5
{'role': 'human', 'content': "Write a short synopsis of Butler's book, Parable of the Sower. What is it about?", 'uuid': '5678d056-7f05-4e70-b8e5-f85efa56db01', 'created_at': '2023-05-25T15:09:41.938974Z', 'token_count': 23} {'role': 'ai', 'content': 'Parable of the Sower is a science fiction novel by Octavia Butler, published in 1993. It follows the story of Lauren Olamina, a young woman living in a dystopian future where society has collapsed due to environmental disasters, poverty, and violence.', 'uuid': '50d64946-9239-4327-83e6-71dcbdd16198', 'created_at': '2023-05-25T15:09:41.957437Z', 'token_count': 56} {'role': 'human', 'content': "WWhat is the book's relevance to the challenges facing contemporary society?", 'uuid': 'a39cfc07-8858-480a-9026-fc47a8ef7001', 'created_at': '2023-05-25T15:09:50.469533Z', 'token_count': 16}
https://python.langchain.com/en/latest/modules/memory/examples/zep_memory.html
288269394a4e-6
{'role': 'ai', 'content': 'Parable of the Sower is a prescient novel that speaks to the challenges facing contemporary society, such as climate change, economic inequality, and the rise of authoritarianism. It is a cautionary tale that warns of the dangers of ignoring these issues and the importance of taking action to address them.', 'uuid': 'a4ecf0fe-fdd0-4aad-b72b-efde2e6830cc', 'created_at': '2023-05-25T15:09:50.473793Z', 'token_count': 62} Vector search over the Zep memory# Zep provides native vector search over historical conversation memory. Embedding happens automatically. search_results = zep_chat_history.search("who are some famous women sci-fi authors?") for r in search_results: print(r.message, r.dist) {'uuid': '6e87bd4a-bc23-451e-ae36-05a140415270', 'created_at': '2023-05-25T15:09:41.923771Z', 'role': 'human', 'content': 'Which other women sci-fi writers might I want to read?', 'token_count': 14} 0.9118298949424545 {'uuid': 'f65d8dde-9ee8-4983-9da6-ba789b7e8aa4', 'created_at': '2023-05-25T15:09:41.935254Z', 'role': 'ai', 'content': 'You might want to read Ursula K. Le Guin or Joanna Russ.', 'token_count': 18} 0.8533024416448016
https://python.langchain.com/en/latest/modules/memory/examples/zep_memory.html
288269394a4e-7
{'uuid': '52cfe3e8-b800-4dd8-a7dd-8e9e4764dfc8', 'created_at': '2023-05-25T15:09:41.913856Z', 'role': 'ai', 'content': "Octavia Butler's contemporaries included Ursula K. Le Guin, Samuel R. Delany, and Joanna Russ.", 'token_count': 27} 0.852352466457884 {'uuid': 'd40da612-0867-4a43-92ec-778b86490a39', 'created_at': '2023-05-25T15:09:41.858543Z', 'role': 'human', 'content': 'Who was Octavia Butler?', 'token_count': 8} 0.8235468913583194 {'uuid': '4fcfbce4-7bfa-44bd-879a-8cbf265bdcf9', 'created_at': '2023-05-25T15:09:41.893848Z', 'role': 'ai', 'content': 'Octavia Estelle Butler (June 22, 1947 – February 24, 2006) was an American science fiction author.', 'token_count': 31} 0.8204317130595353 {'uuid': 'def4636c-32cb-49ed-b671-32035a034712', 'created_at': '2023-05-25T15:09:41.919874Z', 'role': 'ai', 'content': 'Octavia Butler won the Hugo Award, the Nebula Award, and the MacArthur Fellowship.', 'token_count': 21} 0.8196714827228725
https://python.langchain.com/en/latest/modules/memory/examples/zep_memory.html
288269394a4e-8
{'uuid': '862107de-8f6f-43c0-91fa-4441f01b2b3a', 'created_at': '2023-05-25T15:09:41.898149Z', 'role': 'human', 'content': 'Which books of hers were made into movies?', 'token_count': 11} 0.7954322970428519 {'uuid': '97164506-90fe-4c71-9539-69ebcd1d90a2', 'created_at': '2023-05-25T15:09:41.90887Z', 'role': 'human', 'content': 'Who were her contemporaries?', 'token_count': 8} 0.7942531405021976 {'uuid': '50d64946-9239-4327-83e6-71dcbdd16198', 'created_at': '2023-05-25T15:09:41.957437Z', 'role': 'ai', 'content': 'Parable of the Sower is a science fiction novel by Octavia Butler, published in 1993. It follows the story of Lauren Olamina, a young woman living in a dystopian future where society has collapsed due to environmental disasters, poverty, and violence.', 'token_count': 56} 0.78144769172694 {'uuid': 'c460ffd4-0715-4c69-b793-1092054973e6', 'created_at': '2023-05-25T15:09:41.903082Z', 'role': 'ai', 'content': "The most well-known adaptation of Octavia Butler's work is the FX series Kindred, based on her novel of the same name.", 'token_count': 29} 0.7811962820699464
https://python.langchain.com/en/latest/modules/memory/examples/zep_memory.html
288269394a4e-9
previous Redis Chat Message History next Indexes Contents REACT Agent Chat Message History Example Initialize the Zep Chat Message History Class and initialize the Agent Add some history data Run the agent Inspect the Zep memory Vector search over the Zep memory By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/examples/zep_memory.html
537b0345419d-0
.ipynb .pdf Momento Momento# This notebook goes over how to use Momento Cache to store chat message history using the MomentoChatMessageHistory class. See the Momento docs for more detail on how to get set up with Momento. Note that, by default we will create a cache if one with the given name doesn’t already exist. You’ll need to get a Momento auth token to use this class. This can either be passed in to a momento.CacheClient if you’d like to instantiate that directly, as a named parameter auth_token to MomentoChatMessageHistory.from_client_params, or can just be set as an environment variable MOMENTO_AUTH_TOKEN. from datetime import timedelta from langchain.memory import MomentoChatMessageHistory session_id = "foo" cache_name = "langchain" ttl = timedelta(days=1), history = MomentoChatMessageHistory.from_client_params( session_id, cache_name, ttl, ) history.add_user_message("hi!") history.add_ai_message("whats up?") history.messages [HumanMessage(content='hi!', additional_kwargs={}, example=False), AIMessage(content='whats up?', additional_kwargs={}, example=False)] previous Entity Memory with SQLite storage next Mongodb Chat Message History By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/examples/momento_chat_message_history.html
c6ea23b40e2c-0
.ipynb .pdf Dynamodb Chat Message History Contents DynamoDBChatMessageHistory Agent with DynamoDB Memory Dynamodb Chat Message History# This notebook goes over how to use Dynamodb to store chat message history. First make sure you have correctly configured the AWS CLI. Then make sure you have installed boto3. Next, create the DynamoDB Table where we will be storing messages: import boto3 # Get the service resource. dynamodb = boto3.resource('dynamodb') # Create the DynamoDB table. table = dynamodb.create_table( TableName='SessionTable', KeySchema=[ { 'AttributeName': 'SessionId', 'KeyType': 'HASH' } ], AttributeDefinitions=[ { 'AttributeName': 'SessionId', 'AttributeType': 'S' } ], BillingMode='PAY_PER_REQUEST', ) # Wait until the table exists. table.meta.client.get_waiter('table_exists').wait(TableName='SessionTable') # Print out some data about the table. print(table.item_count) 0 DynamoDBChatMessageHistory# from langchain.memory.chat_message_histories import DynamoDBChatMessageHistory history = DynamoDBChatMessageHistory(table_name="SessionTable", session_id="0") history.add_user_message("hi!") history.add_ai_message("whats up?") history.messages [HumanMessage(content='hi!', additional_kwargs={}, example=False), AIMessage(content='whats up?', additional_kwargs={}, example=False)] Agent with DynamoDB Memory# from langchain.agents import Tool from langchain.memory import ConversationBufferMemory from langchain.chat_models import ChatOpenAI from langchain.agents import initialize_agent from langchain.agents import AgentType
https://python.langchain.com/en/latest/modules/memory/examples/dynamodb_chat_message_history.html
c6ea23b40e2c-1
from langchain.agents import initialize_agent from langchain.agents import AgentType from langchain.utilities import PythonREPL from getpass import getpass message_history = DynamoDBChatMessageHistory(table_name="SessionTable", session_id="1") memory = ConversationBufferMemory(memory_key="chat_history", chat_memory=message_history, return_messages=True) python_repl = PythonREPL() # You can create the tool to pass to an agent tools = [Tool( name="python_repl", description="A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.", func=python_repl.run )] llm=ChatOpenAI(temperature=0) agent_chain = initialize_agent(tools, llm, agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=memory) agent_chain.run(input="Hello!") > Entering new AgentExecutor chain... { "action": "Final Answer", "action_input": "Hello! How can I assist you today?" } > Finished chain. 'Hello! How can I assist you today?' agent_chain.run(input="Who owns Twitter?") > Entering new AgentExecutor chain... { "action": "python_repl", "action_input": "import requests\nfrom bs4 import BeautifulSoup\n\nurl = 'https://en.wikipedia.org/wiki/Twitter'\nresponse = requests.get(url)\nsoup = BeautifulSoup(response.content, 'html.parser')\nowner = soup.find('th', text='Owner').find_next_sibling('td').text.strip()\nprint(owner)" }
https://python.langchain.com/en/latest/modules/memory/examples/dynamodb_chat_message_history.html
c6ea23b40e2c-2
} Observation: X Corp. (2023–present)Twitter, Inc. (2006–2023) Thought:{ "action": "Final Answer", "action_input": "X Corp. (2023–present)Twitter, Inc. (2006–2023)" } > Finished chain. 'X Corp. (2023–present)Twitter, Inc. (2006–2023)' agent_chain.run(input="My name is Bob.") > Entering new AgentExecutor chain... { "action": "Final Answer", "action_input": "Hello Bob! How can I assist you today?" } > Finished chain. 'Hello Bob! How can I assist you today?' agent_chain.run(input="Who am I?") > Entering new AgentExecutor chain... { "action": "Final Answer", "action_input": "Your name is Bob." } > Finished chain. 'Your name is Bob.' previous How to create a custom Memory class next Entity Memory with SQLite storage Contents DynamoDBChatMessageHistory Agent with DynamoDB Memory By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/examples/dynamodb_chat_message_history.html
ea1defa2cd25-0
.ipynb .pdf Cassandra Chat Message History Cassandra Chat Message History# This notebook goes over how to use Cassandra to store chat message history. Cassandra is a distributed database that is well suited for storing large amounts of data. It is a good choice for storing chat message history because it is easy to scale and can handle a large number of writes. # List of contact points to try connecting to Cassandra cluster. contact_points = ["cassandra"] from langchain.memory import CassandraChatMessageHistory message_history = CassandraChatMessageHistory( contact_points=contact_points, session_id="test-session" ) message_history.add_user_message("hi!") message_history.add_ai_message("whats up?") message_history.messages [HumanMessage(content='hi!', additional_kwargs={}, example=False), AIMessage(content='whats up?', additional_kwargs={}, example=False)] previous Adding Message Memory backed by a database to an Agent next How to customize conversational memory By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/examples/cassandra_chat_message_history.html
15838d9dbe1e-0
.ipynb .pdf Entity Memory with SQLite storage Entity Memory with SQLite storage# In this walkthrough we’ll create a simple conversation chain which uses ConversationEntityMemory backed by a SqliteEntityStore. from langchain.chains import ConversationChain from langchain.llms import OpenAI from langchain.memory import ConversationEntityMemory from langchain.memory.entity import SQLiteEntityStore from langchain.memory.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE entity_store=SQLiteEntityStore() llm = OpenAI(temperature=0) memory = ConversationEntityMemory(llm=llm, entity_store=entity_store) conversation = ConversationChain( llm=llm, prompt=ENTITY_MEMORY_CONVERSATION_TEMPLATE, memory=memory, verbose=True, ) Notice the usage of EntitySqliteStore as parameter to entity_store on the memory property. conversation.run("Deven & Sam are working on a hackathon project") > Entering new ConversationChain chain... Prompt after formatting: You are an assistant to a human, powered by a large language model trained by OpenAI. You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
https://python.langchain.com/en/latest/modules/memory/examples/entity_memory_with_sqlite.html
15838d9dbe1e-1
You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics. Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist. Context: {'Deven': 'Deven is working on a hackathon project with Sam.', 'Sam': 'Sam is working on a hackathon project with Deven.'} Current conversation: Last line: Human: Deven & Sam are working on a hackathon project You: > Finished chain. ' That sounds like a great project! What kind of project are they working on?' conversation.memory.entity_store.get("Deven") 'Deven is working on a hackathon project with Sam.' conversation.memory.entity_store.get("Sam") 'Sam is working on a hackathon project with Deven.' previous Dynamodb Chat Message History next Momento By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/examples/entity_memory_with_sqlite.html
4d76a36d814a-0
.ipynb .pdf How to create a custom Memory class How to create a custom Memory class# Although there are a few predefined types of memory in LangChain, it is highly possible you will want to add your own type of memory that is optimal for your application. This notebook covers how to do that. For this notebook, we will add a custom memory type to ConversationChain. In order to add a custom memory class, we need to import the base memory class and subclass it. from langchain import OpenAI, ConversationChain from langchain.schema import BaseMemory from pydantic import BaseModel from typing import List, Dict, Any In this example, we will write a custom memory class that uses spacy to extract entities and save information about them in a simple hash table. Then, during the conversation, we will look at the input text, extract any entities, and put any information about them into the context. Please note that this implementation is pretty simple and brittle and probably not useful in a production setting. Its purpose is to showcase that you can add custom memory implementations. For this, we will need spacy. # !pip install spacy # !python -m spacy download en_core_web_lg import spacy nlp = spacy.load('en_core_web_lg') class SpacyEntityMemory(BaseMemory, BaseModel): """Memory class for storing information about entities.""" # Define dictionary to store information about entities. entities: dict = {} # Define key to pass information about entities into prompt. memory_key: str = "entities" def clear(self): self.entities = {} @property def memory_variables(self) -> List[str]: """Define the variables we are providing to the prompt.""" return [self.memory_key]
https://python.langchain.com/en/latest/modules/memory/examples/custom_memory.html
4d76a36d814a-1
return [self.memory_key] def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, str]: """Load the memory variables, in this case the entity key.""" # Get the input text and run through spacy doc = nlp(inputs[list(inputs.keys())[0]]) # Extract known information about entities, if they exist. entities = [self.entities[str(ent)] for ent in doc.ents if str(ent) in self.entities] # Return combined information about entities to put into context. return {self.memory_key: "\n".join(entities)} def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None: """Save context from this conversation to buffer.""" # Get the input text and run through spacy text = inputs[list(inputs.keys())[0]] doc = nlp(text) # For each entity that was mentioned, save this information to the dictionary. for ent in doc.ents: ent_str = str(ent) if ent_str in self.entities: self.entities[ent_str] += f"\n{text}" else: self.entities[ent_str] = text We now define a prompt that takes in information about entities as well as user input from langchain.prompts.prompt import PromptTemplate template = """The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. You are provided with information about entities the Human mentions, if relevant. Relevant entity information: {entities} Conversation: Human: {input} AI:""" prompt = PromptTemplate(
https://python.langchain.com/en/latest/modules/memory/examples/custom_memory.html
4d76a36d814a-2
Conversation: Human: {input} AI:""" prompt = PromptTemplate( input_variables=["entities", "input"], template=template ) And now we put it all together! llm = OpenAI(temperature=0) conversation = ConversationChain(llm=llm, prompt=prompt, verbose=True, memory=SpacyEntityMemory()) In the first example, with no prior knowledge about Harrison, the “Relevant entity information” section is empty. conversation.predict(input="Harrison likes machine learning") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. You are provided with information about entities the Human mentions, if relevant. Relevant entity information: Conversation: Human: Harrison likes machine learning AI: > Finished ConversationChain chain. " That's great to hear! Machine learning is a fascinating field of study. It involves using algorithms to analyze data and make predictions. Have you ever studied machine learning, Harrison?" Now in the second example, we can see that it pulls in information about Harrison. conversation.predict(input="What do you think Harrison's favorite subject in college was?") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. You are provided with information about entities the Human mentions, if relevant. Relevant entity information: Harrison likes machine learning Conversation: Human: What do you think Harrison's favorite subject in college was?
https://python.langchain.com/en/latest/modules/memory/examples/custom_memory.html
4d76a36d814a-3
Conversation: Human: What do you think Harrison's favorite subject in college was? AI: > Finished ConversationChain chain. ' From what I know about Harrison, I believe his favorite subject in college was machine learning. He has expressed a strong interest in the subject and has mentioned it often.' Again, please note that this implementation is pretty simple and brittle and probably not useful in a production setting. Its purpose is to showcase that you can add custom memory implementations. previous How to customize conversational memory next Dynamodb Chat Message History By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/examples/custom_memory.html
6e97adfe5289-0
.ipynb .pdf Postgres Chat Message History Postgres Chat Message History# This notebook goes over how to use Postgres to store chat message history. from langchain.memory import PostgresChatMessageHistory history = PostgresChatMessageHistory(connection_string="postgresql://postgres:mypassword@localhost/chat_history", session_id="foo") history.add_user_message("hi!") history.add_ai_message("whats up?") history.messages previous How to use multiple memory classes in the same chain next Redis Chat Message History By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/examples/postgres_chat_message_history.html
c78b5ef673ef-0
.ipynb .pdf Redis Chat Message History Redis Chat Message History# This notebook goes over how to use Redis to store chat message history. from langchain.memory import RedisChatMessageHistory history = RedisChatMessageHistory("foo") history.add_user_message("hi!") history.add_ai_message("whats up?") history.messages [AIMessage(content='whats up?', additional_kwargs={}), HumanMessage(content='hi!', additional_kwargs={})] previous Postgres Chat Message History next Zep Memory By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/examples/redis_chat_message_history.html
9916889ff898-0
.ipynb .pdf How to add Memory to an Agent How to add Memory to an Agent# This notebook goes over adding memory to an Agent. Before going through this notebook, please walkthrough the following notebooks, as this will build on top of both of them: Adding memory to an LLM Chain Custom Agents In order to add a memory to an agent we are going to the the following steps: We are going to create an LLMChain with memory. We are going to use that LLMChain to create a custom Agent. For the purposes of this exercise, we are going to create a simple custom Agent that has access to a search tool and utilizes the ConversationBufferMemory class. from langchain.agents import ZeroShotAgent, Tool, AgentExecutor from langchain.memory import ConversationBufferMemory from langchain import OpenAI, LLMChain from langchain.utilities import GoogleSearchAPIWrapper search = GoogleSearchAPIWrapper() tools = [ Tool( name = "Search", func=search.run, description="useful for when you need to answer questions about current events" ) ] Notice the usage of the chat_history variable in the PromptTemplate, which matches up with the dynamic key name in the ConversationBufferMemory. prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:""" suffix = """Begin!" {chat_history} Question: {input} {agent_scratchpad}""" prompt = ZeroShotAgent.create_prompt( tools, prefix=prefix, suffix=suffix, input_variables=["input", "chat_history", "agent_scratchpad"] ) memory = ConversationBufferMemory(memory_key="chat_history")
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory.html
9916889ff898-1
) memory = ConversationBufferMemory(memory_key="chat_history") We can now construct the LLMChain, with the Memory object, and then create the agent. llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt) agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True) agent_chain = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True, memory=memory) agent_chain.run(input="How many people live in canada?") > Entering new AgentExecutor chain... Thought: I need to find out the population of Canada Action: Search Action Input: Population of Canada
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory.html
9916889ff898-2
Action: Search Action Input: Population of Canada Observation: The current population of Canada is 38,566,192 as of Saturday, December 31, 2022, based on Worldometer elaboration of the latest United Nations data. · Canada ... Additional information related to Canadian population trends can be found on Statistics Canada's Population and Demography Portal. Population of Canada (real- ... Index to the latest information from the Census of Population. This survey conducted by Statistics Canada provides a statistical portrait of Canada and its ... 14 records ... Estimated number of persons by quarter of a year and by year, Canada, provinces and territories. The 2021 Canadian census counted a total population of 36,991,981, an increase of around 5.2 percent over the 2016 figure. ... Between 1990 and 2008, the ... ( 2 ) Census reports and other statistical publications from national statistical offices, ( 3 ) Eurostat: Demographic Statistics, ( 4 ) United Nations ... Canada is a country in North America. Its ten provinces and three territories extend from ... Population. • Q4 2022 estimate. 39,292,355 (37th). Information is available for the total Indigenous population and each of the three ... The term 'Aboriginal' or 'Indigenous' used on the Statistics Canada ... Jun 14, 2022 ... Determinants of health are the broad range of personal, social, economic and environmental factors that determine individual and population ... COVID-19 vaccination coverage across Canada by demographics and key populations. Updated every Friday at 12:00 PM Eastern Time. Thought: I now know the final answer Final Answer: The current population of Canada is 38,566,192 as of Saturday, December 31, 2022, based on Worldometer elaboration of the latest United Nations data. > Finished AgentExecutor chain.
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory.html
9916889ff898-3
> Finished AgentExecutor chain. 'The current population of Canada is 38,566,192 as of Saturday, December 31, 2022, based on Worldometer elaboration of the latest United Nations data.' To test the memory of this agent, we can ask a followup question that relies on information in the previous exchange to be answered correctly. agent_chain.run(input="what is their national anthem called?") > Entering new AgentExecutor chain... Thought: I need to find out what the national anthem of Canada is called. Action: Search Action Input: National Anthem of Canada
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory.html
9916889ff898-4
Action: Search Action Input: National Anthem of Canada Observation: Jun 7, 2010 ... https://twitter.com/CanadaImmigrantCanadian National Anthem O Canada in HQ - complete with lyrics, captions, vocals & music.LYRICS:O Canada! Nov 23, 2022 ... After 100 years of tradition, O Canada was proclaimed Canada's national anthem in 1980. The music for O Canada was composed in 1880 by Calixa ... O Canada, national anthem of Canada. It was proclaimed the official national anthem on July 1, 1980. “God Save the Queen” remains the royal anthem of Canada ... O Canada! Our home and native land! True patriot love in all of us command. Car ton bras sait porter l'épée,. Il sait porter la croix! "O Canada" (French: Ô Canada) is the national anthem of Canada. The song was originally commissioned by Lieutenant Governor of Quebec Théodore Robitaille ... Feb 1, 2018 ... It was a simple tweak — just two words. But with that, Canada just voted to make its national anthem, “O Canada,” gender neutral, ... "O Canada" was proclaimed Canada's national anthem on July 1,. 1980, 100 years after it was first sung on June 24, 1880. The music. Patriotic music in Canada dates back over 200 years as a distinct category from British or French patriotism, preceding the first legal steps to ... Feb 4, 2022 ... English version: O Canada! Our home and native land! True patriot love in all of us command. With glowing hearts we ... Feb 1, 2018 ... Canada's Senate has passed a bill making the country's national anthem gender-neutral. If you're not familiar with the words to “O Canada,” ...
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory.html
9916889ff898-5
Thought: I now know the final answer. Final Answer: The national anthem of Canada is called "O Canada". > Finished AgentExecutor chain. 'The national anthem of Canada is called "O Canada".' We can see that the agent remembered that the previous question was about Canada, and properly asked Google Search what the name of Canada’s national anthem was. For fun, let’s compare this to an agent that does NOT have memory. prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:""" suffix = """Begin!" Question: {input} {agent_scratchpad}""" prompt = ZeroShotAgent.create_prompt( tools, prefix=prefix, suffix=suffix, input_variables=["input", "agent_scratchpad"] ) llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt) agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True) agent_without_memory = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True) agent_without_memory.run("How many people live in canada?") > Entering new AgentExecutor chain... Thought: I need to find out the population of Canada Action: Search Action Input: Population of Canada
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory.html
9916889ff898-6
Action: Search Action Input: Population of Canada Observation: The current population of Canada is 38,566,192 as of Saturday, December 31, 2022, based on Worldometer elaboration of the latest United Nations data. · Canada ... Additional information related to Canadian population trends can be found on Statistics Canada's Population and Demography Portal. Population of Canada (real- ... Index to the latest information from the Census of Population. This survey conducted by Statistics Canada provides a statistical portrait of Canada and its ... 14 records ... Estimated number of persons by quarter of a year and by year, Canada, provinces and territories. The 2021 Canadian census counted a total population of 36,991,981, an increase of around 5.2 percent over the 2016 figure. ... Between 1990 and 2008, the ... ( 2 ) Census reports and other statistical publications from national statistical offices, ( 3 ) Eurostat: Demographic Statistics, ( 4 ) United Nations ... Canada is a country in North America. Its ten provinces and three territories extend from ... Population. • Q4 2022 estimate. 39,292,355 (37th). Information is available for the total Indigenous population and each of the three ... The term 'Aboriginal' or 'Indigenous' used on the Statistics Canada ... Jun 14, 2022 ... Determinants of health are the broad range of personal, social, economic and environmental factors that determine individual and population ... COVID-19 vaccination coverage across Canada by demographics and key populations. Updated every Friday at 12:00 PM Eastern Time. Thought: I now know the final answer Final Answer: The current population of Canada is 38,566,192 as of Saturday, December 31, 2022, based on Worldometer elaboration of the latest United Nations data. > Finished AgentExecutor chain.
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory.html
9916889ff898-7
> Finished AgentExecutor chain. 'The current population of Canada is 38,566,192 as of Saturday, December 31, 2022, based on Worldometer elaboration of the latest United Nations data.' agent_without_memory.run("what is their national anthem called?") > Entering new AgentExecutor chain... Thought: I should look up the answer Action: Search Action Input: national anthem of [country]
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory.html
9916889ff898-8
Action: Search Action Input: national anthem of [country] Observation: Most nation states have an anthem, defined as "a song, as of praise, devotion, or patriotism"; most anthems are either marches or hymns in style. List of all countries around the world with its national anthem. ... Title and lyrics in the language of the country and translated into English, Aug 1, 2021 ... 1. Afghanistan, "Milli Surood" (National Anthem) · 2. Armenia, "Mer Hayrenik" (Our Fatherland) · 3. Azerbaijan (a transcontinental country with ... A national anthem is a patriotic musical composition symbolizing and evoking eulogies of the history and traditions of a country or nation. National Anthem of Every Country ; Fiji, “Meda Dau Doka” (“God Bless Fiji”) ; Finland, “Maamme”. (“Our Land”) ; France, “La Marseillaise” (“The Marseillaise”). You can find an anthem in the menu at the top alphabetically or you can use the search feature. This site is focussed on the scholarly study of national anthems ... Feb 13, 2022 ... The 38-year-old country music artist had the honor of singing the National Anthem during this year's big game, and she did not disappoint. Oldest of the World's National Anthems ; France, La Marseillaise (“The Marseillaise”), 1795 ; Argentina, Himno Nacional Argentino (“Argentine National Anthem”) ... Mar 3, 2022 ... Country music star Jessie James Decker gained the respect of music and hockey fans alike after a jaw-dropping rendition of "The Star-Spangled ... This list shows the country on the left, the national anthem in the ... There are many countries over the world who have a national anthem of their own.
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory.html
9916889ff898-9
Thought: I now know the final answer Final Answer: The national anthem of [country] is [name of anthem]. > Finished AgentExecutor chain. 'The national anthem of [country] is [name of anthem].' previous How to add memory to a Multi-Input Chain next Adding Message Memory backed by a database to an Agent By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory.html
c347bbb365a2-0
.ipynb .pdf Motörhead Memory Contents Setup Motörhead Memory# Motörhead is a memory server implemented in Rust. It automatically handles incremental summarization in the background and allows for stateless applications. Setup# See instructions at Motörhead for running the server locally. from langchain.memory.motorhead_memory import MotorheadMemory from langchain import OpenAI, LLMChain, PromptTemplate template = """You are a chatbot having a conversation with a human. {chat_history} Human: {human_input} AI:""" prompt = PromptTemplate( input_variables=["chat_history", "human_input"], template=template ) memory = MotorheadMemory( session_id="testing-1", url="http://localhost:8080", memory_key="chat_history" ) await memory.init(); # loads previous state from Motörhead 🤘 llm_chain = LLMChain( llm=OpenAI(), prompt=prompt, verbose=True, memory=memory, ) llm_chain.run("hi im bob") > Entering new LLMChain chain... Prompt after formatting: You are a chatbot having a conversation with a human. Human: hi im bob AI: > Finished chain. ' Hi Bob, nice to meet you! How are you doing today?' llm_chain.run("whats my name?") > Entering new LLMChain chain... Prompt after formatting: You are a chatbot having a conversation with a human. Human: hi im bob AI: Hi Bob, nice to meet you! How are you doing today? Human: whats my name? AI: > Finished chain.
https://python.langchain.com/en/latest/modules/memory/examples/motorhead_memory.html
c347bbb365a2-1
Human: whats my name? AI: > Finished chain. ' You said your name is Bob. Is that correct?' llm_chain.run("whats for dinner?") > Entering new LLMChain chain... Prompt after formatting: You are a chatbot having a conversation with a human. Human: hi im bob AI: Hi Bob, nice to meet you! How are you doing today? Human: whats my name? AI: You said your name is Bob. Is that correct? Human: whats for dinner? AI: > Finished chain. " I'm sorry, I'm not sure what you're asking. Could you please rephrase your question?" previous Mongodb Chat Message History next Motörhead Memory (Managed) Contents Setup By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/examples/motorhead_memory.html
27b0de54cd79-0
.ipynb .pdf Mongodb Chat Message History Mongodb Chat Message History# This notebook goes over how to use Mongodb to store chat message history. MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License (SSPL). - Wikipedia # Provide the connection string to connect to the MongoDB database connection_string = "mongodb://mongo_user:password123@mongo:27017" from langchain.memory import MongoDBChatMessageHistory message_history = MongoDBChatMessageHistory( connection_string=connection_string, session_id="test-session" ) message_history.add_user_message("hi!") message_history.add_ai_message("whats up?") message_history.messages [HumanMessage(content='hi!', additional_kwargs={}, example=False), AIMessage(content='whats up?', additional_kwargs={}, example=False)] previous Momento next Motörhead Memory By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/examples/mongodb_chat_message_history.html
1256a78a49b0-0
.ipynb .pdf Adding Message Memory backed by a database to an Agent Adding Message Memory backed by a database to an Agent# This notebook goes over adding memory to an Agent where the memory uses an external message store. Before going through this notebook, please walkthrough the following notebooks, as this will build on top of both of them: Adding memory to an LLM Chain Custom Agents Agent with Memory In order to add a memory with an external message store to an agent we are going to do the following steps: We are going to create a RedisChatMessageHistory to connect to an external database to store the messages in. We are going to create an LLMChain using that chat history as memory. We are going to use that LLMChain to create a custom Agent. For the purposes of this exercise, we are going to create a simple custom Agent that has access to a search tool and utilizes the ConversationBufferMemory class. from langchain.agents import ZeroShotAgent, Tool, AgentExecutor from langchain.memory import ConversationBufferMemory from langchain.memory.chat_memory import ChatMessageHistory from langchain.memory.chat_message_histories import RedisChatMessageHistory from langchain import OpenAI, LLMChain from langchain.utilities import GoogleSearchAPIWrapper search = GoogleSearchAPIWrapper() tools = [ Tool( name = "Search", func=search.run, description="useful for when you need to answer questions about current events" ) ] Notice the usage of the chat_history variable in the PromptTemplate, which matches up with the dynamic key name in the ConversationBufferMemory. prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:""" suffix = """Begin!" {chat_history} Question: {input} {agent_scratchpad}"""
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory_in_db.html
1256a78a49b0-1
{chat_history} Question: {input} {agent_scratchpad}""" prompt = ZeroShotAgent.create_prompt( tools, prefix=prefix, suffix=suffix, input_variables=["input", "chat_history", "agent_scratchpad"] ) Now we can create the ChatMessageHistory backed by the database. message_history = RedisChatMessageHistory(url='redis://localhost:6379/0', ttl=600, session_id='my-session') memory = ConversationBufferMemory(memory_key="chat_history", chat_memory=message_history) We can now construct the LLMChain, with the Memory object, and then create the agent. llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt) agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True) agent_chain = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True, memory=memory) agent_chain.run(input="How many people live in canada?") > Entering new AgentExecutor chain... Thought: I need to find out the population of Canada Action: Search Action Input: Population of Canada
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory_in_db.html
1256a78a49b0-2
Action: Search Action Input: Population of Canada Observation: The current population of Canada is 38,566,192 as of Saturday, December 31, 2022, based on Worldometer elaboration of the latest United Nations data. · Canada ... Additional information related to Canadian population trends can be found on Statistics Canada's Population and Demography Portal. Population of Canada (real- ... Index to the latest information from the Census of Population. This survey conducted by Statistics Canada provides a statistical portrait of Canada and its ... 14 records ... Estimated number of persons by quarter of a year and by year, Canada, provinces and territories. The 2021 Canadian census counted a total population of 36,991,981, an increase of around 5.2 percent over the 2016 figure. ... Between 1990 and 2008, the ... ( 2 ) Census reports and other statistical publications from national statistical offices, ( 3 ) Eurostat: Demographic Statistics, ( 4 ) United Nations ... Canada is a country in North America. Its ten provinces and three territories extend from ... Population. • Q4 2022 estimate. 39,292,355 (37th). Information is available for the total Indigenous population and each of the three ... The term 'Aboriginal' or 'Indigenous' used on the Statistics Canada ... Jun 14, 2022 ... Determinants of health are the broad range of personal, social, economic and environmental factors that determine individual and population ... COVID-19 vaccination coverage across Canada by demographics and key populations. Updated every Friday at 12:00 PM Eastern Time. Thought: I now know the final answer Final Answer: The current population of Canada is 38,566,192 as of Saturday, December 31, 2022, based on Worldometer elaboration of the latest United Nations data. > Finished AgentExecutor chain.
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory_in_db.html
1256a78a49b0-3
> Finished AgentExecutor chain. 'The current population of Canada is 38,566,192 as of Saturday, December 31, 2022, based on Worldometer elaboration of the latest United Nations data.' To test the memory of this agent, we can ask a followup question that relies on information in the previous exchange to be answered correctly. agent_chain.run(input="what is their national anthem called?") > Entering new AgentExecutor chain... Thought: I need to find out what the national anthem of Canada is called. Action: Search Action Input: National Anthem of Canada
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory_in_db.html
1256a78a49b0-4
Action: Search Action Input: National Anthem of Canada Observation: Jun 7, 2010 ... https://twitter.com/CanadaImmigrantCanadian National Anthem O Canada in HQ - complete with lyrics, captions, vocals & music.LYRICS:O Canada! Nov 23, 2022 ... After 100 years of tradition, O Canada was proclaimed Canada's national anthem in 1980. The music for O Canada was composed in 1880 by Calixa ... O Canada, national anthem of Canada. It was proclaimed the official national anthem on July 1, 1980. “God Save the Queen” remains the royal anthem of Canada ... O Canada! Our home and native land! True patriot love in all of us command. Car ton bras sait porter l'épée,. Il sait porter la croix! "O Canada" (French: Ô Canada) is the national anthem of Canada. The song was originally commissioned by Lieutenant Governor of Quebec Théodore Robitaille ... Feb 1, 2018 ... It was a simple tweak — just two words. But with that, Canada just voted to make its national anthem, “O Canada,” gender neutral, ... "O Canada" was proclaimed Canada's national anthem on July 1,. 1980, 100 years after it was first sung on June 24, 1880. The music. Patriotic music in Canada dates back over 200 years as a distinct category from British or French patriotism, preceding the first legal steps to ... Feb 4, 2022 ... English version: O Canada! Our home and native land! True patriot love in all of us command. With glowing hearts we ... Feb 1, 2018 ... Canada's Senate has passed a bill making the country's national anthem gender-neutral. If you're not familiar with the words to “O Canada,” ...
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory_in_db.html
1256a78a49b0-5
Thought: I now know the final answer. Final Answer: The national anthem of Canada is called "O Canada". > Finished AgentExecutor chain. 'The national anthem of Canada is called "O Canada".' We can see that the agent remembered that the previous question was about Canada, and properly asked Google Search what the name of Canada’s national anthem was. For fun, let’s compare this to an agent that does NOT have memory. prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:""" suffix = """Begin!" Question: {input} {agent_scratchpad}""" prompt = ZeroShotAgent.create_prompt( tools, prefix=prefix, suffix=suffix, input_variables=["input", "agent_scratchpad"] ) llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt) agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True) agent_without_memory = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True) agent_without_memory.run("How many people live in canada?") > Entering new AgentExecutor chain... Thought: I need to find out the population of Canada Action: Search Action Input: Population of Canada
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory_in_db.html
1256a78a49b0-6
Action: Search Action Input: Population of Canada Observation: The current population of Canada is 38,566,192 as of Saturday, December 31, 2022, based on Worldometer elaboration of the latest United Nations data. · Canada ... Additional information related to Canadian population trends can be found on Statistics Canada's Population and Demography Portal. Population of Canada (real- ... Index to the latest information from the Census of Population. This survey conducted by Statistics Canada provides a statistical portrait of Canada and its ... 14 records ... Estimated number of persons by quarter of a year and by year, Canada, provinces and territories. The 2021 Canadian census counted a total population of 36,991,981, an increase of around 5.2 percent over the 2016 figure. ... Between 1990 and 2008, the ... ( 2 ) Census reports and other statistical publications from national statistical offices, ( 3 ) Eurostat: Demographic Statistics, ( 4 ) United Nations ... Canada is a country in North America. Its ten provinces and three territories extend from ... Population. • Q4 2022 estimate. 39,292,355 (37th). Information is available for the total Indigenous population and each of the three ... The term 'Aboriginal' or 'Indigenous' used on the Statistics Canada ... Jun 14, 2022 ... Determinants of health are the broad range of personal, social, economic and environmental factors that determine individual and population ... COVID-19 vaccination coverage across Canada by demographics and key populations. Updated every Friday at 12:00 PM Eastern Time. Thought: I now know the final answer Final Answer: The current population of Canada is 38,566,192 as of Saturday, December 31, 2022, based on Worldometer elaboration of the latest United Nations data. > Finished AgentExecutor chain.
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory_in_db.html
1256a78a49b0-7
> Finished AgentExecutor chain. 'The current population of Canada is 38,566,192 as of Saturday, December 31, 2022, based on Worldometer elaboration of the latest United Nations data.' agent_without_memory.run("what is their national anthem called?") > Entering new AgentExecutor chain... Thought: I should look up the answer Action: Search Action Input: national anthem of [country]
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory_in_db.html
1256a78a49b0-8
Action: Search Action Input: national anthem of [country] Observation: Most nation states have an anthem, defined as "a song, as of praise, devotion, or patriotism"; most anthems are either marches or hymns in style. List of all countries around the world with its national anthem. ... Title and lyrics in the language of the country and translated into English, Aug 1, 2021 ... 1. Afghanistan, "Milli Surood" (National Anthem) · 2. Armenia, "Mer Hayrenik" (Our Fatherland) · 3. Azerbaijan (a transcontinental country with ... A national anthem is a patriotic musical composition symbolizing and evoking eulogies of the history and traditions of a country or nation. National Anthem of Every Country ; Fiji, “Meda Dau Doka” (“God Bless Fiji”) ; Finland, “Maamme”. (“Our Land”) ; France, “La Marseillaise” (“The Marseillaise”). You can find an anthem in the menu at the top alphabetically or you can use the search feature. This site is focussed on the scholarly study of national anthems ... Feb 13, 2022 ... The 38-year-old country music artist had the honor of singing the National Anthem during this year's big game, and she did not disappoint. Oldest of the World's National Anthems ; France, La Marseillaise (“The Marseillaise”), 1795 ; Argentina, Himno Nacional Argentino (“Argentine National Anthem”) ... Mar 3, 2022 ... Country music star Jessie James Decker gained the respect of music and hockey fans alike after a jaw-dropping rendition of "The Star-Spangled ... This list shows the country on the left, the national anthem in the ... There are many countries over the world who have a national anthem of their own.
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory_in_db.html
1256a78a49b0-9
Thought: I now know the final answer Final Answer: The national anthem of [country] is [name of anthem]. > Finished AgentExecutor chain. 'The national anthem of [country] is [name of anthem].' previous How to add Memory to an Agent next Cassandra Chat Message History By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/examples/agent_with_memory_in_db.html
d036019b4137-0
.ipynb .pdf VectorStore-Backed Memory Contents Initialize your VectorStore Create your the VectorStoreRetrieverMemory Using in a chain VectorStore-Backed Memory# VectorStoreRetrieverMemory stores memories in a VectorDB and queries the top-K most “salient” docs every time it is called. This differs from most of the other Memory classes in that it doesn’t explicitly track the order of interactions. In this case, the “docs” are previous conversation snippets. This can be useful to refer to relevant pieces of information that the AI was told earlier in the conversation. from datetime import datetime from langchain.embeddings.openai import OpenAIEmbeddings from langchain.llms import OpenAI from langchain.memory import VectorStoreRetrieverMemory from langchain.chains import ConversationChain from langchain.prompts import PromptTemplate Initialize your VectorStore# Depending on the store you choose, this step may look different. Consult the relevant VectorStore documentation for more details. import faiss from langchain.docstore import InMemoryDocstore from langchain.vectorstores import FAISS embedding_size = 1536 # Dimensions of the OpenAIEmbeddings index = faiss.IndexFlatL2(embedding_size) embedding_fn = OpenAIEmbeddings().embed_query vectorstore = FAISS(embedding_fn, index, InMemoryDocstore({}), {}) Create your the VectorStoreRetrieverMemory# The memory object is instantiated from any VectorStoreRetriever. # In actual usage, you would set `k` to be a higher value, but we use k=1 to show that # the vector lookup still returns the semantically relevant information retriever = vectorstore.as_retriever(search_kwargs=dict(k=1)) memory = VectorStoreRetrieverMemory(retriever=retriever)
https://python.langchain.com/en/latest/modules/memory/types/vectorstore_retriever_memory.html
d036019b4137-1
memory = VectorStoreRetrieverMemory(retriever=retriever) # When added to an agent, the memory object can save pertinent information from conversations or used tools memory.save_context({"input": "My favorite food is pizza"}, {"output": "thats good to know"}) memory.save_context({"input": "My favorite sport is soccer"}, {"output": "..."}) memory.save_context({"input": "I don't the Celtics"}, {"output": "ok"}) # # Notice the first result returned is the memory pertaining to tax help, which the language model deems more semantically relevant # to a 1099 than the other documents, despite them both containing numbers. print(memory.load_memory_variables({"prompt": "what sport should i watch?"})["history"]) input: My favorite sport is soccer output: ... Using in a chain# Let’s walk through an example, again setting verbose=True so we can see the prompt. llm = OpenAI(temperature=0) # Can be any valid LLM _DEFAULT_TEMPLATE = """The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Relevant pieces of previous conversation: {history} (You do not need to use these pieces of information if not relevant) Current conversation: Human: {input} AI:""" PROMPT = PromptTemplate( input_variables=["history", "input"], template=_DEFAULT_TEMPLATE ) conversation_with_summary = ConversationChain( llm=llm, prompt=PROMPT, # We set a very low max_token_limit for the purposes of testing. memory=memory, verbose=True )
https://python.langchain.com/en/latest/modules/memory/types/vectorstore_retriever_memory.html
d036019b4137-2
memory=memory, verbose=True ) conversation_with_summary.predict(input="Hi, my name is Perry, what's up?") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Relevant pieces of previous conversation: input: My favorite food is pizza output: thats good to know (You do not need to use these pieces of information if not relevant) Current conversation: Human: Hi, my name is Perry, what's up? AI: > Finished chain. " Hi Perry, I'm doing well. How about you?" # Here, the basketball related content is surfaced conversation_with_summary.predict(input="what's my favorite sport?") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Relevant pieces of previous conversation: input: My favorite sport is soccer output: ... (You do not need to use these pieces of information if not relevant) Current conversation: Human: what's my favorite sport? AI: > Finished chain. ' You told me earlier that your favorite sport is soccer.' # Even though the language model is stateless, since relavent memory is fetched, it can "reason" about the time. # Timestamping memories and data is useful in general to let the agent determine temporal relevance conversation_with_summary.predict(input="Whats my favorite food") > Entering new ConversationChain chain...
https://python.langchain.com/en/latest/modules/memory/types/vectorstore_retriever_memory.html
d036019b4137-3
> Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Relevant pieces of previous conversation: input: My favorite food is pizza output: thats good to know (You do not need to use these pieces of information if not relevant) Current conversation: Human: Whats my favorite food AI: > Finished chain. ' You said your favorite food is pizza.' # The memories from the conversation are automatically stored, # since this query best matches the introduction chat above, # the agent is able to 'remember' the user's name. conversation_with_summary.predict(input="What's my name?") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Relevant pieces of previous conversation: input: Hi, my name is Perry, what's up? response: Hi Perry, I'm doing well. How about you? (You do not need to use these pieces of information if not relevant) Current conversation: Human: What's my name? AI: > Finished chain. ' Your name is Perry.' previous ConversationTokenBufferMemory next How to add Memory to an LLMChain Contents Initialize your VectorStore Create your the VectorStoreRetrieverMemory Using in a chain By Harrison Chase © Copyright 2023, Harrison Chase.
https://python.langchain.com/en/latest/modules/memory/types/vectorstore_retriever_memory.html
d036019b4137-4
By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/types/vectorstore_retriever_memory.html
4aaeac0bd8f1-0
.ipynb .pdf ConversationBufferWindowMemory Contents Using in a chain ConversationBufferWindowMemory# ConversationBufferWindowMemory keeps a list of the interactions of the conversation over time. It only uses the last K interactions. This can be useful for keeping a sliding window of the most recent interactions, so the buffer does not get too large Let’s first explore the basic functionality of this type of memory. from langchain.memory import ConversationBufferWindowMemory memory = ConversationBufferWindowMemory( k=1) memory.save_context({"input": "hi"}, {"output": "whats up"}) memory.save_context({"input": "not much you"}, {"output": "not much"}) memory.load_memory_variables({}) {'history': 'Human: not much you\nAI: not much'} We can also get the history as a list of messages (this is useful if you are using this with a chat model). memory = ConversationBufferWindowMemory( k=1, return_messages=True) memory.save_context({"input": "hi"}, {"output": "whats up"}) memory.save_context({"input": "not much you"}, {"output": "not much"}) memory.load_memory_variables({}) {'history': [HumanMessage(content='not much you', additional_kwargs={}), AIMessage(content='not much', additional_kwargs={})]} Using in a chain# Let’s walk through an example, again setting verbose=True so we can see the prompt. from langchain.llms import OpenAI from langchain.chains import ConversationChain conversation_with_summary = ConversationChain( llm=OpenAI(temperature=0), # We set a low k=2, to only keep the last 2 interactions in memory memory=ConversationBufferWindowMemory(k=2), verbose=True )
https://python.langchain.com/en/latest/modules/memory/types/buffer_window.html
4aaeac0bd8f1-1
memory=ConversationBufferWindowMemory(k=2), verbose=True ) conversation_with_summary.predict(input="Hi, what's up?") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Hi, what's up? AI: > Finished chain. " Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you?" conversation_with_summary.predict(input="What's their issues?") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Hi, what's up? AI: Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you? Human: What's their issues? AI: > Finished chain. " The customer is having trouble connecting to their Wi-Fi network. I'm helping them troubleshoot the issue and get them connected." conversation_with_summary.predict(input="Is it going well?") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Hi, what's up?
https://python.langchain.com/en/latest/modules/memory/types/buffer_window.html
4aaeac0bd8f1-2
Current conversation: Human: Hi, what's up? AI: Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you? Human: What's their issues? AI: The customer is having trouble connecting to their Wi-Fi network. I'm helping them troubleshoot the issue and get them connected. Human: Is it going well? AI: > Finished chain. " Yes, it's going well so far. We've already identified the problem and are now working on a solution." # Notice here that the first interaction does not appear. conversation_with_summary.predict(input="What's the solution?") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: What's their issues? AI: The customer is having trouble connecting to their Wi-Fi network. I'm helping them troubleshoot the issue and get them connected. Human: Is it going well? AI: Yes, it's going well so far. We've already identified the problem and are now working on a solution. Human: What's the solution? AI: > Finished chain. " The solution is to reset the router and reconfigure the settings. We're currently in the process of doing that." previous ConversationBufferMemory next Entity Memory Contents Using in a chain By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/types/buffer_window.html
91c89f183176-0
.ipynb .pdf ConversationBufferMemory Contents Using in a chain ConversationBufferMemory# This notebook shows how to use ConversationBufferMemory. This memory allows for storing of messages and then extracts the messages in a variable. We can first extract it as a string. from langchain.memory import ConversationBufferMemory memory = ConversationBufferMemory() memory.save_context({"input": "hi"}, {"output": "whats up"}) memory.load_memory_variables({}) {'history': 'Human: hi\nAI: whats up'} We can also get the history as a list of messages (this is useful if you are using this with a chat model). memory = ConversationBufferMemory(return_messages=True) memory.save_context({"input": "hi"}, {"output": "whats up"}) memory.load_memory_variables({}) {'history': [HumanMessage(content='hi', additional_kwargs={}), AIMessage(content='whats up', additional_kwargs={})]} Using in a chain# Finally, let’s take a look at using this in a chain (setting verbose=True so we can see the prompt). from langchain.llms import OpenAI from langchain.chains import ConversationChain llm = OpenAI(temperature=0) conversation = ConversationChain( llm=llm, verbose=True, memory=ConversationBufferMemory() ) conversation.predict(input="Hi there!") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Hi there! AI: > Finished chain.
https://python.langchain.com/en/latest/modules/memory/types/buffer.html
91c89f183176-1
Current conversation: Human: Hi there! AI: > Finished chain. " Hi there! It's nice to meet you. How can I help you today?" conversation.predict(input="I'm doing well! Just having a conversation with an AI.") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Hi there! AI: Hi there! It's nice to meet you. How can I help you today? Human: I'm doing well! Just having a conversation with an AI. AI: > Finished chain. " That's great! It's always nice to have a conversation with someone new. What would you like to talk about?" conversation.predict(input="Tell me about yourself.") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Hi there! AI: Hi there! It's nice to meet you. How can I help you today? Human: I'm doing well! Just having a conversation with an AI. AI: That's great! It's always nice to have a conversation with someone new. What would you like to talk about? Human: Tell me about yourself. AI: > Finished chain.
https://python.langchain.com/en/latest/modules/memory/types/buffer.html
91c89f183176-2
Human: Tell me about yourself. AI: > Finished chain. " Sure! I'm an AI created to help people with their everyday tasks. I'm programmed to understand natural language and provide helpful information. I'm also constantly learning and updating my knowledge base so I can provide more accurate and helpful answers." And that’s it for the getting started! There are plenty of different types of memory, check out our examples to see them all previous How-To Guides next ConversationBufferWindowMemory Contents Using in a chain By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/types/buffer.html
8ab0d904b90b-0
.ipynb .pdf Entity Memory Contents Using in a chain Inspecting the memory store Entity Memory# This notebook shows how to work with a memory module that remembers things about specific entities. It extracts information on entities (using LLMs) and builds up its knowledge about that entity over time (also using LLMs). Let’s first walk through using this functionality. from langchain.llms import OpenAI from langchain.memory import ConversationEntityMemory llm = OpenAI(temperature=0) memory = ConversationEntityMemory(llm=llm) _input = {"input": "Deven & Sam are working on a hackathon project"} memory.load_memory_variables(_input) memory.save_context( _input, {"output": " That sounds like a great project! What kind of project are they working on?"} ) memory.load_memory_variables({"input": 'who is Sam'}) {'history': 'Human: Deven & Sam are working on a hackathon project\nAI: That sounds like a great project! What kind of project are they working on?', 'entities': {'Sam': 'Sam is working on a hackathon project with Deven.'}} memory = ConversationEntityMemory(llm=llm, return_messages=True) _input = {"input": "Deven & Sam are working on a hackathon project"} memory.load_memory_variables(_input) memory.save_context( _input, {"output": " That sounds like a great project! What kind of project are they working on?"} ) memory.load_memory_variables({"input": 'who is Sam'}) {'history': [HumanMessage(content='Deven & Sam are working on a hackathon project', additional_kwargs={}), AIMessage(content=' That sounds like a great project! What kind of project are they working on?', additional_kwargs={})],
https://python.langchain.com/en/latest/modules/memory/types/entity_summary_memory.html
8ab0d904b90b-1
'entities': {'Sam': 'Sam is working on a hackathon project with Deven.'}} Using in a chain# Let’s now use it in a chain! from langchain.chains import ConversationChain from langchain.memory import ConversationEntityMemory from langchain.memory.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE from pydantic import BaseModel from typing import List, Dict, Any conversation = ConversationChain( llm=llm, verbose=True, prompt=ENTITY_MEMORY_CONVERSATION_TEMPLATE, memory=ConversationEntityMemory(llm=llm) ) conversation.predict(input="Deven & Sam are working on a hackathon project") > Entering new ConversationChain chain... Prompt after formatting: You are an assistant to a human, powered by a large language model trained by OpenAI. You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand. You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.
https://python.langchain.com/en/latest/modules/memory/types/entity_summary_memory.html
8ab0d904b90b-2
Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist. Context: {'Deven': 'Deven is working on a hackathon project with Sam.', 'Sam': 'Sam is working on a hackathon project with Deven.'} Current conversation: Last line: Human: Deven & Sam are working on a hackathon project You: > Finished chain. ' That sounds like a great project! What kind of project are they working on?' conversation.memory.entity_store.store {'Deven': 'Deven is working on a hackathon project with Sam, which they are entering into a hackathon.', 'Sam': 'Sam is working on a hackathon project with Deven.'} conversation.predict(input="They are trying to add more complex memory structures to Langchain") > Entering new ConversationChain chain... Prompt after formatting: You are an assistant to a human, powered by a large language model trained by OpenAI. You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
https://python.langchain.com/en/latest/modules/memory/types/entity_summary_memory.html
8ab0d904b90b-3
You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics. Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist. Context: {'Deven': 'Deven is working on a hackathon project with Sam, which they are entering into a hackathon.', 'Sam': 'Sam is working on a hackathon project with Deven.', 'Langchain': ''} Current conversation: Human: Deven & Sam are working on a hackathon project AI: That sounds like a great project! What kind of project are they working on? Last line: Human: They are trying to add more complex memory structures to Langchain You: > Finished chain. ' That sounds like an interesting project! What kind of memory structures are they trying to add?' conversation.predict(input="They are adding in a key-value store for entities mentioned so far in the conversation.") > Entering new ConversationChain chain... Prompt after formatting: You are an assistant to a human, powered by a large language model trained by OpenAI.
https://python.langchain.com/en/latest/modules/memory/types/entity_summary_memory.html
8ab0d904b90b-4
You are an assistant to a human, powered by a large language model trained by OpenAI. You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand. You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics. Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist. Context: {'Deven': 'Deven is working on a hackathon project with Sam, which they are entering into a hackathon. They are trying to add more complex memory structures to Langchain.', 'Sam': 'Sam is working on a hackathon project with Deven, trying to add more complex memory structures to Langchain.', 'Langchain': 'Langchain is a project that is trying to add more complex memory structures.', 'Key-Value Store': ''} Current conversation: Human: Deven & Sam are working on a hackathon project AI: That sounds like a great project! What kind of project are they working on?
https://python.langchain.com/en/latest/modules/memory/types/entity_summary_memory.html
8ab0d904b90b-5
AI: That sounds like a great project! What kind of project are they working on? Human: They are trying to add more complex memory structures to Langchain AI: That sounds like an interesting project! What kind of memory structures are they trying to add? Last line: Human: They are adding in a key-value store for entities mentioned so far in the conversation. You: > Finished chain. ' That sounds like a great idea! How will the key-value store help with the project?' conversation.predict(input="What do you know about Deven & Sam?") > Entering new ConversationChain chain... Prompt after formatting: You are an assistant to a human, powered by a large language model trained by OpenAI. You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand. You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics. Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist. Context:
https://python.langchain.com/en/latest/modules/memory/types/entity_summary_memory.html
8ab0d904b90b-6
Context: {'Deven': 'Deven is working on a hackathon project with Sam, which they are entering into a hackathon. They are trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation.', 'Sam': 'Sam is working on a hackathon project with Deven, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation.'} Current conversation: Human: Deven & Sam are working on a hackathon project AI: That sounds like a great project! What kind of project are they working on? Human: They are trying to add more complex memory structures to Langchain AI: That sounds like an interesting project! What kind of memory structures are they trying to add? Human: They are adding in a key-value store for entities mentioned so far in the conversation. AI: That sounds like a great idea! How will the key-value store help with the project? Last line: Human: What do you know about Deven & Sam? You: > Finished chain. ' Deven and Sam are working on a hackathon project together, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation. They seem to be working hard on this project and have a great idea for how the key-value store can help.' Inspecting the memory store# We can also inspect the memory store directly. In the following examaples, we look at it directly, and then go through some examples of adding information and watch how it changes. from pprint import pprint pprint(conversation.memory.entity_store.store) {'Daimon': 'Daimon is a company founded by Sam, a successful entrepreneur.',
https://python.langchain.com/en/latest/modules/memory/types/entity_summary_memory.html
8ab0d904b90b-7
'Deven': 'Deven is working on a hackathon project with Sam, which they are ' 'entering into a hackathon. They are trying to add more complex ' 'memory structures to Langchain, including a key-value store for ' 'entities mentioned so far in the conversation, and seem to be ' 'working hard on this project with a great idea for how the ' 'key-value store can help.', 'Key-Value Store': 'A key-value store is being added to the project to store ' 'entities mentioned in the conversation.', 'Langchain': 'Langchain is a project that is trying to add more complex ' 'memory structures, including a key-value store for entities ' 'mentioned so far in the conversation.', 'Sam': 'Sam is working on a hackathon project with Deven, trying to add more ' 'complex memory structures to Langchain, including a key-value store ' 'for entities mentioned so far in the conversation. They seem to have ' 'a great idea for how the key-value store can help, and Sam is also ' 'the founder of a company called Daimon.'} conversation.predict(input="Sam is the founder of a company called Daimon.") > Entering new ConversationChain chain... Prompt after formatting: You are an assistant to a human, powered by a large language model trained by OpenAI. You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
https://python.langchain.com/en/latest/modules/memory/types/entity_summary_memory.html
8ab0d904b90b-8
You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics. Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist. Context: {'Daimon': 'Daimon is a company founded by Sam, a successful entrepreneur.', 'Sam': 'Sam is working on a hackathon project with Deven, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation. They seem to have a great idea for how the key-value store can help, and Sam is also the founder of a company called Daimon.'} Current conversation: Human: They are adding in a key-value store for entities mentioned so far in the conversation. AI: That sounds like a great idea! How will the key-value store help with the project? Human: What do you know about Deven & Sam? AI: Deven and Sam are working on a hackathon project together, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation. They seem to be working hard on this project and have a great idea for how the key-value store can help. Human: Sam is the founder of a company called Daimon. AI:
https://python.langchain.com/en/latest/modules/memory/types/entity_summary_memory.html
8ab0d904b90b-9
Human: Sam is the founder of a company called Daimon. AI: That's impressive! It sounds like Sam is a very successful entrepreneur. What kind of company is Daimon? Last line: Human: Sam is the founder of a company called Daimon. You: > Finished chain. " That's impressive! It sounds like Sam is a very successful entrepreneur. What kind of company is Daimon?" from pprint import pprint pprint(conversation.memory.entity_store.store) {'Daimon': 'Daimon is a company founded by Sam, a successful entrepreneur, who ' 'is working on a hackathon project with Deven to add more complex ' 'memory structures to Langchain.', 'Deven': 'Deven is working on a hackathon project with Sam, which they are ' 'entering into a hackathon. They are trying to add more complex ' 'memory structures to Langchain, including a key-value store for ' 'entities mentioned so far in the conversation, and seem to be ' 'working hard on this project with a great idea for how the ' 'key-value store can help.', 'Key-Value Store': 'A key-value store is being added to the project to store ' 'entities mentioned in the conversation.', 'Langchain': 'Langchain is a project that is trying to add more complex ' 'memory structures, including a key-value store for entities ' 'mentioned so far in the conversation.', 'Sam': 'Sam is working on a hackathon project with Deven, trying to add more ' 'complex memory structures to Langchain, including a key-value store ' 'for entities mentioned so far in the conversation. They seem to have '
https://python.langchain.com/en/latest/modules/memory/types/entity_summary_memory.html
8ab0d904b90b-10
'for entities mentioned so far in the conversation. They seem to have ' 'a great idea for how the key-value store can help, and Sam is also ' 'the founder of a successful company called Daimon.'} conversation.predict(input="What do you know about Sam?") > Entering new ConversationChain chain... Prompt after formatting: You are an assistant to a human, powered by a large language model trained by OpenAI. You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand. You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics. Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist. Context:
https://python.langchain.com/en/latest/modules/memory/types/entity_summary_memory.html
8ab0d904b90b-11
Context: {'Deven': 'Deven is working on a hackathon project with Sam, which they are entering into a hackathon. They are trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation, and seem to be working hard on this project with a great idea for how the key-value store can help.', 'Sam': 'Sam is working on a hackathon project with Deven, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation. They seem to have a great idea for how the key-value store can help, and Sam is also the founder of a successful company called Daimon.', 'Langchain': 'Langchain is a project that is trying to add more complex memory structures, including a key-value store for entities mentioned so far in the conversation.', 'Daimon': 'Daimon is a company founded by Sam, a successful entrepreneur, who is working on a hackathon project with Deven to add more complex memory structures to Langchain.'} Current conversation: Human: What do you know about Deven & Sam? AI: Deven and Sam are working on a hackathon project together, trying to add more complex memory structures to Langchain, including a key-value store for entities mentioned so far in the conversation. They seem to be working hard on this project and have a great idea for how the key-value store can help. Human: Sam is the founder of a company called Daimon. AI: That's impressive! It sounds like Sam is a very successful entrepreneur. What kind of company is Daimon? Human: Sam is the founder of a company called Daimon. AI: That's impressive! It sounds like Sam is a very successful entrepreneur. What kind of company is Daimon? Last line:
https://python.langchain.com/en/latest/modules/memory/types/entity_summary_memory.html
8ab0d904b90b-12
Last line: Human: What do you know about Sam? You: > Finished chain. ' Sam is the founder of a successful company called Daimon. He is also working on a hackathon project with Deven to add more complex memory structures to Langchain. They seem to have a great idea for how the key-value store can help.' previous ConversationBufferWindowMemory next Conversation Knowledge Graph Memory Contents Using in a chain Inspecting the memory store By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/types/entity_summary_memory.html
406662fccf3d-0
.ipynb .pdf ConversationTokenBufferMemory Contents Using in a chain ConversationTokenBufferMemory# ConversationTokenBufferMemory keeps a buffer of recent interactions in memory, and uses token length rather than number of interactions to determine when to flush interactions. Let’s first walk through how to use the utilities from langchain.memory import ConversationTokenBufferMemory from langchain.llms import OpenAI llm = OpenAI() memory = ConversationTokenBufferMemory(llm=llm, max_token_limit=10) memory.save_context({"input": "hi"}, {"output": "whats up"}) memory.save_context({"input": "not much you"}, {"output": "not much"}) memory.load_memory_variables({}) {'history': 'Human: not much you\nAI: not much'} We can also get the history as a list of messages (this is useful if you are using this with a chat model). memory = ConversationTokenBufferMemory(llm=llm, max_token_limit=10, return_messages=True) memory.save_context({"input": "hi"}, {"output": "whats up"}) memory.save_context({"input": "not much you"}, {"output": "not much"}) Using in a chain# Let’s walk through an example, again setting verbose=True so we can see the prompt. from langchain.chains import ConversationChain conversation_with_summary = ConversationChain( llm=llm, # We set a very low max_token_limit for the purposes of testing. memory=ConversationTokenBufferMemory(llm=OpenAI(), max_token_limit=60), verbose=True ) conversation_with_summary.predict(input="Hi, what's up?") > Entering new ConversationChain chain... Prompt after formatting:
https://python.langchain.com/en/latest/modules/memory/types/token_buffer.html
406662fccf3d-1
> Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Hi, what's up? AI: > Finished chain. " Hi there! I'm doing great, just enjoying the day. How about you?" conversation_with_summary.predict(input="Just working on writing some documentation!") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Hi, what's up? AI: Hi there! I'm doing great, just enjoying the day. How about you? Human: Just working on writing some documentation! AI: > Finished chain. ' Sounds like a productive day! What kind of documentation are you writing?' conversation_with_summary.predict(input="For LangChain! Have you heard of it?") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Hi, what's up? AI: Hi there! I'm doing great, just enjoying the day. How about you? Human: Just working on writing some documentation! AI: Sounds like a productive day! What kind of documentation are you writing?
https://python.langchain.com/en/latest/modules/memory/types/token_buffer.html
406662fccf3d-2
AI: Sounds like a productive day! What kind of documentation are you writing? Human: For LangChain! Have you heard of it? AI: > Finished chain. " Yes, I have heard of LangChain! It is a decentralized language-learning platform that connects native speakers and learners in real time. Is that the documentation you're writing about?" # We can see here that the buffer is updated conversation_with_summary.predict(input="Haha nope, although a lot of people confuse it for that") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: For LangChain! Have you heard of it? AI: Yes, I have heard of LangChain! It is a decentralized language-learning platform that connects native speakers and learners in real time. Is that the documentation you're writing about? Human: Haha nope, although a lot of people confuse it for that AI: > Finished chain. " Oh, I see. Is there another language learning platform you're referring to?" previous ConversationSummaryBufferMemory next VectorStore-Backed Memory Contents Using in a chain By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/types/token_buffer.html
1a34a9441f2d-0
.ipynb .pdf Conversation Knowledge Graph Memory Contents Using in a chain Conversation Knowledge Graph Memory# This type of memory uses a knowledge graph to recreate memory. Let’s first walk through how to use the utilities from langchain.memory import ConversationKGMemory from langchain.llms import OpenAI llm = OpenAI(temperature=0) memory = ConversationKGMemory(llm=llm) memory.save_context({"input": "say hi to sam"}, {"output": "who is sam"}) memory.save_context({"input": "sam is a friend"}, {"output": "okay"}) memory.load_memory_variables({"input": 'who is sam'}) {'history': 'On Sam: Sam is friend.'} We can also get the history as a list of messages (this is useful if you are using this with a chat model). memory = ConversationKGMemory(llm=llm, return_messages=True) memory.save_context({"input": "say hi to sam"}, {"output": "who is sam"}) memory.save_context({"input": "sam is a friend"}, {"output": "okay"}) memory.load_memory_variables({"input": 'who is sam'}) {'history': [SystemMessage(content='On Sam: Sam is friend.', additional_kwargs={})]} We can also more modularly get current entities from a new message (will use previous messages as context.) memory.get_current_entities("what's Sams favorite color?") ['Sam'] We can also more modularly get knowledge triplets from a new message (will use previous messages as context.) memory.get_knowledge_triplets("her favorite color is red") [KnowledgeTriple(subject='Sam', predicate='favorite color', object_='red')] Using in a chain# Let’s now use this in a chain! llm = OpenAI(temperature=0)
https://python.langchain.com/en/latest/modules/memory/types/kg.html
1a34a9441f2d-1
llm = OpenAI(temperature=0) from langchain.prompts.prompt import PromptTemplate from langchain.chains import ConversationChain template = """The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. The AI ONLY uses information contained in the "Relevant Information" section and does not hallucinate. Relevant Information: {history} Conversation: Human: {input} AI:""" prompt = PromptTemplate( input_variables=["history", "input"], template=template ) conversation_with_kg = ConversationChain( llm=llm, verbose=True, prompt=prompt, memory=ConversationKGMemory(llm=llm) ) conversation_with_kg.predict(input="Hi, what's up?") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. The AI ONLY uses information contained in the "Relevant Information" section and does not hallucinate. Relevant Information: Conversation: Human: Hi, what's up? AI: > Finished chain. " Hi there! I'm doing great. I'm currently in the process of learning about the world around me. I'm learning about different cultures, languages, and customs. It's really fascinating! How about you?" conversation_with_kg.predict(input="My name is James and I'm helping Will. He's an engineer.") > Entering new ConversationChain chain...
https://python.langchain.com/en/latest/modules/memory/types/kg.html
1a34a9441f2d-2
> Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. The AI ONLY uses information contained in the "Relevant Information" section and does not hallucinate. Relevant Information: Conversation: Human: My name is James and I'm helping Will. He's an engineer. AI: > Finished chain. " Hi James, it's nice to meet you. I'm an AI and I understand you're helping Will, the engineer. What kind of engineering does he do?" conversation_with_kg.predict(input="What do you know about Will?") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. The AI ONLY uses information contained in the "Relevant Information" section and does not hallucinate. Relevant Information: On Will: Will is an engineer. Conversation: Human: What do you know about Will? AI: > Finished chain. ' Will is an engineer.' previous Entity Memory next ConversationSummaryMemory Contents Using in a chain By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/types/kg.html
b8f53041aea3-0
.ipynb .pdf ConversationSummaryBufferMemory Contents Using in a chain ConversationSummaryBufferMemory# ConversationSummaryBufferMemory combines the last two ideas. It keeps a buffer of recent interactions in memory, but rather than just completely flushing old interactions it compiles them into a summary and uses both. Unlike the previous implementation though, it uses token length rather than number of interactions to determine when to flush interactions. Let’s first walk through how to use the utilities from langchain.memory import ConversationSummaryBufferMemory from langchain.llms import OpenAI llm = OpenAI() memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=10) memory.save_context({"input": "hi"}, {"output": "whats up"}) memory.save_context({"input": "not much you"}, {"output": "not much"}) memory.load_memory_variables({}) {'history': 'System: \nThe human says "hi", and the AI responds with "whats up".\nHuman: not much you\nAI: not much'} We can also get the history as a list of messages (this is useful if you are using this with a chat model). memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=10, return_messages=True) memory.save_context({"input": "hi"}, {"output": "whats up"}) memory.save_context({"input": "not much you"}, {"output": "not much"}) We can also utilize the predict_new_summary method directly. messages = memory.chat_memory.messages previous_summary = "" memory.predict_new_summary(messages, previous_summary) '\nThe human and AI state that they are not doing much.' Using in a chain# Let’s walk through an example, again setting verbose=True so we can see the prompt. from langchain.chains import ConversationChain conversation_with_summary = ConversationChain(
https://python.langchain.com/en/latest/modules/memory/types/summary_buffer.html
b8f53041aea3-1
from langchain.chains import ConversationChain conversation_with_summary = ConversationChain( llm=llm, # We set a very low max_token_limit for the purposes of testing. memory=ConversationSummaryBufferMemory(llm=OpenAI(), max_token_limit=40), verbose=True ) conversation_with_summary.predict(input="Hi, what's up?") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Hi, what's up? AI: > Finished chain. " Hi there! I'm doing great. I'm learning about the latest advances in artificial intelligence. What about you?" conversation_with_summary.predict(input="Just working on writing some documentation!") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Hi, what's up? AI: Hi there! I'm doing great. I'm spending some time learning about the latest developments in AI technology. How about you? Human: Just working on writing some documentation! AI: > Finished chain. ' That sounds like a great use of your time. Do you have experience with writing documentation?' # We can see here that there is a summary of the conversation and then some previous interactions conversation_with_summary.predict(input="For LangChain! Have you heard of it?") > Entering new ConversationChain chain...
https://python.langchain.com/en/latest/modules/memory/types/summary_buffer.html
b8f53041aea3-2
> Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: System: The human asked the AI what it was up to and the AI responded that it was learning about the latest developments in AI technology. Human: Just working on writing some documentation! AI: That sounds like a great use of your time. Do you have experience with writing documentation? Human: For LangChain! Have you heard of it? AI: > Finished chain. " No, I haven't heard of LangChain. Can you tell me more about it?" # We can see here that the summary and the buffer are updated conversation_with_summary.predict(input="Haha nope, although a lot of people confuse it for that") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: System: The human asked the AI what it was up to and the AI responded that it was learning about the latest developments in AI technology. The human then mentioned they were writing documentation, to which the AI responded that it sounded like a great use of their time and asked if they had experience with writing documentation. Human: For LangChain! Have you heard of it? AI: No, I haven't heard of LangChain. Can you tell me more about it? Human: Haha nope, although a lot of people confuse it for that AI:
https://python.langchain.com/en/latest/modules/memory/types/summary_buffer.html
b8f53041aea3-3
Human: Haha nope, although a lot of people confuse it for that AI: > Finished chain. ' Oh, okay. What is LangChain?' previous ConversationSummaryMemory next ConversationTokenBufferMemory Contents Using in a chain By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/types/summary_buffer.html
f084ccdd4c84-0
.ipynb .pdf ConversationSummaryMemory Contents Initializing with messages Using in a chain ConversationSummaryMemory# Now let’s take a look at using a slightly more complex type of memory - ConversationSummaryMemory. This type of memory creates a summary of the conversation over time. This can be useful for condensing information from the conversation over time. Let’s first explore the basic functionality of this type of memory. from langchain.memory import ConversationSummaryMemory, ChatMessageHistory from langchain.llms import OpenAI memory = ConversationSummaryMemory(llm=OpenAI(temperature=0)) memory.save_context({"input": "hi"}, {"output": "whats up"}) memory.load_memory_variables({}) {'history': '\nThe human greets the AI, to which the AI responds.'} We can also get the history as a list of messages (this is useful if you are using this with a chat model). memory = ConversationSummaryMemory(llm=OpenAI(temperature=0), return_messages=True) memory.save_context({"input": "hi"}, {"output": "whats up"}) memory.load_memory_variables({}) {'history': [SystemMessage(content='\nThe human greets the AI, to which the AI responds.', additional_kwargs={})]} We can also utilize the predict_new_summary method directly. messages = memory.chat_memory.messages previous_summary = "" memory.predict_new_summary(messages, previous_summary) '\nThe human greets the AI, to which the AI responds.' Initializing with messages# If you have messages outside this class, you can easily initialize the class with ChatMessageHistory. During loading, a summary will be calculated. history = ChatMessageHistory() history.add_user_message("hi") history.add_ai_message("hi there!")
https://python.langchain.com/en/latest/modules/memory/types/summary.html
f084ccdd4c84-1
history.add_user_message("hi") history.add_ai_message("hi there!") memory = ConversationSummaryMemory.from_messages(llm=OpenAI(temperature=0), chat_memory=history, return_messages=True) memory.buffer '\nThe human greets the AI, to which the AI responds with a friendly greeting.' Using in a chain# Let’s walk through an example of using this in a chain, again setting verbose=True so we can see the prompt. from langchain.llms import OpenAI from langchain.chains import ConversationChain llm = OpenAI(temperature=0) conversation_with_summary = ConversationChain( llm=llm, memory=ConversationSummaryMemory(llm=OpenAI()), verbose=True ) conversation_with_summary.predict(input="Hi, what's up?") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: Human: Hi, what's up? AI: > Finished chain. " Hi there! I'm doing great. I'm currently helping a customer with a technical issue. How about you?" conversation_with_summary.predict(input="Tell me more about it!") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: The human greeted the AI and asked how it was doing. The AI replied that it was doing great and was currently helping a customer with a technical issue.
https://python.langchain.com/en/latest/modules/memory/types/summary.html
f084ccdd4c84-2
Human: Tell me more about it! AI: > Finished chain. " Sure! The customer is having trouble with their computer not connecting to the internet. I'm helping them troubleshoot the issue and figure out what the problem is. So far, we've tried resetting the router and checking the network settings, but the issue still persists. We're currently looking into other possible solutions." conversation_with_summary.predict(input="Very cool -- what is the scope of the project?") > Entering new ConversationChain chain... Prompt after formatting: The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. Current conversation: The human greeted the AI and asked how it was doing. The AI replied that it was doing great and was currently helping a customer with a technical issue where their computer was not connecting to the internet. The AI was troubleshooting the issue and had already tried resetting the router and checking the network settings, but the issue still persisted and they were looking into other possible solutions. Human: Very cool -- what is the scope of the project? AI: > Finished chain. " The scope of the project is to troubleshoot the customer's computer issue and find a solution that will allow them to connect to the internet. We are currently exploring different possibilities and have already tried resetting the router and checking the network settings, but the issue still persists." previous Conversation Knowledge Graph Memory next ConversationSummaryBufferMemory Contents Initializing with messages Using in a chain By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/memory/types/summary.html
80a7eebac8a4-0
.rst .pdf Example Selectors Example Selectors# Note Conceptual Guide If you have a large number of examples, you may need to select which ones to include in the prompt. The ExampleSelector is the class responsible for doing so. The base interface is defined as below: class BaseExampleSelector(ABC): """Interface for selecting examples to include in prompts.""" @abstractmethod def select_examples(self, input_variables: Dict[str, str]) -> List[dict]: """Select which examples to use based on the inputs.""" The only method it needs to expose is a select_examples method. This takes in the input variables and then returns a list of examples. It is up to each specific implementation as to how those examples are selected. Let’s take a look at some below. See below for a list of example selectors. How to create a custom example selector LengthBased ExampleSelector Maximal Marginal Relevance ExampleSelector NGram Overlap ExampleSelector Similarity ExampleSelector previous Chat Prompt Template next How to create a custom example selector By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/prompts/example_selectors.html
72977666f022-0
.rst .pdf Prompt Templates Prompt Templates# Note Conceptual Guide Language models take text as input - that text is commonly referred to as a prompt. Typically this is not simply a hardcoded string but rather a combination of a template, some examples, and user input. LangChain provides several classes and functions to make constructing and working with prompts easy. The following sections of documentation are provided: Getting Started: An overview of all the functionality LangChain provides for working with and constructing prompts. How-To Guides: A collection of how-to guides. These highlight how to accomplish various objectives with our prompt class. Reference: API reference documentation for all prompt classes. previous Getting Started next Getting Started By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/prompts/prompt_templates.html
f13f4b6335f5-0
.rst .pdf Output Parsers Output Parsers# Note Conceptual Guide Language models output text. But many times you may want to get more structured information than just text back. This is where output parsers come in. Output parsers are classes that help structure language model responses. There are two main methods an output parser must implement: get_format_instructions() -> str: A method which returns a string containing instructions for how the output of a language model should be formatted. parse(str) -> Any: A method which takes in a string (assumed to be the response from a language model) and parses it into some structure. And then one optional one: parse_with_prompt(str) -> Any: A method which takes in a string (assumed to be the response from a language model) and a prompt (assumed to the prompt that generated such a response) and parses it into some structure. The prompt is largely provided in the event the OutputParser wants to retry or fix the output in some way, and needs information from the prompt to do so. To start, we recommend familiarizing yourself with the Getting Started section Output Parsers After that, we provide deep dives on all the different types of output parsers. CommaSeparatedListOutputParser Datetime Enum Output Parser OutputFixingParser PydanticOutputParser RetryOutputParser Structured Output Parser previous Similarity ExampleSelector next Output Parsers By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/prompts/output_parsers.html
6aafda63ed5c-0
.ipynb .pdf Chat Prompt Template Contents Format output Different types of MessagePromptTemplate Chat Prompt Template# Chat Models takes a list of chat messages as input - this list commonly referred to as a prompt. These chat messages differ from raw string (which you would pass into a LLM model) in that every message is associated with a role. For example, in OpenAI Chat Completion API, a chat message can be associated with the AI, human or system role. The model is supposed to follow instruction from system chat message more closely. Therefore, LangChain provides several related prompt templates to make constructing and working with prompts easily. You are encouraged to use these chat related prompt templates instead of PromptTemplate when querying chat models to fully exploit the potential of underlying chat model. from langchain.prompts import ( ChatPromptTemplate, PromptTemplate, SystemMessagePromptTemplate, AIMessagePromptTemplate, HumanMessagePromptTemplate, ) from langchain.schema import ( AIMessage, HumanMessage, SystemMessage ) To create a message template associated with a role, you use MessagePromptTemplate. For convenience, there is a from_template method exposed on the template. If you were to use this template, this is what it would look like: template="You are a helpful assistant that translates {input_language} to {output_language}." system_message_prompt = SystemMessagePromptTemplate.from_template(template) human_template="{text}" human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) If you wanted to construct the MessagePromptTemplate more directly, you could create a PromptTemplate outside and then pass it in, eg: prompt=PromptTemplate( template="You are a helpful assistant that translates {input_language} to {output_language}.", input_variables=["input_language", "output_language"], )
https://python.langchain.com/en/latest/modules/prompts/chat_prompt_template.html
6aafda63ed5c-1
input_variables=["input_language", "output_language"], ) system_message_prompt_2 = SystemMessagePromptTemplate(prompt=prompt) assert system_message_prompt == system_message_prompt_2 After that, you can build a ChatPromptTemplate from one or more MessagePromptTemplates. You can use ChatPromptTemplate’s format_prompt – this returns a PromptValue, which you can convert to a string or Message object, depending on whether you want to use the formatted value as input to an llm or chat model. chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt]) # get a chat completion from the formatted messages chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_messages() [SystemMessage(content='You are a helpful assistant that translates English to French.', additional_kwargs={}), HumanMessage(content='I love programming.', additional_kwargs={})] Format output# The output of the format method is available as string, list of messages and ChatPromptValue As string: output = chat_prompt.format(input_language="English", output_language="French", text="I love programming.") output 'System: You are a helpful assistant that translates English to French.\nHuman: I love programming.' # or alternatively output_2 = chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_string() assert output == output_2 As ChatPromptValue chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.") ChatPromptValue(messages=[SystemMessage(content='You are a helpful assistant that translates English to French.', additional_kwargs={}), HumanMessage(content='I love programming.', additional_kwargs={})]) As list of Message objects chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_messages()
https://python.langchain.com/en/latest/modules/prompts/chat_prompt_template.html
6aafda63ed5c-2
[SystemMessage(content='You are a helpful assistant that translates English to French.', additional_kwargs={}), HumanMessage(content='I love programming.', additional_kwargs={})] Different types of MessagePromptTemplate# LangChain provides different types of MessagePromptTemplate. The most commonly used are AIMessagePromptTemplate, SystemMessagePromptTemplate and HumanMessagePromptTemplate, which create an AI message, system message and human message respectively. However, in cases where the chat model supports taking chat message with arbitrary role, you can use ChatMessagePromptTemplate, which allows user to specify the role name. from langchain.prompts import ChatMessagePromptTemplate prompt = "May the {subject} be with you" chat_message_prompt = ChatMessagePromptTemplate.from_template(role="Jedi", template=prompt) chat_message_prompt.format(subject="force") ChatMessage(content='May the force be with you', additional_kwargs={}, role='Jedi') LangChain also provides MessagesPlaceholder, which gives you full control of what messages to be rendered during formatting. This can be useful when you are uncertain of what role you should be using for your message prompt templates or when you wish to insert a list of messages during formatting. from langchain.prompts import MessagesPlaceholder human_prompt = "Summarize our conversation so far in {word_count} words." human_message_template = HumanMessagePromptTemplate.from_template(human_prompt) chat_prompt = ChatPromptTemplate.from_messages([MessagesPlaceholder(variable_name="conversation"), human_message_template]) human_message = HumanMessage(content="What is the best way to learn programming?") ai_message = AIMessage(content="""\ 1. Choose a programming language: Decide on a programming language that you want to learn. 2. Start with the basics: Familiarize yourself with the basic programming concepts such as variables, data types and control structures.
https://python.langchain.com/en/latest/modules/prompts/chat_prompt_template.html
6aafda63ed5c-3
3. Practice, practice, practice: The best way to learn programming is through hands-on experience\ """) chat_prompt.format_prompt(conversation=[human_message, ai_message], word_count="10").to_messages() [HumanMessage(content='What is the best way to learn programming?', additional_kwargs={}), AIMessage(content='1. Choose a programming language: Decide on a programming language that you want to learn. \n\n2. Start with the basics: Familiarize yourself with the basic programming concepts such as variables, data types and control structures.\n\n3. Practice, practice, practice: The best way to learn programming is through hands-on experience', additional_kwargs={}), HumanMessage(content='Summarize our conversation so far in 10 words.', additional_kwargs={})] previous Output Parsers next Example Selectors Contents Format output Different types of MessagePromptTemplate By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/prompts/chat_prompt_template.html
48c2549ef01a-0
.ipynb .pdf Getting Started Contents PromptTemplates to_string to_messages Getting Started# This section contains everything related to prompts. A prompt is the value passed into the Language Model. This value can either be a string (for LLMs) or a list of messages (for Chat Models). The data types of these prompts are rather simple, but their construction is anything but. Value props of LangChain here include: A standard interface for string prompts and message prompts A standard (to get started) interface for string prompt templates and message prompt templates Example Selectors: methods for inserting examples into the prompt for the language model to follow OutputParsers: methods for inserting instructions into the prompt as the format in which the language model should output information, as well as methods for then parsing that string output into a format. We have in depth documentation for specific types of string prompts, specific types of chat prompts, example selectors, and output parsers. Here, we cover a quick-start for a standard interface for getting started with simple prompts. PromptTemplates# PromptTemplates are responsible for constructing a prompt value. These PromptTemplates can do things like formatting, example selection, and more. At a high level, these are basically objects that expose a format_prompt method for constructing a prompt. Under the hood, ANYTHING can happen. from langchain.prompts import PromptTemplate, ChatPromptTemplate string_prompt = PromptTemplate.from_template("tell me a joke about {subject}") chat_prompt = ChatPromptTemplate.from_template("tell me a joke about {subject}") string_prompt_value = string_prompt.format_prompt(subject="soccer") chat_prompt_value = chat_prompt.format_prompt(subject="soccer") to_string# This is what is called when passing to an LLM (which expects raw text) string_prompt_value.to_string() 'tell me a joke about soccer'
https://python.langchain.com/en/latest/modules/prompts/getting_started.html
48c2549ef01a-1
string_prompt_value.to_string() 'tell me a joke about soccer' chat_prompt_value.to_string() 'Human: tell me a joke about soccer' to_messages# This is what is called when passing to ChatModel (which expects a list of messages) string_prompt_value.to_messages() [HumanMessage(content='tell me a joke about soccer', additional_kwargs={}, example=False)] chat_prompt_value.to_messages() [HumanMessage(content='tell me a joke about soccer', additional_kwargs={}, example=False)] previous Prompts next Prompt Templates Contents PromptTemplates to_string to_messages By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/prompts/getting_started.html
a3ad87c8ba96-0
.rst .pdf How-To Guides How-To Guides# If you’re new to the library, you may want to start with the Quickstart. The user guide here shows more advanced workflows and how to use the library in different ways. Connecting to a Feature Store How to create a custom prompt template How to create a prompt template that uses few shot examples How to work with partial Prompt Templates How to serialize prompts previous Getting Started next Connecting to a Feature Store By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/prompts/prompt_templates/how_to_guides.html
d868fbc8b065-0
.md .pdf Getting Started Contents What is a prompt template? Create a prompt template Template formats Validate template Serialize prompt template Pass few shot examples to a prompt template Select examples for a prompt template Getting Started# In this tutorial, we will learn about: what a prompt template is, and why it is needed, how to create a prompt template, how to pass few shot examples to a prompt template, how to select examples for a prompt template. What is a prompt template?# A prompt template refers to a reproducible way to generate a prompt. It contains a text string (“the template”), that can take in a set of parameters from the end user and generate a prompt. The prompt template may contain: instructions to the language model, a set of few shot examples to help the language model generate a better response, a question to the language model. The following code snippet contains an example of a prompt template: from langchain import PromptTemplate template = """ I want you to act as a naming consultant for new companies. What is a good name for a company that makes {product}? """ prompt = PromptTemplate( input_variables=["product"], template=template, ) prompt.format(product="colorful socks") # -> I want you to act as a naming consultant for new companies. # -> What is a good name for a company that makes colorful socks? Create a prompt template# You can create simple hardcoded prompts using the PromptTemplate class. Prompt templates can take any number of input variables, and can be formatted to generate a prompt. from langchain import PromptTemplate # An example prompt with no input variables no_input_prompt = PromptTemplate(input_variables=[], template="Tell me a joke.") no_input_prompt.format() # -> "Tell me a joke."
https://python.langchain.com/en/latest/modules/prompts/prompt_templates/getting_started.html
d868fbc8b065-1
no_input_prompt.format() # -> "Tell me a joke." # An example prompt with one input variable one_input_prompt = PromptTemplate(input_variables=["adjective"], template="Tell me a {adjective} joke.") one_input_prompt.format(adjective="funny") # -> "Tell me a funny joke." # An example prompt with multiple input variables multiple_input_prompt = PromptTemplate( input_variables=["adjective", "content"], template="Tell me a {adjective} joke about {content}." ) multiple_input_prompt.format(adjective="funny", content="chickens") # -> "Tell me a funny joke about chickens." If you do not wish to specify input_variables manually, you can also create a PromptTemplate using from_template class method. langchain will automatically infer the input_variables based on the template passed. template = "Tell me a {adjective} joke about {content}." prompt_template = PromptTemplate.from_template(template) prompt_template.input_variables # -> ['adjective', 'content'] prompt_template.format(adjective="funny", content="chickens") # -> Tell me a funny joke about chickens. You can create custom prompt templates that format the prompt in any way you want. For more information, see Custom Prompt Templates. Template formats# By default, PromptTemplate will treat the provided template as a Python f-string. You can specify other template format through template_format argument: # Make sure jinja2 is installed before running this jinja2_template = "Tell me a {{ adjective }} joke about {{ content }}" prompt_template = PromptTemplate.from_template(template=jinja2_template, template_format="jinja2") prompt_template.format(adjective="funny", content="chickens") # -> Tell me a funny joke about chickens.
https://python.langchain.com/en/latest/modules/prompts/prompt_templates/getting_started.html
d868fbc8b065-2
# -> Tell me a funny joke about chickens. Currently, PromptTemplate only supports jinja2 and f-string templating format. If there is any other templating format that you would like to use, feel free to open an issue in the Github page. Validate template# By default, PromptTemplate will validate the template string by checking whether the input_variables match the variables defined in template. You can disable this behavior by setting validate_template to False template = "I am learning langchain because {reason}." prompt_template = PromptTemplate(template=template, input_variables=["reason", "foo"]) # ValueError due to extra variables prompt_template = PromptTemplate(template=template, input_variables=["reason", "foo"], validate_template=False) # No error Serialize prompt template# You can save your PromptTemplate into a file in your local filesystem. langchain will automatically infer the file format through the file extension name. Currently, langchain supports saving template to YAML and JSON file. prompt_template.save("awesome_prompt.json") # Save to JSON file from langchain.prompts import load_prompt loaded_prompt = load_prompt("awesome_prompt.json") assert prompt_template == loaded_prompt langchain also supports loading prompt template from LangChainHub, which contains a collection of useful prompts you can use in your project. You can read more about LangChainHub and the prompts available with it here. from langchain.prompts import load_prompt prompt = load_prompt("lc://prompts/conversation/prompt.json") prompt.format(history="", input="What is 1 + 1?") You can learn more about serializing prompt template in How to serialize prompts. Pass few shot examples to a prompt template# Few shot examples are a set of examples that can be used to help the language model generate a better response.
https://python.langchain.com/en/latest/modules/prompts/prompt_templates/getting_started.html
d868fbc8b065-3
To generate a prompt with few shot examples, you can use the FewShotPromptTemplate. This class takes in a PromptTemplate and a list of few shot examples. It then formats the prompt template with the few shot examples. In this example, we’ll create a prompt to generate word antonyms. from langchain import PromptTemplate, FewShotPromptTemplate # First, create the list of few shot examples. examples = [ {"word": "happy", "antonym": "sad"}, {"word": "tall", "antonym": "short"}, ] # Next, we specify the template to format the examples we have provided. # We use the `PromptTemplate` class for this. example_formatter_template = """Word: {word} Antonym: {antonym} """ example_prompt = PromptTemplate( input_variables=["word", "antonym"], template=example_formatter_template, ) # Finally, we create the `FewShotPromptTemplate` object. few_shot_prompt = FewShotPromptTemplate( # These are the examples we want to insert into the prompt. examples=examples, # This is how we want to format the examples when we insert them into the prompt. example_prompt=example_prompt, # The prefix is some text that goes before the examples in the prompt. # Usually, this consists of intructions. prefix="Give the antonym of every input\n", # The suffix is some text that goes after the examples in the prompt. # Usually, this is where the user input will go suffix="Word: {input}\nAntonym: ", # The input variables are the variables that the overall prompt expects. input_variables=["input"],
https://python.langchain.com/en/latest/modules/prompts/prompt_templates/getting_started.html
d868fbc8b065-4
input_variables=["input"], # The example_separator is the string we will use to join the prefix, examples, and suffix together with. example_separator="\n", ) # We can now generate a prompt using the `format` method. print(few_shot_prompt.format(input="big")) # -> Give the antonym of every input # -> # -> Word: happy # -> Antonym: sad # -> # -> Word: tall # -> Antonym: short # -> # -> Word: big # -> Antonym: Select examples for a prompt template# If you have a large number of examples, you can use the ExampleSelector to select a subset of examples that will be most informative for the Language Model. This will help you generate a prompt that is more likely to generate a good response. Below, we’ll use the LengthBasedExampleSelector, which selects examples based on the length of the input. This is useful when you are worried about constructing a prompt that will go over the length of the context window. For longer inputs, it will select fewer examples to include, while for shorter inputs it will select more. We’ll continue with the example from the previous section, but this time we’ll use the LengthBasedExampleSelector to select the examples. from langchain.prompts.example_selector import LengthBasedExampleSelector # These are a lot of examples of a pretend task of creating antonyms. examples = [ {"word": "happy", "antonym": "sad"}, {"word": "tall", "antonym": "short"}, {"word": "energetic", "antonym": "lethargic"}, {"word": "sunny", "antonym": "gloomy"}, {"word": "windy", "antonym": "calm"}, ]
https://python.langchain.com/en/latest/modules/prompts/prompt_templates/getting_started.html
d868fbc8b065-5
{"word": "windy", "antonym": "calm"}, ] # We'll use the `LengthBasedExampleSelector` to select the examples. example_selector = LengthBasedExampleSelector( # These are the examples is has available to choose from. examples=examples, # This is the PromptTemplate being used to format the examples. example_prompt=example_prompt, # This is the maximum length that the formatted examples should be. # Length is measured by the get_text_length function below. max_length=25 # This is the function used to get the length of a string, which is used # to determine which examples to include. It is commented out because # it is provided as a default value if none is specified. # get_text_length: Callable[[str], int] = lambda x: len(re.split("\n| ", x)) ) # We can now use the `example_selector` to create a `FewShotPromptTemplate`. dynamic_prompt = FewShotPromptTemplate( # We provide an ExampleSelector instead of examples. example_selector=example_selector, example_prompt=example_prompt, prefix="Give the antonym of every input", suffix="Word: {input}\nAntonym:", input_variables=["input"], example_separator="\n\n", ) # We can now generate a prompt using the `format` method. print(dynamic_prompt.format(input="big")) # -> Give the antonym of every input # -> # -> Word: happy # -> Antonym: sad # -> # -> Word: tall # -> Antonym: short # -> # -> Word: energetic # -> Antonym: lethargic # -> # -> Word: sunny
https://python.langchain.com/en/latest/modules/prompts/prompt_templates/getting_started.html
d868fbc8b065-6
# -> Antonym: lethargic # -> # -> Word: sunny # -> Antonym: gloomy # -> # -> Word: windy # -> Antonym: calm # -> # -> Word: big # -> Antonym: In contrast, if we provide a very long input, the LengthBasedExampleSelector will select fewer examples to include in the prompt. long_string = "big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else" print(dynamic_prompt.format(input=long_string)) # -> Give the antonym of every input # -> Word: happy # -> Antonym: sad # -> # -> Word: big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else # -> Antonym: LangChain comes with a few example selectors that you can use. For more details on how to use them, see Example Selectors. You can create custom example selectors that select examples based on any criteria you want. For more details on how to do this, see Creating a custom example selector. previous Prompt Templates next How-To Guides Contents What is a prompt template? Create a prompt template Template formats Validate template Serialize prompt template Pass few shot examples to a prompt template Select examples for a prompt template By Harrison Chase © Copyright 2023, Harrison Chase. Last updated on Jun 02, 2023.
https://python.langchain.com/en/latest/modules/prompts/prompt_templates/getting_started.html
7cd9c88f0159-0
.ipynb .pdf Connecting to a Feature Store Contents Feast Load Feast Store Prompts Use in a chain Tecton Prerequisites Define and Load Features Prompts Use in a chain Featureform Initialize Featureform Prompts Use in a chain Connecting to a Feature Store# Feature stores are a concept from traditional machine learning that make sure data fed into models is up-to-date and relevant. For more on this, see here. This concept is extremely relevant when considering putting LLM applications in production. In order to personalize LLM applications, you may want to combine LLMs with up-to-date information about particular users. Feature stores can be a great way to keep that data fresh, and LangChain provides an easy way to combine that data with LLMs. In this notebook we will show how to connect prompt templates to feature stores. The basic idea is to call a feature store from inside a prompt template to retrieve values that are then formatted into the prompt. Feast# To start, we will use the popular open source feature store framework Feast. This assumes you have already run the steps in the README around getting started. We will build of off that example in getting started, and create and LLMChain to write a note to a specific driver regarding their up-to-date statistics. Load Feast Store# Again, this should be set up according to the instructions in the Feast README from feast import FeatureStore # You may need to update the path depending on where you stored it feast_repo_path = "../../../../../my_feature_repo/feature_repo/" store = FeatureStore(repo_path=feast_repo_path) Prompts# Here we will set up a custom FeastPromptTemplate. This prompt template will take in a driver id, look up their stats, and format those stats into a prompt.
https://python.langchain.com/en/latest/modules/prompts/prompt_templates/examples/connecting_to_a_feature_store.html