-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdspy_react_chat_example.py
More file actions
405 lines (334 loc) · 14.8 KB
/
dspy_react_chat_example.py
File metadata and controls
405 lines (334 loc) · 14.8 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/env python3
"""
DSPy ReAct Weather Agent Example
A simple weather agent demonstrating standard DSPy ReAct with tool usage.
This example shows how dspy.ReAct works out-of-the-box, in contrast to ReActMachina.
Key differences from ReActMachina:
- Manual conversation history management (must manually append to history after each turn)
- No control over internal predictors (uses fixed dspy.Predict and dspy.ChainOfThought)
- Different trajectory structure (thought/tool_name/tool_args/observation vs state-based)
- Uses max_iters instead of max_steps
- History must be explicitly added to signature as input field
Usage:
Interactive mode: uv run examples/dspy_react_chat_example.py
One-turn mode: uv run examples/dspy_react_chat_example.py --query "What's the weather in Paris?"
Async mode: uv run examples/dspy_react_chat_example.py --async
Inspect history: uv run examples/dspy_react_chat_example.py --inspect-history
"""
import argparse
import asyncio
import json
import logging
import os
from datetime import datetime
from io import StringIO
from pathlib import Path
import dspy
from dspy import inspect_history
# Try to import instrumentation if available
try:
from instrumentation import configure_instrumentation
except ImportError:
# Optional instrumentation module not available
def configure_instrumentation(project_name="dspy-react-machina", endpoint="http://localhost:6006/v1/traces"): # noqa: ARG001
pass
logger = logging.getLogger(__name__)
def save_inspect_history_to_file(prefix: str = "dspy_react") -> str:
"""
Save the output of dspy.inspect_history() to a file.
Args:
prefix: Prefix for the filename (e.g., 'react_machina' or 'dspy_react')
Returns:
Path to the saved file
"""
import re
import sys
# Capture inspect_history output
old_stdout = sys.stdout
sys.stdout = StringIO()
try:
inspect_history()
output = sys.stdout.getvalue()
finally:
sys.stdout = old_stdout
# Strip ANSI color codes
ansi_escape = re.compile(r"\x1b\[[0-9;]*m")
output = ansi_escape.sub("", output)
# Generate filename with timestamp
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
examples_dir = Path(__file__).parent
filename = examples_dir / f"{prefix}_history_{timestamp}.txt"
# Save to file
filename.write_text(output)
return str(filename)
# Weather tool functions
def get_weather(city: str = "current location") -> str:
"""Get the current weather for a specified city."""
# Mock weather data
weather_data = {
"paris": "Sunny, 18°C, light breeze",
"london": "Cloudy, 12°C, moderate rain",
"tokyo": "Partly cloudy, 22°C, calm",
"new york": "Clear, 15°C, windy",
"current location": "Sunny, 20°C, perfect weather",
}
city_lower = city.lower()
weather = weather_data.get(city_lower, f"Weather data unavailable for {city}")
return weather
def list_weather_cities() -> str:
"""List all cities available for weather checking."""
cities = ["Paris", "London", "Tokyo", "New York", "current location"]
return f"Available cities for weather: {', '.join(cities)}"
async def async_main(args, agent):
"""Async main function for async mode execution."""
# Handle one-turn query
if args.query:
history = dspy.History(messages=[])
try:
response = await agent.acall(question=args.query, history=history)
print(response.answer)
# If inspect-history flag is also provided, show history after query
if args.inspect_history:
print("\n" + "=" * 50 + " INTERACTION HISTORY " + "=" * 50)
inspect_history()
saved_path = save_inspect_history_to_file("dspy_react")
print(f"\n💾 History saved to: {saved_path}")
except Exception as e:
print(f"Error: {e}")
return
# Handle inspect history request (standalone)
if args.inspect_history:
inspect_history()
saved_path = save_inspect_history_to_file("dspy_react")
print(f"\n💾 History saved to: {saved_path}")
return
# Interactive mode
history = dspy.History(messages=[])
last_response = None
print("🤖 DSPy ReAct Weather Agent (Async Mode)")
print("=" * 60)
print("I'm a weather agent that can check weather and list available cities.")
print("Available tools: get_weather, list_weather_cities")
print(
"Commands: '/quit' to exit, '/tools' to list tools, '/inspect_history' to view full interaction history, '/trajectory' to view last trajectory"
)
print("Note: Conversation history is maintained across turns.\n")
while True:
try:
user_input = input("You: ").strip()
if user_input.lower() in ["/quit", "quit", "exit"]:
print("\n👋 Goodbye!")
break
if user_input.lower() in ["/tools", "tools"]:
print("\n" + "=" * 60 + " AVAILABLE TOOLS " + "=" * 60)
for tool in agent.tools.values():
print(f"\n• {tool.name}")
print(f" Description: {tool.desc}")
if tool.args:
args_str = ", ".join(f"{k}: {v}" for k, v in tool.args.items())
print(f" Arguments: {args_str}")
else:
print(" Arguments: None")
print("\n" + "=" * 120 + "\n")
continue
if user_input.lower() in ["/inspect_history", "inspect_history"]:
print("\n" + "=" * 60 + " INTERACTION HISTORY " + "=" * 60)
inspect_history()
saved_path = save_inspect_history_to_file("dspy_react")
print(f"\n💾 History saved to: {saved_path}")
print("=" * 120 + "\n")
continue
if user_input.lower() in ["/trajectory", "trajectory"]:
print("\n" + "=" * 60 + " LAST TRAJECTORY " + "=" * 60)
if last_response and hasattr(last_response, "trajectory"):
trajectory = last_response.trajectory
if trajectory:
# Determine number of steps from trajectory keys
steps = len([k for k in trajectory.keys() if k.startswith("thought_")])
for i in range(steps):
print(f"\n--- Iteration {i} ---")
if f"thought_{i}" in trajectory:
print(f"Thought: {trajectory[f'thought_{i}']}")
if f"tool_name_{i}" in trajectory:
print(f"Tool: {trajectory[f'tool_name_{i}']}")
if f"tool_args_{i}" in trajectory:
print(f"Args: {json.dumps(trajectory[f'tool_args_{i}'], indent=2)}")
if f"observation_{i}" in trajectory:
print(f"Observation: {trajectory[f'observation_{i}']}")
else:
print("Trajectory is empty.")
else:
print("No trajectory available yet. Make a query first.")
print("=" * 120 + "\n")
continue
if not user_input:
continue
# Process user input with conversation history (async)
response = await agent.acall(question=user_input, history=history)
print(f"\nAgent: {response.answer}")
# Count steps from trajectory
if hasattr(response, "trajectory") and response.trajectory:
steps = len([k for k in response.trajectory.keys() if k.startswith("thought_")])
print(f"(Completed in {steps} steps)\n")
else:
print()
# Store last response for trajectory inspection
last_response = response
# Manually update conversation history
history.messages.append({"question": user_input, **response})
except KeyboardInterrupt:
print("\n\n👋 Goodbye!")
break
except Exception as e:
print(f"Error: {e}")
print("Please try again.\n")
def main():
"""Main function for command-line interface."""
parser = argparse.ArgumentParser(description="DSPy ReAct Weather Agent")
parser.add_argument("--query", "-q", type=str, help="Single query for one-turn response")
parser.add_argument("--inspect-history", "-i", action="store_true", help="Inspect the interaction history")
parser.add_argument(
"--async",
"-a",
dest="use_async",
action="store_true",
help="Use async mode for agent execution",
)
parser.add_argument(
"--max-iters",
"-m",
type=int,
default=10,
help="Maximum number of agent iterations before forcing completion (default: 10)",
)
args = parser.parse_args()
configure_instrumentation("dspy-react")
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
print("Error: OPENAI_API_KEY not found in environment variables.")
print("Please set your OpenAI API key in the .env file:")
print("OPENAI_API_KEY=your_api_key_here")
return
lm = dspy.LM(
"openai/gpt-4o-mini",
api_key=api_key,
temperature=0,
cache=False,
)
dspy.settings.configure(lm=lm)
# Initialize the standard DSPy ReAct agent with weather tools
# Note: We add 'history' as an input field to maintain conversation context
tools = [get_weather, list_weather_cities]
signature = "question, history -> answer"
agent = dspy.ReAct(
signature,
tools,
max_iters=args.max_iters,
)
# Route to async or sync execution
if args.use_async:
asyncio.run(async_main(args, agent))
return
# Handle one-turn query (sync mode)
if args.query:
history = dspy.History(messages=[])
try:
response = agent(question=args.query, history=history)
print(response.answer)
# If inspect-history flag is also provided, show history after query
if args.inspect_history:
print("\n" + "=" * 50 + " INTERACTION HISTORY " + "=" * 50)
inspect_history()
saved_path = save_inspect_history_to_file("dspy_react")
print(f"\n💾 History saved to: {saved_path}")
except Exception as e:
print(f"Error: {e}")
return
# Handle inspect history request (standalone)
if args.inspect_history:
inspect_history()
saved_path = save_inspect_history_to_file("dspy_react")
print(f"\n💾 History saved to: {saved_path}")
return
# Interactive mode
history = dspy.History(messages=[])
last_response = None
print("🤖 DSPy ReAct Weather Agent")
print("=" * 60)
print("I'm a weather agent that can check weather and list available cities.")
print("Available tools: get_weather, list_weather_cities")
print(
"Commands: '/quit' to exit, '/tools' to list tools, '/inspect_history' to view full interaction history, '/trajectory' to view last trajectory"
)
print("Note: Conversation history is maintained across turns.\n")
while True:
try:
user_input = input("You: ").strip()
if user_input.lower() in ["/quit", "quit", "exit"]:
print("\n👋 Goodbye!")
break
if user_input.lower() in ["/tools", "tools"]:
print("\n" + "=" * 60 + " AVAILABLE TOOLS " + "=" * 60)
for tool in agent.tools.values():
print(f"\n• {tool.name}")
print(f" Description: {tool.desc}")
if tool.args:
args_str = ", ".join(f"{k}: {v}" for k, v in tool.args.items())
print(f" Arguments: {args_str}")
else:
print(" Arguments: None")
print("\n" + "=" * 120 + "\n")
continue
if user_input.lower() in ["/inspect_history", "inspect_history"]:
print("\n" + "=" * 60 + " INTERACTION HISTORY " + "=" * 60)
inspect_history()
saved_path = save_inspect_history_to_file("dspy_react")
print(f"\n💾 History saved to: {saved_path}")
print("=" * 120 + "\n")
continue
if user_input.lower() in ["/trajectory", "trajectory"]:
print("\n" + "=" * 60 + " LAST TRAJECTORY " + "=" * 60)
if last_response and hasattr(last_response, "trajectory"):
trajectory = last_response.trajectory
if trajectory:
# Determine number of steps from trajectory keys
steps = len([k for k in trajectory.keys() if k.startswith("thought_")])
for i in range(steps):
print(f"\n--- Iteration {i} ---")
if f"thought_{i}" in trajectory:
print(f"Thought: {trajectory[f'thought_{i}']}")
if f"tool_name_{i}" in trajectory:
print(f"Tool: {trajectory[f'tool_name_{i}']}")
if f"tool_args_{i}" in trajectory:
print(f"Args: {json.dumps(trajectory[f'tool_args_{i}'], indent=2)}")
if f"observation_{i}" in trajectory:
print(f"Observation: {trajectory[f'observation_{i}']}")
else:
print("Trajectory is empty.")
else:
print("No trajectory available yet. Make a query first.")
print("=" * 120 + "\n")
continue
if not user_input:
continue
# Process user input with conversation history
response = agent(question=user_input, history=history)
print(f"\nAgent: {response.answer}")
# Count steps from trajectory
if hasattr(response, "trajectory") and response.trajectory:
steps = len([k for k in response.trajectory.keys() if k.startswith("thought_")])
print(f"(Completed in {steps} steps)\n")
else:
print()
# Store last response for trajectory inspection
last_response = response
# Manually update conversation history
history.messages.append({"question": user_input, **response})
except KeyboardInterrupt:
print("\n\n👋 Goodbye!")
break
except Exception as e:
print(f"Error: {e}")
print("Please try again.\n")
if __name__ == "__main__":
main()