From 56d1864a5bc329a2c5f68d483a9e4ea295cc491f Mon Sep 17 00:00:00 2001 From: Charles Marion Date: Fri, 20 Sep 2024 20:44:47 +0000 Subject: [PATCH] bug: Upgrade openai library following the langchain upgrade. --- .../adapters/azureopenai/azuregpt.py | 4 +-- .../request-handler/adapters/openai/gpt.py | 2 +- .../file-import-batch-job/requirements.txt | 2 +- lib/shared/layers/common/requirements.txt | 3 +- .../python/genai_core/embeddings.py | 4 +-- .../python-sdk/python/genai_core/models.py | 29 ++++++++++--------- 6 files changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/model-interfaces/langchain/functions/request-handler/adapters/azureopenai/azuregpt.py b/lib/model-interfaces/langchain/functions/request-handler/adapters/azureopenai/azuregpt.py index 31b232827..d38ca05b3 100644 --- a/lib/model-interfaces/langchain/functions/request-handler/adapters/azureopenai/azuregpt.py +++ b/lib/model-interfaces/langchain/functions/request-handler/adapters/azureopenai/azuregpt.py @@ -1,5 +1,5 @@ import os -from langchain_community.chat_models import AzureChatOpenAI +from langchain_openai import AzureChatOpenAI from ..base import ModelAdapter from genai_core.registry import registry @@ -24,7 +24,7 @@ def get_llm(self, model_kwargs={}): params["max_tokens"] = model_kwargs["maxTokens"] return AzureChatOpenAI( - openai_api_base=os.environ.get(f"AZURE_OPENAI_API_BASE__{self.model_id}"), + azure_endpoint=os.environ.get(f"AZURE_OPENAI_API_BASE__{self.model_id}"), deployment_name=os.environ.get( f"AZURE_OPENAI_API_DEPLOYMENT_NAME__{self.model_id}" ), diff --git a/lib/model-interfaces/langchain/functions/request-handler/adapters/openai/gpt.py b/lib/model-interfaces/langchain/functions/request-handler/adapters/openai/gpt.py index 7210982dd..468280282 100644 --- a/lib/model-interfaces/langchain/functions/request-handler/adapters/openai/gpt.py +++ b/lib/model-interfaces/langchain/functions/request-handler/adapters/openai/gpt.py @@ -1,5 +1,5 @@ import os -from langchain_community.chat_models import ChatOpenAI +from langchain_openai import ChatOpenAI from ..base import ModelAdapter from genai_core.registry import registry diff --git a/lib/shared/file-import-batch-job/requirements.txt b/lib/shared/file-import-batch-job/requirements.txt index 1be3cefbf..58d6d7d39 100644 --- a/lib/shared/file-import-batch-job/requirements.txt +++ b/lib/shared/file-import-batch-job/requirements.txt @@ -11,7 +11,7 @@ psycopg2-binary==2.9.7 pgvector==0.2.2 pydantic==2.4.0 urllib3<2 -openai==0.28.0 +openai==1.47.0 beautifulsoup4==4.12.2 requests==2.32.2 attrs==23.1.0 diff --git a/lib/shared/layers/common/requirements.txt b/lib/shared/layers/common/requirements.txt index 6ef6cb403..0ce373a24 100644 --- a/lib/shared/layers/common/requirements.txt +++ b/lib/shared/layers/common/requirements.txt @@ -6,12 +6,13 @@ requests-aws4auth==1.2.3 langchain==0.2.14 langchain-community==0.2.12 langchain-aws==0.1.17 +langchain-openai==0.1.25 +openai==1.47.0 opensearch-py==2.4.2 psycopg2-binary==2.9.7 pgvector==0.2.2 pydantic==2.4.0 urllib3<2 -openai==0.28.1 beautifulsoup4==4.12.2 requests==2.32.0 attrs==23.1.0 diff --git a/lib/shared/layers/python-sdk/python/genai_core/embeddings.py b/lib/shared/layers/python-sdk/python/genai_core/embeddings.py index c1f88a259..6d67cdc57 100644 --- a/lib/shared/layers/python-sdk/python/genai_core/embeddings.py +++ b/lib/shared/layers/python-sdk/python/genai_core/embeddings.py @@ -62,8 +62,8 @@ def _generate_embeddings_openai(model: EmbeddingsModel, input: List[str]): if not openai: raise CommonError("OpenAI API is not available. Please set OPENAI_API_KEY.") - data = openai.Embedding.create(input=input, model=model.name)["data"] - ret_value = list(map(lambda x: x["embedding"], data)) + data = openai.embeddings.create(input=input, model=model.name).data + ret_value = list(map(lambda x: x.embedding, data)) return ret_value diff --git a/lib/shared/layers/python-sdk/python/genai_core/models.py b/lib/shared/layers/python-sdk/python/genai_core/models.py index a72580266..0b500457b 100644 --- a/lib/shared/layers/python-sdk/python/genai_core/models.py +++ b/lib/shared/layers/python-sdk/python/genai_core/models.py @@ -39,21 +39,22 @@ def list_openai_models(): if not openai: return None - models = openai.Model.list() + models = [] + for model in openai.models.list(): + if model.id.startswith("gpt"): + models.append( + { + "provider": Provider.OPENAI.value, + "name": model.id, + "streaming": True, + "inputModalities": [Modality.TEXT.value], + "outputModalities": [Modality.TEXT.value], + "interface": ModelInterface.LANGCHAIN.value, + "ragSupported": True, + } + ) - return [ - { - "provider": Provider.OPENAI.value, - "name": model["id"], - "streaming": True, - "inputModalities": [Modality.TEXT.value], - "outputModalities": [Modality.TEXT.value], - "interface": ModelInterface.LANGCHAIN.value, - "ragSupported": True, - } - for model in models.data - if model["id"].startswith("gpt") - ] + return models def list_azure_openai_models():