Chat history in the 0G AI SDK is managed through the Memory system, which provides both ephemeral (temporary) and persistent storage for conversation messages. This allows you to maintain context across sessions and build sophisticated conversational AI applications.
import asynciofrom zg_ai_sdk import create_agent, ChatMessageasync def main(): agent = await create_agent({ 'name': 'History Assistant', 'provider_address': '0xf07240Efa67755B5311bc75784a061eDB47165Dd', 'memory_bucket': 'history-demo', 'private_key': 'your-private-key' }) # Have a conversation await agent.ask('Hello, my name is Alice') await agent.ask('What is my name?') # Get conversation history messages = agent.memory.get_messages() print(f"Conversation has {len(messages)} messages:") for msg in messages: print(f"{msg.role}: {msg.content}")asyncio.run(main())
def get_user_questions(agent): messages = agent.memory.get_messages() questions = [] for msg in messages: if msg.role == 'user' and '?' in msg.content: questions.append(msg.content) return questions# Get all user questions from the conversationquestions = get_user_questions(agent)print("User asked:", questions)
Organize conversations using different memory buckets:
# Different agents for different purposessupport_agent = await create_agent({ 'memory_bucket': 'customer-support', # ... other config})research_agent = await create_agent({ 'memory_bucket': 'research-assistant', # ... other config})