Skip to content

Commit a03bce3

Browse files
committed
Applied formatting
1 parent 27fe361 commit a03bce3

4 files changed

Lines changed: 34 additions & 24 deletions

File tree

integrations/dspy/examples/chat_generator_example.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import dspy
22
from haystack import Pipeline
33
from haystack.dataclasses import ChatMessage
4+
45
from haystack_integrations.components.generators.dspy import DSPyChatGenerator
56

67

@@ -52,4 +53,4 @@ def string_signature_example():
5253

5354
if __name__ == "__main__":
5455
basic_qa_example()
55-
string_signature_example()
56+
string_signature_example()

integrations/dspy/src/haystack_integrations/components/generators/dspy/chat/chat_generator.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Any, Callable, Dict, List, Optional, Type, Union
1+
from collections.abc import Callable
2+
from typing import Any
23

34
import dspy
45
from haystack import component, default_from_dict, default_to_dict
@@ -75,14 +76,14 @@ class QASignature(dspy.Signature):
7576

7677
def __init__(
7778
self,
78-
signature: Union[str, Type[dspy.Signature]],
79+
signature: str | type[dspy.Signature],
7980
model: str = "openai/gpt-5-mini",
8081
api_key: Secret = Secret.from_env_var("OPENAI_API_KEY"),
8182
module_type: str = "ChainOfThought",
8283
output_field: str = "answer",
83-
generation_kwargs: Optional[Dict[str, Any]] = None,
84-
input_mapping: Optional[Dict[str, str]] = None,
85-
streaming_callback: Optional[Callable] = None,
84+
generation_kwargs: dict[str, Any] | None = None,
85+
input_mapping: dict[str, str] | None = None,
86+
streaming_callback: Callable | None = None,
8687
):
8788
"""
8889
Initialize the DSPyChatGenerator.
@@ -119,7 +120,7 @@ def __init__(
119120
module_class = get_dspy_module_class(self.module_type)
120121
self._module = module_class(self.signature)
121122

122-
def _build_dspy_inputs(self, prompt: str, **kwargs) -> Dict[str, Any]:
123+
def _build_dspy_inputs(self, prompt: str, **kwargs) -> dict[str, Any]:
123124
"""Build the input dict for the DSPy module call."""
124125
if self.input_mapping:
125126
dspy_inputs = {}
@@ -139,15 +140,15 @@ def _build_dspy_inputs(self, prompt: str, **kwargs) -> Dict[str, Any]:
139140

140141
return dspy_inputs
141142

142-
def _get_input_field_names(self) -> List[str]:
143+
def _get_input_field_names(self) -> list[str]:
143144
"""Get input field names from the signature."""
144145
if isinstance(self.signature, str):
145146
input_part = self.signature.split("->")[0].strip()
146147
return [f.strip() for f in input_part.split(",")]
147148
return list(self.signature.input_fields.keys())
148149

149150
@staticmethod
150-
def _extract_last_user_message(messages: List[ChatMessage]) -> str:
151+
def _extract_last_user_message(messages: list[ChatMessage]) -> str:
151152
"""Extract the text of the last user message from a list of chat messages."""
152153
for msg in reversed(messages):
153154
if msg.role == ChatRole.USER:
@@ -162,9 +163,9 @@ def _signature_to_string(self) -> str:
162163
output_names = list(self.signature.output_fields.keys())
163164
return ", ".join(input_names) + " -> " + ", ".join(output_names)
164165

165-
def to_dict(self) -> Dict[str, Any]:
166+
def to_dict(self) -> dict[str, Any]:
166167
"""Serialize this component to a dictionary."""
167-
kwargs: Dict[str, Any] = {
168+
kwargs: dict[str, Any] = {
168169
"signature": self._signature_to_string(),
169170
"model": self.model,
170171
"module_type": self.module_type,
@@ -179,19 +180,19 @@ def to_dict(self) -> Dict[str, Any]:
179180
return default_to_dict(self, **kwargs)
180181

181182
@classmethod
182-
def from_dict(cls, data: Dict[str, Any]) -> "DSPyChatGenerator":
183+
def from_dict(cls, data: dict[str, Any]) -> "DSPyChatGenerator":
183184
"""Deserialize a component from a dictionary."""
184185
init_params = data.get("init_parameters", {})
185186
deserialize_secrets_inplace(init_params, ["api_key"])
186187
return default_from_dict(cls, data)
187188

188-
@component.output_types(replies=List[ChatMessage])
189+
@component.output_types(replies=list[ChatMessage])
189190
def run(
190191
self,
191-
messages: List[ChatMessage],
192-
generation_kwargs: Optional[Dict[str, Any]] = None,
192+
messages: list[ChatMessage],
193+
generation_kwargs: dict[str, Any] | None = None,
193194
**kwargs,
194-
) -> Dict[str, Any]:
195+
) -> dict[str, Any]:
195196
"""
196197
Run the DSPy module on the given messages.
197198
@@ -217,13 +218,13 @@ def run(
217218
replies = [ChatMessage.from_assistant(text=output_text)]
218219
return {"replies": replies}
219220

220-
@component.output_types(replies=List[ChatMessage])
221+
@component.output_types(replies=list[ChatMessage])
221222
async def run_async(
222223
self,
223-
messages: List[ChatMessage],
224-
generation_kwargs: Optional[Dict[str, Any]] = None,
224+
messages: list[ChatMessage],
225+
generation_kwargs: dict[str, Any] | None = None,
225226
**kwargs,
226-
) -> Dict[str, Any]:
227+
) -> dict[str, Any]:
227228
"""
228229
Asynchronously run the DSPy module on the given messages.
229230

integrations/dspy/tests/test_chat_generator.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ def mock_dspy_module():
2222
with (
2323
patch("haystack_integrations.components.generators.dspy.chat.chat_generator.dspy.LM") as mock_lm_class,
2424
patch("haystack_integrations.components.generators.dspy.chat.chat_generator.dspy.configure"),
25-
patch("haystack_integrations.components.generators.dspy.chat.chat_generator.dspy.ChainOfThought") as mock_cot_class,
26-
patch("haystack_integrations.components.generators.dspy.chat.chat_generator.dspy.Predict") as mock_predict_class,
25+
patch(
26+
"haystack_integrations.components.generators.dspy.chat.chat_generator.dspy.ChainOfThought"
27+
) as mock_cot_class,
28+
patch(
29+
"haystack_integrations.components.generators.dspy.chat.chat_generator.dspy.Predict"
30+
) as mock_predict_class,
2731
patch("haystack_integrations.components.generators.dspy.chat.chat_generator.dspy.ReAct") as mock_react_class,
2832
):
2933
mock_lm = MagicMock()

integrations/dspy/tests/test_chat_generator_async.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ def mock_dspy_module():
1515
with (
1616
patch("haystack_integrations.components.generators.dspy.chat.chat_generator.dspy.LM") as mock_lm_class,
1717
patch("haystack_integrations.components.generators.dspy.chat.chat_generator.dspy.configure"),
18-
patch("haystack_integrations.components.generators.dspy.chat.chat_generator.dspy.ChainOfThought") as mock_cot_class,
19-
patch("haystack_integrations.components.generators.dspy.chat.chat_generator.dspy.Predict") as mock_predict_class,
18+
patch(
19+
"haystack_integrations.components.generators.dspy.chat.chat_generator.dspy.ChainOfThought"
20+
) as mock_cot_class,
21+
patch(
22+
"haystack_integrations.components.generators.dspy.chat.chat_generator.dspy.Predict"
23+
) as mock_predict_class,
2024
patch("haystack_integrations.components.generators.dspy.chat.chat_generator.dspy.ReAct") as mock_react_class,
2125
):
2226
mock_lm = MagicMock()

0 commit comments

Comments
 (0)