dspy.Adapter
dspy.Adapter(callbacks: Optional[list[BaseCallback]] = None)
Source code in dspy/adapters/base.py
Functions
__call__(lm: LM, lm_kwargs: dict[str, Any], signature: Type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]) -> list[dict[str, Any]]
Source code in dspy/adapters/base.py
format(signature: Type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]) -> list[dict[str, Any]]
Format the input messages for the LM call.
This method converts the DSPy structured input along with few-shot examples and conversation history into multiturn messages as expected by the LM. For custom adapters, this method can be overridden to customize the formatting of the input messages.
In general we recommend the messages to have the following structure:
[
{"role": "system", "content": system_message},
# Begin few-shot examples
{"role": "user", "content": few_shot_example_1_input},
{"role": "assistant", "content": few_shot_example_1_output},
{"role": "user", "content": few_shot_example_2_input},
{"role": "assistant", "content": few_shot_example_2_output},
...
# End few-shot examples
# Begin conversation history
{"role": "user", "content": conversation_history_1_input},
{"role": "assistant", "content": conversation_history_1_output},
{"role": "user", "content": conversation_history_2_input},
{"role": "assistant", "content": conversation_history_2_output},
...
# End conversation history
{"role": "user", "content": current_input},
]
And system message should contain the field description, field structure, and task description.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signature
|
Type[Signature]
|
The DSPy signature for which to format the input messages. |
required |
demos
|
list[dict[str, Any]]
|
A list of few-shot examples. |
required |
inputs
|
dict[str, Any]
|
The input arguments to the DSPy module. |
required |
Returns:
Type | Description |
---|---|
list[dict[str, Any]]
|
A list of multiturn messages as expected by the LM. |
Source code in dspy/adapters/base.py
format_assistant_message_content(signature: Type[Signature], outputs: dict[str, Any], missing_field_message: str = None) -> str
Format the assistant message content.
This method formats the assistant message content, which can be used in formatting few-shot examples, conversation history.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signature
|
Type[Signature]
|
The DSPy signature for which to format the assistant message content. |
required |
outputs
|
dict[str, Any]
|
The output fields to be formatted. |
required |
missing_field_message
|
str
|
A message to be used when a field is missing. |
None
|
Returns:
Type | Description |
---|---|
str
|
A string that contains the assistant message content. |
Source code in dspy/adapters/base.py
format_conversation_history(signature: Type[Signature], history_field_name: str, inputs: dict[str, Any]) -> list[dict[str, Any]]
Format the conversation history.
This method formats the conversation history and the current input as multiturn messages.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signature
|
Type[Signature]
|
The DSPy signature for which to format the conversation history. |
required |
history_field_name
|
str
|
The name of the history field in the signature. |
required |
inputs
|
dict[str, Any]
|
The input arguments to the DSPy module. |
required |
Returns:
Type | Description |
---|---|
list[dict[str, Any]]
|
A list of multiturn messages. |
Source code in dspy/adapters/base.py
format_demos(signature: Type[Signature], demos: list[dict[str, Any]]) -> list[dict[str, Any]]
Format the few-shot examples.
This method formats the few-shot examples as multiturn messages.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signature
|
Type[Signature]
|
The DSPy signature for which to format the few-shot examples. |
required |
demos
|
list[dict[str, Any]]
|
A list of few-shot examples, each element is a dictionary with keys of the input and output fields of the signature. |
required |
Returns:
Type | Description |
---|---|
list[dict[str, Any]]
|
A list of multiturn messages. |
Source code in dspy/adapters/base.py
format_field_description(signature: Type[Signature]) -> str
Format the field description for the system message.
This method formats the field description for the system message. It should return a string that contains the field description for the input fields and the output fields.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signature
|
Type[Signature]
|
The DSPy signature for which to format the field description. |
required |
Returns:
Type | Description |
---|---|
str
|
A string that contains the field description for the input fields and the output fields. |
Source code in dspy/adapters/base.py
format_field_structure(signature: Type[Signature]) -> str
Format the field structure for the system message.
This method formats the field structure for the system message. It should return a string that dictates the format the input fields should be provided to the LM, and the format the output fields will be in the response. Refer to the ChatAdapter and JsonAdapter for an example.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signature
|
Type[Signature]
|
The DSPy signature for which to format the field structure. |
required |
Source code in dspy/adapters/base.py
format_task_description(signature: Type[Signature]) -> str
Format the task description for the system message.
This method formats the task description for the system message. In most cases this is just a thin wrapper
over signature.instructions
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signature
|
Type[Signature]
|
The DSPy signature of the DSpy module. |
required |
Returns:
Type | Description |
---|---|
str
|
A string that describes the task. |
Source code in dspy/adapters/base.py
format_user_message_content(signature: Type[Signature], inputs: dict[str, Any], prefix: str = '', suffix: str = '', main_request: bool = False) -> str
Format the user message content.
This method formats the user message content, which can be used in formatting few-shot examples, conversation history, and the current input.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signature
|
Type[Signature]
|
The DSPy signature for which to format the user message content. |
required |
inputs
|
dict[str, Any]
|
The input arguments to the DSPy module. |
required |
prefix
|
str
|
A prefix to the user message content. |
''
|
suffix
|
str
|
A suffix to the user message content. |
''
|
Returns:
Type | Description |
---|---|
str
|
A string that contains the user message content. |
Source code in dspy/adapters/base.py
parse(signature: Type[Signature], completion: str) -> dict[str, Any]
Parse the LM output into a dictionary of the output fields.
This method parses the LM output into a dictionary of the output fields.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signature
|
Type[Signature]
|
The DSPy signature for which to parse the LM output. |
required |
completion
|
str
|
The LM output to be parsed. |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
A dictionary of the output fields. |