Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a274cea
Add Cohere Provider
utkarsharma2 Oct 13, 2023
feea240
Add Cohere Provider
utkarsharma2 Oct 13, 2023
32f5dfe
Move link to seealso sphinx directive
utkarsharma2 Oct 16, 2023
3035a47
Updated check for parameters
utkarsharma2 Oct 16, 2023
2dcb0bb
Update dependency of the cohere
utkarsharma2 Oct 16, 2023
2476fd6
Move the dag out of rst and into system tests
utkarsharma2 Oct 16, 2023
84da2eb
Add dependency to of cohere python sdk
utkarsharma2 Oct 16, 2023
e681009
Add cache_property for the cohere client
utkarsharma2 Oct 16, 2023
bbebd93
Remove unwanted get_conn method
utkarsharma2 Oct 16, 2023
96d2f97
Add correct label to password field
utkarsharma2 Oct 16, 2023
90d4695
Expose timeout, max_retries and api_url to user
utkarsharma2 Oct 16, 2023
d60cfa0
Fix documentation
utkarsharma2 Oct 25, 2023
aeb37af
Update interface of CohereEmbeddingOperator operator
utkarsharma2 Oct 25, 2023
e38fdda
Updated testcases
utkarsharma2 Oct 25, 2023
649ead5
Updated testcases
utkarsharma2 Oct 25, 2023
4dec45c
Fix static check and docs build
utkarsharma2 Oct 26, 2023
9341c17
Update CONTRIBUTING.rst
utkarsharma2 Oct 31, 2023
22aad72
Update docs/apache-airflow-providers-cohere/operators/embedding.rst
utkarsharma2 Oct 31, 2023
c9444d3
Update airflow/providers/cohere/operators/embedding.py
utkarsharma2 Oct 31, 2023
b8d1b6e
Update airflow/providers/cohere/hooks/cohere.py
utkarsharma2 Nov 1, 2023
914c631
Update airflow/providers/cohere/CHANGELOG.rst
utkarsharma2 Nov 1, 2023
07316a8
Address the PR comments
utkarsharma2 Nov 1, 2023
2254142
Resolve conflicts
utkarsharma2 Nov 1, 2023
d479161
Fix breaking tests
utkarsharma2 Nov 2, 2023
40ed8ee
Fix static checks
utkarsharma2 Nov 2, 2023
38d79ff
Update airflow/providers/cohere/operators/embedding.py
utkarsharma2 Nov 2, 2023
120c4c1
Fix docstring
utkarsharma2 Nov 3, 2023
6460b10
Add note for initial release
utkarsharma2 Nov 3, 2023
c590d4f
Add security.rst file
utkarsharma2 Nov 3, 2023
02898c7
Update airflow/providers/cohere/hooks/cohere.py
utkarsharma2 Nov 3, 2023
9d8aca3
Update airflow/providers/cohere/operators/embedding.py
utkarsharma2 Nov 3, 2023
bcf94c1
Update docs/apache-airflow-providers-cohere/operators/embedding.rst
utkarsharma2 Nov 3, 2023
2f54f72
Add ref to sequrity.rst
pankajastro Nov 4, 2023
f21a85d
Update docs/apache-airflow-providers-cohere/security.rst
pankajastro Nov 4, 2023
c730abb
Add /changelog.rst
pankajastro Nov 4, 2023
7f1e0ae
Resolve conflicts
utkarsharma2 Nov 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/airflow_providers_bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ body:
- celery
- cloudant
- cncf-kubernetes
- cohere
- common-io
- common-sql
- daskexecutor
Expand Down
6 changes: 3 additions & 3 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,9 @@ aiobotocore, airbyte, alibaba, all, all_dbs, amazon, apache.atlas, apache.beam,
apache.drill, apache.druid, apache.flink, apache.hdfs, apache.hive, apache.impala, apache.kafka,
apache.kylin, apache.livy, apache.pig, apache.pinot, apache.spark, apache.sqoop, apache.webhdfs,
apprise, arangodb, asana, async, atlas, atlassian.jira, aws, azure, cassandra, celery, cgroups,
cloudant, cncf.kubernetes, common.io, common.sql, crypto, dask, daskexecutor, databricks, datadog,
dbt.cloud, deprecated_api, devel, devel_all, devel_ci, devel_hadoop, dingding, discord, doc,
doc_gen, docker, druid, elasticsearch, exasol, facebook, ftp, gcp, gcp_api, github,
cloudant, cncf.kubernetes, cohere, common.io, common.sql, crypto, dask, daskexecutor, databricks,
datadog, dbt.cloud, deprecated_api, devel, devel_all, devel_ci, devel_hadoop, dingding, discord,
doc, doc_gen, docker, druid, elasticsearch, exasol, facebook, ftp, gcp, gcp_api, github,
github_enterprise, google, google_auth, grpc, hashicorp, hdfs, hive, http, imap, influxdb, jdbc,
jenkins, kerberos, kubernetes, ldap, leveldb, microsoft.azure, microsoft.mssql, microsoft.psrp,
microsoft.winrm, mongo, mssql, mysql, neo4j, odbc, openfaas, openlineage, opensearch, opsgenie,
Expand Down
6 changes: 3 additions & 3 deletions INSTALL
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ aiobotocore, airbyte, alibaba, all, all_dbs, amazon, apache.atlas, apache.beam,
apache.drill, apache.druid, apache.flink, apache.hdfs, apache.hive, apache.impala, apache.kafka,
apache.kylin, apache.livy, apache.pig, apache.pinot, apache.spark, apache.sqoop, apache.webhdfs,
apprise, arangodb, asana, async, atlas, atlassian.jira, aws, azure, cassandra, celery, cgroups,
cloudant, cncf.kubernetes, common.io, common.sql, crypto, dask, daskexecutor, databricks, datadog,
dbt.cloud, deprecated_api, devel, devel_all, devel_ci, devel_hadoop, dingding, discord, doc,
doc_gen, docker, druid, elasticsearch, exasol, facebook, ftp, gcp, gcp_api, github,
cloudant, cncf.kubernetes, cohere, common.io, common.sql, crypto, dask, daskexecutor, databricks,
datadog, dbt.cloud, deprecated_api, devel, devel_all, devel_ci, devel_hadoop, dingding, discord,
doc, doc_gen, docker, druid, elasticsearch, exasol, facebook, ftp, gcp, gcp_api, github,
github_enterprise, google, google_auth, grpc, hashicorp, hdfs, hive, http, imap, influxdb, jdbc,
jenkins, kerberos, kubernetes, ldap, leveldb, microsoft.azure, microsoft.mssql, microsoft.psrp,
microsoft.winrm, mongo, mssql, mysql, neo4j, odbc, openfaas, openlineage, opensearch, opsgenie,
Expand Down
1 change: 1 addition & 0 deletions airflow/providers/cohere/.latest-doc-only-change.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c645d8e40c167ea1f6c332cdc3ea0ca5a9363205
26 changes: 26 additions & 0 deletions airflow/providers/cohere/CHANGELOG.rst
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.
24 changes: 24 additions & 0 deletions airflow/providers/cohere/__init__.py
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
#
24 changes: 24 additions & 0 deletions airflow/providers/cohere/hooks/__init__.py
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
#
83 changes: 83 additions & 0 deletions airflow/providers/cohere/hooks/cohere.py
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)
16 changes: 16 additions & 0 deletions airflow/providers/cohere/operators/__init__.py
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.
69 changes: 69 additions & 0 deletions airflow/providers/cohere/operators/embedding.py
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)
54 changes: 54 additions & 0 deletions airflow/providers/cohere/provider.yaml
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
18 changes: 18 additions & 0 deletions docs/apache-airflow-providers-cohere/changelog.rst
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
19 changes: 19 additions & 0 deletions docs/apache-airflow-providers-cohere/commits.rst
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
-------------------------------------------
Loading