Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Fix coding style in dspy/experimental (#8178)

Chen Qiane6b2a212294f
diff --git a/dspy/experimental/__init__.py b/dspy/experimental/__init__.py
index b22528415..25cdf7b7b 100644
--- a/dspy/experimental/__init__.py
+++ b/dspy/experimental/__init__.py
@@ -1,4 +1,3 @@
from dspy.experimental.module_graph import *
-
from dspy.experimental.synthesizer import *
from dspy.experimental.synthetic_data import *
diff --git a/dspy/experimental/module_graph.py b/dspy/experimental/module_graph.py
index 4857f9703..98e53f6ed 100644
--- a/dspy/experimental/module_graph.py
+++ b/dspy/experimental/module_graph.py
@@ -2,6 +2,7 @@ import dspy
try:
import graphviz # type: ignore
+
graphviz_available = True
except ImportError:
graphviz_available = False
@@ -12,9 +13,10 @@ class ModuleGraph:
if graphviz_available is False:
raise ImportError(
"""Please install graphviz to use this feature.
- Run 'pip install graphviz'""")
+ Run 'pip install graphviz'"""
+ )
- self.graph = graphviz.Digraph(format='png')
+ self.graph = graphviz.Digraph(format="png")
self.nodes = set()
self.module_name = module_name
self.module = module
@@ -23,13 +25,18 @@ class ModuleGraph:
def inspect_settings(self, settings):
"""Check for the existence and configuration of LM and RM and add them to the graph."""
- components = {'lm': settings.lm, 'rm': settings.rm}
+ components = {"lm": settings.lm, "rm": settings.rm}
for component_name, component in components.items():
if component:
- details = {attr: getattr(component, attr) for attr in dir(component)
- if not attr.startswith('_') and not callable(getattr(component, attr))}
- component_details = f"{component_name.upper()} Details: " + ', '.join(f"{k}: {v}" for k, v in details.items() if k!='history')
- self.graph.node(component_name, label=component_details, shape='box')
+ details = {
+ attr: getattr(component, attr)
+ for attr in dir(component)
+ if not attr.startswith("_") and not callable(getattr(component, attr))
+ }
+ component_details = f"{component_name.upper()} Details: " + ", ".join(
+ f"{k}: {v}" for k, v in details.items() if k != "history"
+ )
+ self.graph.node(component_name, label=component_details, shape="box")
self.nodes.add(component_name)
def add_module(self, module_name, module):
@@ -37,21 +44,21 @@ class ModuleGraph:
module_type = type(module)
- if 'dspy.predict' in str(module_type):
+ if "dspy.predict" in str(module_type):
module_name = self.generate_module_name(module_name, module_type)
self.process_submodule(module_name, module)
else:
self.process_submodules(module_name, module)
- def generate_module_name(self, base_name, module_type): # noqa: ANN201
- """ Generate a module name based on the module type"""
+ def generate_module_name(self, base_name, module_type):
+ """Generate a module name based on the module type"""
type_map = {
- 'Predict': '__Predict',
- 'ReAct': '__ReAct',
- 'ChainOfThought': '__ChainOfThought',
- 'ProgramOfThought': '__ProgramOfThought',
- 'MultiChainComparison': '__MultiChainComparison',
- 'majority': '__majority',
+ "Predict": "__Predict",
+ "ReAct": "__ReAct",
+ "ChainOfThought": "__ChainOfThought",
+ "ProgramOfThought": "__ProgramOfThought",
+ "MultiChainComparison": "__MultiChainComparison",
+ "majority": "__majority",
}
for key, suffix in type_map.items():
@@ -60,7 +67,7 @@ class ModuleGraph:
return base_name
def process_submodules(self, module_name, module):
- """ Process submodules of a module and add them to the graph"""
+ """Process submodules of a module and add them to the graph"""
for sub_module_name, sub_module in module.__dict__.items():
if isinstance(sub_module, dspy.Predict):
@@ -69,29 +76,31 @@ class ModuleGraph:
elif isinstance(sub_module, (dspy.Module, dspy.Retrieve)):
self.add_module(f"{module_name}__{sub_module_name}", sub_module)
if isinstance(sub_module, dspy.Retrieve):
- self.graph.edge("rm", 'lm', label='RM used in Module')
+ self.graph.edge("rm", "lm", label="RM used in Module")
def process_submodule(self, sub_module_name, sub_module):
"""Process a submodule and add it to the graph"""
- for field_type, fields in [('input', sub_module.signature.input_fields),
- ('output', sub_module.signature.output_fields)]:
+ for field_type, fields in [
+ ("input", sub_module.signature.input_fields),
+ ("output", sub_module.signature.output_fields),
+ ]:
for field_name, field in fields.items():
node_id = f"{sub_module_name}_{field_type}_{field_name}"
if node_id not in self.nodes:
label = f"{field_name}: ({field.json_schema_extra['desc']})"
- self.graph.node(node_id, label=label, shape='ellipse')
+ self.graph.node(node_id, label=label, shape="ellipse")
self.nodes.add(node_id)
- edge_direction = (node_id, sub_module_name) if field_type == 'input' else (sub_module_name, node_id)
+ edge_direction = (node_id, sub_module_name) if field_type == "input" else (sub_module_name, node_id)
self.graph.edge(*edge_direction)
# Add node for the submodule itself
- self.graph.node(sub_module_name, label=sub_module_name, shape='box')
+ self.graph.node(sub_module_name, label=sub_module_name, shape="box")
self.nodes.add(sub_module_name)
# Connect submodule to LM if configured
- if 'lm' in self.nodes:
- self.graph.edge('lm', sub_module_name, label='LM used in Module')
+ if "lm" in self.nodes:
+ self.graph.edge("lm", sub_module_name, label="LM used in Module")
def render_graph(self, filename=None):
"""Render the graph to a file(png)"""
diff --git a/dspy/experimental/synthesizer/__init__.py b/dspy/experimental/synthesizer/__init__.py
index 78f879c74..861a14ce9 100644
--- a/dspy/experimental/synthesizer/__init__.py
+++ b/dspy/experimental/synthesizer/__init__.py
@@ -1,6 +1,5 @@
-from dspy.experimental.synthesizer.synthesizer import Synthesizer
from dspy.experimental.synthesizer.config import SynthesizerArguments
-
+from dspy.experimental.synthesizer.synthesizer import Synthesizer
__all__ = [
"Synthesizer",
diff --git a/dspy/experimental/synthesizer/config.py b/dspy/experimental/synthesizer/config.py
index 557299cfa..ff2f04f14 100644
--- a/dspy/experimental/synthesizer/config.py
+++ b/dspy/experimental/synthesizer/config.py
@@ -13,12 +13,12 @@ class SynthesizerArguments(BaseModel):
num_example_for_optim: Optional[int] = None
- @model_validator(mode='after')
+ @model_validator(mode="after")
def validate_feedback_mode(self):
if self.feedback_mode and self.feedback_mode not in ["human", "llm"]:
raise ValueError("Feedback mode should be either 'human' or 'llm'.")
-
+
if self.feedback_mode and not self.num_example_for_feedback:
raise ValueError("Number of examples for feedback is required when feedback mode is provided.")
- return self
\ No newline at end of file
+ return self
diff --git a/dspy/experimental/synthesizer/instruction_suffixes.py b/dspy/experimental/synthesizer/instruction_suffixes.py
index 53404a2a6..753550bd4 100644
--- a/dspy/experimental/synthesizer/instruction_suffixes.py
+++ b/dspy/experimental/synthesizer/instruction_suffixes.py
@@ -1,3 +1,3 @@
INPUT_GENERATION_TASK_WITH_EXAMPLES_SUFFIX = """\n\nI'll also be providing you some data I generated before hand, make sure the data you generate if consistent with task I provided but different from the data I provided in every way possible."""
-INPUT_GENERATION_TASK_WITH_FEEDBACK_SUFFIX = "\n\nAdditionally, I'll be providing you with feedback on the data you generate, while generating the data make sure to take into account the feedback I provide and try to improve the data you generate based on the feedback I provide."
\ No newline at end of file
+INPUT_GENERATION_TASK_WITH_FEEDBACK_SUFFIX = "\n\nAdditionally, I'll be providing you with feedback on the data you generate, while generating the data make sure to take into account the feedback I provide and try to improve the data you generate based on the feedback I provide."
diff --git a/dspy/experimental/synthesizer/signatures.py b/dspy/experimental/synthesizer/signatures.py
index e1a50c689..022b1de95 100644
--- a/dspy/experimental/synthesizer/signatures.py
+++ b/dspy/experimental/synthesizer/signatures.py
@@ -15,6 +15,7 @@ class UnderstandTask(dspy.Signature):
desc="Explanation of the task.",
)
+
class ExplainTask(dspy.Signature):
"""Analyze the provided set of datapoints carefully, and prepare a concise, comprehensible summary that captures the broad essence and purpose of the task these datapoints aim to address. Your summary should illuminate the general objective and the type of problem being solved, offering a clear picture of what the task entails at a high level. Avoid getting into the nuances of individual datapoints, specifics about models, examples, algorithms, or any intricate technicalities. Your explanation should serve to clarify the task's overall goal and its basic premise, without touching on methodologies or solutions."""
@@ -28,6 +29,7 @@ class ExplainTask(dspy.Signature):
desc="Explanation of the task.",
)
+
class UpdateTaskDescriptionBasedOnFeedback(dspy.Signature):
"""Update the task description based on the feedback provided. Ensure that the revised task description incorporates the feedback to improve its overall clarity and effectiveness. Focus on enhancing the task's goal and basic premise, without delving into specific data points, models, examples, algorithms, or technical intricacies. Your explanation should aim to clarify the task's fundamental objective and purpose."""
@@ -44,6 +46,7 @@ class UpdateTaskDescriptionBasedOnFeedback(dspy.Signature):
desc="Updated description of the task.",
)
+
class GetFeedbackOnGeneration(dspy.Signature):
"""Provide constructive feedback on the synthetic data generated, focusing on its quality, relevance, and diversity. Highlight any areas that require improvement and offer suggestions for enhancement. The feedback should center on the overall effectiveness of the synthetic data in aligning with the task description and knowledge seed. Avoid delving into specific data points, models, examples, algorithms, or technical intricacies. Your feedback should be critical but constructive, aiming to improve the synthetic data and the task description."""
@@ -61,6 +64,7 @@ class GetFeedbackOnGeneration(dspy.Signature):
desc="Feedback on the synthetic data.",
)
+
class GenerateFieldDescription(dspy.Signature):
"""Generate a concise and informative description for a given field based on the provided name and task description. This description should be no longer than 10 words and should be in simple english."""
@@ -77,10 +81,11 @@ class GenerateFieldDescription(dspy.Signature):
desc="Description of the field.",
)
+
class GenerateInputFieldsData(dspy.Signature):
"""Create synthetic data using the task description and the provided knowledge seed. Your task is to generate diverse and imaginative data that aligns with the given task description and knowledge seed. You are encouraged to be creative and not limit yourself, allowing for a wide range of synthetic data that reflects the characteristics and details provided in the task description. The data should be unique and varied, showcasing originality and creativity while maintaining relevance to the task and knowledge seed.
-A knowledge seed is the index of the knowledge base you have, each index represents a different knowledge base."""
+ A knowledge seed is the index of the knowledge base you have, each index represents a different knowledge base."""
knowledge_seed = dspy.InputField(
prefix="Knowledge Seed:",
@@ -92,5 +97,6 @@ A knowledge seed is the index of the knowledge base you have, each index represe
desc="Description of the task the field is an input to.",
)
+
class GenerateOutputFieldsData(dspy.Signature):
- pass
\ No newline at end of file
+ pass
diff --git a/dspy/experimental/synthesizer/synthesizer.py b/dspy/experimental/synthesizer/synthesizer.py
index e1184c5e5..666074ba8 100644
--- a/dspy/experimental/synthesizer/synthesizer.py
+++ b/dspy/experimental/synthesizer/synthesizer.py
@@ -42,18 +42,18 @@ class Synthesizer:
if self.config.feedback_mode == "human":
input_keys = examples.inputs().keys()
- print("-"*75)
+ print("-" * 75)
print_text = "[bold blue]Generated Data:[bold blue]\n[bold red]Inputs:[bold red]\n"
-
+
for key in input_keys:
print_text += f"\t[bold yellow]{key}[bold yellow]: [green]{examples[key]}[green]\n"
-
+
rprint(print_text)
feedback = input("Provide feedback on the generated data: ")
- print("-"*75)
+ print("-" * 75)
return feedback
-
+
elif self.config.feedback_mode == "llm":
feedback = self.get_feedback_on_generation(
synthetic_data=[examples],
@@ -143,16 +143,16 @@ class Synthesizer:
task_description = ground_source.__doc__
if task_description.startswith("Given the fields"):
task_description = self.understand_task(examples=ground_source.__doc__).explanation
-
- input_keys = {k:v.json_schema_extra["desc"] for k,v in ground_source.input_fields.items()}
- output_keys = {k:v.json_schema_extra["desc"] for k,v in ground_source.output_fields.items()}
+
+ input_keys = {k: v.json_schema_extra["desc"] for k, v in ground_source.input_fields.items()}
+ output_keys = {k: v.json_schema_extra["desc"] for k, v in ground_source.output_fields.items()}
return task_description, input_keys, output_keys
elif isinstance(ground_source, list) and isinstance(ground_source[0], dspy.Example):
task_description = self.explain_task(examples=ground_source).explanation
- input_keys = {key:f"${{{key}}}" for key in ground_source[0].inputs()}
- output_keys = {key:f"${{{key}}}" for key in ground_source[0].labels()}
+ input_keys = {key: f"${{{key}}}" for key in ground_source[0].inputs()}
+ output_keys = {key: f"${{{key}}}" for key in ground_source[0].labels()}
return task_description, input_keys, output_keys
@@ -170,7 +170,7 @@ class Synthesizer:
if self.config.num_example_for_optim:
self.generate_input_data.__doc__ += INPUT_GENERATION_TASK_WITH_EXAMPLES_SUFFIX
-
+
if self.config.feedback_mode:
self.generate_input_data.__doc__ += INPUT_GENERATION_TASK_WITH_FEEDBACK_SUFFIX
@@ -186,27 +186,28 @@ class Synthesizer:
feedback = ""
for idx in trange(0, num_data, batch_size, desc="Generating Synthetic Data"):
- iter_temperature = 0.7+0.01*idx
+ iter_temperature = 0.7 + 0.01 * idx
iter_seed = random.randint(0, 1000000)
kwargs = {
"task_description": task_description,
"knowledge_seed": iter_seed,
- "config": dict(temperature=iter_temperature, n=batch_size),
+ "config": {"temperature": iter_temperature, "n": batch_size},
}
if self.config.num_example_for_optim:
if not isinstance(ground_source, list):
- raise ValueError("Ground source must be a list of examples when `num_example_for_optim` is provided.")
+ raise ValueError(
+ "Ground source must be a list of examples when `num_example_for_optim` is provided."
+ )
kwargs["ground_source"] = random.sample(ground_source, k=self.config.num_example_for_optim)
-
+
with dspy.context(lm=self.input_lm):
inputs = self.input_predictor(**kwargs)
- input_kwargs = [{
- key: getattr(completions, key)
- for key in input_keys
- } for completions in inputs.completions]
+ input_kwargs = [
+ {key: getattr(completions, key) for key in input_keys} for completions in inputs.completions
+ ]
for kwargs in input_kwargs:
outputs = None
@@ -216,18 +217,15 @@ class Synthesizer:
outputs = self.config.output_teacher_module(**kwargs)
else:
- outputs = self.output_predictor(**kwargs, config=dict(temperature=iter_temperature))
+ outputs = self.output_predictor(**kwargs, config={"temperature": iter_temperature})
- output_kwargs = {
- key: getattr(outputs, key)
- for key in output_keys
- }
+ output_kwargs = {key: getattr(outputs, key) for key in output_keys}
data.append(dspy.Example(**kwargs, **output_kwargs).with_inputs(*input_keys))
-
+
if self.config.feedback_mode and idx < self.config.num_example_for_feedback:
feedback = self._gather_feedback(data[-1])
-
+
task_description = self.update_task_description(
task_description=task_description,
feedback=feedback,
@@ -237,7 +235,7 @@ class Synthesizer:
return data
- def export(self, data: List[dspy.Example], path: str, mode: str = None, **kwargs):
+ def export(self, data: List[dspy.Example], path: str, mode: Optional[str] = None, **kwargs):
extension = mode or path.split(".")[-1]
from datasets import Dataset
diff --git a/dspy/experimental/synthesizer/utils.py b/dspy/experimental/synthesizer/utils.py
index f08b142e1..8e92fc097 100644
--- a/dspy/experimental/synthesizer/utils.py
+++ b/dspy/experimental/synthesizer/utils.py
@@ -21,4 +21,4 @@ def format_examples(examples: List[dspy.Example]) -> str:
for key in label_keys:
formatted_example += f"{key}: {example[key]}\n"
- return formatted_example
\ No newline at end of file
+ return formatted_example
diff --git a/dspy/experimental/synthetic_data.py b/dspy/experimental/synthetic_data.py
index 9f965bcba..197dcc4e4 100644
--- a/dspy/experimental/synthetic_data.py
+++ b/dspy/experimental/synthetic_data.py
@@ -45,15 +45,19 @@ class SyntheticDataGenerator:
Returns:
dict: dictionary of fields to generate
- """ # noqa: D205
+ """
if self.schema_class:
data_schema = self.schema_class.model_json_schema()
- properties = data_schema['properties']
+ properties = data_schema["properties"]
elif self.examples:
- inferred_schema = self.examples[0].__dict__['_store']
+ inferred_schema = self.examples[0].__dict__["_store"]
descriptor = dspy.Predict(DescriptionSignature)
- properties = {field: {'description': str((descriptor(field_name=field, example=str(inferred_schema[field]))).description)}
- for field in inferred_schema.keys()}
+ properties = {
+ field: {
+ "description": str((descriptor(field_name=field, example=str(inferred_schema[field]))).description)
+ }
+ for field in inferred_schema.keys()
+ }
else:
properties = {}
return properties
@@ -75,18 +79,23 @@ class SyntheticDataGenerator:
generator = dspy.Predict(signature_class, n=additional_samples_needed)
response = generator(sindex=str(random.randint(1, additional_samples_needed)))
- return [dspy.Example({field_name: getattr(completion, field_name) for field_name in properties.keys()})
- for completion in response.completions]
+ return [
+ dspy.Example({field_name: getattr(completion, field_name) for field_name in properties.keys()})
+ for completion in response.completions
+ ]
def _prepare_fields(self, properties) -> dict:
"""Prepare fields to generate in an appropriate format."""
return {
- '__doc__': f"Generates the following outputs: {{{', '.join(properties.keys())}}}.",
- 'sindex': dspy.InputField(desc="a random string"),
- **{field_name: dspy.OutputField(desc=properties[field_name].get('description', 'No description'))
- for field_name in properties.keys()},
+ "__doc__": f"Generates the following outputs: {{{', '.join(properties.keys())}}}.",
+ "sindex": dspy.InputField(desc="a random string"),
+ **{
+ field_name: dspy.OutputField(desc=properties[field_name].get("description", "No description"))
+ for field_name in properties.keys()
+ },
}
+
# # Usage example
# # Generating synthetic data via a pydantic model
# generator = SyntheticDataGenerator(schema_class=SyntheticFacts)