-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_agent_example.py
More file actions
244 lines (205 loc) · 8.39 KB
/
multi_agent_example.py
File metadata and controls
244 lines (205 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python3
"""
Multi-user ACL example for RiceDB.
This example demonstrates:
1. Access Control List (ACL) functionality
2. Multiple users with different permissions
3. User-specific data isolation
"""
import os
from dotenv import load_dotenv
from ricedb import RiceDBClient
load_dotenv()
HOST = os.environ.get("HOST", "localhost")
PORT = int(os.environ.get("PORT", "50051"))
PASSWORD = os.environ.get("PASSWORD", "admin")
SSL = os.environ.get("SSL", "false").lower() == "true"
def main():
print(" RiceDB Python Client - Multi-User ACL Example\n")
# Initialize client
client = RiceDBClient(HOST, port=PORT)
client.ssl = SSL
# Connect to the server
print("1 Connecting to RiceDB server...")
if not client.connect():
print(" Failed to connect to RiceDB server")
return
print(f" Connected via {client.get_transport_info()['type'].upper()}")
# Authenticate as Admin
print(" Logging in as Admin...")
try:
client.login("admin", PASSWORD)
except Exception as e:
print(f" Login failed: {e}")
return
# User configuration
users_config = {
"alice": {"role": "Manager", "dept": "Finance", "pass": "alice123"},
"bob": {"role": "Engineer", "dept": "Engineering", "pass": "bob123"},
"charlie": {"role": "Marketing", "dept": "Marketing", "pass": "charlie123"},
"diana": {"role": "HR", "dept": "Human Resources", "pass": "diana123"},
}
# Create users and get real IDs
print("\n2 Creating users...")
users = {}
user_clients = {}
for name, info in users_config.items():
try:
# Cleanup if exists
try:
client.delete_user(name)
except:
pass
user_id = client.create_user(name, info["pass"], role="user")
users[name] = {**info, "id": user_id}
print(f" Created {name} (ID: {user_id})")
# Create authenticated client for this user
u_client = RiceDBClient(HOST, port=PORT)
u_client.ssl = SSL
u_client.connect()
u_client.login(name, info["pass"])
user_clients[name] = u_client
except Exception as e:
print(f" Failed to create {name}: {e}")
# Prepare documents for each user
print("\n3 Preparing user-specific documents...")
documents_by_user = {
"alice": [ # Alice - Finance Manager
{"id": 1001, "text": "Q4 Budget Report", "type": "Financial Report"},
{"id": 1002, "text": "Salary Structure Review", "type": "HR Document"},
{
"id": 1003,
"text": "Investment Portfolio Analysis",
"type": "Financial Analysis",
},
],
"bob": [ # Bob - Engineer
{"id": 2001, "text": "API Documentation", "type": "Technical Doc"},
{
"id": 2002,
"text": "System Architecture Design",
"type": "Technical Spec",
},
{"id": 2003, "text": "Code Review Guidelines", "type": "Process Doc"},
],
"charlie": [ # Charlie - Marketing
{"id": 3001, "text": "Product Launch Campaign", "type": "Marketing Plan"},
{"id": 3002, "text": "Social Media Strategy", "type": "Marketing Doc"},
{"id": 3003, "text": "Brand Guidelines", "type": "Brand Doc"},
],
"diana": [ # Diana - HR
{"id": 4001, "text": "Employee Handbook", "type": "HR Policy"},
{"id": 4002, "text": "Performance Review Process", "type": "HR Process"},
{"id": 4003, "text": "Training Programs", "type": "Training Doc"},
],
}
# Insert documents for each user (using their own client)
print("\n4 Inserting documents with user-specific ACL...")
for user_name, docs in documents_by_user.items():
user_id = users[user_name]["id"]
u_client = user_clients[user_name]
print(f"\n Inserting documents for {user_name.title()} (User ID: {user_id}):")
for doc in docs:
try:
# We use insert. Note that user_id param is ignored by server for ownership,
# but might be used by client logic if we pass it.
# Since u_client is authenticated as user, they become the owner.
result = u_client.insert(
node_id=doc["id"],
text=doc["text"],
metadata={
"type": doc["type"],
"owner": user_name,
"department": users[user_name]["dept"],
},
)
print(f" {doc['text']} ({doc['type']})")
except Exception as e:
print(f" Failed to insert: {e}")
# Search as each user
print("\n5 Searching as each user (ACL-enforced)...")
search_query = "guidelines"
for user_name, user_info in users.items():
user_id = user_info["id"]
u_client = user_clients[user_name]
print(f"\n Search as {user_name.title()} (User ID: {user_id}):")
print(f" Query: '{search_query}'")
try:
results = u_client.search(
query=search_query,
k=5,
user_id=user_id,
)
print(f" Found {len(results)} results:")
for i, result in enumerate(results, 1):
metadata = result["metadata"]
owner = metadata.get("owner", "Unknown")
doc_type = metadata.get("type", "Unknown")
text = metadata.get("text", "No text")
print(f" {i}. [{owner}] {text} ({doc_type})")
if len(results) == 0:
print(" (No accessible documents - ACL working!)")
except Exception as e:
print(f" Search error: {e}")
# Cross-user access attempt
print("\n6 Testing cross-user access control...")
alice_client = user_clients["alice"]
bob_client = user_clients["bob"]
# Alice trying to search Bob's documents
print(f"\n Alice searching for 'API' (Bob's docs):")
try:
results = alice_client.search(query="API", k=5, user_id=users["alice"]["id"])
print(f" Alice found {len(results)} API-related documents")
# Should find 0 if ACL is working properly
except Exception as e:
print(f" Error: {e}")
# Bob searching for his own API documents
print(f"\n Bob searching for 'API':")
try:
results = bob_client.search(query="API", k=5, user_id=users["bob"]["id"])
print(f" Bob found {len(results)} API-related documents:")
for result in results:
metadata = result["metadata"]
text = metadata.get("text", "No text")
print(f" - {text}")
except Exception as e:
print(f" Error: {e}")
# Shared document example (accessible by all)
print("\n7 Inserting a shared document...")
try:
# Alice creates a document and shares it with everyone
shared_text = "Company Holiday Schedule 2024"
shared_id = 9999
alice_client.insert(
node_id=shared_id,
text=shared_text,
metadata={"type": "Company Policy", "access": "all", "shared": True},
)
print(f" Alice inserted '{shared_text}'")
# Alice grants read permission to others
print(" Granting read access to Bob, Charlie, Diana...")
for name in ["bob", "charlie", "diana"]:
uid = users[name]["id"]
alice_client.grant_permission(
shared_id, uid, {"read": True, "write": False, "delete": False}
)
# Each user should now be able to find it
print("\n All users searching for 'holiday schedule':")
for user_name, u_client in user_clients.items():
results = u_client.search(
query="holiday schedule",
k=3,
user_id=users[user_name]["id"],
)
print(f" - {user_name.title()}: Found {len(results)} result(s)")
except Exception as e:
print(f" Shared document error: {e}")
# Cleanup
print("\n8 Cleanup...")
for c in user_clients.values():
c.disconnect()
client.disconnect()
print(" Disconnected from server")
print("\n Multi-user ACL example completed!")
if __name__ == "__main__":
main()