Spaces:
Runtime error
Runtime error
import json | |
import openai | |
class OpenAIService: | |
def __init__(self, api_key, api_endpoint, api_version, deployment_name): | |
openai.api_key = api_key | |
openai.api_base = api_endpoint | |
openai.api_type = "azure" | |
openai.api_version = api_version | |
self.openai_deployment_name = deployment_name | |
def generate_response(self, system_prompt, user_prompt, openai_function): | |
try: | |
response = openai.ChatCompletion.create(engine=self.openai_deployment_name, | |
messages=[ | |
{"role": "system", "content": system_prompt}, | |
{"role": "user", "content": user_prompt}, | |
], | |
functions = openai_function, | |
function_call = {"name": openai_function[0]['name']} | |
) | |
openai_output = response["choices"] | |
usage = response["usage"].to_dict() | |
print(response) | |
function_output = json.loads(openai_output[0].message.function_call.arguments, strict=False) | |
print(function_output) | |
openai_out = { | |
'function_output':function_output, | |
'usage':usage, | |
'status':'Success' | |
} | |
return openai_out | |
except Exception as ex: | |
print(f"Openai generate response exceptin ::: {ex}") | |
openai_out = { | |
'function_output':{}, | |
'usage':{}, | |
'status':ex | |
} | |
return openai_out | |