dspy.JSONAdapter¶
dspy.JSONAdapter(callbacks: list[BaseCallback] | None = None, use_native_function_calling: bool = True, parallel_tool_calls: bool | None = None) ¶
Bases: ChatAdapter
Source code in dspy/adapters/json_adapter.py
Methods:¶
__call__(lm: BaseLM, 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/chat_adapter.py
acall(lm: BaseLM, lm_kwargs: dict[str, Any], signature: type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]) -> list[dict[str, Any]] async ¶
Source code in dspy/adapters/chat_adapter.py
format(signature: type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any]) -> Prompt ¶
Render the prompt for the LM call.
This method converts the DSPy structured input along with few-shot examples and conversation history into a system instruction plus multiturn lm15 messages. For custom adapters, this method can be overridden to customize the rendering.
The recommended structure is:
Prompt(
system=system_message, # field descriptions, field structure and task description
messages=(
# Begin few-shot examples
Message.user(few_shot_example_1_input),
Message.assistant(few_shot_example_1_output),
...
# End few-shot examples
# Begin conversation history
Message.user(conversation_history_1_input),
Message.assistant(conversation_history_1_output),
...
# End conversation history
Message.user(current_input),
),
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signature | type[Signature] | The DSPy signature for which to format the prompt. | 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 |
|---|---|
Prompt | The rendered |
Source code in dspy/adapters/base.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | |
format_assistant_message_content(signature: type[Signature], outputs: dict[str, Any], missing_field_message=None) -> str ¶
Source code in dspy/adapters/json_adapter.py
format_conversation_history(signature: type[Signature], history_field_name: str, inputs: dict[str, Any]) -> list[Message] ¶
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[Message] | A list of multiturn lm15 messages. |
Source code in dspy/adapters/base.py
format_demos(signature: type[Signature], demos: list[dict[str, Any]]) -> list[Message] ¶
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[Message] | A list of multiturn lm15 messages. |
Source code in dspy/adapters/base.py
format_field_description(signature: type[Signature]) -> str ¶
Source code in dspy/adapters/chat_adapter.py
format_field_structure(signature: type[Signature]) -> str ¶
Source code in dspy/adapters/json_adapter.py
format_field_with_value(fields_with_values: dict[FieldInfoWithName, Any], role: str = 'user') -> str ¶
Formats the values of the specified fields according to the field’s DSPy type (input or output), annotation (e.g. str, int, etc.), and the type of the value itself. Joins the formatted values into a single string, which is a multiline string if there are multiple fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fields_with_values | dict[FieldInfoWithName, Any] | A dictionary mapping information about a field to its corresponding value. | required |
Returns: The joined formatted values of the fields, represented as a string.
Source code in dspy/adapters/json_adapter.py
format_finetune_data(signature: type[Signature], demos: list[dict[str, Any]], inputs: dict[str, Any], outputs: dict[str, Any]) -> dict[str, list[Any]] ¶
Source code in dspy/adapters/json_adapter.py
format_system_message(signature: type[Signature]) -> str ¶
Format the system message for the LM call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signature | type[Signature] | The DSPy signature for which to format the system message. | required |