From 0134d3306624fa7f2cf861cb5b811a6fad505a75 Mon Sep 17 00:00:00 2001 From: "cody.kesler" Date: Thu, 18 Apr 2024 16:56:49 -0500 Subject: [PATCH 1/2] added azure openai model --- superpipe/clients.py | 23 ++++++++++++++++++----- superpipe/llm.py | 6 +++++- superpipe/models.py | 3 +++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/superpipe/clients.py b/superpipe/clients.py index daa23b1..3191143 100644 --- a/superpipe/clients.py +++ b/superpipe/clients.py @@ -1,6 +1,7 @@ import requests import os from openai import OpenAI +from openai import AzureOpenAI from anthropic import Anthropic from superpipe.models import * @@ -10,10 +11,17 @@ openrouter_models = [] -def init_openai(api_key, base_url=None): - openai_client = OpenAI(api_key=api_key, base_url=base_url) - client_for_model[gpt35] = openai_client - client_for_model[gpt4] = openai_client +def init_openai(api_key, base_url=None, api_version=None): + if base_url and api_version: + openai_client = AzureOpenAI(api_key=api_key, + azure_endpoint=base_url, + api_version=api_version) + client_for_model[gpt35_azure] = openai_client + client_for_model[gpt4_azure] = openai_client + else: + openai_client = OpenAI(api_key=api_key) + client_for_model[gpt35] = openai_client + client_for_model[gpt4] = openai_client def init_anthropic(api_key): @@ -45,8 +53,13 @@ def get_client(model): if client_for_model.get(gpt35) is None or \ client_for_model.get(gpt4) is None: api_key = os.getenv("OPENAI_API_KEY") + base_url, api_version = None, None + if client_for_model.get("OPENAI_API_BASE") is None: + base_url = os.getenv("OPENAI_API_BASE") + if client_for_model.get("OPEN_API_VERSION") is None: + api_version = os.getenv("OPENAI_API_VERSION") if api_key is not None: - init_openai(api_key) + init_openai(api_key, base_url, api_version) if client_for_model.get(claude3_haiku) is None or \ client_for_model.get(claude3_sonnet) is None or \ client_for_model.get(claude3_opus) is None: diff --git a/superpipe/llm.py b/superpipe/llm.py index 5c61bc9..d179cbe 100644 --- a/superpipe/llm.py +++ b/superpipe/llm.py @@ -192,7 +192,11 @@ def get_structured_llm_response_openai( model=gpt35, args: CompletionCreateParamsNonStreaming = {}) -> StructuredLLMResponse: system = "You are a helpful assistant designed to output JSON." - updated_args = {**args, "response_format": {"type": "json_object"}} + if model not in azure_ai_models: + updated_args = {**args, "response_format": {"type": "json_object"}} + else: + updated_args = args + response = get_llm_response_openai(prompt, model, updated_args, system) return StructuredLLMResponse( input_tokens=response.input_tokens, diff --git a/superpipe/models.py b/superpipe/models.py index 4b27759..24e656f 100644 --- a/superpipe/models.py +++ b/superpipe/models.py @@ -2,6 +2,9 @@ gpt4 = "gpt-4-turbo-preview" gpt35 = "gpt-3.5-turbo-0125" +gpt4_azure = "gpt4" +gpt35_azure = "gpt35model" +azure_ai_models = [gpt4_azure, gpt35_azure] mixtral = "mistralai/Mixtral-8x7B-Instruct-v0.1" mistral = "mistralai/Mistral-7B-Instruct-v0.1" codellama = "togethercomputer/CodeLlama-34b-Instruct" From 1dae58fc2466c28d115f4ef3c465999dfe34de5a Mon Sep 17 00:00:00 2001 From: "cody.kesler" Date: Wed, 1 May 2024 13:17:41 -0500 Subject: [PATCH 2/2] made fixes for AzureOpenAI --- superpipe/clients.py | 4 ++-- superpipe/llm.py | 8 ++++---- superpipe/models.py | 3 --- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/superpipe/clients.py b/superpipe/clients.py index 3191143..af2c23e 100644 --- a/superpipe/clients.py +++ b/superpipe/clients.py @@ -16,8 +16,8 @@ def init_openai(api_key, base_url=None, api_version=None): openai_client = AzureOpenAI(api_key=api_key, azure_endpoint=base_url, api_version=api_version) - client_for_model[gpt35_azure] = openai_client - client_for_model[gpt4_azure] = openai_client + client_for_model[gpt35] = openai_client + client_for_model[gpt4] = openai_client else: openai_client = OpenAI(api_key=api_key) client_for_model[gpt35] = openai_client diff --git a/superpipe/llm.py b/superpipe/llm.py index d179cbe..651ea61 100644 --- a/superpipe/llm.py +++ b/superpipe/llm.py @@ -192,12 +192,12 @@ def get_structured_llm_response_openai( model=gpt35, args: CompletionCreateParamsNonStreaming = {}) -> StructuredLLMResponse: system = "You are a helpful assistant designed to output JSON." - if model not in azure_ai_models: - updated_args = {**args, "response_format": {"type": "json_object"}} - else: - updated_args = args + updated_args = {**args, "response_format": {"type": "json_object"}} response = get_llm_response_openai(prompt, model, updated_args, system) + if response.error: # models before 1163 do not support response_format param + response = get_llm_response_openai(prompt, model, args, system) + return StructuredLLMResponse( input_tokens=response.input_tokens, output_tokens=response.output_tokens, diff --git a/superpipe/models.py b/superpipe/models.py index 24e656f..4b27759 100644 --- a/superpipe/models.py +++ b/superpipe/models.py @@ -2,9 +2,6 @@ gpt4 = "gpt-4-turbo-preview" gpt35 = "gpt-3.5-turbo-0125" -gpt4_azure = "gpt4" -gpt35_azure = "gpt35model" -azure_ai_models = [gpt4_azure, gpt35_azure] mixtral = "mistralai/Mixtral-8x7B-Instruct-v0.1" mistral = "mistralai/Mistral-7B-Instruct-v0.1" codellama = "togethercomputer/CodeLlama-34b-Instruct"