Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

feat(dspy): try from json before applying formatting logic in typed predictor

JONEMI197c2ffffc51c5
diff --git a/dspy/functional/functional.py b/dspy/functional/functional.py
index 667c86d4b..4d6f99823 100644
--- a/dspy/functional/functional.py
+++ b/dspy/functional/functional.py
@@ -1,8 +1,7 @@
import inspect
import json
-import textwrap
import typing
-from typing import Annotated, List, Tuple, Union # noqa: UP035
+from typing import Annotated, Callable, List, Tuple, Union # noqa: UP035
import pydantic
import ujson
@@ -14,7 +13,7 @@ from dspy.primitives.prediction import Prediction
from dspy.signatures.signature import ensure_signature, make_signature
-def predictor(*args, **kwargs):
+def predictor(*args: tuple, **kwargs) -> Callable[..., dspy.Module]:
def _predictor(func) -> dspy.Module:
"""Decorator that creates a predictor module based on the provided function."""
signature = _func_to_signature(func)
@@ -28,7 +27,7 @@ def predictor(*args, **kwargs):
return _predictor
-def cot(*args, **kwargs):
+def cot(*args: tuple, **kwargs) -> Callable[..., dspy.Module]:
def _cot(func) -> dspy.Module:
"""Decorator that creates a chain of thought module based on the provided function."""
signature = _func_to_signature(func)
@@ -72,11 +71,11 @@ def TypedChainOfThought(signature, instructions=None, reasoning=None, *, max_ret
signature = ensure_signature(signature, instructions)
output_keys = ", ".join(signature.output_fields.keys())
- DEFAULT_RATIONALE = dspy.OutputField(
+ default_rationale = dspy.OutputField(
prefix="Reasoning: Let's think step by step in order to",
desc="${produce the " + output_keys + "}. We ...",
)
- reasoning = reasoning or DEFAULT_RATIONALE
+ reasoning = reasoning or default_rationale
return TypedPredictor(
signature.prepend(
@@ -93,8 +92,10 @@ class TypedPredictor(dspy.Module):
Args:
signature: The signature of the module. Can use type annotations.
+ instructions: A description of what the model should do.
max_retries: The number of times to retry the prediction if the output is invalid.
wrap_json: If True, json objects in the input will be wrapped in ```json ... ```
+ explain_errors: If True, the model will try to explain the errors it encounters.
"""
super().__init__()
self.signature = ensure_signature(signature, instructions)
@@ -128,7 +129,7 @@ class TypedPredictor(dspy.Module):
)(json_schema=schema).json_object
# We use the model_validate_json method to make sure the example is valid
try:
- type_.model_validate_json(_unwrap_json(json_object))
+ type_.model_validate_json(_unwrap_json(json_object, type_.model_validate_json))
except (pydantic.ValidationError, ValueError):
return "" # Unable to make an example
return json_object
@@ -172,11 +173,10 @@ class TypedPredictor(dspy.Module):
def _make_explanation(self, task_description: str, model_output: str, error: str) -> str:
class Signature(dspy.Signature):
- __doc__ = textwrap.dedent(
- """
- I gave my language model a task, but it failed. Figure out what went wrong,
- and write instructions to help it avoid the error next time.""",
- )
+ """I gave my language model a task, but it failed.
+
+ Figure out what went wrong, and write instructions to help it avoid the error next time.
+ """
task_description: str = dspy.InputField(desc="What I asked the model to do")
language_model_output: str = dspy.InputField(desc="The output of the model")
@@ -259,7 +259,7 @@ class TypedPredictor(dspy.Module):
desc=field.json_schema_extra.get("desc", "")
+ (". Respond with a single JSON object. JSON Schema: " + schema),
format=lambda x, to_json=to_json: (x if isinstance(x, str) else to_json(x)),
- parser=lambda x, from_json=from_json: from_json(_unwrap_json(x)),
+ parser=lambda x, from_json=from_json: from_json(_unwrap_json(x, from_json)),
type_=type_,
)
else: # If input field
@@ -405,14 +405,17 @@ def _func_to_signature(func):
return dspy.Signature(fields, instructions)
-def _unwrap_json(output):
- output = output.strip()
- if output.startswith("```"):
- if not output.startswith("```json"):
- raise ValueError("json output should start with ```json")
- if not output.endswith("```"):
- raise ValueError("Don't write anything after the final json ```")
- output = output[7:-3].strip()
- if not output.startswith("{") or not output.endswith("}"):
- raise ValueError("json output should start and end with { and }")
- return ujson.dumps(ujson.loads(output)) # ujson is a bit more robust than the standard json
+def _unwrap_json(output, native_parser: Callable[[str], dict | pydantic.BaseModel] = None):
+ try:
+ return native_parser(output)
+ except (ValueError, pydantic.ValidationError):
+ output = output.strip()
+ if output.startswith("```"):
+ if not output.startswith("```json"):
+ raise ValueError("json output should start with ```json") from None
+ if not output.endswith("```"):
+ raise ValueError("Don't write anything after the final json ```") from None
+ output = output[7:-3].strip()
+ if not output.startswith("{") or not output.endswith("}"):
+ raise ValueError("json output should start and end with { and }") from None
+ return ujson.dumps(ujson.loads(output)) # ujson is a bit more robust than the standard json