Skip to main content

Advanced Conversation

This example demonstrates advanced features of RecoAgent's memory persistence system including multi-threaded conversations, tool usage, and complex conversation workflows.

Overview

The advanced conversation example shows:

  • Multi-threaded conversation management
  • Tool integration and execution
  • Complex conversation analysis
  • Advanced search and filtering capabilities
  • Conversation analytics and reporting

Code Example

import asyncio
from recoagent.memory import MemoryManager, MessageType, ConversationFilter

class AdvancedConversationAgent:
def __init__(self, memory_manager):
self.memory_manager = memory_manager
self.user_id = "advanced_user"
self.session_id = None
self.active_threads = {}
self.available_tools = {
"search": self._tool_search,
"calculate": self._tool_calculate,
"get_weather": self._tool_get_weather
}

async def start_session(self):
self.session_id = await self.memory_manager.thread_manager.create_session(
user_id=self.user_id,
metadata={"environment": "advanced_demo"}
)

async def create_conversation_thread(self, topic: str):
thread_id = await self.memory_manager.thread_manager.create_thread(
user_id=self.user_id,
session_id=self.session_id,
metadata={"topic": topic, "type": "advanced_conversation"}
)
self.active_threads[topic] = thread_id
return thread_id

async def process_message(self, message: str, topic: str = "general"):
if topic not in self.active_threads:
await self.create_conversation_thread(topic)

thread_id = self.active_threads[topic]
state = await self.memory_manager.thread_manager.get_thread_state(thread_id)

# Add user message
state.add_message(MessageType.USER, message)

# Analyze message and determine response strategy
analysis = self._analyze_message(message, state)

# Execute tool calls if needed
if analysis["requires_tools"]:
tool_calls = await self._execute_tool_calls(analysis["suggested_tools"], message)
for tool_call in tool_calls:
state.add_message(
MessageType.TOOL,
f"Tool '{tool_call.name}' executed",
tool_calls=[{"name": tool_call.name, "args": tool_call.args}],
tool_results=[{"result": tool_call.result}]
)

# Generate response
response = self._generate_advanced_response(message, state, analysis)
state.add_message(MessageType.ASSISTANT, response)

# Update context and save
await self.memory_manager.thread_manager.update_thread_state(thread_id, state)

return {
"response": response,
"thread_id": thread_id,
"analysis": analysis
}

async def main():
memory_manager = MemoryManager(db_path="advanced_demo.db")
await memory_manager.initialize()

try:
agent = AdvancedConversationAgent(memory_manager)
await agent.start_session()

# Demo scenarios
scenarios = [
("search", "Search for information about machine learning"),
("calculation", "Calculate 25 + 17"),
("weather", "What's the weather like today?"),
("general", "Tell me about our conversation history")
]

for topic, message in scenarios:
result = await agent.process_message(message, topic)
print(f"Topic: {topic}")
print(f"User: {message}")
print(f"Agent: {result['response']}")
print(f"Analysis: {result['analysis']['intent']}")
print()

finally:
await memory_manager.close()

asyncio.run(main())

Running the Example

  1. Install Dependencies: 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 advanced_conversation_example.py

Expected Output

Topic: search
User: Search for information about machine learning
Agent: I used 1 tool(s) to help with your request. Results: search: Search results for 'machine learning': Found 3 relevant articles about this topic.
Analysis: search

Topic: calculation
User: Calculate 25 + 17
Agent: I used 1 tool(s) to help with your request. Results: calculate: Calculation result: 42
Analysis: calculation

Topic: weather
User: What's the weather like today?
Agent: I used 1 tool(s) to help with your request. Results: get_weather: Weather for current_location: Sunny, 72°F, Light winds from the west.
Analysis: weather

Topic: general
User: Tell me about our conversation history
Agent: Building on our previous discussion, I understand your message: 'Tell me about our conversation history'. How can I help you further?
Analysis: general

Key Features Demonstrated

  • Multi-Threaded Conversations: Managing multiple conversation topics simultaneously
  • Tool Integration: Executing tools and storing results in conversation history
  • Message Analysis: Analyzing user intent and determining appropriate responses
  • Advanced State Management: Complex conversation state with tool calls and results
  • Session Persistence: Maintaining conversation context across multiple interactions

Advanced Capabilities

Tool Management

  • Dynamic tool discovery and execution
  • Tool result storage in conversation history
  • Tool call metadata and error handling

Conversation Analysis

  • Intent detection and classification
  • Response strategy determination
  • Context-aware response generation

Search and Analytics

  • Cross-thread conversation search
  • Conversation analytics and reporting
  • Export capabilities for conversation data

Next Steps