Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Fixes lint issues in functional.py

Thomas D Ahle182a6a6c7f67
diff --git a/dspy/functional/functional.py b/dspy/functional/functional.py
index d1d9bf7ca..96c218ebb 100644
--- a/dspy/functional/functional.py
+++ b/dspy/functional/functional.py
@@ -4,7 +4,7 @@ import openai
import dspy
import typing
import pydantic
-from typing import Annotated, List, Tuple
+from typing import Annotated, List, Tuple # noqa: UP035
from dsp.templates import passages2text
import json
@@ -14,13 +14,15 @@ from dspy.signatures.signature import ensure_signature
MAX_RETRIES = 3
-def predictor(func):
+def predictor(func) -> dspy.Module:
+ """Decorator that creates a predictor module based on the provided function."""
signature = _func_to_signature(func)
*_, output_key = signature.output_fields.keys()
return _StripOutput(TypedPredictor(signature), output_key)
-def cot(func):
+def cot(func) -> dspy.Module:
+ """Decorator that creates a chain of thought module based on the provided function."""
signature = _func_to_signature(func)
*_, output_key = signature.output_fields.keys()
return _StripOutput(TypedChainOfThought(signature), output_key)
@@ -51,7 +53,7 @@ class FunctionalModule(dspy.Module):
self.__dict__[name] = attr.copy()
-def TypedChainOfThought(signature):
+def TypedChainOfThought(signature) -> dspy.Module: # noqa: N802
"""Just like TypedPredictor, but adds a ChainOfThought OutputField."""
signature = ensure_signature(signature)
output_keys = ", ".join(signature.output_fields.keys())
@@ -72,11 +74,11 @@ class TypedPredictor(dspy.Module):
self.signature = signature
self.predictor = dspy.Predict(signature)
- def copy(self):
+ def copy(self) -> "TypedPredictor":
return TypedPredictor(self.signature)
@staticmethod
- def _make_example(type_):
+ def _make_example(type_) -> str:
# Note: DSPy will cache this call so we only pay the first time TypedPredictor is called.
json_object = dspy.Predict(
dspy.Signature(
@@ -96,9 +98,10 @@ class TypedPredictor(dspy.Module):
# TODO: Instead of using a language model to create the example, we can also just use a
# library like https://pypi.org/project/polyfactory/ that's made exactly to do this.
- def _prepare_signature(self):
+ def _prepare_signature(self) -> dspy.Signature:
"""Add formats and parsers to the signature fields, based on the type
- annotations of the fields."""
+ annotations of the fields.
+ """
signature = self.signature
for name, field in self.signature.fields.items():
is_output = field.json_schema_extra["__dspy_field_type"] == "output"
@@ -114,12 +117,12 @@ class TypedPredictor(dspy.Module):
)
else:
# Anything else we wrap in a pydantic object
- unwrap = lambda x: x
- wrap = lambda x: x
+ to_json = lambda x: x.model_dump_json()
+ from_json = lambda x, type_=type_: type_.model_validate_json(x)
if not (inspect.isclass(type_) and issubclass(type_, pydantic.BaseModel)):
type_ = pydantic.create_model("Output", value=(type_, ...), __base__=pydantic.BaseModel)
- wrap = lambda x: type_(value=x)
- unwrap = lambda x: x.value
+ to_json = lambda x, type_=type_: type_(value=x).model_dump_json()
+ from_json = lambda x, type_=type_: type_.model_validate_json(x).value
signature = signature.with_updated_fields(
name,
desc=field.json_schema_extra.get("desc", "")
@@ -127,21 +130,21 @@ class TypedPredictor(dspy.Module):
". Respond with a single JSON object. JSON Schema: "
+ json.dumps(type_.model_json_schema())
),
- format=lambda x: (x if isinstance(x, str) else wrap(x).model_dump_json()),
- parser=lambda x: unwrap(type_.model_validate_json(_unwrap_json(x))),
+ 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)),
type_=type_,
)
else: # If input field
- format = lambda x: x if isinstance(x, str) else str(x)
+ format_ = lambda x: x if isinstance(x, str) else str(x)
if type_ in (List[str], list[str], Tuple[str], tuple[str]):
- format = passages2text
+ format_ = passages2text
elif inspect.isclass(type_) and issubclass(type_, pydantic.BaseModel):
- format = lambda x: x if isinstance(x, str) else x.model_dump_json()
- signature = signature.with_updated_fields(name, format=format)
+ format_ = lambda x: x if isinstance(x, str) else x.model_dump_json()
+ signature = signature.with_updated_fields(name, format=format_)
return signature
- def forward(self, **kwargs):
+ def forward(self, **kwargs) -> dspy.Prediction:
modified_kwargs = kwargs.copy()
# We have to re-prepare the signature on every forward call, because the base
# signature might have been modified by an optimizer or something like that.
@@ -165,11 +168,12 @@ class TypedPredictor(dspy.Module):
continue # Only add examples to JSON objects
suffix, current_desc = current_desc[i:], current_desc[:i]
prefix = "You MUST use this format: "
- if try_i + 1 < MAX_RETRIES and prefix not in current_desc:
- if example := self._make_example(field.annotation):
- signature = signature.with_updated_fields(
- name, desc=current_desc + "\n" + prefix + example + "\n" + suffix,
- )
+ if try_i + 1 < MAX_RETRIES \
+ and prefix not in current_desc \
+ and (example := self._make_example(field.annotation)):
+ signature = signature.with_updated_fields(
+ name, desc=current_desc + "\n" + prefix + example + "\n" + suffix,
+ )
if errors:
# Add new fields for each error
for name, error in errors.items():
@@ -249,7 +253,7 @@ def _unwrap_json(output):
################################################################################
-def main():
+def main() -> None:
class Answer(pydantic.BaseModel):
value: float
certainty: float
@@ -277,8 +281,8 @@ def main():
question, answer = qa(topic="Physics")
# lm.inspect_history(n=5)
- print("Question:", question)
- print("Answer:", answer)
+ print("Question:", question) # noqa: T201
+ print("Answer:", answer) # noqa: T201
################################################################################
@@ -286,7 +290,7 @@ def main():
################################################################################
-def validate_context_and_answer_and_hops(example, pred, trace=None):
+def validate_context_and_answer_and_hops(example, pred, trace=None) -> bool:
if not dspy.evaluate.answer_exact_match(example, pred):
return False
if not dspy.evaluate.answer_passage_match(example, pred):
@@ -302,25 +306,25 @@ def validate_context_and_answer_and_hops(example, pred, trace=None):
return True
-def gold_passages_retrieved(example, pred, trace=None):
+def gold_passages_retrieved(example, pred, _trace=None) -> bool:
gold_titles = set(map(dspy.evaluate.normalize_text, example["gold_titles"]))
found_titles = set(map(dspy.evaluate.normalize_text, [c.split(" | ")[0] for c in pred.context]))
return gold_titles.issubset(found_titles)
-def hotpot():
+def hotpot() -> None:
from dsp.utils import deduplicate
import dspy.evaluate
from dspy.datasets import HotPotQA
from dspy.evaluate.evaluate import Evaluate
from dspy.teleprompt.bootstrap import BootstrapFewShot
- print("Load the dataset.")
+ print("Load the dataset.") # noqa: T201
dataset = HotPotQA(train_seed=1, train_size=20, eval_seed=2023, dev_size=50, test_size=0)
trainset = [x.with_inputs("question") for x in dataset.train]
devset = [x.with_inputs("question") for x in dataset.dev]
- print("Done")
+ print("Done") # noqa: T201
class SimplifiedBaleen(FunctionalModule):
def __init__(self, passages_per_hop=3, max_hops=1):
@@ -341,7 +345,7 @@ def hotpot():
def forward(self, question):
context = []
- for hop in range(self.max_hops):
+ for _ in range(self.max_hops):
query = self.generate_query(context=context, question=question)
passages = self.retrieve(query).passages
context = deduplicate(context + passages)
@@ -358,7 +362,7 @@ def hotpot():
# uncompiled (i.e., zero-shot) program
uncompiled_baleen = SimplifiedBaleen()
- print(
+ print( # noqa: T201
"Uncompiled Baleen retrieval score:",
evaluate_on_hotpotqa(uncompiled_baleen, metric=gold_passages_retrieved),
)
@@ -369,7 +373,7 @@ def hotpot():
teacher=SimplifiedBaleen(passages_per_hop=2),
trainset=trainset,
)
- print(
+ print( # noqa: T201
"Compiled Baleen retrieval score:",
evaluate_on_hotpotqa(compiled_baleen, metric=gold_passages_retrieved),
)
diff --git a/pyproject.toml b/pyproject.toml
index c01ca1d08..bdb4bc75d 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -254,6 +254,15 @@ ignore = [
"RET505",
# Within an `except` clause, raise exceptions with `raise
"B904",
+ # We don't need docstrings for every method
+ "ANN202",
+ "D107",
+ "D102",
+ "D103",
+ # Inline lambdas
+ "E731",
+ # Sometimes we need List and Tuple
+ "UP006",
]
# Allow fix for all enabled rules (when `--fix`) is provided.