diff --git a/.github/workflows/cohere.yml b/.github/workflows/cohere.yml new file mode 100644 index 000000000..b40cd4953 --- /dev/null +++ b/.github/workflows/cohere.yml @@ -0,0 +1,56 @@ +# This workflow comes from https://github.com/ofek/hatch-mypyc +# https://github.com/ofek/hatch-mypyc/blob/5a198c0ba8660494d02716cfc9d79ce4adfb1442/.github/workflows/test.yml +name: Test / cohere + +on: + schedule: + - cron: "0 0 * * *" + pull_request: + paths: + - 'integrations/cohere/**' + - '.github/workflows/cohere.yml' + +defaults: + run: + working-directory: integrations/cohere + +concurrency: + group: cohere-${{ github.head_ref }} + cancel-in-progress: true + +env: + PYTHONUNBUFFERED: "1" + FORCE_COLOR: "1" + +jobs: + run: + name: Python ${{ matrix.python-version }} on ${{ startsWith(matrix.os, 'macos-') && 'macOS' || startsWith(matrix.os, 'windows-') && 'Windows' || 'Linux' }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + python-version: ['3.9', '3.10'] + + steps: + - name: Support longpaths + if: matrix.os == 'windows-latest' + working-directory: . + run: git config --system core.longpaths true + + - uses: actions/checkout@v4 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + + - name: Install Hatch + run: pip install --upgrade hatch + + - name: Lint + if: matrix.python-version == '3.9' && runner.os == 'Linux' + run: hatch run lint:all + + - name: Run tests + run: hatch run cov \ No newline at end of file diff --git a/README.md b/README.md index e0aae3b1f..bbd816d98 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ deepset-haystack | Package | Type | PyPi Package | Status | | ------------------------------------------------------------------------------- | ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | [chroma-haystack](integrations/chroma/) | Document Store | [![PyPI - Version](https://img.shields.io/pypi/v/chroma-haystack.svg)](https://pypi.org/project/chroma-haystack) | [![Test / chroma](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/chroma.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/chroma.yml) | +| [cohere-haystack](integrations/cohere/) | Generator | [![PyPI - Version](https://img.shields.io/pypi/v/chroma-haystack.svg)](https://pypi.org/project/cohere-haystack) | [![Test / cohere](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/cohere.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/cohere.yml) | | [elasticsearch-haystack](integrations/elasticsearch/) | Document Store | [![PyPI - Version](https://img.shields.io/pypi/v/elasticsearch-haystack.svg)](https://pypi.org/project/elasticsearch-haystack) | [![Test / elasticsearch](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/elasticsearch.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/elasticsearch.yml) | | [gradient-haystack](integrations/gradient/) | Embedder, Generator | [![PyPI - Version](https://img.shields.io/pypi/v/gradient-haystack.svg)](https://pypi.org/project/gradient-haystack) | [![Test / gradient](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/gradient.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/gradient.yml) | | [instructor-embedders-haystack](integrations/instructor-embedders/) | Embedder | [![PyPI - Version](https://img.shields.io/pypi/v/instructor-embedders-haystack.svg)](https://pypi.org/project/instructor-embedders-haystack) | [![Test / instructor-embedders](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/instructor_embedders.yml/badge.svg)](https://github.com/deepset-ai/haystack-core-integrations/actions/workflows/instructor_embedders.yml) | diff --git a/integrations/cohere/LICENSE.txt b/integrations/cohere/LICENSE.txt new file mode 100644 index 000000000..6134ab324 --- /dev/null +++ b/integrations/cohere/LICENSE.txt @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2023-present deepset GmbH + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/integrations/cohere/README.md b/integrations/cohere/README.md new file mode 100644 index 000000000..79cefed21 --- /dev/null +++ b/integrations/cohere/README.md @@ -0,0 +1,21 @@ +# cohere-haystack + +[![PyPI - Version](https://img.shields.io/pypi/v/cohere-haystack.svg)](https://pypi.org/project/cohere-haystack) +[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/cohere-haystack.svg)](https://pypi.org/project/cohere-haystack) + +----- + +**Table of Contents** + +- [Installation](#installation) +- [License](#license) + +## Installation + +```console +pip install cohere-haystack +``` + +## License + +`cohere-haystack` is distributed under the terms of the [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) license. diff --git a/integrations/cohere/pyproject.toml b/integrations/cohere/pyproject.toml new file mode 100644 index 000000000..e291907fd --- /dev/null +++ b/integrations/cohere/pyproject.toml @@ -0,0 +1,168 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "cohere-haystack" +dynamic = ["version"] +description = '' +readme = "README.md" +requires-python = ">=3.7" +license = "Apache-2.0" +keywords = [] +authors = [ + { name = "deepset GmbH", email = "info@deepset.ai" }, +] +classifiers = [ + "Development Status :: 4 - Beta", + "Programming Language :: Python", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: Implementation :: CPython", + "Programming Language :: Python :: Implementation :: PyPy", +] +dependencies = [ + "haystack-ai", + "cohere", +] + +[project.urls] +Documentation = "https://github.com/unknown/cohere-haystack#readme" +Issues = "https://github.com/unknown/cohere-haystack/issues" +Source = "https://github.com/unknown/cohere-haystack" + +[tool.hatch.version] +path = "src/cohere_haystack/__about__.py" + +[tool.hatch.envs.default] +dependencies = [ + "coverage[toml]>=6.5", + "pytest", +] +[tool.hatch.envs.default.scripts] +test = "pytest {args:tests}" +test-cov = "coverage run -m pytest {args:tests}" +cov-report = [ + "- coverage combine", + "coverage report", +] +cov = [ + "test-cov", + "cov-report", +] + +[[tool.hatch.envs.all.matrix]] +python = ["3.7", "3.8", "3.9", "3.10", "3.11"] + +[tool.hatch.envs.lint] +detached = true +dependencies = [ + "black>=23.1.0", + "mypy>=1.0.0", + "ruff>=0.0.243", +] +[tool.hatch.envs.lint.scripts] +typing = "mypy --install-types --non-interactive {args:src/cohere_haystack tests}" +style = [ + "ruff {args:.}", + "black --check --diff {args:.}", +] +fmt = [ + "black {args:.}", + "ruff --fix {args:.}", + "style", +] +all = [ + "style", + "typing", +] + +[tool.black] +target-version = ["py37"] +line-length = 120 +skip-string-normalization = true + +[tool.ruff] +target-version = "py37" +line-length = 120 +select = [ + "A", + "ARG", + "B", + "C", + "DTZ", + "E", + "EM", + "F", + "FBT", + "I", + "ICN", + "ISC", + "N", + "PLC", + "PLE", + "PLR", + "PLW", + "Q", + "RUF", + "S", + "T", + "TID", + "UP", + "W", + "YTT", +] +ignore = [ + # Allow non-abstract empty methods in abstract base classes + "B027", + # Allow boolean positional values in function calls, like `dict.get(... True)` + "FBT003", + # Ignore checks for possible passwords + "S105", "S106", "S107", + # Ignore complexity + "C901", "PLR0911", "PLR0912", "PLR0913", "PLR0915", +] +unfixable = [ + # Don't touch unused imports + "F401", +] + +[tool.ruff.isort] +known-first-party = ["cohere_haystack"] + +[tool.ruff.flake8-tidy-imports] +ban-relative-imports = "all" + +[tool.ruff.per-file-ignores] +# Tests can use magic values, assertions, and relative imports +"tests/**/*" = ["PLR2004", "S101", "TID252"] + +[tool.coverage.run] +source_pkgs = ["cohere_haystack", "tests"] +branch = true +parallel = true +omit = [ + "src/cohere_haystack/__about__.py", +] + +[tool.coverage.paths] +cohere_haystack = ["src/cohere_haystack", "*/cohere-haystack/src/cohere_haystack"] +tests = ["tests", "*/cohere-haystack/tests"] + +[tool.coverage.report] +exclude_lines = [ + "no cov", + "if __name__ == .__main__.:", + "if TYPE_CHECKING:", +] + +[[tool.mypy.overrides]] +module = [ + "cohere.*", + "haystack.*", + "pytest.*" +] +ignore_missing_imports = true \ No newline at end of file diff --git a/integrations/cohere/src/cohere_haystack/__about__.py b/integrations/cohere/src/cohere_haystack/__about__.py new file mode 100644 index 000000000..0e4fa27cf --- /dev/null +++ b/integrations/cohere/src/cohere_haystack/__about__.py @@ -0,0 +1,4 @@ +# SPDX-FileCopyrightText: 2023-present deepset GmbH +# +# SPDX-License-Identifier: Apache-2.0 +__version__ = "0.0.1" diff --git a/integrations/cohere/src/cohere_haystack/__init__.py b/integrations/cohere/src/cohere_haystack/__init__.py new file mode 100644 index 000000000..e873bc332 --- /dev/null +++ b/integrations/cohere/src/cohere_haystack/__init__.py @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2023-present deepset GmbH +# +# SPDX-License-Identifier: Apache-2.0 diff --git a/integrations/cohere/src/cohere_haystack/generator.py b/integrations/cohere/src/cohere_haystack/generator.py new file mode 100644 index 000000000..4b18fb75d --- /dev/null +++ b/integrations/cohere/src/cohere_haystack/generator.py @@ -0,0 +1,177 @@ +# SPDX-FileCopyrightText: 2023-present deepset GmbH +# +# SPDX-License-Identifier: Apache-2.0 +import logging +import os +import sys +from typing import Any, Callable, Dict, List, Optional + +from haystack import DeserializationError, component, default_from_dict, default_to_dict +from haystack.lazy_imports import LazyImport + +with LazyImport(message="Run 'pip install cohere'") as cohere_import: + from cohere import COHERE_API_URL, Client + +logger = logging.getLogger(__name__) + + +@component +class CohereGenerator: + """LLM Generator compatible with Cohere's generate endpoint. + + Queries the LLM using Cohere's API. Invocations are made using 'cohere' package. + See [Cohere API](https://docs.cohere.com/reference/generate) for more details. + + Example usage: + + ```python + from haystack.generators import CohereGenerator + generator = CohereGenerator(api_key="test-api-key") + generator.run(prompt="What's the capital of France?") + ``` + """ + + def __init__( + self, + api_key: Optional[str] = None, + model_name: str = "command", + streaming_callback: Optional[Callable] = None, + api_base_url: Optional[str] = None, + **kwargs, + ): + """ + Instantiates a `CohereGenerator` component. + + :param api_key: The API key for the Cohere API. If not set, it will be read from the COHERE_API_KEY env var. + :param model_name: The name of the model to use. Available models are: [command, command-light, command-nightly, + command-nightly-light]. Defaults to "command". + :param streaming_callback: A callback function to be called with the streaming response. Defaults to None. + :param api_base_url: The base URL of the Cohere API. Defaults to "https://api.cohere.ai". + :param kwargs: Additional model parameters. These will be used during generation. Refer to + https://docs.cohere.com/reference/generate for more details. + Some of the parameters are: + - 'max_tokens': The maximum number of tokens to be generated. Defaults to 1024. + - 'truncate': One of NONE|START|END to specify how the API will handle inputs longer than the maximum token + length. Defaults to END. + - 'temperature': A non-negative float that tunes the degree of randomness in generation. Lower temperatures + mean less random generations. + - 'preset': Identifier of a custom preset. A preset is a combination of parameters, such as prompt, + temperature etc. You can create presets in the playground. + - 'end_sequences': The generated text will be cut at the beginning of the earliest occurrence of an end + sequence. The sequence will be excluded from the text. + - 'stop_sequences': The generated text will be cut at the end of the earliest occurrence of a stop sequence. + The sequence will be included the text. + - 'k': Defaults to 0, min value of 0.01, max value of 0.99. + - 'p': Ensures that only the most likely tokens, with total probability mass of `p`, are considered for + generation at each step. If both `k` and `p` are enabled, `p` acts after `k`. + - 'frequency_penalty': Used to reduce repetitiveness of generated tokens. The higher the value, the stronger + a penalty is applied to previously present tokens, proportional to how many times they have already + appeared in the prompt or prior generation.' + - 'presence_penalty': Defaults to 0.0, min value of 0.0, max value of 1.0. Can be used to reduce + repetitiveness of generated tokens. Similar to `frequency_penalty`, except that this penalty is applied + equally to all tokens that have already appeared, regardless of their exact frequencies. + - 'return_likelihoods': One of GENERATION|ALL|NONE to specify how and if the token likelihoods are returned + with the response. Defaults to NONE. + - 'logit_bias': Used to prevent the model from generating unwanted tokens or to incentivize it to include + desired tokens. The format is {token_id: bias} where bias is a float between -10 and 10. + """ + cohere_import.check() + + if not api_key: + api_key = os.environ.get("COHERE_API_KEY") + if not api_key: + msg = ( + "CohereGenerator needs an API key to run." + "Either provide it as init parameter or set the env var COHERE_API_KEY." + ) + raise ValueError(msg) + + if not api_base_url: + api_base_url = COHERE_API_URL + + self.api_key = api_key + self.model_name = model_name + self.streaming_callback = streaming_callback + self.api_base_url = api_base_url + self.model_parameters = kwargs + self.client = Client(api_key=self.api_key, api_url=self.api_base_url) + + def to_dict(self) -> Dict[str, Any]: + """ + Serialize this component to a dictionary. + """ + if self.streaming_callback: + module = self.streaming_callback.__module__ + if module == "builtins": + callback_name = self.streaming_callback.__name__ + else: + callback_name = f"{module}.{self.streaming_callback.__name__}" + else: + callback_name = None + + return default_to_dict( + self, + model_name=self.model_name, + streaming_callback=callback_name, + api_base_url=self.api_base_url, + **self.model_parameters, + ) + + @classmethod + def from_dict(cls, data: Dict[str, Any]) -> "CohereGenerator": + """ + Deserialize this component from a dictionary. + """ + init_params = data.get("init_parameters", {}) + streaming_callback = None + if "streaming_callback" in init_params and init_params["streaming_callback"]: + parts = init_params["streaming_callback"].split(".") + module_name = ".".join(parts[:-1]) + function_name = parts[-1] + module = sys.modules.get(module_name, None) + if not module: + msg = f"Could not locate the module of the streaming callback: {module_name}" + raise DeserializationError(msg) + streaming_callback = getattr(module, function_name, None) + if not streaming_callback: + msg = f"Could not locate the streaming callback: {function_name}" + raise DeserializationError(msg) + data["init_parameters"]["streaming_callback"] = streaming_callback + return default_from_dict(cls, data) + + @component.output_types(replies=List[str], metadata=List[Dict[str, Any]]) + def run(self, prompt: str): + """ + Queries the LLM with the prompts to produce replies. + :param prompt: The prompt to be sent to the generative model. + """ + response = self.client.generate( + model=self.model_name, prompt=prompt, stream=self.streaming_callback is not None, **self.model_parameters + ) + if self.streaming_callback: + metadata_dict: Dict[str, Any] = {} + for chunk in response: + self.streaming_callback(chunk) + metadata_dict["index"] = chunk.index + replies = response.texts + metadata_dict["finish_reason"] = response.finish_reason + metadata = [metadata_dict] + self._check_truncated_answers(metadata) + return {"replies": replies, "metadata": metadata} + + metadata = [{"finish_reason": resp.finish_reason} for resp in response] + replies = [resp.text for resp in response] + self._check_truncated_answers(metadata) + return {"replies": replies, "metadata": metadata} + + def _check_truncated_answers(self, metadata: List[Dict[str, Any]]): + """ + Check the `finish_reason` returned with the Cohere response. + If the `finish_reason` is `MAX_TOKEN`, log a warning to the user. + :param metadata: The metadata returned by the Cohere API. + """ + if metadata[0]["finish_reason"] == "MAX_TOKENS": + logger.warning( + "Responses have been truncated before reaching a natural stopping point. " + "Increase the max_tokens parameter to allow for longer completions." + ) diff --git a/integrations/cohere/tests/__init__.py b/integrations/cohere/tests/__init__.py new file mode 100644 index 000000000..e873bc332 --- /dev/null +++ b/integrations/cohere/tests/__init__.py @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2023-present deepset GmbH +# +# SPDX-License-Identifier: Apache-2.0 diff --git a/integrations/cohere/tests/test_cohere_generators.py b/integrations/cohere/tests/test_cohere_generators.py new file mode 100644 index 000000000..d267847a4 --- /dev/null +++ b/integrations/cohere/tests/test_cohere_generators.py @@ -0,0 +1,181 @@ +# SPDX-FileCopyrightText: 2023-present deepset GmbH +# +# SPDX-License-Identifier: Apache-2.0 +import os + +import pytest + +from cohere_haystack.generator import CohereGenerator + + +def default_streaming_callback(chunk): + """ + Default callback function for streaming responses from Cohere API. + Prints the tokens of the first completion to stdout as soon as they are received and returns the chunk unchanged. + """ + print(chunk.text, flush=True, end="") # noqa: T201 + + +@pytest.mark.integration +class TestCohereGenerator: + def test_init_default(self): + import cohere + + component = CohereGenerator(api_key="test-api-key") + assert component.api_key == "test-api-key" + assert component.model_name == "command" + assert component.streaming_callback is None + assert component.api_base_url == cohere.COHERE_API_URL + assert component.model_parameters == {} + + def test_init_with_parameters(self): + callback = lambda x: x # noqa: E731 + component = CohereGenerator( + api_key="test-api-key", + model_name="command-light", + max_tokens=10, + some_test_param="test-params", + streaming_callback=callback, + api_base_url="test-base-url", + ) + assert component.api_key == "test-api-key" + assert component.model_name == "command-light" + assert component.streaming_callback == callback + assert component.api_base_url == "test-base-url" + assert component.model_parameters == {"max_tokens": 10, "some_test_param": "test-params"} + + def test_to_dict_default(self): + import cohere + + component = CohereGenerator(api_key="test-api-key") + data = component.to_dict() + assert data == { + "type": "cohere_haystack.generator.CohereGenerator", + "init_parameters": { + "model_name": "command", + "streaming_callback": None, + "api_base_url": cohere.COHERE_API_URL, + }, + } + + def test_to_dict_with_parameters(self): + component = CohereGenerator( + api_key="test-api-key", + model_name="command-light", + max_tokens=10, + some_test_param="test-params", + streaming_callback=default_streaming_callback, + api_base_url="test-base-url", + ) + data = component.to_dict() + assert data == { + "type": "cohere_haystack.generator.CohereGenerator", + "init_parameters": { + "model_name": "command-light", + "max_tokens": 10, + "some_test_param": "test-params", + "api_base_url": "test-base-url", + "streaming_callback": "tests.test_cohere_generators.default_streaming_callback", + }, + } + + def test_to_dict_with_lambda_streaming_callback(self): + component = CohereGenerator( + api_key="test-api-key", + model_name="command", + max_tokens=10, + some_test_param="test-params", + streaming_callback=lambda x: x, + api_base_url="test-base-url", + ) + data = component.to_dict() + assert data == { + "type": "cohere_haystack.generator.CohereGenerator", + "init_parameters": { + "model_name": "command", + "streaming_callback": "tests.test_cohere_generators.", + "api_base_url": "test-base-url", + "max_tokens": 10, + "some_test_param": "test-params", + }, + } + + def test_from_dict(self, monkeypatch): + monkeypatch.setenv("COHERE_API_KEY", "test-key") + data = { + "type": "cohere_haystack.generator.CohereGenerator", + "init_parameters": { + "model_name": "command", + "max_tokens": 10, + "some_test_param": "test-params", + "api_base_url": "test-base-url", + "streaming_callback": "tests.test_cohere_generators.default_streaming_callback", + }, + } + component = CohereGenerator.from_dict(data) + assert component.api_key == "test-key" + assert component.model_name == "command" + assert component.streaming_callback == default_streaming_callback + assert component.api_base_url == "test-base-url" + assert component.model_parameters == {"max_tokens": 10, "some_test_param": "test-params"} + + def test_check_truncated_answers(self, caplog): + component = CohereGenerator(api_key="test-api-key") + metadata = [{"finish_reason": "MAX_TOKENS"}] + component._check_truncated_answers(metadata) + assert caplog.records[0].message == ( + "Responses have been truncated before reaching a natural stopping point. " + "Increase the max_tokens parameter to allow for longer completions." + ) + + @pytest.mark.skipif( + not os.environ.get("COHERE_API_KEY", None), + reason="Export an env var called CO_API_KEY containing the Cohere API key to run this test.", + ) + @pytest.mark.integration + def test_cohere_generator_run(self): + component = CohereGenerator(api_key=os.environ.get("COHERE_API_KEY")) + results = component.run(prompt="What's the capital of France?") + assert len(results["replies"]) == 1 + assert "Paris" in results["replies"][0] + assert len(results["metadata"]) == 1 + assert results["metadata"][0]["finish_reason"] == "COMPLETE" + + @pytest.mark.skipif( + not os.environ.get("COHERE_API_KEY", None), + reason="Export an env var called COHERE_API_KEY containing the Cohere API key to run this test.", + ) + @pytest.mark.integration + def test_cohere_generator_run_wrong_model_name(self): + import cohere + + component = CohereGenerator(model_name="something-obviously-wrong", api_key=os.environ.get("COHERE_API_KEY")) + with pytest.raises( + cohere.CohereAPIError, + match="model not found, make sure the correct model ID was used and that you have access to the model.", + ): + component.run(prompt="What's the capital of France?") + + @pytest.mark.skipif( + not os.environ.get("COHERE_API_KEY", None), + reason="Export an env var called COHERE_API_KEY containing the Cohere API key to run this test.", + ) + @pytest.mark.integration + def test_cohere_generator_run_streaming(self): + class Callback: + def __init__(self): + self.responses = "" + + def __call__(self, chunk): + self.responses += chunk.text + return chunk + + callback = Callback() + component = CohereGenerator(os.environ.get("COHERE_API_KEY"), streaming_callback=callback) + results = component.run(prompt="What's the capital of France?") + + assert len(results["replies"]) == 1 + assert "Paris" in results["replies"][0] + assert len(results["metadata"]) == 1 + assert results["metadata"][0]["finish_reason"] == "COMPLETE" + assert callback.responses == results["replies"][0]