Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Rename dspy.BaseType to dspy.Type (#8510)

Chen Qian538fbb3a4c49
diff --git a/dspy/__init__.py b/dspy/__init__.py
index 9113e4f5e..f2e3c20a9 100644
--- a/dspy/__init__.py
+++ b/dspy/__init__.py
@@ -6,7 +6,7 @@ from dspy.teleprompt import *
from dspy.evaluate import Evaluate # isort: skip
from dspy.clients import * # isort: skip
-from dspy.adapters import Adapter, ChatAdapter, JSONAdapter, XMLAdapter, TwoStepAdapter, Image, Audio, History, BaseType, Tool, ToolCalls # isort: skip
+from dspy.adapters import Adapter, ChatAdapter, JSONAdapter, XMLAdapter, TwoStepAdapter, Image, Audio, History, Type, Tool, ToolCalls # isort: skip
from dspy.utils.logging_utils import configure_dspy_loggers, disable_logging, enable_logging
from dspy.utils.asyncify import asyncify
from dspy.utils.saving import load
diff --git a/dspy/adapters/__init__.py b/dspy/adapters/__init__.py
index 6d342df33..c2c6d23a3 100644
--- a/dspy/adapters/__init__.py
+++ b/dspy/adapters/__init__.py
@@ -2,13 +2,13 @@ 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 Audio, BaseType, History, Image, Tool, ToolCalls
+from dspy.adapters.types import Audio, History, Image, Tool, ToolCalls, Type
from dspy.adapters.xml_adapter import XMLAdapter
__all__ = [
"Adapter",
"ChatAdapter",
- "BaseType",
+ "Type",
"History",
"Image",
"Audio",
diff --git a/dspy/adapters/types/__init__.py b/dspy/adapters/types/__init__.py
index 68607e527..bc57d50d5 100644
--- a/dspy/adapters/types/__init__.py
+++ b/dspy/adapters/types/__init__.py
@@ -1,7 +1,7 @@
from dspy.adapters.types.audio import Audio
-from dspy.adapters.types.base_type import BaseType
+from dspy.adapters.types.base_type import Type
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"]
+__all__ = ["History", "Image", "Audio", "Type", "Tool", "ToolCalls"]
diff --git a/dspy/adapters/types/audio.py b/dspy/adapters/types/audio.py
index b4637a063..83e041562 100644
--- a/dspy/adapters/types/audio.py
+++ b/dspy/adapters/types/audio.py
@@ -7,7 +7,7 @@ from typing import Any, Union
import pydantic
import requests
-from dspy.adapters.types.base_type import BaseType
+from dspy.adapters.types.base_type import Type
try:
import soundfile as sf
@@ -17,7 +17,7 @@ except ImportError:
SF_AVAILABLE = False
-class Audio(BaseType):
+class Audio(Type):
data: str
audio_format: str
diff --git a/dspy/adapters/types/base_type.py b/dspy/adapters/types/base_type.py
index 4ac90970d..e35369227 100644
--- a/dspy/adapters/types/base_type.py
+++ b/dspy/adapters/types/base_type.py
@@ -9,7 +9,7 @@ CUSTOM_TYPE_START_IDENTIFIER = "<<CUSTOM-TYPE-START-IDENTIFIER>>"
CUSTOM_TYPE_END_IDENTIFIER = "<<CUSTOM-TYPE-END-IDENTIFIER>>"
-class BaseType(pydantic.BaseModel):
+class Type(pydantic.BaseModel):
"""Base class to support creating custom types for DSPy signatures.
This is the parent class of DSPy custom types, e.g, dspy.Image. Subclasses must implement the `format` method to
@@ -18,7 +18,7 @@ class BaseType(pydantic.BaseModel):
Example:
```python
- class Image(BaseType):
+ class Image(Type):
url: str
def format(self) -> list[dict[str, Any]]:
@@ -85,7 +85,7 @@ def split_message_content_for_custom_types(messages: list[dict[str, Any]]) -> li
This is implemented by finding the `<<CUSTOM-TYPE-START-IDENTIFIER>>` and `<<CUSTOM-TYPE-END-IDENTIFIER>>`
in the user message content and splitting the content around them. The `<<CUSTOM-TYPE-START-IDENTIFIER>>`
- and `<<CUSTOM-TYPE-END-IDENTIFIER>>` are the reserved identifiers for the custom types as in `dspy.BaseType`.
+ and `<<CUSTOM-TYPE-END-IDENTIFIER>>` are the reserved identifiers for the custom types as in `dspy.Type`.
Args:
messages: a list of messages sent to the LM. The format is the same as [OpenAI API's messages
diff --git a/dspy/adapters/types/image.py b/dspy/adapters/types/image.py
index e8b44ffe3..32fb786ed 100644
--- a/dspy/adapters/types/image.py
+++ b/dspy/adapters/types/image.py
@@ -8,7 +8,7 @@ from urllib.parse import urlparse
import pydantic
import requests
-from dspy.adapters.types.base_type import BaseType
+from dspy.adapters.types.base_type import Type
try:
from PIL import Image as PILImage
@@ -18,7 +18,7 @@ except ImportError:
PIL_AVAILABLE = False
-class Image(BaseType):
+class Image(Type):
url: str
model_config = {
diff --git a/dspy/adapters/types/tool.py b/dspy/adapters/types/tool.py
index c2ca4990c..8e4aeda72 100644
--- a/dspy/adapters/types/tool.py
+++ b/dspy/adapters/types/tool.py
@@ -5,7 +5,7 @@ from typing import TYPE_CHECKING, Any, Callable, Tuple, Type, get_origin, get_ty
from jsonschema import ValidationError, validate
from pydantic import BaseModel, TypeAdapter, create_model
-from dspy.adapters.types.base_type import BaseType
+from dspy.adapters.types.base_type import Type
from dspy.dsp.utils.settings import settings
from dspy.utils.callback import with_callbacks
@@ -16,7 +16,7 @@ if TYPE_CHECKING:
_TYPE_MAPPING = {"string": str, "integer": int, "number": float, "boolean": bool, "array": list, "object": dict}
-class Tool(BaseType):
+class Tool(Type):
"""Tool class.
This class is used to simplify the creation of tools for tool calling (function calling) in LLMs. Only supports
@@ -254,7 +254,7 @@ class Tool(BaseType):
return f"{self.name}{desc} {arg_desc}"
-class ToolCalls(BaseType):
+class ToolCalls(Type):
class ToolCall(BaseModel):
name: str
args: dict[str, Any]
@@ -303,7 +303,8 @@ class ToolCalls(BaseType):
"name": tool_call.name,
"arguments": tool_call.args,
},
- } for tool_call in self.tool_calls
+ }
+ for tool_call in self.tool_calls
],
}
]
diff --git a/dspy/adapters/utils.py b/dspy/adapters/utils.py
index 6632d325c..476765da2 100644
--- a/dspy/adapters/utils.py
+++ b/dspy/adapters/utils.py
@@ -10,7 +10,7 @@ import pydantic
from pydantic import TypeAdapter
from pydantic.fields import FieldInfo
-from dspy.adapters.types.base_type import BaseType
+from dspy.adapters.types.base_type import Type
from dspy.signatures.utils import get_dspy_field_type
@@ -204,7 +204,7 @@ def get_field_description_string(fields: dict) -> str:
field_message += f" ({get_annotation_name(v.annotation)})"
desc = v.json_schema_extra["desc"] if v.json_schema_extra["desc"] != f"${{{k}}}" else ""
- custom_types = BaseType.extract_custom_type_from_annotation(v.annotation)
+ custom_types = Type.extract_custom_type_from_annotation(v.annotation)
for custom_type in custom_types:
if len(custom_type.description()) > 0:
desc += f"\n Type description of {get_annotation_name(custom_type)}: {custom_type.description()}"
diff --git a/tests/adapters/test_base_type.py b/tests/adapters/test_base_type.py
index 1776d3192..b7d308642 100644
--- a/tests/adapters/test_base_type.py
+++ b/tests/adapters/test_base_type.py
@@ -1,11 +1,10 @@
-
import pydantic
import dspy
def test_basic_extract_custom_type_from_annotation():
- class Event(dspy.BaseType):
+ class Event(dspy.Type):
event_name: str
start_date_time: str
end_date_time: str | None
@@ -17,7 +16,7 @@ def test_basic_extract_custom_type_from_annotation():
email: str = dspy.InputField()
event: Event = dspy.OutputField()
- assert dspy.BaseType.extract_custom_type_from_annotation(ExtractEvent.output_fields["event"].annotation) == [Event]
+ assert dspy.Type.extract_custom_type_from_annotation(ExtractEvent.output_fields["event"].annotation) == [Event]
class ExtractEvents(dspy.Signature):
"""Extract all events from the email content."""
@@ -25,19 +24,17 @@ def test_basic_extract_custom_type_from_annotation():
email: str = dspy.InputField()
events: list[Event] = dspy.OutputField()
- assert dspy.BaseType.extract_custom_type_from_annotation(ExtractEvents.output_fields["events"].annotation) == [
- Event
- ]
+ assert dspy.Type.extract_custom_type_from_annotation(ExtractEvents.output_fields["events"].annotation) == [Event]
def test_extract_custom_type_from_annotation_with_nested_type():
- class Event(dspy.BaseType):
+ class Event(dspy.Type):
event_name: str
start_date_time: str
end_date_time: str | None
location: str | None
- class EventIdentifier(dspy.BaseType):
+ class EventIdentifier(dspy.Type):
model_config = pydantic.ConfigDict(frozen=True) # Make it hashable
event_id: str
event_name: str
@@ -48,7 +45,7 @@ def test_extract_custom_type_from_annotation_with_nested_type():
email: str = dspy.InputField()
events: list[dict[EventIdentifier, Event]] = dspy.OutputField()
- assert dspy.BaseType.extract_custom_type_from_annotation(ExtractEvents.output_fields["events"].annotation) == [
+ assert dspy.Type.extract_custom_type_from_annotation(ExtractEvents.output_fields["events"].annotation) == [
EventIdentifier,
Event,
]