Question Answering System
Build a question answering system with DSPy Code.
Overview
This tutorial shows you how to build a question answering system that can answer questions based on context.
Step 1: Start and Initialize
Step 2: Create the QA System
Step 3: Review Generated Code
import dspy
class QuestionAnswering(dspy.Module):
"""Question answering module."""
def __init__(self):
super().__init__()
self.answer = dspy.ChainOfThought(
"context, question -> reasoning -> answer"
)
def forward(self, context, question):
return self.answer(context=context, question=question)
Step 4: Save and Test
/save qa_system.py
/run qa_system.py --input context="DSPy is a framework..." question="What is DSPy?"
Complete Example
import dspy
# Configure model
lm = dspy.LM(model="ollama/llama3.1:8b")
dspy.configure(lm=lm)
# Create QA module
class QuestionAnswering(dspy.Module):
def __init__(self):
super().__init__()
self.answer = dspy.ChainOfThought(
"context, question -> reasoning -> answer"
)
def forward(self, context, question):
return self.answer(context=context, question=question)
# Use it
qa = QuestionAnswering()
result = qa(
context="DSPy is a framework for building AI systems.",
question="What is DSPy?"
)
print(result.answer)
Enhancements
Add Reasoning
Add Confidence
class QuestionAnswering(dspy.Module):
def __init__(self):
super().__init__()
self.answer = dspy.ChainOfThought(
"context, question -> reasoning -> answer, confidence"
)
Next Steps
- Add retrieval for RAG
- Optimize with GEPA
- Add evaluation metrics
For more details, see RAG System