kaxil commented on code in PR #34921: URL: https://github.com/apache/airflow/pull/34921#discussion_r1358319073
########## airflow/providers/cohere/provider.yaml: ########## @@ -0,0 +1,52 @@ +# 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 + tags: [software] + +dependencies: + - apache-airflow>=2.4.0 Review Comment: Any specific reason for 2.4.0? ########## docs/apache-airflow-providers-cohere/operators/cohere.rst: ########## @@ -0,0 +1,57 @@ + .. 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. + +.. _howto/operator:CohereEmbeddingOperator: + +CohereEmbeddingOperator +======================== + +Use the :class:`~astronomer_providers_llm.providers.cohere.operators.embedding.CohereEmbeddingOperator` to +interact with Cohere APIs to create embeddings for a given text. + + +Using the Operator +^^^^^^^^^^^^^^^^^^ + +The CohereEmbeddingOperator requires the ``texts`` as an input to embedding API. Use the ``cohere_conn_id`` parameter to specify the Cohere connection to use to +connect to your account. + +An example using the operator is in way: + +Example Code: +------------- + +.. code-block:: python + + from datetime import datetime + + from airflow import DAG + + from astronomer_providers_llm.providers.cohere.operators.embedding import CohereEmbeddingOperator + + with DAG("example_cohere_embedding", schedule=None, start_date=datetime(2023, 1, 1), catchup=False) as dag: + texts = [ + "On Kernel-Target Alignment. We describe a family of global optimization procedures", + " that automatically decompose optimization problems into smaller loosely coupled", + " problems, then combine the solutions of these with message passing algorithms.", + ] + + def get_text(): + return texts + + CohereEmbeddingOperator(input_text=texts, task_id="embedding_via_text") + CohereEmbeddingOperator(input_callable=get_text, task_id="embedding_via_callable") Review Comment: Could we just embed a example DAG? ########## airflow/providers/cohere/hooks/cohere.py: ########## @@ -0,0 +1,72 @@ +# +# 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 typing import Any + +import cohere + +from airflow.hooks.base import BaseHook + + +class CohereHook(BaseHook): + """ + Use Cohere(https://docs.cohere.com/docs) to interact with Cohere platform. Review Comment: Isn't a valid rst syntax for link ########## docs/apache-airflow-providers-cohere/index.rst: ########## @@ -0,0 +1,98 @@ + + .. 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`` +====================================== + + +.. toctree:: + :hidden: + :maxdepth: 1 + :caption: Basics + + Home <self> + +.. toctree:: + :hidden: + :maxdepth: 1 + :caption: Guides + + Connection types <connections> + Operators <operators/cohere> + + +.. toctree:: + :hidden: + :maxdepth: 1 + :caption: Commits + + Detailed list of commits <commits> + + +.. toctree:: + :hidden: + :maxdepth: 1 + :caption: Resources + + Python API <_api/airflow/providers/cohere/index> + PyPI Repository <https://pypi.org/project/apache-airflow-providers-cohere/> + Installing from sources <installing-providers-from-sources> + + +Package apache-airflow-providers-cohere +----------------------------------------- + +`Cohere <https://cohere.com>`__ + + +Release: 1.0.0 + +Provider package +---------------- + +This is a provider package for ``cohere`` APIs. All classes for this provider package +are in ``airflow.providers.cohere`` python module. + +Installation +------------ + +You can install this package on top of an existing Airflow 2 installation (see ``Requirements`` below) +for the minimum Airflow version supported) via +``pip install apache-airflow-providers-cohere`` + + +Requirements +------------ + +The minimum Apache Airflow version supported by this provider package is ``2.4.0``. + +================== ================== +PIP package Version required +================== ================== +``apache-airflow`` ``>=2.4.0`` Review Comment: shouldn't `cohere` be listed here 🤔 ########## docs/apache-airflow-providers-cohere/index.rst: ########## @@ -0,0 +1,98 @@ + + .. 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`` +====================================== + + +.. toctree:: + :hidden: + :maxdepth: 1 + :caption: Basics + + Home <self> + +.. toctree:: + :hidden: + :maxdepth: 1 + :caption: Guides + + Connection types <connections> + Operators <operators/cohere> + + +.. toctree:: + :hidden: + :maxdepth: 1 + :caption: Commits + + Detailed list of commits <commits> + + +.. toctree:: + :hidden: + :maxdepth: 1 + :caption: Resources + + Python API <_api/airflow/providers/cohere/index> + PyPI Repository <https://pypi.org/project/apache-airflow-providers-cohere/> + Installing from sources <installing-providers-from-sources> + + +Package apache-airflow-providers-cohere +----------------------------------------- + +`Cohere <https://cohere.com>`__ + + +Release: 1.0.0 + +Provider package +---------------- + +This is a provider package for ``cohere`` APIs. All classes for this provider package +are in ``airflow.providers.cohere`` python module. + +Installation +------------ + +You can install this package on top of an existing Airflow 2 installation (see ``Requirements`` below) +for the minimum Airflow version supported) via +``pip install apache-airflow-providers-cohere`` + + +Requirements +------------ + +The minimum Apache Airflow version supported by this provider package is ``2.4.0``. + +================== ================== +PIP package Version required +================== ================== +``apache-airflow`` ``>=2.4.0`` Review Comment: shouldn't `cohere` be listed here 🤔 ########## airflow/providers/cohere/operators/embedding.py: ########## @@ -0,0 +1,82 @@ +# 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, Callable, Collection, Mapping + +from airflow.exceptions import AirflowException +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's hosted services. + + :param conn_id: Optional. The name of the Airflow connection to get connection + information for Cohere. Defaults to "cohere_default". + :param input_text: list of text items that need to be embedded. Only one of input_text or input_callable + should be provided. + :param input_callable: The callable that provides the input texts to generate embeddings for. + Only one of input_text or input_callable should be provided. + :param input_callable_args: The list of arguments to be passed to ``input_callable`` + :param input_callable_kwargs: The kwargs to be passed to ``input_callable`` + """ + + def __init__( + self, + input_text: list[str] | None = None, + input_callable: Callable[[Any], list[str]] | None = None, + input_callable_args: Collection[Any] | None = None, + input_callable_kwargs: Mapping[str, Any] | None = None, + conn_id: str = CohereHook.default_conn_name, + **kwargs: Any, + ): + super().__init__(**kwargs) + self.conn_id = conn_id + self.input_text = input_text + self.input_callable = input_callable + self.input_callable_args = input_callable_args or () + self.input_callable_kwargs = input_callable_kwargs or {} + self.text_to_embed = self._get_text_to_embed() + + @cached_property + def hook(self) -> CohereHook: + """Return an instance of the CohereHook.""" + return CohereHook(conn_id=self.conn_id) + + def _get_text_to_embed(self) -> list[str]: + """Get the text to embed by evaluating ``input_text`` and ``input_callable``.""" + if all([self.input_text, self.input_callable]): Review Comment: All feels weird for two item check :) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
