-
Notifications
You must be signed in to change notification settings - Fork 16.4k
Add OpenAI Provider #35023
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 OpenAI Provider #35023
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
039b058
Add OpenAI Provider
utkarsharma2 83c8903
Apply suggestions from code review
pankajkoti 0c9712c
Remove create_completions method from hook
utkarsharma2 43bb187
Change type of input_text param
utkarsharma2 cc763f0
Updated min-airflow version to 2.5.0
utkarsharma2 c263316
Updated the interface and fix docs and static files
utkarsharma2 a015629
Fix tests
utkarsharma2 6a8d35a
Fix tests
utkarsharma2 5ab55f9
Change the version
utkarsharma2 7d2cee1
Add embedding_kwargs as a param to operator
utkarsharma2 ca141ee
Update tests/providers/openai/hooks/test_openai.py
utkarsharma2 bc70d5c
Remove unwanted params in docstring
utkarsharma2 a6fdf0c
Update Changelog
utkarsharma2 3b0fd02
Add security.rst file
utkarsharma2 c861a4d
Update docs/apache-airflow-providers-openai/index.rst
utkarsharma2 66a468a
Add host field for connections
utkarsharma2 e10fbc2
Update docs/apache-airflow-providers-openai/index.rst
utkarsharma2 9ef59a6
Add changelog.rst file to docs
utkarsharma2 ad0d206
Change version to 1.0.0
utkarsharma2 ec79881
Resolve conflicts
utkarsharma2 b7f60cb
Fix tests
utkarsharma2 4fb0c2e
Fixed tests
utkarsharma2 ba2fc44
Fix test
utkarsharma2 5791deb
Resolve Conflict
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 |
|---|---|---|
|
|
@@ -82,6 +82,7 @@ body: | |
| - odbc | ||
| - openfaas | ||
| - openlineage | ||
| - openai | ||
| - opensearch | ||
| - opsgenie | ||
| - oracle | ||
|
|
||
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,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-openai`` | ||
|
|
||
| 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,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,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,86 @@ | ||
| # 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 openai | ||
|
|
||
| from airflow.hooks.base import BaseHook | ||
|
|
||
|
|
||
| class OpenAIHook(BaseHook): | ||
| """ | ||
| Use OpenAI SDK to interact with OpenAI APIs. | ||
|
|
||
| .. seealso:: https://platform.openai.com/docs/introduction/overview | ||
|
|
||
| :param conn_id: :ref:`OpenAI connection id <howto/connection:openai>` | ||
| """ | ||
|
|
||
| conn_name_attr = "conn_id" | ||
| default_conn_name = "openai_default" | ||
| conn_type = "openai" | ||
| hook_name = "OpenAI" | ||
|
|
||
| def __init__(self, conn_id: str = default_conn_name, *args: Any, **kwargs: Any) -> None: | ||
| super().__init__(*args, **kwargs) | ||
| self.conn_id = conn_id | ||
| openai.api_key = self._get_api_key() | ||
| api_base = self._get_api_base() | ||
| if api_base: | ||
| openai.api_base = api_base | ||
|
|
||
| @staticmethod | ||
| def get_ui_field_behaviour() -> dict[str, Any]: | ||
| """Return custom field behaviour.""" | ||
| return { | ||
| "hidden_fields": ["schema", "port", "login", "extra"], | ||
| "relabeling": {"password": "API Key"}, | ||
| "placeholders": {}, | ||
| } | ||
|
|
||
| def test_connection(self) -> tuple[bool, str]: | ||
| try: | ||
| openai.Model.list() | ||
| return True, "Connection established!" | ||
| except Exception as e: | ||
| return False, str(e) | ||
|
|
||
| def _get_api_key(self) -> str: | ||
| """Get the OpenAI API key from the connection.""" | ||
| conn = self.get_connection(self.conn_id) | ||
| if not conn.password: | ||
| raise ValueError("OpenAI API key not found in connection") | ||
| return str(conn.password) | ||
|
|
||
| def _get_api_base(self) -> None | str: | ||
| conn = self.get_connection(self.conn_id) | ||
| return conn.host | ||
|
|
||
| def create_embeddings( | ||
| self, text: str | list[Any], model: str = "text-embedding-ada-002", **kwargs: Any | ||
| ) -> list[float]: | ||
| """Generate embeddings for the given text using the given model. | ||
|
|
||
| :param text: The text to generate embeddings for. | ||
| :param model: The model to use for generating embeddings. | ||
| """ | ||
| response = openai.Embedding.create(model=model, input=text, **kwargs) | ||
| embeddings: list[float] = response["data"][0]["embedding"] | ||
| return embeddings | ||
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,71 @@ | ||
| # 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.openai.hooks.openai import OpenAIHook | ||
|
|
||
| if TYPE_CHECKING: | ||
| from airflow.utils.context import Context | ||
|
|
||
|
|
||
| class OpenAIEmbeddingOperator(BaseOperator): | ||
| """ | ||
| Operator that accepts input text to generate OpenAI embeddings using the specified model. | ||
|
|
||
| .. seealso:: | ||
| For more information on how to use this operator, take a look at the guide: | ||
| :ref:`howto/operator:OpenAIEmbeddingOperator` | ||
|
|
||
| :param conn_id: The OpenAI connection. | ||
| :param input_text: The text to generate OpenAI embeddings on. Either input_text or input_callable | ||
| should be provided. | ||
| :param model: The OpenAI model to be used for generating the embeddings. | ||
| :param embedding_kwargs: For possible option check | ||
| .. seealso:: https://platform.openai.com/docs/api-reference/embeddings/create | ||
| """ | ||
|
|
||
| template_fields: Sequence[str] = ("input_text",) | ||
|
|
||
| def __init__( | ||
| self, | ||
| conn_id: str, | ||
| input_text: str | list[Any], | ||
| model: str = "text-embedding-ada-002", | ||
| embedding_kwargs: dict | None = None, | ||
| **kwargs: Any, | ||
| ): | ||
| self.embedding_kwargs = embedding_kwargs or {} | ||
| super().__init__(**kwargs) | ||
| self.conn_id = conn_id | ||
| self.input_text = input_text | ||
| self.model = model | ||
|
|
||
| @cached_property | ||
| def hook(self) -> OpenAIHook: | ||
| """Return an instance of the OpenAIHook.""" | ||
| return OpenAIHook(conn_id=self.conn_id) | ||
|
|
||
| def execute(self, context: Context) -> list[float]: | ||
| self.log.info("Input text: %s", self.input_text) | ||
| embeddings = self.hook.create_embeddings(self.input_text, model=self.model, **self.embedding_kwargs) | ||
| self.log.info("Embeddings: %s", embeddings) | ||
| return embeddings |
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-openai | ||
|
|
||
| name: OpenAI | ||
|
|
||
| description: | | ||
| `OpenAI <https://platform.openai.com/docs/introduction>`__ | ||
|
|
||
| suspended: false | ||
|
|
||
| versions: | ||
| - 1.0.0 | ||
|
|
||
| integrations: | ||
| - integration-name: OpenAI | ||
| external-doc-url: https://platform.openai.com/docs/introduction | ||
| how-to-guide: | ||
| - /docs/apache-airflow-providers-openai/operators/openai.rst | ||
| tags: [software] | ||
|
|
||
| dependencies: | ||
| - apache-airflow>=2.5.0 | ||
utkarsharma2 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| - openai[datalib]>=0.28.1,<1.0 | ||
|
|
||
| hooks: | ||
| - integration-name: OpenAI | ||
| python-modules: | ||
| - airflow.providers.openai.hooks.openai | ||
|
|
||
| operators: | ||
| - integration-name: OpenAI | ||
| python-modules: | ||
| - airflow.providers.openai.operators.openai | ||
|
|
||
| connection-types: | ||
| - hook-class-name: airflow.providers.openai.hooks.openai.OpenAIHook | ||
| connection-type: openai | ||
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/odbc/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-openai | ||
| ---------------------------------------- |
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.