Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

fix style errors of propose (#8185)

TomuHirataeb39943ff44c
diff --git a/dspy/propose/dataset_summary_generator.py b/dspy/propose/dataset_summary_generator.py
index 77839a87d..901f3d4a9 100644
--- a/dspy/propose/dataset_summary_generator.py
+++ b/dspy/propose/dataset_summary_generator.py
@@ -3,6 +3,7 @@ import re
import dspy
from dspy.propose.utils import strip_prefix
+
class ObservationSummarizer(dspy.Signature):
("""Given a series of observations I have made about my dataset, please summarize them into a brief 2-3 sentence summary which highlights only the most important details.""")
observations = dspy.InputField(desc="Observations I have made about my dataset")
@@ -12,7 +13,7 @@ class DatasetDescriptor(dspy.Signature):
("""Given several examples from a dataset please write observations about trends that hold for most or all of the samples. """
"""Some areas you may consider in your observations: topics, content, syntax, conciceness, etc. """
"""It will be useful to make an educated guess as to the nature of the task this dataset will enable. Don't be afraid to be creative""")
-
+
examples = dspy.InputField(desc="Sample data points from the dataset")
observations = dspy.OutputField(desc="Somethings that holds true for most or all of the data you observed")
@@ -21,7 +22,7 @@ class DatasetDescriptorWithPriorObservations(dspy.Signature):
"""I will also provide you with a few observations I have already made. Please add your own observations or if you feel the observations are comprehensive say 'COMPLETE' """
"""Some areas you may consider in your observations: topics, content, syntax, conciceness, etc. """
"""It will be useful to make an educated guess as to the nature of the task this dataset will enable. Don't be afraid to be creative""")
-
+
examples = dspy.InputField(desc="Sample data points from the dataset")
prior_observations = dspy.InputField(desc="Some prior observations I made about the data")
observations = dspy.OutputField(desc="Somethings that holds true for most or all of the data you observed or COMPLETE if you have nothing to add")
@@ -75,8 +76,8 @@ def create_dataset_summary(trainset, view_data_batch_size, prompt_model, log_fil
break
continue
observations += output["observations"]
-
- if log_file:
+
+ if log_file:
log_file.write(f"observations {observations}\n")
except Exception as e:
if verbose:
@@ -91,7 +92,7 @@ def create_dataset_summary(trainset, view_data_batch_size, prompt_model, log_fil
print(f"summary: {summary}")
if log_file:
log_file.write(f"summary: {summary}\n")
-
+
if verbose:
print(f"\nGenerated summary: {strip_prefix(summary.summary)}\n")
diff --git a/dspy/propose/grounded_proposer.py b/dspy/propose/grounded_proposer.py
index 59ef35b14..c7c764296 100644
--- a/dspy/propose/grounded_proposer.py
+++ b/dspy/propose/grounded_proposer.py
@@ -2,9 +2,14 @@ import random
import dspy
from dspy.propose.dataset_summary_generator import create_dataset_summary
-from dspy.propose.utils import create_example_string, create_predictor_level_history_string, strip_prefix, get_dspy_source_code
-from dspy.teleprompt.utils import get_signature, get_prompt_model
from dspy.propose.propose_base import Proposer
+from dspy.propose.utils import (
+ create_example_string,
+ create_predictor_level_history_string,
+ get_dspy_source_code,
+ strip_prefix,
+)
+from dspy.teleprompt.utils import get_prompt_model, get_signature
# Hardcoded variables (TODO: update)
MAX_INSTRUCT_IN_HISTORY = 5 # 10
@@ -184,7 +189,7 @@ class GenerateModuleInstruction(dspy.Module):
# Construct full program demo or single module demo depending on settings
basic_instruction = get_signature(program.predictors()[pred_i]).instructions
task_demos = ""
-
+
if self.use_task_demos:
# Combine current and adjacent sets
adjacent_sets = (
@@ -192,7 +197,7 @@ class GenerateModuleInstruction(dspy.Module):
demo_candidates[pred_i][demo_set_i + 1:] +
demo_candidates[pred_i][:demo_set_i]
)
-
+
# Gather examples up to the required count
example_strings = gather_examples_from_sets(adjacent_sets, num_demos_in_context)
task_demos = "\n\n".join(example_strings) + "\n\n"
@@ -220,7 +225,7 @@ class GenerateModuleInstruction(dspy.Module):
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')
-
+
# Based on the '__dspy_field_type', append to the respective list
if dspy_field_type == "input":
inputs.append(field_name)
@@ -236,9 +241,9 @@ class GenerateModuleInstruction(dspy.Module):
module=module_code,
max_depth=10,
).module_description
- except:
+ except Exception as e:
if self.verbose:
- print("Error getting program description. Running without program aware proposer.")
+ print(f"Error getting program description. Running without program aware proposer. Error: {e}")
self.program_aware = False
# Generate an instruction for our chosen module
@@ -324,12 +329,12 @@ class GroundedProposer(Proposer):
program,
demo_candidates,
trial_logs,
- N,
- T,
+ N, # noqa: N803
+ T, # noqa: N803
) -> list[str]:
"""This method is responsible for returning the full set of new instructions for our program, given the specified criteria."""
- proposed_instructions = {}
+ proposed_instructions = {}
if self.set_history_randomly:
# Randomly select whether or not we're using instruction history
@@ -347,7 +352,7 @@ class GroundedProposer(Proposer):
else:
num_demos = max(len(demo_candidates[0]), 1)
- # Create an instruction for each predictor
+ # Create an instruction for each predictor
for pred_i, predictor in enumerate(program.predictors()):
for demo_set_i in range(num_demos)[:min(N, num_demos)]:
if pred_i not in proposed_instructions:
@@ -377,7 +382,7 @@ class GroundedProposer(Proposer):
tip=selected_tip,
),
)
-
+
return proposed_instructions
def propose_instruction_for_predictor(
@@ -385,7 +390,7 @@ class GroundedProposer(Proposer):
program,
predictor,
pred_i,
- T,
+ T, # noqa: N803
demo_candidates,
demo_set_i,
trial_logs,
diff --git a/dspy/propose/instruction_proposal.py b/dspy/propose/instruction_proposal.py
deleted file mode 100644
index 2967427ca..000000000
--- a/dspy/propose/instruction_proposal.py
+++ /dev/null
@@ -1,126 +0,0 @@
-import dspy
-from dspy.signatures import Signature
-
-
-class BasicGenerateInstruction(dspy.Signature):
- ("""You are an instruction optimizer for large language models. I will give you a ``signature`` of fields (inputs and outputs) in English. Specifically, I will provide you with one or more ``example instruction(s)`` that were previously attempted for this task.
-
-Your task is to propose a new improved instruction and prefix for the output field that will lead a good language model to perform the task well. Don't be afraid to be creative.""")
- example_instructions = dspy.InputField(desc="Example instruction(s) for this task.")
- proposed_instruction = dspy.InputField(desc="The improved instructions for the language model")
- proposed_prefix_for_output_field = dspy.OutputField(desc="The string at the end of the prompt, which will help the model start solving the task")
-
-class BasicGenerateInstructionWithExamplesAndDataObservationsAndTip(dspy.Signature):
- ("""You are an instruction optimizer for large language models. I will give you a ``signature`` of fields (inputs and outputs) in English. Specifically, I will give you a summary I have made about the dataset, along with some ``examples`` of the expected inputs and outputs for this task. I will also provide you with one or more ``example instruction(s)`` that were previously attempted for this task.
-
-Your task is to propose a new improved instruction and prefix for the output field that will lead a good language model to perform the task well. Don't be afraid to be creative.""")
- dataset_summary = dspy.InputField(desc="Summary of the dataset.")
- examples = dspy.InputField(desc="Example(s) of the task")
- example_instructions = dspy.InputField(desc="Example instruction(s) for this task.")
- tip = dspy.InputField(desc="A tip for something to keep in mind when generating the new instruction.")
- proposed_instruction = dspy.OutputField(desc="The improved instructions for the language model")
- proposed_prefix_for_output_field = dspy.OutputField(desc="The string at the end of the prompt, which will help the model start solving the task")
-
-class BasicGenerateInstructionWithDataObservationsAndTip(dspy.Signature):
- ("""You are an instruction optimizer for large language models. I will give you a ``signature`` of fields (inputs and outputs) in English. Specifically, I will give you a summary I have made about the dataset, along with some ``examples`` of the expected inputs and outputs for this task. I will also provide you with one or more ``example instruction(s)`` that were previously attempted for this task.
-
-Your task is to propose a new improved instruction and prefix for the output field that will lead a good language model to perform the task well. Don't be afraid to be creative.""")
- dataset_summary = dspy.InputField(desc="Summary of the dataset.")
- example_instructions = dspy.InputField(desc="Example instruction(s) for this task.")
- tip = dspy.InputField(desc="A tip for something to keep in mind when generating the new instruction.")
- proposed_instruction = dspy.OutputField(desc="The improved instructions for the language model")
- proposed_prefix_for_output_field = dspy.OutputField(desc="The string at the end of the prompt, which will help the model start solving the task")
-
-class BasicGenerateInstructionWithExamplesAndTip(dspy.Signature):
- ("""You are an instruction optimizer for large language models. I will give you a ``signature`` of fields (inputs and outputs) in English. Specifically, I will give you some ``examples`` of the expected inputs and outputs for this task. I will also provide you with one or more ``example instruction(s)`` that were previously attempted for this task.
-
-Your task is to propose a new improved instruction and prefix for the output field that will lead a good language model to perform the task well. Don't be afraid to be creative.""")
- examples = dspy.InputField(desc="Example(s) of the task")
- example_instructions = dspy.InputField(desc="Example instruction(s) for this task.")
- tip = dspy.InputField(desc="A tip for something to keep in mind when generating the new instruction.")
- proposed_instruction = dspy.OutputField(desc="The improved instructions for the language model")
- proposed_prefix_for_output_field = dspy.OutputField(desc="The string at the end of the prompt, which will help the model start solving the task")
-
-class BasicGenerateInstructionWithTip(dspy.Signature):
- ("""You are an instruction optimizer for large language models. I will give you a ``signature`` of fields (inputs and outputs) in English. Specifically, I will provide you with one or more ``example instruction(s)`` that were previously attempted for this task.
-
-Your task is to propose a new improved instruction and prefix for the output field that will lead a good language model to perform the task well. Don't be afraid to be creative.""")
- example_instructions = dspy.InputField(desc="Example instruction(s) for this task.")
- tip = dspy.InputField(desc="A tip for something to keep in mind when generating the new instruction.")
- proposed_instruction = dspy.OutputField(desc="The improved instructions for the language model")
- proposed_prefix_for_output_field = dspy.OutputField(desc="The string at the end of the prompt, which will help the model start solving the task")
-
-
-class BasicGenerateInstructionWithExamplesAndDataObservations(dspy.Signature):
- ("""You are an instruction optimizer for large language models. I will give you a ``signature`` of fields (inputs and outputs) in English. Specifically, I will give you some ``observations`` I have made about the dataset and task, along with some ``examples`` of the expected inputs and outputs. I will also provide you with the current ``basic instruction`` that is being used for this task.
-
-Your task is to propose a new improved instruction and prefix for the output field that will lead a good language model to perform the task well. Don't be afraid to be creative.""")
- observations = dspy.InputField(desc="Observations about the dataset and task")
- examples = dspy.InputField(desc="Example(s) of the task")
- basic_instruction = dspy.InputField(desc="The initial instructions before optimization")
- proposed_instruction = dspy.OutputField(desc="The improved instructions for the language model")
- proposed_prefix_for_output_field = dspy.OutputField(desc="The string at the end of the prompt, which will help the model start solving the task")
-
-class BasicGenerateInstructionWithExamples(dspy.Signature):
- ("""You are an instruction optimizer for large language models. I will also give you one or more ``examples`` of the expected inputs and outputs.
-
-Your task is to propose an instruction that will lead a good language model to perform the task well. Don't be afraid to be creative.""")
- basic_instruction = dspy.InputField(desc="The initial instructions before optimization")
- examples = dspy.InputField(desc="Example(s) of the task")
- proposed_instruction = dspy.OutputField(desc="The improved instructions for the language model")
- proposed_prefix_for_output_field = dspy.OutputField(desc="The string at the end of the prompt, which will help the model start solving the task")
-
-
-class BasicGenerateInstructionWithDataObservations(Signature):
- """You are an instruction optimizer for large language models. I will give you a ``signature`` of fields (inputs and outputs) in English. I will also give you some ``observations`` I have made about the dataset and task. Your task is to propose an instruction that will lead a good language model to perform the task well. Don't be afraid to be creative."""
-
- basic_instruction = dspy.InputField(desc="The initial instructions before optimization")
- observations = dspy.InputField(desc="Observations about the dataset and task")
- proposed_instruction = dspy.OutputField(desc="The improved instructions for the language model")
- proposed_prefix_for_output_field = dspy.OutputField(desc="The string at the end of the prompt, which will help the model start solving the task")
-
-
-class BasicGenerateInstruction(Signature):
- """You are an instruction optimizer for large language models. I will give you a ``signature`` of fields (inputs and outputs) in English. Your task is to propose an instruction that will lead a good language model to perform the task well. Don't be afraid to be creative."""
-
- basic_instruction = dspy.InputField(desc="The initial instructions before optimization")
- proposed_instruction = dspy.OutputField(desc="The improved instructions for the language model")
- proposed_prefix_for_output_field = dspy.OutputField(desc="The string at the end of the prompt, which will help the model start solving the task")
-
-class BasicGenerateInstructionAllFields(Signature):
- """You are an instruction optimizer for large language models. Your task is to propose an instruction that will lead a good language model to perform the task well. Don't be afraid to be creative."""
- ("""You are an instruction optimizer for large language models. I will provide you with""")
- ("""the current instruction that I am using, along with a template consisting of names""")
- ("""and prefixes for the different input and output fields for the model. Your task is to propose a new instruction""")
- ("""and template for these input and output fields that will help the language model perform the task well. Don't be afraid to be creative.""")
-
- initial_prompt_template = dspy.InputField(desc="The initial prompt template for this task.")
- proposed_prompt_template = dspy.OutputField(desc="The improved prompt template for this task.")
- # proposed_prefix_for_output_field = dspy.OutputField(desc="The string at the end of the prompt, which will help the model start solving the task")
-
-class BasicGenerateInstructionOnly(Signature):
- ("""You are an instruction optimizer for large language models. I will provide you with an instruction I'm currently using. Your task is to propose an instruction that will lead a good language model to perform the task well. Don't be afraid to be creative.""")
-
- current_instruction = dspy.InputField(desc="The current instruction.")
- # examples_of_field_in_use = dspy.InputField(desc="Examples of this field in use on examples from our task.")
- proposed_instruction = dspy.OutputField(desc="The proposed instruction (reply with the instruction only).")
-
-class BasicGenerateField(Signature):
- ("""You are an instruction optimizer for large language models. Your task is to propose a better string to use for one of the fields
- in a prompt that is being inputted to a large language model to perform a certain task. The goal is for this improved field to improve
- the performance of the language model on this task. Don't be afraid to be creative.""")
-
- current_field = dspy.InputField(desc="The current string in use for this field.")
- examples_of_field_in_use = dspy.InputField(desc="Examples of this field in use on examples from our task.")
- proposed_field = dspy.OutputField(desc="The proposed string for the field (respond with the new field string only).")
- # proposed_prefix_for_output_field = dspy.OutputField(desc="The string at the end of the prompt, which will help the model start solving the task")
-
-class GenerateInstructionGivenAttempts(dspy.Signature):
- """You are an instruction optimizer for large language models. I will give some task instructions I've tried, along with the corresponding validation scores, where higher scores indicate better quality. I will also include an example of each instruction in use on a randomly chosen sample from our validation set.
-
-Your task is to propose a new instruction that will lead a good language model to perform the task even better. Don't be afraid to be creative."""
-
- attempted_instructions = dspy.InputField(format=dsp.passages2text)
- # attempted_instructions = dspy.InputField(desc="Previously attempted task instructions, along with their resulting validation score, and an example of the instruction in use on a sample from our dataset.")
- proposed_instruction = dspy.OutputField(desc="The improved instructions for the language model")
- proposed_prefix_for_output_field = dspy.OutputField(desc="The string at the end of the prompt, which will help the model start solving the task")
\ No newline at end of file
diff --git a/dspy/propose/propose_base.py b/dspy/propose/propose_base.py
index db1ec8132..b972dc085 100644
--- a/dspy/propose/propose_base.py
+++ b/dspy/propose/propose_base.py
@@ -10,4 +10,4 @@ class Proposer(ABC):
pass
def propose_instruction_for_predictor(self):
- pass
\ No newline at end of file
+ pass
diff --git a/dspy/propose/utils.py b/dspy/propose/utils.py
index 7d93e3c8c..51f3e13f7 100644
--- a/dspy/propose/utils.py
+++ b/dspy/propose/utils.py
@@ -1,7 +1,9 @@
+import inspect
import json
import re
+
import dspy
-import inspect
+
try:
from IPython.core.magics.code import extract_symbols
except ImportError:
@@ -9,8 +11,8 @@ except ImportError:
extract_symbols = None
from dspy.predict.parameter import Parameter
+from dspy.teleprompt.utils import get_signature, new_getfile
-from dspy.teleprompt.utils import get_signature
def strip_prefix(text):
pattern = r'^[\*\s]*(([\w\'\-]+\s+){0,4}[\w\'\-]+):\s*'
@@ -38,7 +40,7 @@ def create_instruction_set_history_string(base_program, trial_logs, top_n):
if instruction_set not in seen_programs:
seen_programs.add(instruction_set)
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.reverse()
@@ -50,7 +52,7 @@ def create_instruction_set_history_string(base_program, trial_logs, top_n):
score = entry["score"]
instruction_set = get_program_instruction_set_string(program)
instruction_set_history_string += instruction_set + f" | Score: {score}\n\n"
-
+
return instruction_set_history_string
def parse_list_of_instructions(instruction_string):
@@ -60,7 +62,7 @@ def parse_list_of_instructions(instruction_string):
return instructions
except json.JSONDecodeError:
pass
-
+
# If JSON decoding fails, extract strings within quotes
instructions = re.findall(r'"([^"]*)"', instruction_string)
return instructions
@@ -76,7 +78,7 @@ def get_program_instruction_set_string(program):
def create_predictor_level_history_string(base_program, predictor_i, trial_logs, top_n):
instruction_aggregate = {}
instruction_history = []
-
+
# Load trial programs
for trial_num in trial_logs:
trial = trial_logs[trial_num]
@@ -93,19 +95,19 @@ def create_predictor_level_history_string(base_program, predictor_i, trial_logs,
predictor = history_item["program"].predictors()[predictor_i]
instruction = get_signature(predictor).instructions
score = history_item["score"]
-
+
if instruction in instruction_aggregate:
instruction_aggregate[instruction]['total_score'] += score
instruction_aggregate[instruction]['count'] += 1
else:
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']
predictor_history.append((instruction, average_score))
-
+
# Deduplicate and sort by average score, then select top N
seen_instructions = set()
unique_predictor_history = []
@@ -116,12 +118,12 @@ def create_predictor_level_history_string(base_program, predictor_i, trial_logs,
top_instructions = sorted(unique_predictor_history, key=lambda x: x[1], reverse=True)[:top_n]
top_instructions.reverse()
-
+
# Create formatted history string
predictor_history_string = ""
for instruction, score in top_instructions:
predictor_history_string += instruction + f" | Score: {score}\n\n"
-
+
return predictor_history_string
def create_example_string(fields, example):
@@ -180,5 +182,5 @@ def get_dspy_source_code(module):
header.append(code)
completed_set.add(code)
completed_set.add(item)
-
- return '\n\n'.join(header) + '\n\n' + base_code
\ No newline at end of file
+
+ return '\n\n'.join(header) + '\n\n' + base_code