josh-fell commented on code in PR #34921: URL: https://github.com/apache/airflow/pull/34921#discussion_r1376210849
########## CONTRIBUTING.rst: ########## @@ -1443,7 +1444,7 @@ You can join the channels via links at the `Airflow Community page <https://airf * The `Airflow CWiki <https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Home?src=breadcrumbs>`_ for: * detailed discussions on big proposals (Airflow Improvement Proposals also name AIPs) * helpful, shared resources (for example Apache Airflow logos - * information that can be reused by others (for example instructions on preparing workshops) + * information that can be re-used by others (for example instructions on preparing workshops) Review Comment: ```suggestion * information that can be reused by others (for example instructions on preparing workshops) ``` Nit, but this isn't hyphenated. ########## tests/system/providers/cohere/example_cohere_embedding_operator.py: ########## @@ -0,0 +1,49 @@ +# 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 datetime import datetime + +from airflow import DAG +from airflow.decorators import task +from airflow.providers.cohere.operators.embedding import CohereEmbeddingOperator + +with DAG("example_cohere_embedding", schedule=None, start_date=datetime(2023, 1, 1), catchup=False) as dag: + # [START howto_operator_cohere_embedding] + 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.", + ] + + @task + def task_to_get_text(): + return texts + + def get_task(): Review Comment: WDYT about renaming this callable to `get_texts` instead? Seems clearer to what I think you're trying to show. Of course, ignore this if using a callable's return as an input is no longer an option from the other discussion. ########## airflow/providers/cohere/hooks/cohere.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 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>` + """ + + 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.client = None Review Comment: Is this needed? ########## docs/apache-airflow-providers-cohere/operators/embedding.rst: ########## @@ -0,0 +1,42 @@ + .. 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:`~airflow.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 Review Comment: ```suggestion The CohereEmbeddingOperator requires the ``input_text`` as an input to embedding API. Use the ``cohere_conn_id`` parameter to specify the Cohere connection to use to ``` ########## airflow/providers/cohere/operators/embedding.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 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 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``. Review Comment: Is using a callable an option anymore? ########## airflow/providers/cohere/operators/embedding.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 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 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``. + :param timeout: Timeout in seconds for Cohere API. + :param max_retries: No. of times to retry before failing. + """ + + template_fields: Sequence[str] = ("input_text",) + + def __init__( + self, + input_text: list[str], Review Comment: Is it worth, perhaps, to also accept a string and then normalize to a list if a string is provided? WDYT? ########## tests/system/providers/cohere/example_cohere_embedding_operator.py: ########## @@ -0,0 +1,49 @@ +# 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 datetime import datetime + +from airflow import DAG +from airflow.decorators import task +from airflow.providers.cohere.operators.embedding import CohereEmbeddingOperator + +with DAG("example_cohere_embedding", schedule=None, start_date=datetime(2023, 1, 1), catchup=False) as dag: Review Comment: ```suggestion with DAG("example_cohere_embedding", schedule=None, start_date=datetime(2023, 1, 1), catchup=False): ``` No longer needed after Airflow 2.4.0. ########## docs/apache-airflow-providers-cohere/operators/embedding.rst: ########## @@ -0,0 +1,42 @@ + .. 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:`~airflow.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: Review Comment: ```suggestion An example using the operator in this way: ``` ########## airflow/providers/cohere/operators/embedding.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 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 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``. + :param timeout: Timeout in seconds for Cohere API. + :param max_retries: No. of times to retry before failing. Review Comment: ```suggestion :param max_retries: Number of times to retry before failing. ``` Let's use the full word rather than an abbreviation for non-native-English users. -- 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]
