Skip to content

Commit cfd51b5

Browse files
committed
Refinements
1 parent 7a76a2c commit cfd51b5

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

docs/docs/api/optimizers/GEPA.md

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ gepa = dspy.GEPA(
147147

148148
We invite community contributions of new instruction proposers for specialized domains as the [GEPA library](https://github.com/gepa-ai/gepa) continues to grow.
149149

150-
### How to Implement Custom Proposer
150+
### How to Implement Custom Instruction Proposers
151151

152152
Custom instruction proposers must implement the `ProposalFn` protocol:
153153

@@ -167,7 +167,7 @@ def __call__(
167167
- `ReflectiveExample` TypedDict contains:
168168
- `Inputs: dict[str, Any]` - Predictor inputs (may include dspy.Image objects)
169169
- `Generated_Outputs: dict[str, Any] | str` - Success: output fields dict, Failure: error message
170-
- `Feedback: str` - Always a string from metric function or parsing error
170+
- `Feedback: str` - Always a string from metric function or auto-generated by GEPA
171171

172172
#### Basic Example: Word Limit Proposer
173173

@@ -210,7 +210,7 @@ class WordLimitProposer(ProposalFn):
210210
result = self.instruction_improver(
211211
current_instruction=current_instruction,
212212
issues_found=feedback_text,
213-
max_words=str(self.max_words)
213+
max_words=self.max_words
214214
)
215215

216216
updated_components[component_name] = result.improved_instruction
@@ -243,7 +243,7 @@ class GenerateDocumentationQuery(dspy.Signature):
243243

244244
failure_patterns: str = dspy.OutputField(desc="Summarize the common failure patterns identified in the examples")
245245

246-
retrieval_queries: list[str] = dspy.OutputField(desc="Specific search query to find relevant documentation in the database that addresses the common issue patterns identified in the problematic examples")
246+
retrieval_queries: list[str] = dspy.OutputField(desc="Specific search queries to find relevant documentation in the database that addresses the common issue patterns identified in the problematic examples")
247247

248248
class GenerateRAGEnhancedInstruction(dspy.Signature):
249249
"""Generate improved instructions using retrieved documentation and examples analysis."""
@@ -273,19 +273,22 @@ class RAGInstructionImprover(dspy.Module):
273273
)
274274

275275
# Retrieve relevant documentation using multiple queries
276-
all_retrieved_docs = []
277-
for query in query_result.retrieval_queries:
278-
retrieved_docs = self.retrieve(query)
279-
if hasattr(retrieved_docs, 'passages'):
280-
all_retrieved_docs.extend(retrieved_docs.passages)
281-
else:
282-
all_retrieved_docs.append(str(retrieved_docs))
276+
results = self.retrieve.query(
277+
query_texts=query_result.retrieval_queries,
278+
n_results=3
279+
)
280+
281+
# Format retrieved documentation by query
282+
relevant_docs_parts = []
283+
for i, (query, query_docs) in enumerate(zip(query_result.retrieval_queries, results['documents'])):
284+
if query_docs: # Only include queries that found results
285+
docs_formatted = "\n".join([f" - {doc}" for doc in query_docs])
286+
relevant_docs_parts.append(
287+
f"**Search Query #{i+1}**: {query}\n"
288+
f"**Retrieved Guidelines**:\n{docs_formatted}"
289+
)
283290

284-
# Format retrieved documentation
285-
relevant_docs = "\n\n".join([
286-
f"Guideline {i+1}: {doc}"
287-
for i, doc in enumerate(all_retrieved_docs)
288-
])
291+
relevant_docs = "\n\n" + "="*60 + "\n\n".join(relevant_docs_parts) + "\n" + "="*60
289292

290293
# Generate improved instruction with retrieved context
291294
result = self.generate_answer(
@@ -327,22 +330,18 @@ class DocumentationEnhancedProposer(ProposalFn):
327330

328331
return updated_components
329332

330-
# Usage - Setup specialized documentation retriever
331-
import dspy
332-
from dspy.retrieve.chromadb_rm import ChromadbRM
333+
# Usage - Connect to existing ChromaDB collection
334+
import chromadb
333335

334-
# Setup retriever for your specialized documentation
335-
doc_retriever = ChromadbRM(
336-
collection_name="instruction_guidelines",
337-
persist_directory="./specialized_docs_db",
338-
k=5 # Retrieve top 5 relevant documents
339-
)
336+
# Connect to existing ChromaDB collection with instruction guidelines
337+
client = chromadb.Client()
338+
collection = client.get_collection("instruction_guidelines")
340339

341340
# Configure the RAG-enhanced proposer
342341
gepa = dspy.GEPA(
343342
metric=task_specific_metric,
344343
reflection_lm=dspy.LM(model="gpt-5", temperature=1.0, max_tokens=32000, api_key=api_key),
345-
instruction_proposer=DocumentationEnhancedProposer(doc_retriever),
344+
instruction_proposer=DocumentationEnhancedProposer(collection),
346345
auto="medium"
347346
)
348347
```

0 commit comments

Comments
 (0)