-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontext_injection.py
More file actions
214 lines (163 loc) · 6.29 KB
/
context_injection.py
File metadata and controls
214 lines (163 loc) · 6.29 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
"""Context Injection example (Pydantic AI style).
This example demonstrates dependency injection for tools:
- Defining typed context with dataclasses
- Injecting context into tools via RunContext
- Using deps_type for type-safe context access
"""
import asyncio
from dataclasses import dataclass, field
from typing import List, Optional
from miiflow_agent import LLMClient, Agent, RunContext, tool
# Define your application context
@dataclass
class UserContext:
"""Context containing user information and preferences."""
user_id: str
username: str
role: str = "user"
preferences: dict = field(default_factory=dict)
@dataclass
class DatabaseContext:
"""Context with database connection info."""
db_name: str
connection_string: str = "localhost:5432"
tables: List[str] = field(default_factory=list)
@dataclass
class AppContext:
"""Combined application context."""
user: UserContext
db: DatabaseContext
session_id: str = "session_001"
# Tools that use context injection
@tool("get_user_profile", "Get the current user's profile information")
def get_user_profile(ctx: RunContext[UserContext]) -> str:
"""Get profile info from the injected context.
The ctx parameter is automatically injected by the agent.
"""
user = ctx.deps
return f"""
User Profile:
- ID: {user.user_id}
- Username: {user.username}
- Role: {user.role}
- Preferences: {user.preferences}
"""
@tool("get_user_preferences", "Get the user's saved preferences")
def get_user_preferences(ctx: RunContext[UserContext], category: Optional[str] = None) -> str:
"""Get user preferences, optionally filtered by category."""
prefs = ctx.deps.preferences
if category and category in prefs:
return f"Preference for {category}: {prefs[category]}"
return f"All preferences: {prefs}"
@tool("check_permission", "Check if user has a specific permission")
def check_permission(ctx: RunContext[UserContext], permission: str) -> str:
"""Check if the current user has a permission based on their role."""
role_permissions = {
"admin": ["read", "write", "delete", "admin"],
"user": ["read", "write"],
"guest": ["read"],
}
user_perms = role_permissions.get(ctx.deps.role, [])
has_perm = permission in user_perms
return f"User {ctx.deps.username} {'has' if has_perm else 'does not have'} '{permission}' permission"
@tool("query_database", "Query the database for information")
def query_database(ctx: RunContext[AppContext], table: str, query: str) -> str:
"""Query the database using the injected connection context."""
db = ctx.deps.db
if table not in db.tables:
return f"Error: Table '{table}' not found. Available tables: {db.tables}"
return f"""
Query executed on {db.db_name}:
- Table: {table}
- Query: {query}
- Session: {ctx.deps.session_id}
- Result: [Simulated results for '{query}']
"""
async def user_context_example():
"""Example with user context injection."""
client = LLMClient.create("openai", model="gpt-4o-mini")
# Create agent with typed context
agent = Agent(client, deps_type=UserContext)
# Add context-aware tools
agent.add_tool(get_user_profile)
agent.add_tool(get_user_preferences)
agent.add_tool(check_permission)
# Create context
user = UserContext(
user_id="user_123",
username="alice",
role="admin",
preferences={"theme": "dark", "language": "en", "notifications": True},
)
# Run with injected context
print("Query: What is my profile and do I have admin permission?")
result = await agent.run(
"What is my profile and do I have admin permission?",
deps=user,
)
print(f"Answer: {result.data}\n")
async def app_context_example():
"""Example with combined application context."""
client = LLMClient.create("openai", model="gpt-4o-mini")
# Create agent with complex context type
agent = Agent(client, deps_type=AppContext)
agent.add_tool(query_database)
# Create nested context
app_ctx = AppContext(
user=UserContext(user_id="u1", username="bob", role="user"),
db=DatabaseContext(
db_name="production_db",
connection_string="postgres://localhost:5432",
tables=["users", "orders", "products"],
),
session_id="sess_abc123",
)
print("Query: Find all orders from the orders table")
result = await agent.run(
"Find all orders from the orders table",
deps=app_ctx,
)
print(f"Answer: {result.data}\n")
async def context_without_injection():
"""Example showing tools without context injection still work."""
client = LLMClient.create("openai", model="gpt-4o-mini")
# Tool without context
@tool("add_numbers", "Add two numbers together")
def add_numbers(a: int, b: int) -> int:
"""Simple tool that doesn't need context."""
return a + b
agent = Agent(client)
agent.add_tool(add_numbers)
print("Query: What is 15 + 27?")
result = await agent.run("What is 15 + 27?")
print(f"Answer: {result.data}\n")
async def mixed_tools_example():
"""Example mixing context-injected and regular tools."""
client = LLMClient.create("openai", model="gpt-4o-mini")
@tool("calculate", "Calculate a math expression")
def calculate(expression: str) -> str:
"""Tool without context."""
try:
return str(eval(expression))
except:
return "Error evaluating expression"
agent = Agent(client, deps_type=UserContext)
# Add both types of tools
agent.add_tool(calculate) # No context
agent.add_tool(get_user_profile) # With context
user = UserContext(user_id="u1", username="carol", role="user")
print("Query: What's my username and what's 100 / 4?")
result = await agent.run(
"What's my username and what's 100 / 4?",
deps=user,
)
print(f"Answer: {result.data}\n")
if __name__ == "__main__":
print("=== User Context Injection ===")
asyncio.run(user_context_example())
print("=== Application Context Injection ===")
asyncio.run(app_context_example())
print("=== Tools Without Context ===")
asyncio.run(context_without_injection())
print("=== Mixed Tools Example ===")
asyncio.run(mixed_tools_example())