From a868a39b88a89483bca556f6026e56b3d9c9d991 Mon Sep 17 00:00:00 2001 From: Peter Edwards Date: Wed, 5 Apr 2023 19:44:28 +0200 Subject: [PATCH 1/2] Added functionality to allow the use of GPT on a Microsoft Azure instance --- .env.template | 6 +++++- README.md | 1 + scripts/config.py | 10 ++++++++++ scripts/llm_utils.py | 21 +++++++++++++++------ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/.env.template b/.env.template index c64d85028f02..b8e896639abb 100644 --- a/.env.template +++ b/.env.template @@ -3,4 +3,8 @@ ELEVENLABS_API_KEY=your-elevenlabs-api-key SMART_LLM_MODEL="gpt-4" FAST_LLM_MODEL="gpt-3.5-turbo" GOOGLE_API_KEY= -CUSTOM_SEARCH_ENGINE_ID= \ No newline at end of file +CUSTOM_SEARCH_ENGINE_ID= +USE_AZURE=False +OPENAI_API_BASE=your-base-url-for-azure +OPENAI_API_VERSION=api-version-for-azure +OPENAI_DEPLOYMENT_ID=deployment-id-for-azure \ No newline at end of file diff --git a/README.md b/README.md index d66a60222449..6acb0f6c5772 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,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_API_BASE`, `OPENAI_API_VERSION` and `OPENAI_DEPLOYMENT_ID` values as explained here: https://pypi.org/project/openai/ in the `Microsoft Azure Endpoints` section ## 🔧 Usage diff --git a/scripts/config.py b/scripts/config.py index 766cb94f415f..d97ded9cac85 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -34,6 +34,16 @@ def __init__(self): self.smart_token_limit = int(os.getenv("SMART_TOKEN_LIMIT", 8000)) self.openai_api_key = os.getenv("OPENAI_API_KEY") + self.use_azure = False + self.use_azure = os.getenv("USE_AZURE") + if self.use_azure: + self.openai_api_base = os.getenv("OPENAI_API_BASE") + self.openai_api_version = os.getenv("OPENAI_API_VERSION") + self.openai_deployment_id = os.getenv("OPENAI_DEPLOYMENT_ID") + openai.api_type = "azure" + openai.api_base = self.openai_api_base + openai.api_version = self.openai_api_version + self.elevenlabs_api_key = os.getenv("ELEVENLABS_API_KEY") self.google_api_key = os.getenv("GOOGLE_API_KEY") diff --git a/scripts/llm_utils.py b/scripts/llm_utils.py index 41f396250f26..5a471ab7a124 100644 --- a/scripts/llm_utils.py +++ b/scripts/llm_utils.py @@ -6,11 +6,20 @@ # Overly simple abstraction until we create something better def create_chat_completion(messages, model=None, temperature=None, max_tokens=None)->str: - response = openai.ChatCompletion.create( - model=model, - messages=messages, - temperature=temperature, - max_tokens=max_tokens - ) + if cfg.use_azure: + response = openai.ChatCompletion.create( + deployment_id=cfg.openai_deployment_id, + model=model, + messages=messages, + temperature=temperature, + max_tokens=max_tokens + ) + else: + response = openai.ChatCompletion.create( + model=model, + messages=messages, + temperature=temperature, + max_tokens=max_tokens + ) return response.choices[0].message["content"] From 34b6f47f7128c1eb69309beddab6dac65a0d1236 Mon Sep 17 00:00:00 2001 From: Peter Edwards Date: Thu, 6 Apr 2023 09:15:45 +0200 Subject: [PATCH 2/2] Fix for boolean eval from .env --- scripts/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/config.py b/scripts/config.py index d97ded9cac85..a2ddd4309670 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -35,7 +35,7 @@ def __init__(self): self.openai_api_key = os.getenv("OPENAI_API_KEY") self.use_azure = False - self.use_azure = os.getenv("USE_AZURE") + self.use_azure = os.getenv("USE_AZURE") == 'True' if self.use_azure: self.openai_api_base = os.getenv("OPENAI_API_BASE") self.openai_api_version = os.getenv("OPENAI_API_VERSION")