Skip to content

dspy.Adapter

dspy.Adapter(callbacks=None)

Bases: ABC

Source code in dspy/adapters/base.py
def __init__(self, callbacks=None):
    self.callbacks = callbacks or []

Functions

__call__(lm, lm_kwargs, signature, demos, inputs)

Source code in dspy/adapters/base.py
def __call__(self, lm, lm_kwargs, signature, demos, inputs):
    inputs_ = self.format(signature, demos, inputs)
    inputs_ = dict(prompt=inputs_) if isinstance(inputs_, str) else dict(messages=inputs_)

    outputs = lm(**inputs_, **lm_kwargs)
    values = []

    try:
        for output in outputs:
            output_logprobs = None

            if isinstance(output, dict):
                output, output_logprobs = output["text"], output["logprobs"]

            value = self.parse(signature, output)

            assert set(value.keys()) == set(signature.output_fields.keys()), \
                f"Expected {signature.output_fields.keys()} but got {value.keys()}"

            if output_logprobs is not None:
                value["logprobs"] = output_logprobs

            values.append(value)

        return values

    except Exception as e:
        from .json_adapter import JSONAdapter
        if not isinstance(self, JSONAdapter):
            return JSONAdapter()(lm, lm_kwargs, signature, demos, inputs)
        raise e

format(signature, demos, inputs) abstractmethod

Source code in dspy/adapters/base.py
@abstractmethod
def format(self, signature, demos, inputs):
   raise NotImplementedError

format_finetune_data(signature, demos, inputs, outputs)

Source code in dspy/adapters/base.py
def format_finetune_data(self, signature, demos, inputs, outputs):
    raise NotImplementedError

parse(signature, completion) abstractmethod

Source code in dspy/adapters/base.py
@abstractmethod
def parse(self, signature, completion):
   raise NotImplementedError