From 984379fe71d294136d7d52bfb4d6c8ed053aed1e Mon Sep 17 00:00:00 2001 From: Sander Hilven Date: Fri, 1 Sep 2023 09:53:17 +0200 Subject: [PATCH 1/5] Adde Azure OpenAI endpoint. Tested and confirmed working. --- pilot/.env.example | 5 +++++ pilot/utils/llm_connection.py | 42 +++++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/pilot/.env.example b/pilot/.env.example index d53210e..5041cce 100644 --- a/pilot/.env.example +++ b/pilot/.env.example @@ -1,4 +1,9 @@ +#OPENAI or AZURE +ENDPOINT=OPENAI OPENAI_API_KEY= +AZURE_API_KEY= +AZURE_ENDPOINT= +AZURE_MODEL_NAME= DB_NAME=gpt-pilot DB_HOST=localhost DB_PORT=5432 diff --git a/pilot/utils/llm_connection.py b/pilot/utils/llm_connection.py index c191d8e..cace0d5 100644 --- a/pilot/utils/llm_connection.py +++ b/pilot/utils/llm_connection.py @@ -46,8 +46,15 @@ def get_tokens_in_messages(messages: List[str]) -> int: tokenized_messages = [tokenizer.encode(message['content']) for message in messages] return sum(len(tokens) for tokens in tokenized_messages) - -def num_tokens_from_functions(functions, model="gpt-4"): +# Check if the ENDPOINT is AZURE +endpoint = os.getenv('ENDPOINT') +if endpoint == 'AZURE': + # If yes, get the model name from .ENV file + model = os.getenv('AZURE_MODEL_NAME') +else: + model="gpt-4" + +def num_tokens_from_functions(functions, model=model): """Return the number of tokens used by a list of functions.""" encoding = tiktoken.get_encoding("cl100k_base") @@ -94,7 +101,7 @@ def create_gpt_chat_completion(messages: List[dict], req_type, min_tokens=MIN_TO raise ValueError(f'Too many tokens in messages: {tokens_in_messages}. Please try a different test.') gpt_data = { - 'model': 'gpt-4', + 'model': model, 'n': 1, 'max_tokens': min(4096, MAX_GPT_MODEL_TOKENS - tokens_in_messages), 'temperature': 1, @@ -172,15 +179,32 @@ def stream_gpt_completion(data, req_type): # spinner = spinner_start(colored("Waiting for OpenAI API response...", 'yellow')) # print(colored("Stream response from OpenAI:", 'yellow')) api_key = os.getenv("OPENAI_API_KEY") + azure_api_key = os.getenv('AZURE_API_KEY') + headers = {'Content-Type': 'application/json', 'api-key': azure_api_key} + openai_endpoint = 'https://api.openai.com/v1/chat/completions' logger.info(f'Request data: {data}') - response = requests.post( - 'https://api.openai.com/v1/chat/completions', - headers={'Content-Type': 'application/json', 'Authorization': 'Bearer ' + api_key}, - json=data, - stream=True - ) + # Check if the ENDPOINT is AZURE + if endpoint == 'AZURE': + # If yes, get the AZURE_ENDPOINT from .ENV file + azure_endpoint = os.getenv('AZURE_ENDPOINT') + + # Send the request to the Azure endpoint + response = requests.post( + azure_endpoint + '/openai/deployments/GPT-4/chat/completions?api-version=2023-05-15', + headers=headers, + json=data, + stream=True + ) + else: + # If not, send the request to the OpenAI endpoint + response = requests.post( + openai_endpoint, + headers=headers, + json=data, + stream=True + ) # Log the response status code and message logger.info(f'Response status code: {response.status_code}') From 660047a071ec685a2217188a3b4d6c07f900dc6b Mon Sep 17 00:00:00 2001 From: Sander Hilven Date: Fri, 1 Sep 2023 09:56:39 +0200 Subject: [PATCH 2/5] Hardcoded model in endpoint URL, now fixed. --- pilot/utils/llm_connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pilot/utils/llm_connection.py b/pilot/utils/llm_connection.py index cace0d5..baeda37 100644 --- a/pilot/utils/llm_connection.py +++ b/pilot/utils/llm_connection.py @@ -192,7 +192,7 @@ def stream_gpt_completion(data, req_type): # Send the request to the Azure endpoint response = requests.post( - azure_endpoint + '/openai/deployments/GPT-4/chat/completions?api-version=2023-05-15', + azure_endpoint + '/openai/deployments/' + model + '/chat/completions?api-version=2023-05-15', headers=headers, json=data, stream=True From a4d520763f33a2c097abd94b58fa9fae9848b0ad Mon Sep 17 00:00:00 2001 From: Sander Hilven Date: Fri, 1 Sep 2023 10:34:12 +0200 Subject: [PATCH 3/5] Added model selection to .env and update readme --- README.md | 2 +- pilot/.env.example | 3 ++- pilot/utils/llm_connection.py | 8 ++------ 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 32fe06e..6c798a0 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Obviously, it still can't create any production-ready app but the general concep 5. `pip install -r requirements.txt` (install the dependencies) 6. `cd pilot` 7. `mv .env.example .env` (create the .env file) -8. Add your OpenAI API key and the database info to the `.env` file +8. Add your environment (OpenAI/Azure), your API key and the database info to the `.env` file 9. `python db_init.py` (initialize the database) 10. `python main.py` (start GPT Pilot) diff --git a/pilot/.env.example b/pilot/.env.example index 5041cce..da30616 100644 --- a/pilot/.env.example +++ b/pilot/.env.example @@ -3,7 +3,8 @@ ENDPOINT=OPENAI OPENAI_API_KEY= AZURE_API_KEY= AZURE_ENDPOINT= -AZURE_MODEL_NAME= +#In case of Azure endpoint, change this to your deployed model name +MODEL_NAME=gpt-4 DB_NAME=gpt-pilot DB_HOST=localhost DB_PORT=5432 diff --git a/pilot/utils/llm_connection.py b/pilot/utils/llm_connection.py index baeda37..c53f909 100644 --- a/pilot/utils/llm_connection.py +++ b/pilot/utils/llm_connection.py @@ -46,13 +46,9 @@ def get_tokens_in_messages(messages: List[str]) -> int: tokenized_messages = [tokenizer.encode(message['content']) for message in messages] return sum(len(tokens) for tokens in tokenized_messages) -# Check if the ENDPOINT is AZURE +#get endpoint and model name from .ENV file +model = os.getenv('MODEL_NAME') endpoint = os.getenv('ENDPOINT') -if endpoint == 'AZURE': - # If yes, get the model name from .ENV file - model = os.getenv('AZURE_MODEL_NAME') -else: - model="gpt-4" def num_tokens_from_functions(functions, model=model): """Return the number of tokens used by a list of functions.""" From 34a53972093355992324b9c38f8720257d6139eb Mon Sep 17 00:00:00 2001 From: Sander Hilven Date: Fri, 1 Sep 2023 10:41:39 +0200 Subject: [PATCH 4/5] Added MAX_TOKENS to the .env as a variable --- pilot/.env.example | 1 + pilot/const/llm.py | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pilot/.env.example b/pilot/.env.example index da30616..e7b661c 100644 --- a/pilot/.env.example +++ b/pilot/.env.example @@ -5,6 +5,7 @@ AZURE_API_KEY= AZURE_ENDPOINT= #In case of Azure endpoint, change this to your deployed model name MODEL_NAME=gpt-4 +MAX_TOKENS=8192 DB_NAME=gpt-pilot DB_HOST=localhost DB_PORT=5432 diff --git a/pilot/const/llm.py b/pilot/const/llm.py index ca42267..8ae8ab8 100644 --- a/pilot/const/llm.py +++ b/pilot/const/llm.py @@ -1,4 +1,5 @@ +import os +MAX_GPT_MODEL_TOKENS = int(os.getenv('MAX_TOKENS')) MIN_TOKENS_FOR_GPT_RESPONSE = 600 -MAX_GPT_MODEL_TOKENS = 8192 MAX_QUESTIONS = 5 -END_RESPONSE = "EVERYTHING_CLEAR" +END_RESPONSE = "EVERYTHING_CLEAR" \ No newline at end of file From a9ead6ecbbfa62a8aa1cf9d6f05a49d423f1a8af Mon Sep 17 00:00:00 2001 From: Zvonimir Sabljic Date: Tue, 5 Sep 2023 22:50:48 +0200 Subject: [PATCH 5/5] Fix to enable regular OpenAI access --- pilot/utils/llm_connection.py | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/pilot/utils/llm_connection.py b/pilot/utils/llm_connection.py index c53f909..8fce916 100644 --- a/pilot/utils/llm_connection.py +++ b/pilot/utils/llm_connection.py @@ -174,33 +174,25 @@ def stream_gpt_completion(data, req_type): # spinner = spinner_start(colored("Waiting for OpenAI API response...", 'yellow')) # print(colored("Stream response from OpenAI:", 'yellow')) - api_key = os.getenv("OPENAI_API_KEY") - azure_api_key = os.getenv('AZURE_API_KEY') - headers = {'Content-Type': 'application/json', 'api-key': azure_api_key} - openai_endpoint = 'https://api.openai.com/v1/chat/completions' logger.info(f'Request data: {data}') # Check if the ENDPOINT is AZURE if endpoint == 'AZURE': # If yes, get the AZURE_ENDPOINT from .ENV file - azure_endpoint = os.getenv('AZURE_ENDPOINT') - - # Send the request to the Azure endpoint - response = requests.post( - azure_endpoint + '/openai/deployments/' + model + '/chat/completions?api-version=2023-05-15', - headers=headers, - json=data, - stream=True - ) + endpoint_url = os.getenv('AZURE_ENDPOINT') + '/openai/deployments/' + model + '/chat/completions?api-version=2023-05-15' + headers = {'Content-Type': 'application/json', 'api-key': os.getenv('AZURE_API_KEY')} else: # If not, send the request to the OpenAI endpoint - response = requests.post( - openai_endpoint, - headers=headers, - json=data, - stream=True - ) + headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + os.getenv("OPENAI_API_KEY")} + endpoint_url = 'https://api.openai.com/v1/chat/completions' + + response = requests.post( + endpoint_url, + headers=headers, + json=data, + stream=True + ) # Log the response status code and message logger.info(f'Response status code: {response.status_code}')