from autogen_ext.memory import VectorMemoryfrom autogen_ext.memory.embeddings import OpenAIEmbeddings# Create embeddingsembeddings = OpenAIEmbeddings(api_key="sk-...")# Create vector memorymemory = VectorMemory( embeddings=embeddings, collection_name="my_docs")# Add documentsawait memory.add_documents([ "AutoGen is a framework for building AI agents.", "Agents can use tools to interact with external systems.", "Teams orchestrate multiple agents working together."])# Queryresults = await memory.query("What are teams?")print(results) # ["Teams orchestrate multiple agents working together."]
Agents maintain state across messages through AgentState:
from autogen_agentchat.state import AssistantAgentState# Get agent statestate = agent.get_state()print(state.llm_messages) # Message historyprint(state.tool_use) # Tool call history# Save statestate_dict = agent.save_state()# Load state (e.g., after restart)agent.load_state(state_dict)
from autogen_agentchat.teams import RoundRobinGroupChatteam = RoundRobinGroupChat([agent1, agent2])# Run teamresult = await team.run(task="Solve a problem")# Access team stateteam_state = team.stateprint(team_state.messages) # All messages in team conversation
agent = AssistantAgent( "assistant", model_client=model_client)# First interactionresult1 = await agent.run(task="My name is Alice")# Later interaction - agent remembersresult2 = await agent.run(task="What's my name?")print(result2.messages[-1].content) # "Your name is Alice"
import json# After conversationstate = agent.save_state()# Save to filewith open("agent_state.json", "w") as f: json.dump(state, f)# Later: Restore from filewith open("agent_state.json", "r") as f: state = json.load(f)agent.load_state(state)# Agent continues from previous state
from autogen_agentchat.messages import MemoryQueryEvent# Listen for memory queriesasync for event in agent.run_stream(task="..."): if isinstance(event, MemoryQueryEvent): print(f"Queried memory: {event.query}") print(f"Retrieved: {event.results}")
from autogen_ext.memory import VectorMemoryfrom autogen_ext.memory.embeddings import OpenAIEmbeddings# Load FAQ documents into memoryfaqs = [ "Q: What is AutoGen? A: A framework for building AI agents.", "Q: How do I install? A: pip install autogen-agentchat", # ... more FAQs]memory = VectorMemory(embeddings=OpenAIEmbeddings())await memory.add_documents(faqs)# Create FAQ agentfaq_agent = AssistantAgent( "faq_bot", model_client=model_client, memory=[memory], system_message="Answer questions using the provided FAQ knowledge.")result = await faq_agent.run(task="How do I install AutoGen?")
from autogen_core.model_context import BufferedChatCompletionContext# Keep only recent messagescontext = BufferedChatCompletionContext(buffer_size=20)agent = AssistantAgent( "assistant", model_client=model_client, model_context=context, system_message="You are a helpful assistant. Summarize long discussions.")# Long conversation - only last 20 messages sent to LLMfor i in range(50): await agent.run(task=f"Message {i}")