Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions dspy/clients/databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ def deploy_finetuned_model(
databricks_token: str | None = None,
deploy_timeout: int = 900,
):
"""
Args:
model: Name of the model as stored in the databricks store

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is databricks store?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that databricks provides a store for ML models and dspy client for databricks provides functions to manage/use them through dspy's paradigm. The store is question is the databricks feature store https://docs.databricks.com/aws/en/machine-learning/feature-store/. Is my understanding correct?

data_format: One of TrainDataFormat.CHAT or TrainDataFormat.COMPLETION based on what the model's supposed to do
databricks_host: URL of databricks host.
databricks_token: Databrick token
deploy_timeout: Time in seconds. Deployment is cancelled if this time period exceeds. (Default: 900s)
"""
workspace_client = _get_workspace_client()
model_version = next(workspace_client.model_versions.list(model)).version

Expand Down Expand Up @@ -172,6 +180,22 @@ def finetune(
train_data_format: TrainDataFormat | str | None = "chat",
train_kwargs: dict[str, Any] | None = None,
) -> str:
"""
Args:
job (TrainingJobDatabricks): Training job to be finetuned
model: Model name
train_data (list[dict[str, Any]]): Training data
train_data_format: One of "chat" or "completion" (Default: "chat")
train_kwargs: Can take following options:
1. train_data_path: path to training data in Databricks provider
2. register_to: Required for finetune on Databricks
3. databricks_host (Optional): Databricks host URL
4. databricks_token (Optional): Databricks Token
5. skip_deploy (Optional): Skip deploying the model. (Default: False)
6. deploy_timeout (Optional): Time in seconds. (Default: 900s)
Returns:
str: Path to the model in the format "databricks/{job.endpoint_name}"
"""
if isinstance(train_data_format, str):
if train_data_format == "chat":
train_data_format = TrainDataFormat.CHAT
Expand Down
19 changes: 15 additions & 4 deletions dspy/predict/avatar/avatar.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@ def get_number_with_suffix(number: int) -> str:


class Avatar(dspy.Module):
"""
Module based on the Avatar Optimizer

This modules implements the Avatar optimizer (https://arxiv.org/pdf/2406.11200) that allows effective use of tools using contrastive reasoning with batch-wise sampling. See paper for details.

Args:
signature (Type[dspy.Signature]): The signature of the module.
tools (list[dspy.Tool]): a list of dspy tools to be run
max_iters (Int): The maximum number of iterations to retry code generation and execution.
verbose (bool): Toggle verbosity
"""
def __init__(
Comment thread
banana-bison marked this conversation as resolved.
self,
signature,
tools,
max_iters=3,
verbose=False,
signature: Type[dspy.Signature],
tools: list[dspy.Tool],
max_iters: int = 3,
verbose: bool = False,
):
self.signature = ensure_signature(signature)
self.input_fields = self.signature.input_fields
Expand Down
55 changes: 51 additions & 4 deletions dspy/teleprompt/simba_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,22 @@ def prepare_models_for_resampling(program: dspy.Module, n: int, teacher_settings
return models

def wrap_program(program: dspy.Module, metric: Callable):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also add type hint?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function (wrap_program), already has type hints. Did you mean the function below it: wrapped_program?

def wrapped_program(example):
"""
Wraps a program into a function that returns a dictionary of various metrics

Args:
program (dspy.Module): dspy.Module that contains instructions to run the LM.
metric (Callable[str,str]): A function that takes examples from your data and output of the LM and compares them
Returns:
A functions that take a dspy.Example as argument and returns a dict with the following keys when called: {
"prediction",
"trace",
"score",
"example",
"output_metadata",
}
"""
def wrapped_program(example: dspy.Example):
with dspy.context(trace=[]):
prediction, trace, score = None, None, 0.0
try:
Expand Down Expand Up @@ -70,7 +85,22 @@ def wrapped_program(example):

return wrapped_program

def append_a_demo(demo_input_field_maxlen):
def append_a_demo(demo_input_field_maxlen: int):
"""
One of the strategies (chosen at random) of the SIMBA optimizer. The other one is append_a_rule
Args:
demo_input_field_maxlen: Max length of characters in the input field
Returns:
A function of the following form:
Args:
bucket: A list of dictionaries with atleast a "score" key
system: A program selected from a list of programs on the basis of softmax sampling of top k baseline candidates
predictor2name: A dict that maps predictors to their names
name2predictor: A dict that maps names to their predictors
batch_10p_score: 10th percentile score
Returns:
bool: True if demo successful, False if bucket[0] score is less than 10th percentile
"""
def append_a_demo_(bucket, system, **kwargs):
predictor2name, name2predictor = kwargs["predictor2name"], kwargs["name2predictor"]
batch_10p_score = kwargs["batch_10p_score"]
Expand Down Expand Up @@ -103,7 +133,18 @@ def append_a_demo_(bucket, system, **kwargs):
return append_a_demo_


def append_a_rule(bucket, system, **kwargs):
def append_a_rule(bucket: list[dict], system: dspy.Module, **kwargs):
"""
One of the strategies (chosen at random) of the SIMBA optimizer. The other one is append_a_demo
Args:
bucket: A list of dictionaries with atleast a "score" key
system: A program selected from a list of programs on the basis of softmax sampling of top k baseline candidates
predictor2name: A dict that maps predictors to their names
name2predictor: A dict that maps names to their predictors
batch_10p_score: 10th percentile score
Returns:
bool: True if demo successful, False if bucket[0] score is less than 10th percentile or greater than 90th percentile
"""
predictor2name = kwargs["predictor2name"]
batch_10p_score, batch_90p_score = kwargs["batch_10p_score"], kwargs["batch_90p_score"]
prompt_model = kwargs["prompt_model"] or dspy.settings.lm
Expand Down Expand Up @@ -207,7 +248,13 @@ class OfferFeedback(dspy.Signature):
"like the successful trajectory rather than the lower-scoring trajectory."
)

def inspect_modules(program):
def inspect_modules(program: dspy.Module):
"""
Args:
program (dspy.Module): A dspy.Module
Returns:
str: A listing of input_fields and output_fields from program.named_predictors
"""
separator = "-" * 80
output = [separator]

Expand Down