|
20 | 20 |
|
21 | 21 |
|
22 | 22 | class Adapter: |
| 23 | + """Base Adapter class. |
| 24 | +
|
| 25 | + The Adapter serves as the interface layer between DSPy module/signature and Language Models (LMs). It handles the |
| 26 | + complete transformation pipeline from DSPy inputs to LM calls and back to structured outputs. |
| 27 | +
|
| 28 | + Key responsibilities: |
| 29 | + - Transform user inputs and signatures into properly formatted LM prompts, which also instructs the LM to format |
| 30 | + the response in a specific format. |
| 31 | + - Parse LM outputs into dictionaries matching the signature's output fields. |
| 32 | + - Enable/disable native LM features (function calling, citations, etc.) based on configuration. |
| 33 | + - Handle conversation history, few-shot examples, and custom type processing. |
| 34 | +
|
| 35 | + The adapter pattern allows DSPy to work with different LM interfaces while maintaining a consistent programming |
| 36 | + model for users. |
| 37 | + """ |
| 38 | + |
23 | 39 | def __init__( |
24 | 40 | self, |
25 | 41 | callbacks: list[BaseCallback] | None = None, |
26 | 42 | use_native_function_calling: bool = False, |
27 | 43 | native_response_types: list[type[Type]] | None = None, |
28 | 44 | ): |
| 45 | + """ |
| 46 | + Args: |
| 47 | + callbacks: List of callback functions to execute during `format()` and `parse()` methods. Callbacks can be |
| 48 | + used for logging, monitoring, or custom processing. Defaults to None (empty list). |
| 49 | + use_native_function_calling: Whether to enable native function calling capabilities when the LM supports it. |
| 50 | + If True, the adapter will automatically configure function calling when input fields contain `dspy.Tool` |
| 51 | + or `list[dspy.Tool]` types. Defaults to False. |
| 52 | + native_response_types: List of output field types that should be handled by native LM features rather than |
| 53 | + adapter parsing. For example, `dspy.Citations` can be populated directly by citation APIs |
| 54 | + (e.g., Anthropic's citation feature). Defaults to `[Citations]`. |
| 55 | + """ |
29 | 56 | self.callbacks = callbacks or [] |
30 | 57 | self.use_native_function_calling = use_native_function_calling |
31 | 58 | self.native_response_types = native_response_types or _DEFAULT_NATIVE_RESPONSE_TYPES |
@@ -149,6 +176,22 @@ def __call__( |
149 | 176 | demos: list[dict[str, Any]], |
150 | 177 | inputs: dict[str, Any], |
151 | 178 | ) -> list[dict[str, Any]]: |
| 179 | + """ |
| 180 | + Execute the adapter pipeline: format inputs, call LM, and parse outputs. |
| 181 | +
|
| 182 | + Args: |
| 183 | + lm: The Language Model instance to use for generation. Must be an instance of `dspy.BaseLM`. |
| 184 | + lm_kwargs: Additional keyword arguments to pass to the LM call (e.g., temperature, max_tokens). These are |
| 185 | + passed directly to the LM. |
| 186 | + signature: The DSPy signature associated with this LM call. |
| 187 | + demos: List of few-shot examples to include in the prompt. Each dictionary should contain keys matching the |
| 188 | + signature's input and output field names. Examples are formatted as user/assistant message pairs. |
| 189 | + inputs: The current input values for this call. Keys must match the signature's input field names. |
| 190 | +
|
| 191 | + Returns: |
| 192 | + List of dictionaries representing parsed LM responses. Each dictionary contains keys matching the |
| 193 | + signature's output field names. For multiple generations (n > 1), returns multiple dictionaries. |
| 194 | + """ |
152 | 195 | processed_signature = self._call_preprocess(lm, lm_kwargs, signature, inputs) |
153 | 196 | inputs = self.format(processed_signature, demos, inputs) |
154 | 197 |
|
|
0 commit comments