pythonadvanced
DSPy Chain-of-Thought Module
Use DSPy to programmatically build and optimise chain-of-thought reasoning pipelines.
pythonPress ⌘/Ctrl + Shift + C to copy
import dspy
dspy.configure(lm=dspy.OpenAI(model='gpt-4o-mini'))
class QASignature(dspy.Signature):
'''Answer questions with step-by-step reasoning.'''
question: str = dspy.InputField()
answer: str = dspy.OutputField(desc='concise final answer')
class CoTQA(dspy.Module):
def __init__(self):
self.cot = dspy.ChainOfThought(QASignature)
def forward(self, question: str) -> dspy.Prediction:
return self.cot(question=question)
qa = CoTQA()
questions = ['If a train travels 60mph for 2.5 hours, how far does it go?', 'What is the factorial of 6?']
for q in questions:
pred = qa(question=q)
print(f'Q: {q}')
print(f'A: {pred.answer}\n')Use Cases
- systematic reasoning
- LLM optimization
- research pipelines
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptadvanced
Semantic Caching Layer for LLM Calls
Cache LLM responses by semantic similarity of prompts to reduce API costs and improve latency.
Best for: Reducing LLM API costs for repeated queries
#caching#embeddings
typescriptintermediate
Batch Embeddings Processing
Generate embeddings for large document sets in batches with rate limiting and progress tracking.
Best for: Indexing large document collections for search
#embeddings#batch-processing
typescriptadvanced
AI Model Router for Cost Optimization
Route prompts to different LLM models based on complexity to optimize cost and response quality.
Best for: Reducing AI API costs for production apps
#routing#optimization
pythonbeginner
Token Counting and Cost Estimation
Count tokens accurately and estimate API costs before making LLM calls.
Best for: Budget management
#ai#tokens