Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

auto style fix (#8309)

Chen Qian62bcf129a84a
diff --git a/dspy/adapters/__init__.py b/dspy/adapters/__init__.py
index 15fc74a8a..9bb499f38 100644
--- a/dspy/adapters/__init__.py
+++ b/dspy/adapters/__init__.py
@@ -2,7 +2,7 @@ from dspy.adapters.base import Adapter
from dspy.adapters.chat_adapter import ChatAdapter
from dspy.adapters.json_adapter import JSONAdapter
from dspy.adapters.two_step_adapter import TwoStepAdapter
-from dspy.adapters.types import History, Image, Audio, BaseType, Tool, ToolCalls
+from dspy.adapters.types import Audio, BaseType, History, Image, Tool, ToolCalls
__all__ = [
"Adapter",
diff --git a/dspy/adapters/types/__init__.py b/dspy/adapters/types/__init__.py
index 11d09227c..68607e527 100644
--- a/dspy/adapters/types/__init__.py
+++ b/dspy/adapters/types/__init__.py
@@ -1,7 +1,7 @@
-from dspy.adapters.types.history import History
-from dspy.adapters.types.image import Image
from dspy.adapters.types.audio import Audio
from dspy.adapters.types.base_type import BaseType
+from dspy.adapters.types.history import History
+from dspy.adapters.types.image import Image
from dspy.adapters.types.tool import Tool, ToolCalls
__all__ = ["History", "Image", "Audio", "BaseType", "Tool", "ToolCalls"]
diff --git a/dspy/adapters/types/audio.py b/dspy/adapters/types/audio.py
index 77c74b53a..800017e75 100644
--- a/dspy/adapters/types/audio.py
+++ b/dspy/adapters/types/audio.py
@@ -48,7 +48,7 @@ class Audio(BaseType):
"""
if isinstance(values, cls):
return {"data": values.data, "format": values.format}
- return encode_audio(values)
+ return encode_audio(values)
@classmethod
def from_url(cls, url: str) -> "Audio":
@@ -142,4 +142,4 @@ def encode_audio(audio: Union[str, bytes, dict, "Audio", Any], sampling_rate: in
encoded = base64.b64encode(audio).decode("utf-8")
return {"data": encoded, "format": format}
else:
- raise ValueError(f"Unsupported type for encode_audio: {type(audio)}")
\ No newline at end of file
+ raise ValueError(f"Unsupported type for encode_audio: {type(audio)}")
diff --git a/dspy/dsp/utils/dpr.py b/dspy/dsp/utils/dpr.py
index 5a46e61fd..e9fc27813 100644
--- a/dspy/dsp/utils/dpr.py
+++ b/dspy/dsp/utils/dpr.py
@@ -158,7 +158,7 @@ class SimpleTokenizer(Tokenizer):
annotators: None or empty set (only tokenizes).
"""
self._regexp = regex.compile(
- "(%s)|(%s)" % (self.ALPHA_NUM, self.NON_WS), # noqa: UP031
+ "(%s)|(%s)" % (self.ALPHA_NUM, self.NON_WS),
flags=regex.IGNORECASE + regex.UNICODE + regex.MULTILINE,
)
if len(kwargs.get("annotators", {})) > 0:
diff --git a/dspy/predict/__init__.py b/dspy/predict/__init__.py
index f1f54c337..24926ef1b 100644
--- a/dspy/predict/__init__.py
+++ b/dspy/predict/__init__.py
@@ -2,13 +2,13 @@ from dspy.predict.aggregation import majority
from dspy.predict.best_of_n import BestOfN
from dspy.predict.chain_of_thought import ChainOfThought
from dspy.predict.chain_of_thought_with_hint import ChainOfThoughtWithHint
+from dspy.predict.code_act import CodeAct
from dspy.predict.knn import KNN
from dspy.predict.multi_chain_comparison import MultiChainComparison
from dspy.predict.parallel import Parallel
from dspy.predict.predict import Predict
from dspy.predict.program_of_thought import ProgramOfThought
from dspy.predict.react import ReAct, Tool
-from dspy.predict.code_act import CodeAct
from dspy.predict.refine import Refine
__all__ = [
diff --git a/dspy/predict/code_act.py b/dspy/predict/code_act.py
index 1633e3395..c48d2c5ae 100644
--- a/dspy/predict/code_act.py
+++ b/dspy/predict/code_act.py
@@ -68,7 +68,7 @@ class CodeAct(ReAct, ProgramOfThought):
self.extractor = dspy.ChainOfThought(extract_signature)
# It will raises exception when dspy cannot find available deno instance by now.
self.interpreter = PythonInterpreter()
-
+
def _build_instructions(self, signature, tools):
instructions = [f"{signature.instructions}\n"] if signature.instructions else []
inputs = ", ".join([f"`{k}`" for k in signature.input_fields.keys()])
@@ -85,14 +85,14 @@ class CodeAct(ReAct, ProgramOfThought):
for idx, tool in enumerate(tools.values()):
instructions.append(f"({idx + 1}) {tool}")
-
+
return instructions
def forward(self, **kwargs):
# Define the tool funcitons in the interpreter
for tool in self.tools.values():
self.interpreter(inspect.getsource(tool.func))
-
+
trajectory = {}
max_iters = kwargs.pop("max_iters", self.max_iters)
for idx in range(max_iters):
@@ -111,7 +111,7 @@ class CodeAct(ReAct, ProgramOfThought):
trajectory[f"code_output_{idx}"] = output
else:
trajectory[f"observation_{idx}"] = f"Failed to execute the generated code: {error}"
-
+
if code_data.finished:
break
diff --git a/dspy/signatures/signature.py b/dspy/signatures/signature.py
index 4606db87c..c25f4e7e0 100644
--- a/dspy/signatures/signature.py
+++ b/dspy/signatures/signature.py
@@ -19,9 +19,9 @@ import ast
import importlib
import inspect
import re
+import sys
import types
import typing
-import sys
from copy import deepcopy
from typing import Any, Dict, Optional, Tuple, Type, Union
@@ -71,34 +71,34 @@ class SignatureMeta(type(BaseModel)):
# Get type references from caller frames by walking the stack
found_types = {}
-
+
needed_types = set()
dotted_types = {}
-
+
for type_name in type_names:
parts = type_name.split('.')
base_name = parts[0]
-
+
if base_name not in typing.__dict__ and base_name not in __builtins__:
if len(parts) > 1:
dotted_types[type_name] = base_name
needed_types.add(base_name)
else:
needed_types.add(type_name)
-
+
if not needed_types:
return None
-
+
frame = None
try:
frame = sys._getframe(1) # Start one level up (skip this function)
-
+
max_frames = 100
frame_count = 0
-
+
while frame and needed_types and frame_count < max_frames:
frame_count += 1
-
+
for type_name in list(needed_types):
if type_name in frame.f_locals:
found_types[type_name] = frame.f_locals[type_name]
@@ -106,13 +106,13 @@ class SignatureMeta(type(BaseModel)):
elif frame.f_globals and type_name in frame.f_globals:
found_types[type_name] = frame.f_globals[type_name]
needed_types.remove(type_name)
-
+
# If we found all needed types, stop looking
if not needed_types:
break
-
+
frame = frame.f_back
-
+
if needed_types and frame_count >= max_frames:
import logging
logging.getLogger("dspy").warning(
@@ -132,7 +132,7 @@ class SignatureMeta(type(BaseModel)):
return found_types or None
- def __new__(mcs, signature_name, bases, namespace, **kwargs): # noqa: N804
+ def __new__(mcs, signature_name, bases, namespace, **kwargs):
# At this point, the orders have been swapped already.
field_order = [name for name, value in namespace.items() if isinstance(value, FieldInfo)]
# Set `str` as the default type for all fields
@@ -283,10 +283,7 @@ class Signature(BaseModel, metaclass=SignatureMeta):
def delete(cls, name) -> Type["Signature"]:
fields = dict(cls.fields)
- if name in fields:
- del fields[name]
- else:
- raise ValueError(f"Field `{name}` not found in `{cls.__name__}`.")
+ fields.pop(name, None)
return Signature(fields, cls.instructions)
@@ -411,7 +408,7 @@ def make_signature(
if custom_types:
names = dict(typing.__dict__)
names.update(custom_types)
-
+
fields = _parse_signature(signature, names) if isinstance(signature, str) else signature
# Validate the fields, this is important because we sometimes forget the
@@ -549,15 +546,15 @@ def _parse_type_node(node, names=None) -> Any:
if isinstance(node, ast.Attribute):
base = _parse_type_node(node.value, names)
attr_name = node.attr
-
+
if hasattr(base, attr_name):
return getattr(base, attr_name)
-
+
if isinstance(node.value, ast.Name):
full_name = f"{node.value.id}.{attr_name}"
if full_name in names:
return names[full_name]
-
+
raise ValueError(f"Unknown attribute: {attr_name} on {base}")
if isinstance(node, ast.Subscript):
diff --git a/dspy/teleprompt/grpo.py b/dspy/teleprompt/grpo.py
index 7ee397845..0872dd155 100644
--- a/dspy/teleprompt/grpo.py
+++ b/dspy/teleprompt/grpo.py
@@ -298,7 +298,7 @@ class GRPO(FinetuneTeleprompter):
num_student_predictors = len(student.predictors())
logging.info("Preparing the teacher program(s)... We will ensure that the provided programs have the same program structure as the student program.")
- if isinstance(teacher, list) and len(teacher) == 0 or teacher is None:
+ if (isinstance(teacher, list) and len(teacher) == 0) or teacher is None:
teacher = student
teachers = teacher if isinstance(teacher, list) else [teacher]
for t in teachers:
diff --git a/dspy/teleprompt/mipro_optimizer_v2.py b/dspy/teleprompt/mipro_optimizer_v2.py
index 05dd4eec1..eae337e33 100644
--- a/dspy/teleprompt/mipro_optimizer_v2.py
+++ b/dspy/teleprompt/mipro_optimizer_v2.py
@@ -4,9 +4,8 @@ import select
import sys
import textwrap
import time
-from typing import TYPE_CHECKING
from collections import defaultdict
-from typing import Any, Callable, Dict, List, Literal, Optional, Tuple
+from typing import TYPE_CHECKING, Any, Callable, Dict, List, Literal, Optional, Tuple
import numpy as np
diff --git a/dspy/utils/langchain_tool.py b/dspy/utils/langchain_tool.py
index cce44660a..306ede8b9 100644
--- a/dspy/utils/langchain_tool.py
+++ b/dspy/utils/langchain_tool.py
@@ -24,13 +24,13 @@ def convert_langchain_tool(tool: "BaseTool") -> Tool:
result = await tool.ainvoke(kwargs)
return result
except Exception as e:
- raise RuntimeError(f"Failed to call LangChain tool {tool.name}: {str(e)}")
-
+ raise RuntimeError(f"Failed to call LangChain tool {tool.name}: {e!s}")
+
# Get args_schema from the tool
# https://python.langchain.com/api_reference/core/tools/langchain_core.tools.base.BaseTool.html#langchain_core.tools.base.BaseTool.args_schema
args_schema = tool.args_schema
args, _, arg_desc = convert_input_schema_to_tool_args(args_schema.model_json_schema())
-
+
# The args_schema of Langchain tool is a pydantic model, so we can get the type hints from the model fields
arg_types = {
key: field.annotation if field.annotation is not None else Any
@@ -44,4 +44,4 @@ def convert_langchain_tool(tool: "BaseTool") -> Tool:
args=args,
arg_types=arg_types,
arg_desc=arg_desc
- )
\ No newline at end of file
+ )
diff --git a/tests/adapters/test_chat_adapter.py b/tests/adapters/test_chat_adapter.py
index 816c04a31..3d8a67b7b 100644
--- a/tests/adapters/test_chat_adapter.py
+++ b/tests/adapters/test_chat_adapter.py
@@ -5,8 +5,6 @@ import pydantic
import pytest
import dspy
-import pydantic
-import json
@pytest.mark.parametrize(
diff --git a/tests/adapters/test_json_adapter.py b/tests/adapters/test_json_adapter.py
index 622458114..f1449e4ef 100644
--- a/tests/adapters/test_json_adapter.py
+++ b/tests/adapters/test_json_adapter.py
@@ -5,7 +5,6 @@ import pytest
from litellm.utils import Choices, Message, ModelResponse
import dspy
-import json
def test_json_adapter_passes_structured_output_when_supported_by_model():
diff --git a/tests/adapters/test_tool.py b/tests/adapters/test_tool.py
index 925e9b9d4..de2f64d88 100644
--- a/tests/adapters/test_tool.py
+++ b/tests/adapters/test_tool.py
@@ -1,12 +1,10 @@
import asyncio
from typing import Any, Optional
-import dspy
import pytest
from pydantic import BaseModel
from dspy.adapters.types.tool import Tool
-from unittest import mock
# Test fixtures
@@ -259,7 +257,7 @@ def test_tool_str():
def add(x: int, y: int = 0) -> int:
"""Add two integers."""
return x + y
-
+
tool = Tool(add)
assert (
str(tool)
diff --git a/tests/predict/test_code_act.py b/tests/predict/test_code_act.py
index 256593264..3bebfd69b 100644
--- a/tests/predict/test_code_act.py
+++ b/tests/predict/test_code_act.py
@@ -1,6 +1,7 @@
-import pytest
import shutil
+import pytest
+
import dspy
from dspy import Signature
from dspy.predict import CodeAct
diff --git a/tests/primitives/test_module.py b/tests/primitives/test_module.py
index 498510a52..bc1d2975c 100644
--- a/tests/primitives/test_module.py
+++ b/tests/primitives/test_module.py
@@ -1,10 +1,10 @@
import logging
import os
-from litellm import ModelResponse, Message, Choices
import threading
from unittest.mock import patch
import pytest
+from litellm import Choices, Message, ModelResponse
import dspy
from dspy.utils.dummies import DummyLM
diff --git a/tests/signatures/test_custom_types.py b/tests/signatures/test_custom_types.py
index b2d0dfbaa..5393dc24d 100644
--- a/tests/signatures/test_custom_types.py
+++ b/tests/signatures/test_custom_types.py
@@ -1,10 +1,9 @@
+
import pydantic
import pytest
-from typing import List, Dict, Any
import dspy
from dspy import Signature
-from dspy.utils.dummies import DummyLM
def test_basic_custom_type_resolution():
@@ -18,7 +17,7 @@ def test_basic_custom_type_resolution():
custom_types={"CustomType": CustomType}
)
assert explicit_sig.input_fields["input"].annotation == CustomType
-
+
# Custom types can also be auto-resolved from caller's scope
auto_sig = Signature("input: CustomType -> output: str")
assert auto_sig.input_fields["input"].annotation == CustomType
@@ -29,7 +28,7 @@ def test_type_alias_for_nested_types():
class Container:
class NestedType(pydantic.BaseModel):
value: str
-
+
NestedType = Container.NestedType
alias_sig = Signature("input: str -> output: NestedType")
assert alias_sig.output_fields["output"].annotation == Container.NestedType
@@ -66,18 +65,18 @@ class OuterContainer:
def test_recommended_patterns():
"""Test recommended patterns for working with custom types in signatures."""
-
+
# PATTERN 1: Local type with auto-resolution
class LocalType(pydantic.BaseModel):
value: str
-
+
sig1 = Signature("input: str -> output: LocalType")
assert sig1.output_fields["output"].annotation == LocalType
-
+
# PATTERN 2: Module-level type with auto-resolution
sig2 = Signature("input: str -> output: GlobalCustomType")
assert sig2.output_fields["output"].annotation == GlobalCustomType
-
+
# PATTERN 3: Nested type with dot notation
sig3 = Signature("input: str -> output: OuterContainer.InnerType")
assert sig3.output_fields["output"].annotation == OuterContainer.InnerType
@@ -117,4 +116,4 @@ def test_basic_custom_type_resolution():
assert sig.input_fields["input"].annotation == CustomType
sig = Signature("input: CustomType -> output: str")
- assert sig.input_fields["input"].annotation == CustomType
\ No newline at end of file
+ assert sig.input_fields["input"].annotation == CustomType
diff --git a/tests/signatures/test_signature.py b/tests/signatures/test_signature.py
index b1c79ae01..9d58e1f72 100644
--- a/tests/signatures/test_signature.py
+++ b/tests/signatures/test_signature.py
@@ -435,4 +435,4 @@ def test_custom_type_from_different_module():
path_obj = Path("/test/path")
pred = dspy.Predict(test_signature)(input=path_obj)
- assert pred.output == "/test/path"
\ No newline at end of file
+ assert pred.output == "/test/path"
diff --git a/tests/streaming/test_streaming.py b/tests/streaming/test_streaming.py
index 5caca0d1f..6019964c1 100644
--- a/tests/streaming/test_streaming.py
+++ b/tests/streaming/test_streaming.py
@@ -1,12 +1,12 @@
import os
+from unittest import mock
+from unittest.mock import AsyncMock
import pytest
+from litellm.types.utils import Delta, ModelResponseStream, StreamingChoices
import dspy
from dspy.streaming import StatusMessage, StatusMessageProvider, streaming_response
-from unittest import mock
-from unittest.mock import AsyncMock
-from litellm.types.utils import ModelResponseStream, StreamingChoices, Delta
@pytest.mark.anyio
diff --git a/tests/utils/test_langchain_tool.py b/tests/utils/test_langchain_tool.py
index ef12a2cea..8d8c8904e 100644
--- a/tests/utils/test_langchain_tool.py
+++ b/tests/utils/test_langchain_tool.py
@@ -1,4 +1,5 @@
import importlib
+
import pytest
if importlib.util.find_spec("langchain_core") is None:
@@ -41,7 +42,7 @@ async def test_convert_custom_tool_with_custom_class():
def get_age(profile: Profile) -> int:
"""Get the age of the profile."""
return profile.age
-
+
tool = convert_langchain_tool(get_age)
assert tool.name == "get_age"
assert tool.desc == "Get the age of the profile."