Skip to content

Commit 000a492

Browse files
authored
Merge pull request #111 from Rajaniraiyn/sql
Add SQL Conversation Storage
2 parents 920d426 + 951fcbd commit 000a492

File tree

9 files changed

+877
-56
lines changed

9 files changed

+877
-56
lines changed

docs/src/content/docs/storage/dynamodb.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ DynamoDB storage provides a scalable and persistent solution for storing convers
1717
- When long-term persistence of conversation history is required
1818
- For applications that need to scale horizontally
1919

20+
## Python Package
21+
22+
If you haven't already installed the AWS-related dependencies, make sure to install them:
23+
24+
```bash
25+
pip install "multi-agent-orchestrator[aws]"
26+
```
27+
2028
## Implementation
2129

2230
To use DynamoDB storage in your Multi-Agent Orchestrator:

docs/src/content/docs/storage/overview.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,25 @@ The Multi-Agent Orchestrator System offers flexible storage options for maintain
2121
- Provides persistent storage for production environments.
2222
- Allows for scalable and durable conversation history storage.
2323

24-
3. **Custom Storage Solutions**:
24+
3. **SQL Storage**:
25+
- Offers persistent storage using SQLite or Turso databases.
26+
- When you need local-first development with remote deployment options
27+
28+
4. **Custom Storage Solutions**:
2529
- The system allows for implementation of custom storage options to meet specific needs.
2630

2731
## Choosing the Right Storage Option
2832

2933
- Use In-Memory Storage for development, testing, or when persistence between application restarts is not necessary.
3034
- Choose DynamoDB Storage for production environments where conversation history needs to be preserved long-term or across multiple instances of your application.
35+
- Consider SQL Storage for a balance between simplicity and scalability, supporting both local and remote databases.
3136
- Implement a custom storage solution if you have specific requirements not met by the provided options.
3237

3338
## Next Steps
3439

3540
- Learn more about [In-Memory Storage](/multi-agent-orchestrator/storage/in-memory)
3641
- Explore [DynamoDB Storage](/multi-agent-orchestrator/storage/dynamodb) for persistent storage
42+
- Explore [SQL Storage](/multi-agent-orchestrator/storage/sql) for persistent storage using SQLite or Turso.
3743
- Discover how to [implement custom storage solutions](/multi-agent-orchestrator/storage/custom)
3844

