diff --git a/dspy/adapters/chat_adapter.py b/dspy/adapters/chat_adapter.py index 8d92fa34b6..7cd7c9e818 100644 --- a/dspy/adapters/chat_adapter.py +++ b/dspy/adapters/chat_adapter.py @@ -15,6 +15,7 @@ ) from dspy.clients.lm import LM from dspy.signatures.signature import Signature +from dspy.utils.callback import BaseCallback from dspy.utils.exceptions import AdapterParseError field_header_pattern = re.compile(r"\[\[ ## (\w+) ## \]\]") @@ -26,13 +27,33 @@ class FieldInfoWithName(NamedTuple): class ChatAdapter(Adapter): + """Default Adapter for most language models. + + The ChatAdapter formats DSPy signatures into a format compatible with most language models. + It uses delimiter patterns like `[[ ## field_name ## ]]` to clearly separate input and output fields in + the message content. + + Key features: + - Structures inputs and outputs using field header markers for clear field delineation. + - Provides automatic fallback to JSONAdapter if the chat format fails. + """ + def __init__( self, - callbacks=None, + callbacks: list[BaseCallback] | None = None, use_native_function_calling: bool = False, - native_response_types=None, + native_response_types: list[type[type]] | None = None, use_json_adapter_fallback: bool = True, ): + """ + Args: + callbacks: List of callback functions to execute during adapter methods. + use_native_function_calling: Whether to enable native function calling capabilities. + native_response_types: List of output field types handled by native LM features. + use_json_adapter_fallback: Whether to automatically fallback to JSONAdapter if the ChatAdapter fails. + If True, when an error occurs (except ContextWindowExceededError), the adapter will retry using + JSONAdapter. Defaults to True. + """ super().__init__( callbacks=callbacks, use_native_function_calling=use_native_function_calling,