roughed in openai 1.x

This commit is contained in:
Paul Gauthier 2023-12-05 07:37:05 -08:00
parent fd34766aa9
commit 6ebc142377
15 changed files with 136 additions and 110 deletions

View file

@ -6,11 +6,11 @@ import openai
import requests
# from diskcache import Cache
from openai.error import (
from openai import (
APIConnectionError,
APIError,
InternalServerError,
RateLimitError,
ServiceUnavailableError,
Timeout,
)
@ -24,7 +24,7 @@ CACHE = None
(
Timeout,
APIError,
ServiceUnavailableError,
InternalServerError,
RateLimitError,
APIConnectionError,
requests.exceptions.ConnectionError,
@ -34,7 +34,7 @@ CACHE = None
f"{details.get('exception','Exception')}\nRetry in {details['wait']:.1f} seconds."
),
)
def send_with_retries(model_name, messages, functions, stream):
def send_with_retries(client, model_name, messages, functions, stream):
kwargs = dict(
model=model_name,
messages=messages,
@ -44,15 +44,6 @@ def send_with_retries(model_name, messages, functions, stream):
if functions is not None:
kwargs["functions"] = functions
# we are abusing the openai object to stash these values
if hasattr(openai, "api_deployment_id"):
kwargs["deployment_id"] = openai.api_deployment_id
if hasattr(openai, "api_engine"):
kwargs["engine"] = openai.api_engine
if "openrouter.ai" in openai.api_base:
kwargs["headers"] = {"HTTP-Referer": "http://aider.chat", "X-Title": "Aider"}
key = json.dumps(kwargs, sort_keys=True).encode()
# Generate SHA1 hash of kwargs and append it to chat_completion_call_hashes
@ -61,7 +52,7 @@ def send_with_retries(model_name, messages, functions, stream):
if not stream and CACHE is not None and key in CACHE:
return hash_object, CACHE[key]
res = openai.ChatCompletion.create(**kwargs)
res = client.chat.completions.create(**kwargs)
if not stream and CACHE is not None:
CACHE[key] = res
@ -69,14 +60,15 @@ def send_with_retries(model_name, messages, functions, stream):
return hash_object, res
def simple_send_with_retries(model_name, messages):
def simple_send_with_retries(client, model_name, messages):
try:
_hash, response = send_with_retries(
client=client,
model_name=model_name,
messages=messages,
functions=None,
stream=False,
)
return response.choices[0].message.content
except (AttributeError, openai.error.InvalidRequestError):
except (AttributeError, openai.BadRequestError):
return