Skip to content

Commit 86c32ca

Browse files
anakin87davidsbatista
authored andcommitted
chore!: ollama - drop Python 3.9 and use X|Y typing (#2719)
1 parent dda61f6 commit 86c32ca

5 files changed

Lines changed: 47 additions & 52 deletions

File tree

integrations/ollama/pyproject.toml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ name = "ollama-haystack"
77
dynamic = ["version"]
88
description = 'An integration between the Ollama LLM framework and Haystack'
99
readme = "README.md"
10-
requires-python = ">=3.9"
10+
requires-python = ">=3.10"
1111
license = "Apache-2.0"
1212
keywords = []
1313
authors = [
@@ -19,15 +19,14 @@ classifiers = [
1919
"License :: OSI Approved :: Apache Software License",
2020
"Development Status :: 4 - Beta",
2121
"Programming Language :: Python",
22-
"Programming Language :: Python :: 3.9",
2322
"Programming Language :: Python :: 3.10",
2423
"Programming Language :: Python :: 3.11",
2524
"Programming Language :: Python :: 3.12",
2625
"Programming Language :: Python :: 3.13",
2726
"Programming Language :: Python :: Implementation :: CPython",
2827
"Programming Language :: Python :: Implementation :: PyPy",
2928
]
30-
dependencies = ["haystack-ai>=2.19.0", "ollama>=0.5.0", "pydantic"]
29+
dependencies = ["haystack-ai>=2.22.0", "ollama>=0.5.0", "pydantic"]
3130

3231
[project.urls]
3332
Documentation = "https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/ollama#readme"
@@ -88,7 +87,6 @@ known-first-party = ["haystack_integrations"]
8887

8988

9089
[tool.ruff]
91-
target-version = "py39"
9290
line-length = 120
9391

9492
[tool.ruff.lint]
@@ -132,10 +130,6 @@ ignore = [
132130
"PLR0913",
133131
"PLR0915",
134132
]
135-
unfixable = [
136-
# Don't touch unused imports
137-
"F401",
138-
]
139133

140134
[tool.ruff.lint.flake8-tidy-imports]
141135
ban-relative-imports = "parents"

integrations/ollama/src/haystack_integrations/components/embedders/ollama/document_embedder.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import asyncio
2-
from typing import Any, Optional, Union
2+
from typing import Any
33

44
from haystack import Document, component
55
from tqdm import tqdm
@@ -30,13 +30,13 @@ def __init__(
3030
self,
3131
model: str = "nomic-embed-text",
3232
url: str = "http://localhost:11434",
33-
generation_kwargs: Optional[dict[str, Any]] = None,
33+
generation_kwargs: dict[str, Any] | None = None,
3434
timeout: int = 120,
35-
keep_alive: Optional[Union[float, str]] = None,
35+
keep_alive: float | str | None = None,
3636
prefix: str = "",
3737
suffix: str = "",
3838
progress_bar: bool = True,
39-
meta_fields_to_embed: Optional[list[str]] = None,
39+
meta_fields_to_embed: list[str] | None = None,
4040
embedding_separator: str = "\n",
4141
batch_size: int = 32,
4242
):
@@ -123,7 +123,7 @@ def _prepare_texts_to_embed(self, documents: list[Document]) -> list[str]:
123123
return texts_to_embed
124124

125125
def _embed_batch(
126-
self, texts_to_embed: list[str], batch_size: int, generation_kwargs: Optional[dict[str, Any]] = None
126+
self, texts_to_embed: list[str], batch_size: int, generation_kwargs: dict[str, Any] | None = None
127127
) -> list[list[float]]:
128128
"""
129129
Internal method to embed a batch of texts.
@@ -146,7 +146,7 @@ def _embed_batch(
146146
return all_embeddings
147147

148148
async def _embed_batch_async(
149-
self, texts_to_embed: list[str], batch_size: int, generation_kwargs: Optional[dict[str, Any]] = None
149+
self, texts_to_embed: list[str], batch_size: int, generation_kwargs: dict[str, Any] | None = None
150150
) -> list[list[float]]:
151151
"""
152152
Internal method to embed a batch of texts asynchronously.
@@ -177,8 +177,8 @@ async def _embed_batch_async(
177177

178178
@component.output_types(documents=list[Document], meta=dict[str, Any])
179179
def run(
180-
self, documents: list[Document], generation_kwargs: Optional[dict[str, Any]] = None
181-
) -> dict[str, Union[list[Document], dict[str, Any]]]:
180+
self, documents: list[Document], generation_kwargs: dict[str, Any] | None = None
181+
) -> dict[str, list[Document] | dict[str, Any]]:
182182
"""
183183
Runs an Ollama Model to compute embeddings of the provided documents.
184184
@@ -205,15 +205,15 @@ def run(
205205
texts_to_embed=texts_to_embed, batch_size=self.batch_size, generation_kwargs=generation_kwargs
206206
)
207207

208-
for doc, emb in zip(documents, embeddings):
208+
for doc, emb in zip(documents, embeddings, strict=True):
209209
doc.embedding = emb
210210

211211
return {"documents": documents, "meta": {"model": self.model}}
212212

213213
@component.output_types(documents=list[Document], meta=dict[str, Any])
214214
async def run_async(
215-
self, documents: list[Document], generation_kwargs: Optional[dict[str, Any]] = None
216-
) -> dict[str, Union[list[Document], dict[str, Any]]]:
215+
self, documents: list[Document], generation_kwargs: dict[str, Any] | None = None
216+
) -> dict[str, list[Document] | dict[str, Any]]:
217217
"""
218218
Asynchronously run an Ollama Model to compute embeddings of the provided documents.
219219
@@ -241,7 +241,7 @@ async def run_async(
241241
texts_to_embed=texts_to_embed, batch_size=self.batch_size, generation_kwargs=generation_kwargs
242242
)
243243

244-
for doc, emb in zip(documents, embeddings):
244+
for doc, emb in zip(documents, embeddings, strict=True):
245245
doc.embedding = emb
246246

247247
return {"documents": documents, "meta": {"model": self.model}}

integrations/ollama/src/haystack_integrations/components/embedders/ollama/text_embedder.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Optional, Union
1+
from typing import Any
22

33
from haystack import component
44

@@ -25,9 +25,9 @@ def __init__(
2525
self,
2626
model: str = "nomic-embed-text",
2727
url: str = "http://localhost:11434",
28-
generation_kwargs: Optional[dict[str, Any]] = None,
28+
generation_kwargs: dict[str, Any] | None = None,
2929
timeout: int = 120,
30-
keep_alive: Optional[Union[float, str]] = None,
30+
keep_alive: float | str | None = None,
3131
):
3232
"""
3333
:param model:
@@ -60,8 +60,8 @@ def __init__(
6060

6161
@component.output_types(embedding=list[float], meta=dict[str, Any])
6262
def run(
63-
self, text: str, generation_kwargs: Optional[dict[str, Any]] = None
64-
) -> dict[str, Union[list[float], dict[str, Any]]]:
63+
self, text: str, generation_kwargs: dict[str, Any] | None = None
64+
) -> dict[str, list[float] | dict[str, Any]]:
6565
"""
6666
Runs an Ollama Model to compute embeddings of the provided text.
6767
@@ -87,8 +87,8 @@ def run(
8787

8888
@component.output_types(embedding=list[float], meta=dict[str, Any])
8989
async def run_async(
90-
self, text: str, generation_kwargs: Optional[dict[str, Any]] = None
91-
) -> dict[str, Union[list[float], dict[str, Any]]]:
90+
self, text: str, generation_kwargs: dict[str, Any] | None = None
91+
) -> dict[str, list[float] | dict[str, Any]]:
9292
"""
9393
Asynchronously run an Ollama Model to compute embeddings of the provided text.
9494

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
2-
from collections.abc import AsyncIterator, Iterator
3-
from typing import Any, Callable, Literal, Optional, Union
2+
from collections.abc import AsyncIterator, Callable, Iterator
3+
from typing import Any, Literal
44

55
from haystack import component, default_from_dict, default_to_dict
66
from haystack.dataclasses import (
@@ -212,13 +212,13 @@ def __init__(
212212
self,
213213
model: str = "qwen3:0.6b",
214214
url: str = "http://localhost:11434",
215-
generation_kwargs: Optional[dict[str, Any]] = None,
215+
generation_kwargs: dict[str, Any] | None = None,
216216
timeout: int = 120,
217-
keep_alive: Optional[Union[float, str]] = None,
218-
streaming_callback: Optional[Callable[[StreamingChunk], None]] = None,
219-
tools: Optional[ToolsType] = None,
220-
response_format: Optional[Union[None, Literal["json"], JsonSchemaValue]] = None,
221-
think: Union[bool, Literal["low", "medium", "high"]] = False,
217+
keep_alive: float | str | None = None,
218+
streaming_callback: Callable[[StreamingChunk], None] | None = None,
219+
tools: ToolsType | None = None,
220+
response_format: None | Literal["json"] | JsonSchemaValue | None = None,
221+
think: bool | Literal["low", "medium", "high"] = False,
222222
):
223223
"""
224224
:param model:
@@ -315,7 +315,7 @@ def from_dict(cls, data: dict[str, Any]) -> "OllamaChatGenerator":
315315
def _handle_streaming_response(
316316
self,
317317
response_iter: Iterator[ChatResponse],
318-
callback: Optional[SyncStreamingCallbackT],
318+
callback: SyncStreamingCallbackT | None,
319319
) -> dict[str, list[ChatMessage]]:
320320
"""
321321
Merge an Ollama streaming response into a single ChatMessage, preserving
@@ -399,7 +399,7 @@ def _handle_streaming_response(
399399
async def _handle_streaming_response_async(
400400
self,
401401
response_iter: AsyncIterator[ChatResponse],
402-
callback: Optional[AsyncStreamingCallbackT],
402+
callback: AsyncStreamingCallbackT | None,
403403
) -> dict[str, list[ChatMessage]]:
404404
"""
405405
Merge an Ollama async streaming response into a single ChatMessage, preserving
@@ -471,10 +471,10 @@ async def _handle_streaming_response_async(
471471
def run(
472472
self,
473473
messages: list[ChatMessage],
474-
generation_kwargs: Optional[dict[str, Any]] = None,
475-
tools: Optional[ToolsType] = None,
474+
generation_kwargs: dict[str, Any] | None = None,
475+
tools: ToolsType | None = None,
476476
*,
477-
streaming_callback: Optional[StreamingCallbackT] = None,
477+
streaming_callback: StreamingCallbackT | None = None,
478478
) -> dict[str, list[ChatMessage]]:
479479
"""
480480
Runs an Ollama Model on a given chat history.
@@ -537,10 +537,10 @@ def run(
537537
async def run_async(
538538
self,
539539
messages: list[ChatMessage],
540-
generation_kwargs: Optional[dict[str, Any]] = None,
541-
tools: Optional[ToolsType] = None,
540+
generation_kwargs: dict[str, Any] | None = None,
541+
tools: ToolsType | None = None,
542542
*,
543-
streaming_callback: Optional[StreamingCallbackT] = None,
543+
streaming_callback: StreamingCallbackT | None = None,
544544
) -> dict[str, list[ChatMessage]]:
545545
"""
546546
Async version of run. Runs an Ollama Model on a given chat history.

integrations/ollama/src/haystack_integrations/components/generators/ollama/generator.py

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

34
from haystack import component, default_from_dict, default_to_dict
45
from haystack.dataclasses import StreamingChunk
@@ -98,13 +99,13 @@ def __init__(
9899
self,
99100
model: str = "orca-mini",
100101
url: str = "http://localhost:11434",
101-
generation_kwargs: Optional[dict[str, Any]] = None,
102-
system_prompt: Optional[str] = None,
103-
template: Optional[str] = None,
102+
generation_kwargs: dict[str, Any] | None = None,
103+
system_prompt: str | None = None,
104+
template: str | None = None,
104105
raw: bool = False,
105106
timeout: int = 120,
106-
keep_alive: Optional[Union[float, str]] = None,
107-
streaming_callback: Optional[Callable[[StreamingChunk], None]] = None,
107+
keep_alive: float | str | None = None,
108+
streaming_callback: Callable[[StreamingChunk], None] | None = None,
108109
):
109110
"""
110111
:param model:
@@ -208,7 +209,7 @@ def _convert_to_streaming_response(self, chunks: list[StreamingChunk]) -> dict[s
208209
return {"replies": replies, "meta": [meta]}
209210

210211
def _handle_streaming_response(
211-
self, response: Any, streaming_callback: Optional[Callable[[StreamingChunk], None]]
212+
self, response: Any, streaming_callback: Callable[[StreamingChunk], None] | None
212213
) -> list[StreamingChunk]:
213214
"""
214215
Handles Streaming response cases
@@ -236,9 +237,9 @@ def _build_chunk(self, chunk_response: Any) -> StreamingChunk:
236237
def run(
237238
self,
238239
prompt: str,
239-
generation_kwargs: Optional[dict[str, Any]] = None,
240+
generation_kwargs: dict[str, Any] | None = None,
240241
*,
241-
streaming_callback: Optional[Callable[[StreamingChunk], None]] = None,
242+
streaming_callback: Callable[[StreamingChunk], None] | None = None,
242243
) -> dict[str, list[Any]]:
243244
"""
244245
Runs an Ollama Model on the given prompt.

0 commit comments

Comments
 (0)