WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit e902aca

Browse files
Refactor DSPy example
1 parent 8a5f385 commit e902aca

File tree

15 files changed

+2355
-1137
lines changed

15 files changed

+2355
-1137
lines changed
Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,50 @@
11
"""
2-
DSPy + Atomic Agents Integration
2+
DSPy + Atomic Agents Integration Package.
33
4-
This module provides a seamless integration between DSPy's declarative prompt optimization
5-
framework and Atomic Agents' type-safe structured output system.
4+
This package demonstrates how to combine DSPy's automatic prompt optimization
5+
with Atomic Agents' type-safe structured outputs.
66
7-
Key Components:
8-
- DSPyAtomicModule: A DSPy module that wraps Atomic Agents for structured outputs
9-
- AtomicSignature: Bridge between Pydantic schemas and DSPy signatures
10-
- Optimization utilities for improving agent performance with few-shot learning
7+
Package Structure:
8+
domain/ - Core business logic (models, datasets, evaluation)
9+
stages/ - Demonstration stages (dspy, atomic, combined)
10+
presentation/ - UI layer (Rich console output)
11+
bridge.py - DSPy ↔ Atomic Agents integration module
12+
13+
Quick Start:
14+
>>> from dspy_integration import DSPyAtomicModule, MovieReviewInput, MovieGenreOutput
15+
>>> module = DSPyAtomicModule(
16+
... input_schema=MovieReviewInput,
17+
... output_schema=MovieGenreOutput,
18+
... use_chain_of_thought=True,
19+
... )
20+
>>> result = module.run_validated(review="Amazing action movie!")
21+
>>> print(result.genre) # Type-safe output!
22+
23+
Run Demo:
24+
uv run python -m dspy_integration.main
1125
"""
1226

27+
# Domain exports
28+
from dspy_integration.domain.models import (
29+
GENRES,
30+
GenreType,
31+
MovieGenreOutput,
32+
MovieReviewInput,
33+
EvalResult,
34+
)
35+
from dspy_integration.domain.datasets import TRAINING_DATASET, TEST_DATASET
36+
from dspy_integration.domain.evaluation import evaluate_predictions
37+
38+
# Bridge exports
1339
from dspy_integration.bridge import (
1440
DSPyAtomicModule,
15-
pydantic_to_dspy_fields,
41+
DSPyAtomicPipeline,
42+
create_dspy_example,
1643
create_dspy_signature_from_schemas,
44+
pydantic_to_dspy_fields,
1745
)
46+
47+
# Original schemas (for backwards compatibility)
1848
from dspy_integration.schemas import (
1949
SentimentInputSchema,
2050
SentimentOutputSchema,
@@ -24,14 +54,47 @@
2454
SummaryOutputSchema,
2555
)
2656

57+
# Stage exports (for advanced usage)
58+
from dspy_integration.stages import (
59+
run_stage1_raw_dspy,
60+
run_stage2_raw_atomic_agents,
61+
run_stage3_combined,
62+
)
63+
64+
__version__ = "0.1.0"
65+
2766
__all__ = [
67+
# Version
68+
"__version__",
69+
# Domain - Types
70+
"GENRES",
71+
"GenreType",
72+
# Domain - Schemas (new)
73+
"MovieGenreOutput",
74+
"MovieReviewInput",
75+
# Domain - Data structures
76+
"EvalResult",
77+
# Domain - Datasets
78+
"TRAINING_DATASET",
79+
"TEST_DATASET",
80+
# Domain - Evaluation
81+
"evaluate_predictions",
82+
# Bridge - Core classes
2883
"DSPyAtomicModule",
29-
"pydantic_to_dspy_fields",
84+
"DSPyAtomicPipeline",
85+
# Bridge - Utilities
86+
"create_dspy_example",
3087
"create_dspy_signature_from_schemas",
88+
"pydantic_to_dspy_fields",
89+
# Original schemas (backwards compatibility)
3190
"SentimentInputSchema",
3291
"SentimentOutputSchema",
3392
"QuestionInputSchema",
3493
"AnswerOutputSchema",
3594
"SummaryInputSchema",
3695
"SummaryOutputSchema",
96+
# Stages - Runners
97+
"run_stage1_raw_dspy",
98+
"run_stage2_raw_atomic_agents",
99+
"run_stage3_combined",
37100
]

