-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact_machina_chat_example.py
More file actions
431 lines (360 loc) · 15.8 KB
/
Copy pathreact_machina_chat_example.py
File metadata and controls
431 lines (360 loc) · 15.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/env python3
"""
ReActMachina Weather Agent Example
A simple weather agent demonstrating ReActMachina's conversation history and tool usage.
The agent can check weather for different cities and list available cities.
Usage:
Interactive mode: uv run examples/react_machina_chat_example.py
One-turn mode: uv run examples/react_machina_chat_example.py --query "What's the weather in Paris?"
With Predict: uv run examples/react_machina_chat_example.py --predictor predict
Async mode: uv run examples/react_machina_chat_example.py --async
Inspect history: uv run examples/react_machina_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
from dspy_react_machina import ReActMachina
# 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 = "react_machina") -> 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("react_machina")
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("react_machina")
print(f"\n💾 History saved to: {saved_path}")
return
# Interactive mode
history = dspy.History(messages=[])
last_response = None
predictor_name = "ChainOfThought" if args.predictor == "cot" else "Predict"
print("🤖 DSPy ReActMachina Weather Agent (Async Mode)")
print("=" * 60)
print(f"Predictor: {predictor_name}")
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: All tool interactions are preserved in conversation history.\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.tool_registry.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("react_machina")
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("state_")])
for i in range(steps):
print(f"\n--- Iteration {i} ---")
if f"state_{i}" in trajectory:
print(f"State: {trajectory[f'state_{i}']}")
if f"reasoning_{i}" in trajectory:
print(f"Reasoning: {trajectory[f'reasoning_{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 current history (async)
response = await agent.acall(question=user_input, history=history)
print(f"\nAgent: {response.answer}")
print(f"(Completed in {response.steps} steps)\n")
# Store last response for trajectory inspection
last_response = response
# Update history with the agent's full interaction history
history = response.history
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 ReActMachina 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(
"--predictor",
"-p",
type=str,
choices=["predict", "cot"],
default="cot",
help="Predictor type: 'predict' for dspy.Predict, 'cot' for dspy.ChainOfThought (default: cot)",
)
parser.add_argument(
"--async",
"-a",
dest="use_async",
action="store_true",
help="Use async mode for agent execution",
)
parser.add_argument(
"--max-steps",
"-m",
type=int,
default=10,
help="Maximum number of agent steps before forcing completion (default: 10)",
)
parser.add_argument(
"--debug-fail-mode",
type=str,
choices=["malformed-markers", "missing-markers"],
default=None,
help="[DEBUG ONLY] Force LLM to produce malformed output for error handling tests. DO NOT USE IN PRODUCTION.",
)
parser.add_argument(
"--debug-fail-step",
type=int,
default=None,
help="[DEBUG ONLY] Step index (0-based) to inject failure. If not specified, injects on all steps.",
)
args = parser.parse_args()
# Map predictor argument to class
predictor_map = {"predict": dspy.Predict, "cot": dspy.ChainOfThought}
predictor_class = predictor_map[args.predictor]
configure_instrumentation("dspy-react-machina")
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-4.1-mini",
api_key=api_key,
temperature=0,
cache=False,
)
dspy.settings.configure(lm=lm)
# Initialize the ReActMachina agent with weather tools
tools = [get_weather, list_weather_cities]
signature = "question -> answer"
# Build debug config if debug flags are set
debug_config = None
if args.debug_fail_mode:
debug_config = {
"fail_mode": args.debug_fail_mode,
"fail_step": args.debug_fail_step,
}
agent = ReActMachina(
signature,
tools,
max_steps=args.max_steps,
predictor_class=predictor_class,
_debug_config=debug_config,
) # type: ignore[arg-type]
# 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("react_machina")
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("react_machina")
print(f"\n💾 History saved to: {saved_path}")
return
# Interactive mode
history = dspy.History(messages=[])
last_response = None
predictor_name = "ChainOfThought" if predictor_class == dspy.ChainOfThought else "Predict"
print("🤖 DSPy ReActMachina Weather Agent")
print("=" * 60)
print(f"Predictor: {predictor_name}")
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: All tool interactions are preserved in conversation history.\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.tool_registry.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("react_machina")
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("state_")])
for i in range(steps):
print(f"\n--- Iteration {i} ---")
if f"state_{i}" in trajectory:
print(f"State: {trajectory[f'state_{i}']}")
if f"reasoning_{i}" in trajectory:
print(f"Reasoning: {trajectory[f'reasoning_{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 current history
response = agent(question=user_input, history=history)
print(f"\nAgent: {response.answer}")
print(f"(Completed in {response.steps} steps)\n")
# Store last response for trajectory inspection
last_response = response
# Update history with the agent's full interaction history
history = response.history
except KeyboardInterrupt:
print("\n\n👋 Goodbye!")
break
except Exception as e:
print(f"Error: {e}")
print("Please try again.\n")
if __name__ == "__main__":
main()