-
Notifications
You must be signed in to change notification settings - Fork 16.4k
Add Cohere Provider #34921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Cohere Provider #34921
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
a274cea
Add Cohere Provider
utkarsharma2 feea240
Add Cohere Provider
utkarsharma2 32f5dfe
Move link to seealso sphinx directive
utkarsharma2 3035a47
Updated check for parameters
utkarsharma2 2dcb0bb
Update dependency of the cohere
utkarsharma2 2476fd6
Move the dag out of rst and into system tests
utkarsharma2 84da2eb
Add dependency to of cohere python sdk
utkarsharma2 e681009
Add cache_property for the cohere client
utkarsharma2 bbebd93
Remove unwanted get_conn method
utkarsharma2 96d2f97
Add correct label to password field
utkarsharma2 90d4695
Expose timeout, max_retries and api_url to user
utkarsharma2 d60cfa0
Fix documentation
utkarsharma2 aeb37af
Update interface of CohereEmbeddingOperator operator
utkarsharma2 e38fdda
Updated testcases
utkarsharma2 649ead5
Updated testcases
utkarsharma2 4dec45c
Fix static check and docs build
utkarsharma2 9341c17
Update CONTRIBUTING.rst
utkarsharma2 22aad72
Update docs/apache-airflow-providers-cohere/operators/embedding.rst
utkarsharma2 c9444d3
Update airflow/providers/cohere/operators/embedding.py
utkarsharma2 b8d1b6e
Update airflow/providers/cohere/hooks/cohere.py
utkarsharma2 914c631
Update airflow/providers/cohere/CHANGELOG.rst
utkarsharma2 07316a8
Address the PR comments
utkarsharma2 2254142
Resolve conflicts
utkarsharma2 d479161
Fix breaking tests
utkarsharma2 40ed8ee
Fix static checks
utkarsharma2 38d79ff
Update airflow/providers/cohere/operators/embedding.py
utkarsharma2 120c4c1
Fix docstring
utkarsharma2 6460b10
Add note for initial release
utkarsharma2 c590d4f
Add security.rst file
utkarsharma2 02898c7
Update airflow/providers/cohere/hooks/cohere.py
utkarsharma2 9d8aca3
Update airflow/providers/cohere/operators/embedding.py
utkarsharma2 bcf94c1
Update docs/apache-airflow-providers-cohere/operators/embedding.rst
utkarsharma2 2f54f72
Add ref to sequrity.rst
pankajastro f21a85d
Update docs/apache-airflow-providers-cohere/security.rst
pankajastro c730abb
Add /changelog.rst
pankajastro 7f1e0ae
Resolve conflicts
utkarsharma2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,7 @@ body: | |
| - celery | ||
| - cloudant | ||
| - cncf-kubernetes | ||
| - cohere | ||
| - common-io | ||
| - common-sql | ||
| - daskexecutor | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| c645d8e40c167ea1f6c332cdc3ea0ca5a9363205 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| .. Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you 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. | ||
|
|
||
| ``apache-airflow-providers-cohere`` | ||
|
|
||
| Changelog | ||
| --------- | ||
|
|
||
| 1.0.0 | ||
| ..... | ||
|
|
||
| Initial version of the provider. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
| # | ||
| # NOTE! THIS FILE IS AUTOMATICALLY GENERATED AND WILL BE | ||
| # OVERWRITTEN WHEN PREPARING DOCUMENTATION FOR THE PACKAGES. | ||
| # | ||
| # IF YOU WANT TO MODIFY IT, YOU SHOULD MODIFY THE TEMPLATE | ||
| # `PROVIDER__INIT__PY_TEMPLATE.py.jinja2` IN the `dev/provider_packages` DIRECTORY | ||
| # |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
| # | ||
| # NOTE! THIS FILE IS AUTOMATICALLY GENERATED AND WILL BE | ||
| # OVERWRITTEN WHEN PREPARING DOCUMENTATION FOR THE PACKAGES. | ||
| # | ||
| # IF YOU WANT TO MODIFY IT, YOU SHOULD MODIFY THE TEMPLATE | ||
| # `PROVIDER__INIT__PY_TEMPLATE.py.jinja2` IN the `dev/provider_packages` DIRECTORY | ||
| # |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
| from __future__ import annotations | ||
|
|
||
| from functools import cached_property | ||
| from typing import Any | ||
|
|
||
| import cohere | ||
|
|
||
| from airflow.hooks.base import BaseHook | ||
|
|
||
|
|
||
| class CohereHook(BaseHook): | ||
| """ | ||
| Use Cohere Python SDK to interact with Cohere platform. | ||
|
|
||
| .. seealso:: https://docs.cohere.com/docs | ||
|
|
||
| :param conn_id: :ref:`Cohere connection id <howto/connection:cohere>` | ||
| :param timeout: Request timeout in seconds. | ||
| :param max_retries: Maximal number of retries for requests. | ||
| """ | ||
|
|
||
| conn_name_attr = "conn_id" | ||
| default_conn_name = "cohere_default" | ||
| conn_type = "cohere" | ||
| hook_name = "Cohere" | ||
|
|
||
| def __init__( | ||
| self, | ||
| conn_id: str = default_conn_name, | ||
| timeout: int | None = None, | ||
| max_retries: int | None = None, | ||
| ) -> None: | ||
| super().__init__() | ||
| self.conn_id = conn_id | ||
| self.timeout = timeout | ||
| self.max_retries = max_retries | ||
|
|
||
| @cached_property | ||
| def get_conn(self) -> cohere.Client: | ||
| conn = self.get_connection(self.conn_id) | ||
| return cohere.Client( | ||
| api_key=conn.password, timeout=self.timeout, max_retries=self.max_retries, api_url=conn.host | ||
| ) | ||
|
|
||
| def create_embeddings( | ||
| self, texts: list[str], model: str = "embed-multilingual-v2.0" | ||
| ) -> list[list[float]]: | ||
| response = self.get_conn.embed(texts=texts, model=model) | ||
| embeddings = response.embeddings | ||
| return embeddings | ||
|
|
||
| @staticmethod | ||
| def get_ui_field_behaviour() -> dict[str, Any]: | ||
| return { | ||
| "hidden_fields": ["schema", "login", "port", "extra"], | ||
| "relabeling": { | ||
| "password": "API Key", | ||
| }, | ||
| } | ||
|
|
||
| def test_connection(self) -> tuple[bool, str]: | ||
| try: | ||
| self.get_conn.generate("Test", max_tokens=10) | ||
| return True, "Connection established" | ||
| except Exception as e: | ||
| return False, str(e) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from functools import cached_property | ||
| from typing import TYPE_CHECKING, Any, Sequence | ||
|
|
||
| from airflow.models import BaseOperator | ||
| from airflow.providers.cohere.hooks.cohere import CohereHook | ||
|
|
||
| if TYPE_CHECKING: | ||
| from airflow.utils.context import Context | ||
|
|
||
|
|
||
| class CohereEmbeddingOperator(BaseOperator): | ||
| """Creates the embedding base by interacting with cohere hosted services. | ||
|
|
||
| .. seealso:: | ||
| For more information on how to use this operator, take a look at the guide: | ||
| :ref:`howto/operator:CohereEmbeddingOperator` | ||
|
|
||
| :param input_text: single string text or list of text items that need to be embedded. | ||
| :param conn_id: Optional. The name of the Airflow connection to get connection | ||
| information for Cohere. Defaults to "cohere_default". | ||
| :param timeout: Timeout in seconds for Cohere API. | ||
| :param max_retries: Number of times to retry before failing. | ||
| """ | ||
|
|
||
| template_fields: Sequence[str] = ("input_text",) | ||
|
|
||
| def __init__( | ||
| self, | ||
| input_text: list[str] | str, | ||
| conn_id: str = CohereHook.default_conn_name, | ||
| timeout: int | None = None, | ||
| max_retries: int | None = None, | ||
| **kwargs: Any, | ||
| ): | ||
| super().__init__(**kwargs) | ||
| if isinstance(input_text, str): | ||
| input_text = [input_text] | ||
| self.conn_id = conn_id | ||
| self.input_text = input_text | ||
| self.timeout = timeout | ||
| self.max_retries = max_retries | ||
|
|
||
| @cached_property | ||
| def hook(self) -> CohereHook: | ||
| """Return an instance of the CohereHook.""" | ||
| return CohereHook(conn_id=self.conn_id, timeout=self.timeout, max_retries=self.max_retries) | ||
|
|
||
| def execute(self, context: Context) -> list[list[float]]: | ||
| """Embed texts using Cohere embed services.""" | ||
| return self.hook.create_embeddings(self.input_text) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
|
|
||
| --- | ||
| package-name: apache-airflow-providers-cohere | ||
|
|
||
| name: Cohere | ||
|
|
||
| description: | | ||
| `Cohere <https://docs.cohere.com/docs>`__ | ||
|
|
||
| suspended: false | ||
|
|
||
| versions: | ||
| - 1.0.0 | ||
|
|
||
| integrations: | ||
| - integration-name: Cohere | ||
| external-doc-url: https://docs.cohere.com/docs | ||
| how-to-guide: | ||
| - /docs/apache-airflow-providers-cohere/operators/embedding.rst | ||
| tags: [software] | ||
|
|
||
| dependencies: | ||
| - apache-airflow>=2.5.0 | ||
| - cohere>=4.27 | ||
|
|
||
| hooks: | ||
| - integration-name: Cohere | ||
| python-modules: | ||
| - airflow.providers.cohere.hooks.cohere | ||
|
|
||
| operators: | ||
| - integration-name: Cohere | ||
| python-modules: | ||
| - airflow.providers.cohere.operators.embedding | ||
|
|
||
| connection-types: | ||
| - hook-class-name: airflow.providers.cohere.hooks.cohere.CohereHook | ||
| connection-type: cohere |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| .. Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you 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. | ||
|
|
||
| .. include:: ../../airflow/providers/cohere/CHANGELOG.rst |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| .. Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you 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. | ||
|
|
||
| Package apache-airflow-providers-cohere | ||
| ------------------------------------------- |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.