atomic-examples/dspy-integration/dspy_integration/bridge.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
3. Applying DSPy optimizers (BootstrapFewShot, MIPROv2, etc.) to improve agent performance
88
"""
99

10-
from typing import Type, Dict, Any, Optional, List, get_type_hints, get_origin, get_args, Literal
10+
from typing import Any, Dict, List, Literal, Optional, Type, get_args, get_origin
11+
1112
import dspy
12-
from pydantic import BaseModel, Field
13-
from pydantic.fields import FieldInfo
13+
from pydantic import BaseModel
14+
1415
from atomic_agents.base.base_io_schema import BaseIOSchema
1516

1617

@@ -38,11 +39,11 @@ def python_type_to_dspy_type(python_type: Any) -> Any:
3839
return list
3940

4041
# Handle Optional types
41-
if origin is type(None) or (hasattr(origin, '__origin__') and origin.__origin__ is type(None)):
42+
if origin is type(None) or (hasattr(origin, "__origin__") and origin.__origin__ is type(None)):
4243
return python_type
4344

4445
# Handle Union types (including Optional)
45-
if hasattr(origin, '__name__') and origin.__name__ == 'UnionType':
46+
if hasattr(origin, "__name__") and origin.__name__ == "UnionType":
4647
args = get_args(python_type)
4748
# Filter out NoneType for Optional handling
4849
non_none_args = [a for a in args if a is not type(None)]
@@ -58,8 +59,7 @@ def python_type_to_dspy_type(python_type: Any) -> Any:
5859

5960

6061
def pydantic_to_dspy_fields(
61-
schema: Type[BaseModel],
62-
field_type: str = "input"
62+
schema: Type[BaseModel], field_type: str = "input"
6363
) -> Dict[str, tuple]:
6464
"""
6565
Convert Pydantic schema fields to DSPy field definitions.
@@ -184,9 +184,7 @@ def __init__(
184184

185185
# Create DSPy signature from schemas
186186
self.signature = create_dspy_signature_from_schemas(
187-
input_schema,
188-
output_schema,
189-
instructions
187+
input_schema, output_schema, instructions
190188
)
191189

192190
# Create the predictor
@@ -300,7 +298,7 @@ def forward(self, **kwargs) -> Dict[str, Any]:
300298
current_input = {
301299
k: getattr(prediction, k)
302300
for k in dir(prediction)
303-
if not k.startswith('_') and not callable(getattr(prediction, k))
301+
if not k.startswith("_") and not callable(getattr(prediction, k))
304302
}
305303

306304
return results
@@ -336,10 +334,7 @@ def create_dspy_example(
336334
**validated_output.model_dump(),
337335
}
338336

339-
# Create DSPy example with output fields marked
340-
output_keys = list(output_schema.model_fields.keys())
341-
example = dspy.Example(**example_data).with_inputs(
342-
*list(input_schema.model_fields.keys())
343-
)
337+
# Create DSPy example with input fields marked
338+
example = dspy.Example(**example_data).with_inputs(*list(input_schema.model_fields.keys()))
344339

345340
return example
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Domain layer for DSPy + Atomic Agents integration.
3+
4+
This package contains:
5+
- models: Pydantic schemas and data transfer objects
6+
- datasets: Training and test data
7+
- evaluation: Metrics and evaluation utilities
8+
9+
Following Clean Architecture principles, this layer has no dependencies
10+
on external frameworks (except Pydantic for data modeling).
11+
"""
12+
13+
from dspy_integration.domain.models import (
14+
GenreType,
15+
GENRES,
16+
MovieGenreOutput,
17+
MovieReviewInput,
18+
EvalResult,
19+
)
20+
from dspy_integration.domain.datasets import TRAINING_DATASET, TEST_DATASET
21+
from dspy_integration.domain.evaluation import evaluate_predictions
22+
23+
__all__ = [
24+
# Types
25+
"GenreType",
26+
"GENRES",
27+
# Schemas
28+
"MovieGenreOutput",
29+
"MovieReviewInput",
30+
# Data structures
31+
"EvalResult",
32+
# Datasets
33+
"TRAINING_DATASET",
34+
"TEST_DATASET",
35+
# Evaluation
36+
"evaluate_predictions",
37+
]

0 commit comments

Comments
 (0)