I am trying to upgrade the OpenAI 0.28 version to 1.2.2. For doing that I defined the client and replaced the openai chatcompletion with client.
Below is the constant file which will be used in the main file to get the keys from environment.
OPENAI_API_BASE = 'api_base'
OPENAI_API_ENGINE = 'gpt-35-turbo-0613'
OPENAI_API_KEY = 'api_key'
OPENAI_API_TYPE = 'api_type'
OPENAI_API_VERSION = 'api_version'
OPENAI_API_TEMPERATURE = 0
This is the main file which I am using for completing the text. In this file I wanted to make upgrade the OpenAI version. I don’t want to change the structure of the below code
ROLE_KEY = 'role'
ROLE_VALUE = 'user'
CONTENT_KEY = 'content'
@backoff.on_exception(
backoff.expo,
(openai.APIError, openai.Timeout, openai.RateLimitError, KeyError),
max_tries=MAX_ATTEMPT_NUM
)
def _get_completion(it_app: str):
"""
Function to make an API call to OpenAI Chat Completion endpoint and receive a response.
"""
prompt = const.CLASSIFIER_PROMPT.format(it_app)
messages = [{ROLE_KEY: ROLE_VALUE, CONTENT_KEY: prompt}]
try:
client = AzureOpenAI(
azure_endpoint=const.OPENAI_API_BASE,
api_key=const.OPENAI_API_KEY,
api_version=const.OPENAI_API_VERSION
)
response = client.chat.completions.create(
model=const.OPENAI_API_ENGINE,
messages=messages,
temperature=const.OPENAI_API_TEMPERATURE,
)
return response.choices[0].message.content
except openai.BadRequestError as e:
print(str(e))
class TextCompletion():
def __init__(self, config: dict[str, str, str, str]):
"""
:param config: dictionary containing the following keys: 'api_type', 'api_key', 'api_base', 'api_version'
"""
super().__init__()
openai.api_type = config[const.OPENAI_API_TYPE]
openai.api_key = config[const.OPENAI_API_KEY]
openai.api_base = config[const.OPENAI_API_BASE]
openai.api_version = config[const.OPENAI_API_VERSION]
def predict(self, model_input: str):
openai_results = self._get_completion(model_input)
return openai_results
This is the code I am using to test
config = {
const.OPENAI_API_BASE : 'api_base',
const.OPENAI_API_ENGINE : 'api_engine',
const.OPENAI_API_KEY : 'api_key',
const.OPENAI_API_TYPE : 'api_type',
const.OPENAI_API_VERSION : 'api_version',
}
text_completion = TextCompletion(config)
print(test_classifier.predict(model_input='which game is famound in India?'))
Its throwing APIConnection error, while this works for OpenAI 0.28 version.