3945
By leveraging these storage options, you can ensure that your Multi-Agent Orchestrator System maintains the necessary context for coherent and effective conversations across various use cases and deployment scenarios.
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
---
2+
title: SQL Storage
3+
description: Using SQL databases (SQLite/Turso) for persistent conversation storage in the Multi-Agent Orchestrator System
4+
---
5+
6+
SQL storage provides a flexible and reliable solution for storing conversation history in the Multi-Agent Orchestrator System. This implementation supports both local SQLite databases and remote Turso databases, making it suitable for various deployment scenarios from development to production.
7+
8+
## Features
9+
10+
- Persistent storage across application restarts
11+
- Support for both local and remote databases
12+
- Built-in connection pooling and retry mechanisms
13+
- Compatible with edge and serverless deployments
14+
- Transaction support for data consistency
15+
- Efficient indexing for quick data retrieval
16+
17+
## When to Use SQL Storage
18+
19+
- When you need a balance between simplicity and scalability
20+
- For applications requiring persistent storage without complex infrastructure
21+
- In both development and production environments
22+
- When working with edge or serverless deployments
23+
- When you need local-first development with remote deployment options
24+
25+
## Python Package Installation
26+
27+
To use SQL storage in your Python application, make sure to install them:
28+
29+
```bash
30+
pip install "multi-agent-orchestrator[sql]"
31+
```
32+
33+
This will install the `libsql-client` package required for SQL storage functionality.
34+
35+
## Implementation
36+
37+
To use SQL storage in your Multi-Agent Orchestrator:
38+
39+
import { Tabs, TabItem } from '@astrojs/starlight/components';
40+
41+
<Tabs syncKey="runtime">
42+
<TabItem label="TypeScript" icon="seti:typescript" color="blue">
43+
```typescript
44+
import { SqlChatStorage, MultiAgentOrchestrator } from 'multi-agent-orchestrator';
45+
46+
// For local SQLite database
47+
const localStorage = new SqlChatStorage('file:local.db');
48+
await localStorage.waitForInitialization();
49+
50+
// For remote database
51+
const remoteStorage = new SqlChatStorage(
52+
'libsql://your-database-url.example.com',
53+
'your-auth-token'
54+
);
55+
await remoteStorage.waitForInitialization();
56+
57+
const orchestrator = new MultiAgentOrchestrator({
58+
storage: localStorage // or remoteStorage
59+
});
60+
61+
62+
// Close the database connections when done
63+
await localStorage.close();
64+
await remoteStorage.close();
65+
```
66+
</TabItem>
67+
<TabItem label="Python" icon="seti:python">
68+
```python
69+
from multi_agent_orchestrator.storage import SqlChatStorage
70+
from multi_agent_orchestrator.orchestrator import MultiAgentOrchestrator
71+
72+
# For local SQLite database
73+
local_storage = SqlChatStorage('file:local.db')
74+
75+
# For remote Turso database
76+
remote_storage = SqlChatStorage(
77+
url='libsql://your-database-url.turso.io',
78+
auth_token='your-auth-token'
79+
)
80+
81+
orchestrator = MultiAgentOrchestrator(storage=local_storage) # or remote_storage
82+
```
83+
</TabItem>
84+
</Tabs>
85+
86+
## Configuration
87+
88+
### Local DB
89+
For local development, simply provide a file URL:
90+
```typescript
91+
const storage = new SqlChatStorage('file:local.db');
92+
```
93+
94+
### Remote DB
95+
For production with Turso:
96+
1. Create a Turso database through their platform
97+
2. Obtain your database URL and authentication token
98+
3. Configure your storage:
99+
```typescript
100+
const storage = new SqlChatStorage(
101+
'libsql://your-database-url.com',
102+
'your-auth-token'
103+
);
104+
```
105+
106+
## Database Schema
107+
108+
The SQL storage implementation uses the following schema:
109+
110+
```sql
111+
CREATE TABLE conversations (
112+
user_id TEXT NOT NULL,
113+
session_id TEXT NOT NULL,
114+
agent_id TEXT NOT NULL,
115+
message_index INTEGER NOT NULL,
116+
role TEXT NOT NULL,
117+
content TEXT NOT NULL,
118+
timestamp INTEGER NOT NULL,
119+
PRIMARY KEY (user_id, session_id, agent_id, message_index)
120+
);
121+
122+
CREATE INDEX idx_conversations_lookup
123+
ON conversations(user_id, session_id, agent_id);
124+
```
125+
126+
## Considerations
127+
128+
- Automatic table and index creation on initialization
129+
- Built-in transaction support for data consistency
130+
- Efficient query performance through proper indexing
131+
- Support for message history size limits
132+
- Automatic JSON serialization/deserialization of message content
133+
134+
## Best Practices
135+
136+
- Use a single instance of SqlChatStorage throughout your application
137+
- Regularly backup your database
138+
- Implement data cleanup strategies for old conversations
139+
- Monitor database size and performance
140+
- Keep your authentication tokens secure
141+
- Implement proper access controls at the application level
142+
- Close the database connections when done
143+
144+
SQL storage provides a robust and flexible solution for managing conversation history in the Multi-Agent Orchestrator System. It offers a good balance between simplicity and features, making it suitable for both development and production environments.

python/setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ anthropic =
2727
anthropic>=0.40.0
2828
openai =
2929
openai>=1.55.3
30+
sql =
31+
libsql-client>=0.3.1
3032
all =
3133
anthropic==0.40.0
3234
openai==1.55.3
3335
boto3==1.35.0
36+
libsql-client==0.3.1
3437

3538
[options.packages.find]
3639
where = src

python/src/multi_agent_orchestrator/storage/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@
55
from .in_memory_chat_storage import InMemoryChatStorage
66

77
_AWS_AVAILABLE = False
8+
_SQL_AVAILABLE = False
89

910
try:
1011
from .dynamodb_chat_storage import DynamoDbChatStorage
1112
_AWS_AVAILABLE = True
1213
except ImportError:
1314
_AWS_AVAILABLE = False
1415

16+
try:
17+
from .sql_chat_storage import SqlChatStorage
18+
_SQL_AVAILABLE = True
19+
except ImportError:
20+
_SQL_AVAILABLE = False
21+
1522
__all__ = [
1623
'ChatStorage',
1724
'InMemoryChatStorage',
@@ -20,4 +27,9 @@
2027
if _AWS_AVAILABLE:
2128
__all__.extend([
2229
'DynamoDbChatStorage'
30+
])
31+
32+
if _SQL_AVAILABLE:
33+
__all__.extend([
34+
'SqlChatStorage'
2335
])

0 commit comments

Comments
 (0)