|
| 1 | +export const description = |
| 2 | + 'Robyn AI Agents provide intelligent functionality for building AI-powered applications with conversation memory, context awareness, and pluggable agent runners. This is an experimental feature.' |
| 3 | + |
| 4 | +# AI Agents (Experimental) |
| 5 | + |
| 6 | +> **⚠️ Experimental Feature**: AI Agents are currently experimental and the API may change in future versions. Use with caution in production environments. |
| 7 | +
|
| 8 | +Robyn includes built-in AI agent capabilities that allow you to create intelligent applications with conversation memory, context awareness, and pluggable agent runners. The AI module provides abstractions for memory storage and agent execution that can be easily integrated into your Robyn applications. |
| 9 | + |
| 10 | +## Quick Start |
| 11 | + |
| 12 | +```python |
| 13 | +from robyn import Robyn |
| 14 | +from robyn.ai import agent, memory |
| 15 | + |
| 16 | +app = Robyn(__file__) |
| 17 | + |
| 18 | +# Create memory instance |
| 19 | +mem = memory(provider="inmemory", user_id="user123") |
| 20 | + |
| 21 | +# Create agent with memory |
| 22 | +chat_agent = agent(runner="simple", memory=mem) |
| 23 | + |
| 24 | +@app.get("/chat") |
| 25 | +async def chat_endpoint(request): |
| 26 | + query = request.query_params.get("q", [""])[0] |
| 27 | + if not query: |
| 28 | + return {"error": "Query required"} |
| 29 | + |
| 30 | + # Run agent with conversation history |
| 31 | + result = await chat_agent.run(query, history=True) |
| 32 | + return result |
| 33 | + |
| 34 | +app.start() |
| 35 | +``` |
| 36 | + |
| 37 | +## Features |
| 38 | + |
| 39 | +### 🧠 **Memory System** |
| 40 | +- Multiple provider support (InMemory, Mem0) |
| 41 | +- Persistent conversation history |
| 42 | +- Semantic search capabilities |
| 43 | +- Context-aware responses |
| 44 | + |
| 45 | +### 🤖 **Agent Runners** |
| 46 | +- Simple runner with OpenAI integration |
| 47 | +- LangGraph runner for complex workflows |
| 48 | +- Custom runner support |
| 49 | +- Async execution |
| 50 | + |
| 51 | +### 🔧 **Easy Integration** |
| 52 | +- Minimal setup required |
| 53 | +- Type-safe configuration |
| 54 | +- Environment variable support |
| 55 | +- Error handling with fallbacks |
| 56 | + |
| 57 | +## Memory System |
| 58 | + |
| 59 | +The memory system provides persistent storage for conversation history and context. It supports multiple providers and offers a consistent interface for storing and retrieving conversation data. |
| 60 | + |
| 61 | +### Memory Providers |
| 62 | + |
| 63 | +#### InMemory Provider |
| 64 | + |
| 65 | +The simplest provider that stores data in memory. Data is lost when the application restarts. |
| 66 | + |
| 67 | +```python |
| 68 | +from robyn.ai import memory |
| 69 | + |
| 70 | +# Create in-memory storage |
| 71 | +mem = memory(provider="inmemory", user_id="user123") |
| 72 | + |
| 73 | +# Add messages |
| 74 | +await mem.add("Hello, how are you?") |
| 75 | +await mem.add("I'm doing great, thanks!") |
| 76 | + |
| 77 | +# Retrieve all messages |
| 78 | +messages = await mem.get() |
| 79 | + |
| 80 | +# Clear memory |
| 81 | +await mem.clear() |
| 82 | +``` |
| 83 | + |
| 84 | +#### Mem0 Provider |
| 85 | + |
| 86 | +Integration with Mem0 for advanced memory capabilities including semantic search and persistence. |
| 87 | + |
| 88 | +```python |
| 89 | +from robyn.ai import memory |
| 90 | + |
| 91 | +# Create Mem0 memory instance |
| 92 | +mem = memory( |
| 93 | + provider="mem0", |
| 94 | + user_id="user123", |
| 95 | + # Mem0 configuration |
| 96 | + vector_store={ |
| 97 | + "provider": "chroma", |
| 98 | + "config": {"collection_name": "robyn_memory"} |
| 99 | + } |
| 100 | +) |
| 101 | + |
| 102 | +# Add messages with metadata |
| 103 | +await mem.add("I love pizza", metadata={"category": "food_preference"}) |
| 104 | + |
| 105 | +# Search with query |
| 106 | +results = await mem.get(query="food preferences") |
| 107 | +``` |
| 108 | + |
| 109 | +### Memory API |
| 110 | + |
| 111 | +The Memory class provides these key methods: |
| 112 | + |
| 113 | +- `add(message, metadata=None)` - Store a message with optional metadata |
| 114 | +- `get(query=None)` - Retrieve messages, optionally filtered by query |
| 115 | +- `clear()` - Clear all stored messages for the user |
| 116 | + |
| 117 | +## Agent System |
| 118 | + |
| 119 | +Agents provide the execution layer for AI functionality. They can use different runners and integrate with memory for context-aware responses. |
| 120 | + |
| 121 | +### Agent Runners |
| 122 | + |
| 123 | +#### Simple Runner |
| 124 | + |
| 125 | +A runner with OpenAI integration that provides intelligent responses: |
| 126 | + |
| 127 | +```python |
| 128 | +from robyn.ai import agent, configure |
| 129 | + |
| 130 | +# Configure with OpenAI API key |
| 131 | +config = configure(openai_api_key="your-openai-key") |
| 132 | + |
| 133 | +# Create simple agent |
| 134 | +simple_agent = agent(runner="simple", config=config) |
| 135 | + |
| 136 | +# Use the agent |
| 137 | +result = await simple_agent.run("What's the weather like?") |
| 138 | +# Returns structured response with AI-generated content |
| 139 | +``` |
| 140 | + |
| 141 | +#### LangGraph Runner |
| 142 | + |
| 143 | +Integration with LangGraph for complex workflow execution: |
| 144 | + |
| 145 | +```python |
| 146 | +from robyn.ai import agent, memory |
| 147 | + |
| 148 | +# Create memory |
| 149 | +mem = memory(provider="inmemory", user_id="user123") |
| 150 | + |
| 151 | +# Create LangGraph agent with custom workflow |
| 152 | +graph_agent = agent( |
| 153 | + runner="langgraph:path/to/workflow.json", |
| 154 | + memory=mem |
| 155 | +) |
| 156 | + |
| 157 | +# Execute with context |
| 158 | +result = await graph_agent.run("Process this request", history=True) |
| 159 | +``` |
| 160 | + |
| 161 | +#### Custom Runners |
| 162 | + |
| 163 | +Create your own agent runners by extending the `AgentRunner` class: |
| 164 | + |
| 165 | +```python |
| 166 | +from robyn.ai import AgentRunner, Agent |
| 167 | +from typing import Dict, Any |
| 168 | + |
| 169 | +class CustomAgentRunner(AgentRunner): |
| 170 | + async def run(self, query: str, **kwargs) -> Dict[str, Any]: |
| 171 | + # Implement custom agent logic |
| 172 | + return { |
| 173 | + "response": f"Custom response to: {query}", |
| 174 | + "processed": True |
| 175 | + } |
| 176 | + |
| 177 | +# Use custom runner |
| 178 | +custom_agent = Agent(runner=CustomAgentRunner()) |
| 179 | +``` |
| 180 | + |
| 181 | +### Agent API |
| 182 | + |
| 183 | +The Agent class provides: |
| 184 | + |
| 185 | +- `run(query, history=False, **kwargs)` - Execute the agent with optional history context |
| 186 | +- Automatic memory integration when provided |
| 187 | +- Support for custom runners and configuration |
| 188 | + |
| 189 | +## Configuration |
| 190 | + |
| 191 | +### Environment Variables |
| 192 | + |
| 193 | +Robyn AI supports configuration through environment variables: |
| 194 | + |
| 195 | +```bash |
| 196 | +# OpenAI Configuration |
| 197 | +export OPENAI_API_KEY="your-openai-key" |
| 198 | +export AI_MODEL="gpt-4o" |
| 199 | +export AI_TEMPERATURE="0.7" |
| 200 | +export AI_MAX_TOKENS="1000" |
| 201 | + |
| 202 | +# Mem0 Configuration |
| 203 | +export MEM0_API_KEY="your-mem0-key" |
| 204 | + |
| 205 | +# LangGraph Configuration |
| 206 | +export LANGGRAPH_API_KEY="your-langgraph-key" |
| 207 | +``` |
| 208 | + |
| 209 | +### Programmatic Configuration |
| 210 | + |
| 211 | +```python |
| 212 | +from robyn.ai import configure, agent, memory |
| 213 | + |
| 214 | +# Create configuration |
| 215 | +config = configure( |
| 216 | + openai_api_key="your-key", |
| 217 | + model="gpt-4", |
| 218 | + temperature=0.7, |
| 219 | + max_tokens=1000 |
| 220 | +) |
| 221 | + |
| 222 | +# Create agent with config |
| 223 | +chat_agent = agent(runner="simple", config=config) |
| 224 | + |
| 225 | +# Or configure memory |
| 226 | +mem = memory( |
| 227 | + provider="mem0", |
| 228 | + user_id="user123", |
| 229 | + vector_store={"provider": "chroma"} |
| 230 | +) |
| 231 | +``` |
| 232 | + |
| 233 | +## Complete Example |
| 234 | + |
| 235 | +Here's a comprehensive example showing all features: |
| 236 | + |
| 237 | +```python |
| 238 | +from robyn import Robyn |
| 239 | +from robyn.ai import agent, memory, configure |
| 240 | + |
| 241 | +app = Robyn(__file__) |
| 242 | + |
| 243 | +# Configure AI settings |
| 244 | +config = configure( |
| 245 | + openai_api_key="your-openai-key", |
| 246 | + model="gpt-4o", |
| 247 | + temperature=0.7 |
| 248 | +) |
| 249 | + |
| 250 | +# Create memory with Mem0 provider |
| 251 | +mem = memory( |
| 252 | + provider="mem0", |
| 253 | + user_id="guest", |
| 254 | + vector_store={"provider": "chroma"} |
| 255 | +) |
| 256 | + |
| 257 | +# Create agent with memory and config |
| 258 | +chat_agent = agent(runner="simple", memory=mem, config=config) |
| 259 | + |
| 260 | +@app.get("/") |
| 261 | +async def home(): |
| 262 | + return {"message": "Robyn AI Chat API"} |
| 263 | + |
| 264 | +@app.post("/chat") |
| 265 | +async def chat(request): |
| 266 | + """Chat with AI agent""" |
| 267 | + data = request.json() |
| 268 | + query = data.get("query", "") |
| 269 | + include_history = data.get("history", True) |
| 270 | + |
| 271 | + if not query: |
| 272 | + return {"error": "Query is required"} |
| 273 | + |
| 274 | + try: |
| 275 | + result = await chat_agent.run(query, history=include_history) |
| 276 | + return { |
| 277 | + "query": query, |
| 278 | + "response": result.get("response"), |
| 279 | + "history_included": include_history, |
| 280 | + "metadata": result.get("metadata", {}) |
| 281 | + } |
| 282 | + except Exception as e: |
| 283 | + return {"error": str(e)} |
| 284 | + |
| 285 | +@app.get("/memory") |
| 286 | +async def get_memory(): |
| 287 | + """Retrieve conversation history""" |
| 288 | + try: |
| 289 | + memories = await mem.get() |
| 290 | + return {"memories": memories, "count": len(memories)} |
| 291 | + except Exception as e: |
| 292 | + return {"error": str(e)} |
| 293 | + |
| 294 | +@app.delete("/memory") |
| 295 | +async def clear_memory(): |
| 296 | + """Clear conversation history""" |
| 297 | + try: |
| 298 | + await mem.clear() |
| 299 | + return {"message": "Memory cleared"} |
| 300 | + except Exception as e: |
| 301 | + return {"error": str(e)} |
| 302 | + |
| 303 | +@app.post("/memory") |
| 304 | +async def add_memory(request): |
| 305 | + """Add message to memory""" |
| 306 | + data = request.json() |
| 307 | + message = data.get("message", "") |
| 308 | + metadata = data.get("metadata", {}) |
| 309 | + |
| 310 | + if not message: |
| 311 | + return {"error": "Message is required"} |
| 312 | + |
| 313 | + try: |
| 314 | + await mem.add(message, metadata) |
| 315 | + return {"message": "Added to memory"} |
| 316 | + except Exception as e: |
| 317 | + return {"error": str(e)} |
| 318 | + |
| 319 | +if __name__ == "__main__": |
| 320 | + app.start(host="127.0.0.1", port=8080) |
| 321 | +``` |
| 322 | + |
| 323 | +## Best Practices |
| 324 | + |
| 325 | +1. **User Isolation**: Always use unique user IDs to isolate memory between different users |
| 326 | +2. **Error Handling**: Wrap AI operations in try-catch blocks as external services may fail |
| 327 | +3. **Memory Management**: Regularly clear or archive old memories to prevent unbounded growth |
| 328 | +4. **Configuration**: Store sensitive configuration (API keys, etc.) in environment variables |
| 329 | +5. **Testing**: Test thoroughly as this is an experimental feature |
| 330 | +6. **Rate Limiting**: Implement rate limiting for public-facing AI endpoints |
| 331 | +7. **Monitoring**: Log agent interactions for debugging and analytics |
| 332 | +8. **Fallbacks**: Always provide fallback responses when AI services are unavailable |
| 333 | + |
| 334 | +## Troubleshooting |
| 335 | + |
| 336 | +### Common Issues |
| 337 | + |
| 338 | +**ImportError for required packages**: Install the required packages: |
| 339 | +```bash |
| 340 | +pip install openai mem0ai langgraph |
| 341 | +``` |
| 342 | + |
| 343 | +**Memory not persisting**: Ensure you're using a persistent provider like Mem0, not the in-memory provider for production. |
| 344 | + |
| 345 | +**Agent timeouts**: Complex workflows may take time. Consider implementing timeout handling in your endpoints. |
| 346 | + |
| 347 | +**API changes**: As this is experimental, monitor for API changes in future Robyn releases. |
| 348 | + |
| 349 | +**OpenAI API errors**: Ensure your API key is valid and you have sufficient credits. Implement retry logic for transient errors. |
| 350 | + |
| 351 | +## What are AI Agents? |
| 352 | + |
| 353 | +AI Agents in Robyn are intelligent components that can understand natural language queries, maintain conversation context through memory systems, and provide intelligent responses using various AI providers like OpenAI or custom implementations. |
| 354 | + |
| 355 | +They're designed to be: |
| 356 | +- **Context-aware**: Remember previous interactions |
| 357 | +- **Extensible**: Support custom runners and memory providers |
| 358 | +- **Production-ready**: Include error handling, fallbacks, and monitoring |
| 359 | +- **Easy to integrate**: Minimal setup with sensible defaults |
| 360 | + |
| 361 | +> **Note**: This feature is experimental and may evolve significantly. Please provide feedback and report issues to help improve the implementation. |
| 362 | +
|
| 363 | +For more information about the underlying AI concepts, see the [AI module documentation](./ai.mdx). |
0 commit comments