From 9d33a75083ef86c73bf68413210d7e996bb532bc Mon Sep 17 00:00:00 2001 From: Peter Edwards Date: Tue, 11 Apr 2023 13:45:37 +0200 Subject: [PATCH] Changes for Azure embedding handling --- .env.template | 1 + README.md | 2 +- scripts/config.py | 1 + scripts/memory/base.py | 7 ++++++- 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.env.template b/.env.template index 01735615..3a86d1c0 100644 --- a/.env.template +++ b/.env.template @@ -10,6 +10,7 @@ USE_AZURE=False OPENAI_AZURE_API_BASE=your-base-url-for-azure OPENAI_AZURE_API_VERSION=api-version-for-azure OPENAI_AZURE_DEPLOYMENT_ID=deployment-id-for-azure +OPENAI_AZURE_EMBEDDING_DEPLOYMENT_ID=embedding-deployment-id-for-azure IMAGE_PROVIDER=dalle HUGGINGFACE_API_TOKEN= USE_MAC_OS_TTS=False diff --git a/README.md b/README.md index 749c8791..db4df5d4 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ pip install -r requirements.txt 4. Rename `.env.template` to `.env` and fill in your `OPENAI_API_KEY`. If you plan to use Speech Mode, fill in your `ELEVEN_LABS_API_KEY` as well. - Obtain your OpenAI API key from: https://platform.openai.com/account/api-keys. - Obtain your ElevenLabs API key from: https://elevenlabs.io. You can view your xi-api-key using the "Profile" tab on the website. - - If you want to use GPT on an Azure instance, set `USE_AZURE` to `True` and provide the `OPENAI_AZURE_API_BASE`, `OPENAI_AZURE_API_VERSION` and `OPENAI_AZURE_DEPLOYMENT_ID` values as explained here: https://pypi.org/project/openai/ in the `Microsoft Azure Endpoints` section + - If you want to use GPT on an Azure instance, set `USE_AZURE` to `True` and provide the `OPENAI_AZURE_API_BASE`, `OPENAI_AZURE_API_VERSION` and `OPENAI_AZURE_DEPLOYMENT_ID` values as explained here: https://pypi.org/project/openai/ in the `Microsoft Azure Endpoints` section. In addition to your GPT deployment, you will need to deploy a `text-embedding-ada-002 (Version 2)` model which will have a different deployment id, please set `OPENAI_AZURE_EMBEDDING_DEPLOYMENT_ID` accordingly (see here: https://learn.microsoft.com/en-us/azure/cognitive-services/openai/tutorials/embeddings?tabs=command-line). ## 🔧 Usage diff --git a/scripts/config.py b/scripts/config.py index 27cc946c..3ea8a640 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -49,6 +49,7 @@ class Config(metaclass=Singleton): self.openai_api_base = os.getenv("OPENAI_AZURE_API_BASE") self.openai_api_version = os.getenv("OPENAI_AZURE_API_VERSION") self.openai_deployment_id = os.getenv("OPENAI_AZURE_DEPLOYMENT_ID") + self.openai_embedding_deployment_id = os.getenv("OPENAI_AZURE_EMBEDDING_DEPLOYMENT_ID") openai.api_type = "azure" openai.api_base = self.openai_api_base openai.api_version = self.openai_api_version diff --git a/scripts/memory/base.py b/scripts/memory/base.py index d7ab7fcf..30372851 100644 --- a/scripts/memory/base.py +++ b/scripts/memory/base.py @@ -1,12 +1,17 @@ """Base class for memory providers.""" import abc from config import AbstractSingleton +from config import Config import openai +cfg = Config() def get_ada_embedding(text): text = text.replace("\n", " ") - return openai.Embedding.create(input=[text], model="text-embedding-ada-002")["data"][0]["embedding"] + if cfg.use_azure: + return openai.Embedding.create(input=[text], engine=cfg.openai_embedding_deployment_id)["data"][0]["embedding"] + else: + return openai.Embedding.create(input=[text], model="text-embedding-ada-002")["data"][0]["embedding"] class MemoryProviderSingleton(AbstractSingleton):