-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_examples.py
More file actions
363 lines (288 loc) · 11.8 KB
/
basic_examples.py
File metadata and controls
363 lines (288 loc) · 11.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
"""
Basic DSPy Examples
Simple examples demonstrating core DSPy concepts:
- Basic Q&A
- Chain of thought reasoning
- RAG (Retrieval Augmented Generation)
- Pipeline optimization
"""
import dspy
import time
from typing import Optional, List
from dspy.teleprompt import BootstrapFewShot
# Handle both module import and direct script execution
try:
from .dspy_setup import setup_dspy_basic
from .logfire_setup import get_logfire_manager, logfire_span, logfire_log
except ImportError:
from dspy_setup import setup_dspy_basic
from logfire_setup import get_logfire_manager, logfire_span, logfire_log
# Initialize Logfire manager
logfire_manager = get_logfire_manager()
# === Core Signatures ===
class BasicQA(dspy.Signature):
"""Simple question answering."""
question = dspy.InputField()
answer = dspy.OutputField(desc="Clear, helpful answer")
class ChainOfThoughtQA(dspy.Signature):
"""Question answering with reasoning."""
question = dspy.InputField()
reasoning = dspy.OutputField(desc="Step-by-step reasoning")
answer = dspy.OutputField(desc="Final answer")
class RAG(dspy.Signature):
"""Answer questions using provided context."""
context = dspy.InputField(desc="Relevant information")
question = dspy.InputField()
answer = dspy.OutputField(desc="Answer based on context")
# === Pipeline Modules ===
class BasicPipeline(dspy.Module):
"""Simple pipeline that can handle basic Q&A and RAG with Logfire monitoring."""
def __init__(self):
super().__init__()
self.qa = dspy.ChainOfThought(BasicQA)
self.rag = dspy.ChainOfThought(RAG)
logfire_manager.log_event("BasicPipeline initialized", "info", component="basic_pipeline")
@logfire_span("basic_pipeline_forward", component="basic_examples")
def forward(self, question: str, context: Optional[str] = None):
"""Answer question with or without context."""
logfire_manager.log_event(
"Processing question in BasicPipeline",
"info",
question_length=len(question),
has_context=context is not None,
pipeline_type="rag" if context else "qa"
)
start_time = time.time()
try:
if context:
result = self.rag(context=context, question=question)
logfire_manager.log_event(
"RAG processing completed",
"info",
processing_time=time.time() - start_time,
context_length=len(context)
)
else:
result = self.qa(question=question)
logfire_manager.log_event(
"QA processing completed",
"info",
processing_time=time.time() - start_time
)
return result
except Exception as e:
logfire_manager.log_error(e, "BasicPipeline forward failed",
question=question[:100],
has_context=context is not None)
raise
class RAGPipeline(dspy.Module):
"""RAG pipeline with document handling and Logfire monitoring."""
def __init__(self, max_docs: int = 3):
super().__init__()
self.rag = dspy.ChainOfThought(RAG)
self.max_docs = max_docs
logfire_manager.log_event(
"RAGPipeline initialized",
"info",
component="rag_pipeline",
max_docs=max_docs
)
@logfire_span("rag_pipeline_forward", component="basic_examples")
def forward(self, question: str, documents: Optional[List[str]] = None):
"""Answer question using document context."""
if not documents:
documents = [
"DSPy is a framework for programming language models.",
"It provides signatures, modules, and optimizers.",
"DSPy enables automatic optimization of prompts."
]
logfire_manager.log_event(
"Using default documents",
"info",
default_docs_count=len(documents)
)
start_time = time.time()
logfire_manager.log_event(
"Processing RAG query",
"info",
question_length=len(question),
document_count=len(documents),
max_docs=self.max_docs
)
try:
context = " ".join(documents[:self.max_docs])
result = self.rag(context=context, question=question)
processing_time = time.time() - start_time
logfire_manager.log_event(
"RAG pipeline completed",
"info",
processing_time=processing_time,
context_length=len(context),
documents_used=min(len(documents), self.max_docs)
)
return dspy.Prediction(
context=context,
answer=result.answer
)
except Exception as e:
logfire_manager.log_error(e, "RAGPipeline forward failed",
question=question[:100],
document_count=len(documents) if documents else 0)
raise
# === Example Functions ===
@logfire_span("example_basic_qa", component="basic_examples")
def example_basic_qa():
"""Example 1: Basic question answering."""
print("\n=== Example 1: Basic Q&A ===")
logfire_manager.log_event("Starting basic QA example", "info", example="basic_qa")
pipeline = BasicPipeline()
question = "What is artificial intelligence?"
result = pipeline(question=question)
logfire_manager.log_event(
"Basic QA example completed",
"info",
question=question,
answer_length=len(result.answer) if hasattr(result, 'answer') else 0
)
print(f"Question: {question}")
print(f"Answer: {result.answer}")
@logfire_span("example_rag", component="basic_examples")
def example_rag():
"""Example 2: RAG with context."""
print("\n=== Example 2: RAG with Context ===")
logfire_manager.log_event("Starting RAG example", "info", example="rag")
pipeline = BasicPipeline()
context = "Machine learning is a subset of AI that uses algorithms to learn from data."
question = "What is machine learning?"
result = pipeline(question=question, context=context)
logfire_manager.log_event(
"RAG example completed",
"info",
question=question,
context_length=len(context),
answer_length=len(result.answer) if hasattr(result, 'answer') else 0
)
print(f"Question: {question}")
print(f"Context: {context}")
print(f"Answer: {result.answer}")
@logfire_span("example_chain_of_thought", component="basic_examples")
def example_chain_of_thought():
"""Example 3: Chain of thought reasoning."""
print("\n=== Example 3: Chain of Thought ===")
logfire_manager.log_event("Starting chain of thought example", "info", example="chain_of_thought")
cot = dspy.ChainOfThought(ChainOfThoughtQA)
question = "Why is renewable energy important?"
result = cot(question=question)
logfire_manager.log_event(
"Chain of thought example completed",
"info",
question=question,
reasoning_length=len(result.reasoning) if hasattr(result, 'reasoning') else 0
)
print(f"Question: {question}")
print(f"Reasoning: {result.reasoning}")
print(f"Answer: {result.answer}")
@logfire_span("example_rag_pipeline", component="basic_examples")
def example_rag_pipeline():
"""Example 4: RAG pipeline with documents."""
print("\n=== Example 4: RAG Pipeline ===")
logfire_manager.log_event("Starting RAG pipeline example", "info", example="rag_pipeline")
rag_pipeline = RAGPipeline()
question = "What does DSPy provide?"
result = rag_pipeline(question=question)
logfire_manager.log_event(
"RAG pipeline example completed",
"info",
question=question,
answer_length=len(result.answer) if hasattr(result, 'answer') else 0,
context_used_len=len(result.context) if hasattr(result, 'context') else 0
)
print(f"Question: {question}")
print(f"Answer: {result.answer}")
print(f"Context used: {result.context[:100]}...")
# === Optimization Example ===
def create_sample_dataset():
"""Create simple dataset for optimization using sample data."""
try:
from .util import create_training_examples
except ImportError:
from util import create_training_examples
try:
return create_training_examples()
except Exception as e:
print(f"Could not load sample data: {e}")
# Fallback to hardcoded examples
return [
dspy.Example(
question="What is AI?",
answer="AI is artificial intelligence - machines simulating human intelligence."
).with_inputs('question'),
dspy.Example(
question="What is ML?",
answer="ML is machine learning - algorithms that learn from data."
).with_inputs('question'),
dspy.Example(
question="What is NLP?",
answer="NLP is natural language processing - AI for understanding text."
).with_inputs('question')
]
def simple_metric(example, pred, trace=None):
"""Simple evaluation metric."""
try:
from .util import simple_metric as util_metric
except ImportError:
from util import simple_metric as util_metric
return util_metric(example, pred, trace)
@logfire_span("example_optimization", component="basic_examples")
def example_optimization():
"""Example 5: Pipeline optimization."""
print("\n=== Example 5: Pipeline Optimization ===")
logfire_manager.log_event("Starting optimization example", "info", example="optimization")
# Create dataset and pipeline
dataset = create_sample_dataset()
pipeline = BasicPipeline()
print("Before optimization:")
result = pipeline(question="What is AI?")
logfire_manager.log_event("Before optimization", "info", initial_answer=result.answer)
print(f"Answer: {result.answer}")
# Optimize pipeline
try:
optimizer = BootstrapFewShot(metric=simple_metric, max_bootstrapped_demos=2)
optimized = optimizer.compile(pipeline, trainset=dataset[:2])
print("\nAfter optimization:")
result = optimized(question="What is AI?")
logfire_manager.log_event("After optimization", "info", optimized_answer=result.answer)
print(f"Answer: {result.answer}")
except Exception as e:
logfire_manager.log_error(e, "Optimization failed in example_optimization")
print(f"Optimization failed: {e}")
# === Main Execution ===
@logfire_span("run_all_examples", component="basic_examples")
def run_all_examples():
"""Run all examples in sequence."""
logfire_manager.log_event("Starting all basic examples", "info", total_examples=5)
start_time = time.time()
setup_dspy_basic()
print("DSPy Basic Examples")
print("=" * 50)
try:
example_basic_qa()
example_rag()
example_chain_of_thought()
example_rag_pipeline()
example_optimization()
total_time = time.time() - start_time
logfire_manager.log_event(
"All examples completed successfully",
"info",
total_duration=total_time,
examples_completed=5
)
print("\n" + "=" * 50)
print("All examples completed!")
except Exception as e:
logfire_manager.log_error(e, "Failed during basic examples execution")
print(f"\nError during examples: {e}")
raise
if __name__ == "__main__":
run_all_examples()