cohere-haystack 9.4.1__tar.gz → 10.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/CHANGELOG.md +37 -12
  2. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/PKG-INFO +3 -3
  3. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/pydoc/config_docusaurus.yml +0 -1
  4. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/pyproject.toml +2 -1
  5. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/src/haystack_integrations/components/embedders/cohere/document_embedder.py +26 -12
  6. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/src/haystack_integrations/components/embedders/cohere/document_image_embedder.py +26 -12
  7. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/src/haystack_integrations/components/embedders/cohere/text_embedder.py +25 -12
  8. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/src/haystack_integrations/components/generators/cohere/__init__.py +1 -2
  9. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/src/haystack_integrations/components/generators/cohere/chat/chat_generator.py +34 -15
  10. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/src/haystack_integrations/components/rankers/cohere/ranker.py +28 -8
  11. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/tests/test_chat_generator.py +118 -45
  12. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/tests/test_document_embedder.py +46 -1
  13. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/tests/test_document_image_embedder.py +51 -7
  14. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/tests/test_ranker.py +67 -40
  15. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/tests/test_text_embedder.py +47 -1
  16. cohere_haystack-9.4.1/src/haystack_integrations/components/generators/cohere/generator.py +0 -140
  17. cohere_haystack-9.4.1/tests/test_generator.py +0 -159
  18. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/.gitignore +0 -0
  19. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/LICENSE.txt +0 -0
  20. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/README.md +0 -0
  21. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/examples/cohere_embedding.py +0 -0
  22. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/examples/cohere_generation.py +0 -0
  23. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/examples/cohere_ranker.py +0 -0
  24. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/src/haystack_integrations/components/embedders/cohere/__init__.py +0 -0
  25. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/src/haystack_integrations/components/embedders/cohere/embedding_types.py +0 -0
  26. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/src/haystack_integrations/components/embedders/cohere/utils.py +0 -0
  27. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/src/haystack_integrations/components/embedders/py.typed +0 -0
  28. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/src/haystack_integrations/components/generators/cohere/chat/__init__.py +0 -0
  29. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/src/haystack_integrations/components/generators/py.typed +0 -0
  30. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/src/haystack_integrations/components/rankers/cohere/__init__.py +0 -0
  31. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/src/haystack_integrations/components/rankers/py.typed +0 -0
  32. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/tests/__init__.py +0 -0
  33. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/tests/conftest.py +0 -0
  34. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/tests/test_chat_generator_async.py +0 -0
  35. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/tests/test_chat_generator_chunks.py +0 -0
  36. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/tests/test_files/apple.jpg +0 -0
  37. {cohere_haystack-9.4.1 → cohere_haystack-10.0.0}/tests/test_files/sample_pdf_1.pdf +0 -0
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## [integrations/cohere-v9.5.0] - 2026-08-11
4
+
5
+ ### 🐛 Bug Fixes
6
+
7
+ - Fix new issues raised by ruff 0.16.0 (#3670)
8
+ - CohereRanker reordered the top_k fallback to happen after validation (#3747)
9
+
10
+
11
+ ## [integrations/cohere-v9.4.1] - 2026-07-22
12
+
13
+
3
14
  ## [integrations/cohere-v9.4.0] - 2026-07-22
4
15
 
5
16
  ### 🐛 Bug Fixes
@@ -108,7 +119,7 @@
108
119
  - [**breaking**] Cohere - drop Python 3.9 and use X|Y typing (#2696)
109
120
 
110
121
 
111
- ## [integrations/cohere-v6.3.0] - 2025-11-26
122
+ ## [integrations/cohere-v6.2.0] - 2025-11-26
112
123
 
113
124
  ### ⚙️ CI
114
125
 
@@ -317,8 +328,6 @@
317
328
 
318
329
  ### 🚀 Features
319
330
 
320
- - Update Anthropic/Cohere for tools use (#790)
321
- - Update Cohere default LLMs, add examples and update unit tests (#838)
322
331
  - Cohere LLM - adjust token counting meta to match OpenAI format (#1086)
323
332
 
324
333
  ### 🐛 Bug Fixes
@@ -329,10 +338,6 @@
329
338
 
330
339
  - Do not retry tests in `hatch run test` command (#954)
331
340
 
332
- ### ⚙️ CI
333
-
334
- - Retry tests to reduce flakyness (#836)
335
-
336
341
  ### 🧹 Chores
337
342
 
338
343
  - Update ruff invocation to include check parameter (#853)
@@ -343,6 +348,24 @@
343
348
  - Update CohereChatGenerator docstrings (#958)
344
349
  - Update CohereGenerator docstrings (#960)
345
350
 
351
+ ## [integrations/cohere-v1.1.3] - 2024-06-21
352
+
353
+ ### 🚀 Features
354
+
355
+ - Update Cohere default LLMs, add examples and update unit tests (#838)
356
+
357
+ ### ⚙️ CI
358
+
359
+ - Retry tests to reduce flakyness (#836)
360
+
361
+
362
+ ## [integrations/cohere-v1.1.2] - 2024-06-18
363
+
364
+ ### 🚀 Features
365
+
366
+ - Update Anthropic/Cohere for tools use (#790)
367
+
368
+
346
369
  ## [integrations/cohere-v1.1.1] - 2024-06-12
347
370
 
348
371
  ### 🌀 Miscellaneous
@@ -415,11 +438,6 @@
415
438
 
416
439
  ## [integrations/cohere-v0.4.0] - 2024-02-12
417
440
 
418
- ### 🐛 Bug Fixes
419
-
420
- - Fix Cohere tests (#337)
421
- - Cohere inconsistent embeddings and documents lengths (#284)
422
-
423
441
  ### 🚜 Refactor
424
442
 
425
443
  - [**breaking**] Use `Secret` for API keys in Cohere components (#386)
@@ -428,6 +446,13 @@
428
446
 
429
447
  - Fix failing `TestCohereChatGenerator.test_from_dict_fail_wo_env_var` test (#393)
430
448
 
449
+ ## [integrations/cohere-v0.3.1] - 2024-02-08
450
+
451
+ ### 🐛 Bug Fixes
452
+
453
+ - Fix Cohere tests (#337)
454
+ - Cohere inconsistent embeddings and documents lengths (#284)
455
+
431
456
  ### 🌀 Miscellaneous
432
457
 
433
458
  - Cohere: generate api docs (#321)
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: cohere-haystack
3
- Version: 9.4.1
3
+ Version: 10.0.0
4
4
  Project-URL: Documentation, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/cohere#readme
5
5
  Project-URL: Issues, https://github.com/deepset-ai/haystack-core-integrations/issues
6
6
  Project-URL: Source, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/cohere
@@ -19,7 +19,7 @@ Classifier: Programming Language :: Python :: Implementation :: CPython
19
19
  Classifier: Programming Language :: Python :: Implementation :: PyPy
20
20
  Requires-Python: >=3.10
21
21
  Requires-Dist: cohere>=5.17.0
22
- Requires-Dist: haystack-ai>=2.30.0
22
+ Requires-Dist: haystack-ai>=3.0.0
23
23
  Requires-Dist: pydantic>=2.12.0
24
24
  Description-Content-Type: text/markdown
25
25
 
@@ -4,7 +4,6 @@ loaders:
4
4
  - haystack_integrations.components.embedders.cohere.document_image_embedder
5
5
  - haystack_integrations.components.embedders.cohere.text_embedder
6
6
  - haystack_integrations.components.embedders.cohere.utils
7
- - haystack_integrations.components.generators.cohere.generator
8
7
  - haystack_integrations.components.generators.cohere.chat.chat_generator
9
8
  - haystack_integrations.components.rankers.cohere.ranker
10
9
  search_path: [../src]
@@ -23,7 +23,7 @@ classifiers = [
23
23
  "Programming Language :: Python :: Implementation :: CPython",
24
24
  "Programming Language :: Python :: Implementation :: PyPy",
25
25
  ]
26
- dependencies = ["haystack-ai>=2.30.0", "cohere>=5.17.0", "pydantic>=2.12.0"]
26
+ dependencies = ["haystack-ai>=3.0.0", "cohere>=5.17.0", "pydantic>=2.12.0"]
27
27
 
28
28
  [project.urls]
29
29
  Documentation = "https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/cohere#readme"
@@ -131,6 +131,7 @@ ignore = [
131
131
  "PLR0912",
132
132
  "PLR0913",
133
133
  "PLR0915",
134
+ "PLR0917",
134
135
  # Misc
135
136
  "B008",
136
137
  "S101",
@@ -95,18 +95,28 @@ class CohereDocumentEmbedder:
95
95
  self.embedding_separator = embedding_separator
96
96
  self.embedding_type = embedding_type or EmbeddingTypes.FLOAT
97
97
 
98
- self._client = ClientV2(
99
- api_key=self.api_key.resolve_value(),
100
- base_url=self.api_base_url,
101
- timeout=self.timeout,
102
- client_name="haystack",
103
- )
104
- self._async_client = AsyncClientV2(
105
- api_key=self.api_key.resolve_value(),
106
- base_url=self.api_base_url,
107
- timeout=self.timeout,
108
- client_name="haystack",
109
- )
98
+ self._client: ClientV2 | None = None
99
+ self._async_client: AsyncClientV2 | None = None
100
+
101
+ def warm_up(self) -> None:
102
+ """Create the synchronous Cohere client."""
103
+ if self._client is None:
104
+ self._client = ClientV2(
105
+ api_key=self.api_key.resolve_value(),
106
+ base_url=self.api_base_url,
107
+ timeout=self.timeout,
108
+ client_name="haystack",
109
+ )
110
+
111
+ async def warm_up_async(self) -> None:
112
+ """Create the asynchronous Cohere client."""
113
+ if self._async_client is None:
114
+ self._async_client = AsyncClientV2(
115
+ api_key=self.api_key.resolve_value(),
116
+ base_url=self.api_base_url,
117
+ timeout=self.timeout,
118
+ client_name="haystack",
119
+ )
110
120
 
111
121
  def to_dict(self) -> dict[str, Any]:
112
122
  """
@@ -191,6 +201,8 @@ class CohereDocumentEmbedder:
191
201
  if not documents:
192
202
  return {"documents": [], "meta": {}}
193
203
 
204
+ self.warm_up()
205
+ assert self._client is not None
194
206
  texts_to_embed = self._prepare_texts_to_embed(documents)
195
207
 
196
208
  all_embeddings, metadata = get_response(
@@ -227,6 +239,8 @@ class CohereDocumentEmbedder:
227
239
  if not documents:
228
240
  return {"documents": [], "meta": {}}
229
241
 
242
+ await self.warm_up_async()
243
+ assert self._async_client is not None
230
244
  texts_to_embed = self._prepare_texts_to_embed(documents)
231
245
 
232
246
  all_embeddings, metadata = await get_async_response(
@@ -128,18 +128,28 @@ class CohereDocumentImageEmbedder:
128
128
  self._api_base_url = api_base_url
129
129
  self._timeout = timeout
130
130
 
131
- self._client = ClientV2(
132
- api_key=self._api_key.resolve_value(),
133
- base_url=self._api_base_url,
134
- timeout=self._timeout,
135
- client_name="haystack",
136
- )
137
- self._async_client = AsyncClientV2(
138
- api_key=self._api_key.resolve_value(),
139
- base_url=self._api_base_url,
140
- timeout=self._timeout,
141
- client_name="haystack",
142
- )
131
+ self._client: ClientV2 | None = None
132
+ self._async_client: AsyncClientV2 | None = None
133
+
134
+ def warm_up(self) -> None:
135
+ """Create the synchronous Cohere client."""
136
+ if self._client is None:
137
+ self._client = ClientV2(
138
+ api_key=self._api_key.resolve_value(),
139
+ base_url=self._api_base_url,
140
+ timeout=self._timeout,
141
+ client_name="haystack",
142
+ )
143
+
144
+ async def warm_up_async(self) -> None:
145
+ """Create the asynchronous Cohere client."""
146
+ if self._async_client is None:
147
+ self._async_client = AsyncClientV2(
148
+ api_key=self._api_key.resolve_value(),
149
+ base_url=self._api_base_url,
150
+ timeout=self._timeout,
151
+ client_name="haystack",
152
+ )
143
153
 
144
154
  def to_dict(self) -> dict[str, Any]:
145
155
  """
@@ -264,6 +274,8 @@ class CohereDocumentImageEmbedder:
264
274
  - `documents`: Documents with embeddings.
265
275
  """
266
276
 
277
+ self.warm_up()
278
+ assert self._client is not None
267
279
  images_to_embed = self._extract_images_to_embed(documents)
268
280
 
269
281
  embeddings = []
@@ -312,6 +324,8 @@ class CohereDocumentImageEmbedder:
312
324
  - `documents`: Documents with embeddings.
313
325
  """
314
326
 
327
+ await self.warm_up_async()
328
+ assert self._async_client is not None
315
329
  images_to_embed = self._extract_images_to_embed(documents)
316
330
 
317
331
  embeddings = []
@@ -78,19 +78,28 @@ class CohereTextEmbedder:
78
78
  self.timeout = timeout
79
79
  self.embedding_type = embedding_type or EmbeddingTypes.FLOAT
80
80
 
81
- self._client = ClientV2(
82
- api_key=self.api_key.resolve_value(),
83
- base_url=self.api_base_url,
84
- timeout=self.timeout,
85
- client_name="haystack",
86
- )
81
+ self._client: ClientV2 | None = None
82
+ self._async_client: AsyncClientV2 | None = None
83
+
84
+ def warm_up(self) -> None:
85
+ """Create the synchronous Cohere client."""
86
+ if self._client is None:
87
+ self._client = ClientV2(
88
+ api_key=self.api_key.resolve_value(),
89
+ base_url=self.api_base_url,
90
+ timeout=self.timeout,
91
+ client_name="haystack",
92
+ )
87
93
 
88
- self._async_client = AsyncClientV2(
89
- api_key=self.api_key.resolve_value(),
90
- base_url=self.api_base_url,
91
- timeout=self.timeout,
92
- client_name="haystack",
93
- )
94
+ async def warm_up_async(self) -> None:
95
+ """Create the asynchronous Cohere client."""
96
+ if self._async_client is None:
97
+ self._async_client = AsyncClientV2(
98
+ api_key=self.api_key.resolve_value(),
99
+ base_url=self.api_base_url,
100
+ timeout=self.timeout,
101
+ client_name="haystack",
102
+ )
94
103
 
95
104
  def _validate_input(self, text: str) -> None:
96
105
  if not isinstance(text, str):
@@ -155,6 +164,8 @@ class CohereTextEmbedder:
155
164
  If the input is not a string.
156
165
  """
157
166
  self._validate_input(text=text)
167
+ self.warm_up()
168
+ assert self._client is not None
158
169
 
159
170
  embedding, metadata = get_response(
160
171
  cohere_client=self._client,
@@ -187,6 +198,8 @@ class CohereTextEmbedder:
187
198
  If the input is not a string.
188
199
  """
189
200
  self._validate_input(text=text)
201
+ await self.warm_up_async()
202
+ assert self._async_client is not None
190
203
 
191
204
  embedding, metadata = await get_async_response(
192
205
  cohere_async_client=self._async_client,
@@ -2,6 +2,5 @@
2
2
  #
3
3
  # SPDX-License-Identifier: Apache-2.0
4
4
  from .chat.chat_generator import CohereChatGenerator
5
- from .generator import CohereGenerator
6
5
 
7
- __all__ = ["CohereChatGenerator", "CohereGenerator"]
6
+ __all__ = ["CohereChatGenerator"]
@@ -579,22 +579,35 @@ class CohereChatGenerator:
579
579
  self.timeout = timeout
580
580
  self.max_retries = max_retries
581
581
 
582
+ self.client: ClientV2 | None = None
583
+ self.async_client: AsyncClientV2 | None = None
584
+
585
+ def _client_kwargs(self, *, async_client: bool) -> dict[str, Any]:
586
+ """Build the keyword arguments used to create a Cohere client."""
582
587
  client_kwargs: dict[str, Any] = {
583
588
  "api_key": self.api_key.resolve_value(),
584
589
  "base_url": self.api_base_url,
585
590
  "client_name": "haystack",
586
591
  }
587
- if timeout is not None:
588
- client_kwargs["timeout"] = timeout
589
-
590
- sync_kwargs = {**client_kwargs}
591
- async_kwargs = {**client_kwargs}
592
- if max_retries is not None:
593
- sync_kwargs["httpx_client"] = HTTPXClient(transport=HTTPTransport(retries=max_retries))
594
- async_kwargs["httpx_client"] = AsyncHTTPXClient(transport=AsyncHTTPTransport(retries=max_retries))
595
-
596
- self.client = ClientV2(**sync_kwargs)
597
- self.async_client = AsyncClientV2(**async_kwargs)
592
+ if self.timeout is not None:
593
+ client_kwargs["timeout"] = self.timeout
594
+
595
+ if self.max_retries is not None:
596
+ if async_client:
597
+ client_kwargs["httpx_client"] = AsyncHTTPXClient(transport=AsyncHTTPTransport(retries=self.max_retries))
598
+ else:
599
+ client_kwargs["httpx_client"] = HTTPXClient(transport=HTTPTransport(retries=self.max_retries))
600
+ return client_kwargs
601
+
602
+ def warm_up(self) -> None:
603
+ """Create the synchronous Cohere client."""
604
+ if self.client is None:
605
+ self.client = ClientV2(**self._client_kwargs(async_client=False))
606
+
607
+ async def warm_up_async(self) -> None:
608
+ """Create the asynchronous Cohere client."""
609
+ if self.async_client is None:
610
+ self.async_client = AsyncClientV2(**self._client_kwargs(async_client=True))
598
611
 
599
612
  def _get_telemetry_data(self) -> dict[str, Any]:
600
613
  """
@@ -652,8 +665,9 @@ class CohereChatGenerator:
652
665
 
653
666
  :param messages: list of `ChatMessage` instances representing the input messages.
654
667
  If a string is provided, it is converted to a list containing a ChatMessage with user role.
655
- :param generation_kwargs: additional keyword arguments for chat generation. These parameters will
656
- potentially override the parameters passed in the __init__ method.
668
+ :param generation_kwargs: additional keyword arguments for chat generation. These are merged per key
669
+ with the `generation_kwargs` passed at initialization: keys provided here take precedence, keys set
670
+ only at initialization are kept.
657
671
  For more details on the parameters supported by the Cohere API, refer to the
658
672
  Cohere [documentation](https://docs.cohere.com/reference/chat).
659
673
  :param tools: A list of Tool and/or Toolset objects, or a single Toolset for which the model can prepare calls.
@@ -664,6 +678,8 @@ class CohereChatGenerator:
664
678
  :returns: A dictionary with the following keys:
665
679
  - `replies`: a list of `ChatMessage` instances representing the generated responses.
666
680
  """
681
+ self.warm_up()
682
+ assert self.client is not None
667
683
  messages = _normalize_messages(messages)
668
684
 
669
685
  # update generation kwargs by merging with the generation kwargs passed to the run method
@@ -720,8 +736,9 @@ class CohereChatGenerator:
720
736
 
721
737
  :param messages: list of `ChatMessage` instances representing the input messages.
722
738
  If a string is provided, it is converted to a list containing a ChatMessage with user role.
723
- :param generation_kwargs: additional keyword arguments for chat generation. These parameters will
724
- potentially override the parameters passed in the __init__ method.
739
+ :param generation_kwargs: additional keyword arguments for chat generation. These are merged per key
740
+ with the `generation_kwargs` passed at initialization: keys provided here take precedence, keys set
741
+ only at initialization are kept.
725
742
  For more details on the parameters supported by the Cohere API, refer to the
726
743
  Cohere [documentation](https://docs.cohere.com/reference/chat).
727
744
  :param tools: A list of Tool and/or Toolset objects, or a single Toolset for which the model can prepare calls.
@@ -730,6 +747,8 @@ class CohereChatGenerator:
730
747
  :returns: A dictionary with the following keys:
731
748
  - `replies`: a list of `ChatMessage` instances representing the generated responses.
732
749
  """
750
+ await self.warm_up_async()
751
+ assert self.async_client is not None
733
752
  messages = _normalize_messages(messages)
734
753
 
735
754
  # update generation kwargs by merging with the generation kwargs passed to the run method
@@ -54,7 +54,13 @@ class CohereRanker:
54
54
  :param meta_data_separator: Separator used to concatenate the meta fields
55
55
  to the Document content.
56
56
  :param max_tokens_per_doc: The maximum number of tokens to embed for each document defaults to 4096.
57
+
58
+ :raises ValueError: If `top_k` is not > 0.
57
59
  """
60
+ if top_k <= 0:
61
+ msg = f"top_k must be > 0, but got {top_k}"
62
+ raise ValueError(msg)
63
+
58
64
  self.model_name = model
59
65
  self.api_key = api_key
60
66
  self.api_base_url = api_base_url
@@ -63,12 +69,22 @@ class CohereRanker:
63
69
  self.meta_data_separator = meta_data_separator
64
70
  self.max_tokens_per_doc = max_tokens_per_doc
65
71
 
66
- self._cohere_client = ClientV2(
67
- api_key=self.api_key.resolve_value(), base_url=self.api_base_url, client_name="haystack"
68
- )
69
- self._cohere_async_client = AsyncClientV2(
70
- api_key=self.api_key.resolve_value(), base_url=self.api_base_url, client_name="haystack"
71
- )
72
+ self._cohere_client: ClientV2 | None = None
73
+ self._cohere_async_client: AsyncClientV2 | None = None
74
+
75
+ def warm_up(self) -> None:
76
+ """Create the synchronous Cohere client."""
77
+ if self._cohere_client is None:
78
+ self._cohere_client = ClientV2(
79
+ api_key=self.api_key.resolve_value(), base_url=self.api_base_url, client_name="haystack"
80
+ )
81
+
82
+ async def warm_up_async(self) -> None:
83
+ """Create the asynchronous Cohere client."""
84
+ if self._cohere_async_client is None:
85
+ self._cohere_async_client = AsyncClientV2(
86
+ api_key=self.api_key.resolve_value(), base_url=self.api_base_url, client_name="haystack"
87
+ )
72
88
 
73
89
  def to_dict(self) -> dict[str, Any]:
74
90
  """
@@ -116,10 +132,10 @@ class CohereRanker:
116
132
  :return: A tuple of (list of strings to be given as input to Cohere model, resolved top_k).
117
133
  :raises ValueError: If `top_k` is not > 0.
118
134
  """
119
- top_k = top_k or self.top_k
120
- if top_k <= 0:
135
+ if top_k is not None and top_k <= 0:
121
136
  msg = f"top_k must be > 0, but got {top_k}"
122
137
  raise ValueError(msg)
138
+ top_k = self.top_k if top_k is None else top_k
123
139
 
124
140
  concatenated_input_list = []
125
141
  for doc in documents:
@@ -166,6 +182,8 @@ class CohereRanker:
166
182
 
167
183
  :raises ValueError: If `top_k` is not > 0.
168
184
  """
185
+ self.warm_up()
186
+ assert self._cohere_client is not None
169
187
  cohere_input_docs, top_k = self._prepare_cohere_input_docs(documents, top_k)
170
188
 
171
189
  response = self._cohere_client.rerank(
@@ -199,6 +217,8 @@ class CohereRanker:
199
217
 
200
218
  :raises ValueError: If `top_k` is not > 0.
201
219
  """
220
+ await self.warm_up_async()
221
+ assert self._cohere_async_client is not None
202
222
  cohere_input_docs, top_k = self._prepare_cohere_input_docs(documents, top_k)
203
223
 
204
224
  response = await self._cohere_async_client.rerank(