You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
model = SDKRealtimeLLM(model_name="gpt-4o-realtime-preview")
116
124
117
-
# Create the pipeline with tools
125
+
# Create an app context instance (optional)
126
+
app_context = MyAppContext()
127
+
128
+
# Create the pipeline with tools and shared context
118
129
pipeline = RealtimeVoicePipeline(
119
130
model=model,
120
131
tools=[get_weather, get_time],
121
132
config=config,
133
+
shared_context=app_context, # Optional: shared state for context-aware tools
122
134
)
123
135
124
136
# Start the pipeline
@@ -147,6 +159,117 @@ while True:
147
159
break
148
160
```
149
161
162
+
### Using Shared Context with Tools
163
+
164
+
The `RealtimeVoicePipeline` supports passing a shared context object to tools, allowing them to access and modify shared state across multiple interactions. This is useful for building more complex voice applications that need to maintain state, such as:
165
+
166
+
- Tracking user preferences
167
+
- Maintaining conversation history
168
+
- Counting interactions
169
+
- Storing user information
170
+
171
+
#### Setting up a shared context
172
+
173
+
To use shared context with tools:
174
+
175
+
1. Define a context class (typically a dataclass) to hold your application state
176
+
2. Create an instance of this class
177
+
3. Pass it to the `RealtimeVoicePipeline` using the `shared_context` parameter
178
+
4. Create tools that accept a `RunContextWrapper[YourContextType]` as their first parameter
shared_context=app_context, # Pass the context here
219
+
)
220
+
```
221
+
222
+
#### How it works
223
+
224
+
1. The `RealtimeVoicePipeline` passes the shared context to its internal `ToolExecutor`
225
+
2. When the LLM calls a tool, the `ToolExecutor` checks if the tool's first parameter is named `context`
226
+
3. If it is, the executor wraps your context object in a `RunContextWrapper` and passes it to the tool
227
+
4. The tool can then access and modify your context object via `context.context`
228
+
5. Since all tools share the same context object, changes made by one tool are visible to other tools in future calls
229
+
230
+
This mechanism allows your tools to maintain shared state across turns and interactions in your voice application, without needing to set up a separate state management system.
231
+
232
+
#### Context-Aware vs. Standard Tools
233
+
234
+
You can mix both context-aware and standard tools in the same `RealtimeVoicePipeline`:
# Get the OpenAI API key from environment variables
79
121
api_key=os.environ.get("OPENAI_API_KEY")
80
122
ifnotapi_key:
@@ -117,18 +159,22 @@ async def main():
117
159
realtime_settings={
118
160
"turn_detection": "server_vad", # Use server-side VAD
119
161
"assistant_voice": "alloy",
120
-
"system_message": "You are a helpful assistant that responds concisely.",
162
+
"system_message": "You are a helpful assistant that responds concisely. You can use the greet_user_and_count tool to greet the user by name and the get_user_details tool to retrieve information about the user.",
0 commit comments