π§ Memory
What is Memory?
Code Snippet
Importing Necessary Components
from beyondllm.memory import ChatBufferMemoryBasic Implementation of Memory
# Initialize memory with a specified window size
memory = ChatBufferMemory(window_size=3) # Retains the last three interactions
# Initialize the language model
llm = GPT4oOpenAIModel(model="gpt-4o", api_key="sk-proj-xxxxxxxx")
# Define a function to handle the conversation
def ask_question(question):
# Create a retriever for the sourced data (this should be defined earlier in your code)
retriever = retrieve.auto_retriever(
data=data, # Ensure 'data' is defined with your source content
type='normal',
embed_model=embed_model,
top_k=4,
)
# Generate a response using the memory
pipeline = Generate(retriever=retriever, question=question, llm=llm, memory=memory, system_prompt="Answer the user questions based on the chat history")
response = pipeline.call()
return response
# Example interaction
response = ask_question("My name is Rupert Grint.")
print("Response:", response)
# Access the memory content after the conversation
print("\nMemory:", memory.get_memory())Example Usage
Conclusion
Last updated