Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

add quote rule (#8383)

TomuHirata2787453581a3
diff --git a/dspy/adapters/utils.py b/dspy/adapters/utils.py
index 138ba7729..64c98cc68 100644
--- a/dspy/adapters/utils.py
+++ b/dspy/adapters/utils.py
@@ -90,7 +90,7 @@ def translate_field_type(field_name, field_info):
elif field_type in (int, float):
desc = f"must be a single {field_type.__name__} value"
elif inspect.isclass(field_type) and issubclass(field_type, enum.Enum):
- enum_vals = '; '.join(str(member.value) for member in field_type)
+ enum_vals = "; ".join(str(member.value) for member in field_type)
desc = f"must be one of: {enum_vals}"
elif hasattr(field_type, "__origin__") and field_type.__origin__ is Literal:
desc = (
diff --git a/dspy/primitives/python_interpreter.py b/dspy/primitives/python_interpreter.py
index 1d56eb564..d9c960d86 100644
--- a/dspy/primitives/python_interpreter.py
+++ b/dspy/primitives/python_interpreter.py
@@ -56,7 +56,7 @@ class PythonInterpreter:
if deno_command:
self.deno_command = list(deno_command)
else:
- args = ['deno', 'run', '--allow-read']
+ args = ["deno", "run", "--allow-read"]
self._env_arg = ""
if self.enable_env_vars:
user_vars = [str(v).strip() for v in self.enable_env_vars]
diff --git a/dspy/propose/dataset_summary_generator.py b/dspy/propose/dataset_summary_generator.py
index 901f3d4a9..6b2d6f7a5 100644
--- a/dspy/propose/dataset_summary_generator.py
+++ b/dspy/propose/dataset_summary_generator.py
@@ -36,7 +36,7 @@ def order_input_keys_in_string(unordered_repr):
# Extracting the keys from the match
keys_str = match.group(1)
# Splitting the keys, stripping extra spaces, and sorting them
- keys = sorted(key.strip() for key in keys_str.split(','))
+ keys = sorted(key.strip() for key in keys_str.split(","))
# Formatting the sorted keys back into the expected structure
return f"input_keys={{{', '.join(keys)}}}"
diff --git a/dspy/propose/grounded_proposer.py b/dspy/propose/grounded_proposer.py
index c7c764296..cfb434e13 100644
--- a/dspy/propose/grounded_proposer.py
+++ b/dspy/propose/grounded_proposer.py
@@ -224,7 +224,7 @@ class GenerateModuleInstruction(dspy.Module):
outputs = []
for field_name, field in get_signature(program.predictors()[pred_i]).fields.items():
# Access the '__dspy_field_type' from the extra metadata
- dspy_field_type = field.json_schema_extra.get('__dspy_field_type')
+ dspy_field_type = field.json_schema_extra.get("__dspy_field_type")
# Based on the '__dspy_field_type', append to the respective list
if dspy_field_type == "input":
diff --git a/dspy/propose/utils.py b/dspy/propose/utils.py
index 51f3e13f7..8fa70e3e6 100644
--- a/dspy/propose/utils.py
+++ b/dspy/propose/utils.py
@@ -15,9 +15,9 @@ from dspy.teleprompt.utils import get_signature, new_getfile
def strip_prefix(text):
- pattern = r'^[\*\s]*(([\w\'\-]+\s+){0,4}[\w\'\-]+):\s*'
- modified_text = re.sub(pattern, '', text)
- return modified_text.strip("\"")
+ pattern = r"^[\*\s]*(([\w\'\-]+\s+){0,4}[\w\'\-]+):\s*"
+ modified_text = re.sub(pattern, "", text)
+ return modified_text.strip('"')
def create_instruction_set_history_string(base_program, trial_logs, top_n):
program_history = []
@@ -42,7 +42,7 @@ def create_instruction_set_history_string(base_program, trial_logs, top_n):
unique_program_history.append(entry)
# Get the top n programs from program history
- top_n_program_history = sorted(unique_program_history, key=lambda x: x['score'], reverse=True)[:top_n]
+ top_n_program_history = sorted(unique_program_history, key=lambda x: x["score"], reverse=True)[:top_n]
top_n_program_history.reverse()
# Create formatted string
@@ -71,7 +71,7 @@ def get_program_instruction_set_string(program):
instruction_list = []
for _, pred in enumerate(program.predictors()):
pred_instructions = get_signature(pred).instructions
- instruction_list.append(f"\"{pred_instructions}\"")
+ instruction_list.append(f'"{pred_instructions}"')
# Joining the list into a single string that looks like a list
return f"[{', '.join(instruction_list)}]"
@@ -97,15 +97,15 @@ def create_predictor_level_history_string(base_program, predictor_i, trial_logs,
score = history_item["score"]
if instruction in instruction_aggregate:
- instruction_aggregate[instruction]['total_score'] += score
- instruction_aggregate[instruction]['count'] += 1
+ instruction_aggregate[instruction]["total_score"] += score
+ instruction_aggregate[instruction]["count"] += 1
else:
- instruction_aggregate[instruction] = {'total_score': score, 'count': 1}
+ instruction_aggregate[instruction] = {"total_score": score, "count": 1}
# Calculate average score for each instruction and prepare for sorting
predictor_history = []
for instruction, data in instruction_aggregate.items():
- average_score = data['total_score'] / data['count']
+ average_score = data["total_score"] / data["count"]
predictor_history.append((instruction, average_score))
# Deduplicate and sort by average score, then select top N
@@ -141,7 +141,7 @@ def create_example_string(fields, example):
output.append(field_str)
# Joining all the field strings
- return '\n'.join(output)
+ return "\n".join(output)
def get_dspy_source_code(module):
header = []
@@ -169,13 +169,13 @@ def get_dspy_source_code(module):
if item in completed_set:
continue
if isinstance(item, Parameter):
- if hasattr(item, 'signature') and item.signature is not None and item.signature.__pydantic_parent_namespace__['signature_name'] + "_sig" not in completed_set:
+ if hasattr(item, "signature") and item.signature is not None and item.signature.__pydantic_parent_namespace__["signature_name"] + "_sig" not in completed_set:
try:
header.append(inspect.getsource(item.signature))
print(inspect.getsource(item.signature))
except (TypeError, OSError):
header.append(str(item.signature))
- completed_set.add(item.signature.__pydantic_parent_namespace__['signature_name'] + "_sig")
+ completed_set.add(item.signature.__pydantic_parent_namespace__["signature_name"] + "_sig")
if isinstance(item, dspy.Module):
code = get_dspy_source_code(item).strip()
if code not in completed_set:
@@ -183,4 +183,4 @@ def get_dspy_source_code(module):
completed_set.add(code)
completed_set.add(item)
- return '\n\n'.join(header) + '\n\n' + base_code
+ return "\n\n".join(header) + "\n\n" + base_code
diff --git a/dspy/teleprompt/avatar_optimizer.py b/dspy/teleprompt/avatar_optimizer.py
index 5d0490941..164db5b67 100644
--- a/dspy/teleprompt/avatar_optimizer.py
+++ b/dspy/teleprompt/avatar_optimizer.py
@@ -187,7 +187,7 @@ class AvatarOptimizer(Teleprompter):
best_score = -999 if self.optimize_for == "max" else 999
for i in range(self.max_iters):
- print(20*'=')
+ print(20*"=")
print(f"Iteration {i+1}/{self.max_iters}")
score, pos_inputs, neg_inputs = self._get_pos_neg_results(best_actor, trainset)
diff --git a/dspy/teleprompt/grpo.py b/dspy/teleprompt/grpo.py
index fa7cabfdd..68f3d0f49 100644
--- a/dspy/teleprompt/grpo.py
+++ b/dspy/teleprompt/grpo.py
@@ -40,8 +40,8 @@ class GRPO(FinetuneTeleprompter):
report_train_scores: bool = False,
failure_score: float = 0,
format_failure_score: float = -1,
- variably_invoked_predictor_grouping_mode: Union[Literal['truncate'], Literal['fill'], Literal['ragged']] = 'truncate',
- variably_invoked_predictor_fill_strategy: Optional[Union[Literal['randint'], Literal['max']]] = None,
+ variably_invoked_predictor_grouping_mode: Union[Literal["truncate"], Literal["fill"], Literal["ragged"]] = "truncate",
+ variably_invoked_predictor_fill_strategy: Optional[Union[Literal["randint"], Literal["max"]]] = None,
):
super().__init__(train_kwargs=train_kwargs)
self.metric = metric
@@ -70,9 +70,9 @@ class GRPO(FinetuneTeleprompter):
# The backend will be called with a batch of (num_dspy_examples_per_grpo_step * num_rollouts_per_grpo_step * num_predictors) per training set if multitask is True
# If multitask is False, the backend will be called with a batch of (num_dspy_examples_per_grpo_step * num_rollouts_per_grpo_step) per training job
self.variably_invoked_predictor_grouping_mode = variably_invoked_predictor_grouping_mode
- if variably_invoked_predictor_grouping_mode == 'fill':
+ if variably_invoked_predictor_grouping_mode == "fill":
assert variably_invoked_predictor_fill_strategy is not None, "variably_invoked_predictor_fill_strategy must be set when variably_invoked_predictor_grouping_mode is 'fill'"
- assert variably_invoked_predictor_fill_strategy in ['randint', 'max'], "variably_invoked_predictor_fill_strategy must be either 'randint' or 'max'"
+ assert variably_invoked_predictor_fill_strategy in ["randint", "max"], "variably_invoked_predictor_fill_strategy must be either 'randint' or 'max'"
self.variably_invoked_predictor_fill_strategy = variably_invoked_predictor_fill_strategy
self.shuffled_trainset_ids = []
@@ -374,7 +374,7 @@ class GRPO(FinetuneTeleprompter):
format_failure_score=self.format_failure_score,
)
for data_dict in round_data:
- example_ind_in_subsample = data_dict['example_ind'] % len(subsample_training_dataset)
+ example_ind_in_subsample = data_dict["example_ind"] % len(subsample_training_dataset)
data_dict["example_ind"] = example_ind_in_subsample
trace_data[example_ind_in_subsample][tind].append(data_dict)
@@ -425,10 +425,10 @@ class GRPO(FinetuneTeleprompter):
logger.warning(f"Skipping example {example_ind} for predictor {pred_id} as it has no invocations.")
continue
- if self.variably_invoked_predictor_grouping_mode == 'truncate':
+ if self.variably_invoked_predictor_grouping_mode == "truncate":
predictor_example_invocations = [invocation[:min_len] for invocation in predictor_example_invocations]
- elif self.variably_invoked_predictor_grouping_mode == 'fill':
- if self.variably_invoked_predictor_fill_strategy == 'randint':
+ elif self.variably_invoked_predictor_grouping_mode == "fill":
+ if self.variably_invoked_predictor_fill_strategy == "randint":
selector = lambda l: self.rng.choice(l) # noqa: E731, E741
else:
selector = lambda l: l[-1] # noqa: E731, E741
@@ -437,7 +437,7 @@ class GRPO(FinetuneTeleprompter):
for invocation in predictor_example_invocations
]
else:
- assert self.variably_invoked_predictor_grouping_mode == 'ragged', f"Unknown variably invoked predictor grouping mode {self.variably_invoked_predictor_grouping_mode}"
+ assert self.variably_invoked_predictor_grouping_mode == "ragged", f"Unknown variably invoked predictor grouping mode {self.variably_invoked_predictor_grouping_mode}"
max_len = max([len(predictor_example_invocations[i]) for i in range(len(predictor_example_invocations))])
example_training_data: List[GRPOGroup] = [[] for _ in range(max_len)]
@@ -481,7 +481,7 @@ class GRPO(FinetuneTeleprompter):
inputs=trace_instance[1],
outputs=trace_instance[2],
demos=[] # TODO: Add support for demos
- )['messages']
+ )["messages"]
assert all_messages[:-1] == inp_messages, f"Input messages {inp_messages} do not match the expected messages {all_messages[:-1]}"
diff --git a/pyproject.toml b/pyproject.toml
index 4139986d7..c7d3eb892 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -137,6 +137,7 @@ select = [
"UP", # pyupgrade
"N", # pep8-naming
"RUF", # ruff-specific rules
+ "Q", # flake8-quotes
]
ignore = [
diff --git a/tests/predict/test_code_act.py b/tests/predict/test_code_act.py
index 3bebfd69b..39f958af0 100644
--- a/tests/predict/test_code_act.py
+++ b/tests/predict/test_code_act.py
@@ -39,8 +39,8 @@ def test_codeact_code_generation():
res = program(question="What is 1+1?")
assert res.answer == "2"
assert res.trajectory == {
- 'code_output_0': '"2\\n"',
- 'generated_code_0': 'result = add(1,1)\nprint(result)',
+ "code_output_0": '"2\\n"',
+ "generated_code_0": "result = add(1,1)\nprint(result)",
}
assert program.interpreter.deno_process is None
@@ -72,8 +72,8 @@ def test_codeact_support_multiple_fields():
assert res.maximum == "6"
assert res.minimum == "2"
assert res.trajectory == {
- 'code_output_0': '"{\'maximum\': 6.0, \'minimum\': 2.0}\\n"',
- 'generated_code_0': "result = extract_maximum_minimum('2, 3, 5, 6')\nprint(result)",
+ "code_output_0": '"{\'maximum\': 6.0, \'minimum\': 2.0}\\n"',
+ "generated_code_0": "result = extract_maximum_minimum('2, 3, 5, 6')\nprint(result)",
}
assert program.interpreter.deno_process is None
@@ -100,10 +100,10 @@ def test_codeact_code_parse_failure():
res = program(question="What is 1+1?")
assert res.answer == "2"
assert res.trajectory == {
- 'generated_code_0': 'parse(error',
- 'observation_0': 'Failed to execute the generated code: Invalid Python syntax. message: ',
- 'generated_code_1': 'result = add(1,1)\nprint(result)',
- 'code_output_1': '"2\\n"',
+ "generated_code_0": "parse(error",
+ "observation_0": "Failed to execute the generated code: Invalid Python syntax. message: ",
+ "generated_code_1": "result = add(1,1)\nprint(result)",
+ "code_output_1": '"2\\n"',
}
assert program.interpreter.deno_process is None
@@ -130,10 +130,10 @@ def test_codeact_code_execution_failure():
res = program(question="What is 1+1?")
assert res.answer == "2"
assert res.trajectory == {
- 'generated_code_0': 'unknown+1',
- 'observation_0': 'Failed to execute the generated code: NameError: ["name \'unknown\' is not defined"]',
- 'generated_code_1': 'result = add(1,1)\nprint(result)',
- 'code_output_1': '"2\\n"',
+ "generated_code_0": "unknown+1",
+ "observation_0": 'Failed to execute the generated code: NameError: ["name \'unknown\' is not defined"]',
+ "generated_code_1": "result = add(1,1)\nprint(result)",
+ "code_output_1": '"2\\n"',
}
assert program.interpreter.deno_process is None