Skip to main content

Basic Conversation

This example demonstrates how to use RecoAgent's memory persistence system for basic conversation scenarios with session management.

Overview

The basic conversation example shows:

  • Creating and managing conversation sessions
  • Adding messages to conversation threads
  • Persisting conversation state across interactions
  • Retrieving conversation history and summaries

Code Example

import asyncio
from recoagent.memory import MemoryManager, MessageType

async def main():
# Initialize memory manager
memory_manager = MemoryManager(db_path="conversations.db")
await memory_manager.initialize()

try:
# Create session and thread
session_id = await memory_manager.thread_manager.create_session("user123")
thread_id = await memory_manager.thread_manager.create_thread("user123", session_id)

# Get conversation state
state = await memory_manager.thread_manager.get_thread_state(thread_id)

# Add messages
state.add_message(MessageType.USER, "Hello, I need help with AI!")
state.add_message(MessageType.ASSISTANT, "I'd be happy to help! What specific AI topic are you interested in?")

# Update state
await memory_manager.thread_manager.update_thread_state(thread_id, state)

# Search conversations
summaries, total = await memory_manager.history_api.search_conversations(
query="AI help",
filters={"user_id": "user123"}
)

print(f"Found {total} conversations")

finally:
await memory_manager.close()

asyncio.run(main())

Running the Example

  1. Install RecoAgent: Ensure you have RecoAgent installed
  2. Save the code: Save the example code to a Python file
  3. Run the example: Execute the Python file
python basic_conversation_example.py

Expected Output

✅ Started session: abc123-session
✅ Created thread: def456-thread
Found 1 conversations

Key Features Demonstrated

  • Session Management: Creating and managing user sessions
  • Thread Management: Creating conversation threads within sessions
  • Message Handling: Adding different types of messages
  • State Persistence: Saving and loading conversation state
  • Search Capabilities: Finding conversations based on content

Next Steps