Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ConversationManager:
- def __init__(self, system_prompt, memory_limit=5):
- self.system_prompt = system_prompt
- self.memory = []
- self.memory_limit = memory_limit
- def add_to_memory(self, role, content):
- self.memory.append({"role": role, "content": content})
- if len(self.memory) > self.memory_limit:
- self.memory.pop(0)
- def get_response(self, user_input):
- self.add_to_memory("user", user_input)
- full_prompt = f"{self.system_prompt}\n\n"
- for message in self.memory:
- full_prompt += f"{message['role'].capitalize()}: {message['content']}\n"
- full_prompt += "Assistant: "
- response = llm.generate(full_prompt)
- self.add_to_memory("assistant", response)
- return response
- # Usage
- system_prompt = "You are a helpful AI assistant. Maintain context across the conversation."
- manager = ConversationManager(system_prompt)
- print(manager.get_response("Hi, what's the weather like today?"))
- print(manager.get_response("Will I need an umbrella?"))
- print(manager.get_response("Thanks! By the way, what was my first question?"))
Advertisement
Add Comment
Please, Sign In to add comment