Files
pr-agent/pr_agent/algo/ai_handler.py

101 lines
4.3 KiB
Python
Raw Normal View History

2023-07-06 00:21:08 +03:00
import logging
import openai
from openai.error import APIError, RateLimitError, Timeout, TryAgain
2023-07-06 00:21:08 +03:00
from retry import retry
2023-08-03 16:05:46 -07:00
import litellm
from litellm import acompletion
from pr_agent.config_loader import get_settings
2023-08-05 22:50:41 -07:00
import traceback
OPENAI_RETRIES=5
2023-07-06 00:21:08 +03:00
class AiHandler:
2023-07-20 10:51:21 +03:00
"""
This class handles interactions with the OpenAI API for chat completions.
It initializes the API key and other settings from a configuration file,
and provides a method for performing chat completions using the OpenAI ChatCompletion API.
"""
2023-07-06 00:21:08 +03:00
def __init__(self):
2023-07-20 10:51:21 +03:00
"""
Initializes the OpenAI API key and other settings from a configuration file.
Raises a ValueError if the OpenAI key is missing.
"""
2023-07-06 00:21:08 +03:00
try:
openai.api_key = get_settings().openai.key
2023-08-03 16:05:46 -07:00
litellm.openai_key = get_settings().openai.key
2023-08-05 22:50:41 -07:00
self.azure = False
if get_settings().get("OPENAI.ORG", None):
2023-08-05 22:50:41 -07:00
litellm.organization = get_settings().openai.org
if get_settings().get("OPENAI.API_TYPE", None):
2023-08-05 22:50:41 -07:00
if get_settings().openai.api_type == "azure":
self.azure = True
litellm.azure_key = get_settings().openai.key
if get_settings().get("OPENAI.API_VERSION", None):
2023-08-05 22:50:41 -07:00
litellm.api_version = get_settings().openai.api_version
if get_settings().get("OPENAI.API_BASE", None):
2023-08-03 16:05:46 -07:00
litellm.api_base = get_settings().openai.api_base
2023-08-05 22:50:41 -07:00
if get_settings().get("ANTHROPIC.KEY", None):
litellm.anthropic_key = get_settings().anthropic.key
if get_settings().get("COHERE.KEY", None):
litellm.cohere_key = get_settings().cohere.key
if get_settings().get("REPLICATE.KEY", None):
litellm.replicate_key = get_settings().replicate.key
2023-07-06 00:21:08 +03:00
except AttributeError as e:
raise ValueError("OpenAI key is required") from e
@property
def deployment_id(self):
"""
Returns the deployment ID for the OpenAI API.
"""
return get_settings().get("OPENAI.DEPLOYMENT_ID", None)
@retry(exceptions=(APIError, Timeout, TryAgain, AttributeError, RateLimitError),
2023-07-06 00:21:08 +03:00
tries=OPENAI_RETRIES, delay=2, backoff=2, jitter=(1, 3))
async def chat_completion(self, model: str, temperature: float, system: str, user: str):
2023-07-20 10:51:21 +03:00
"""
Performs a chat completion using the OpenAI ChatCompletion API.
Retries in case of API errors or timeouts.
Args:
model (str): The model to use for chat completion.
temperature (float): The temperature parameter for chat completion.
system (str): The system message for chat completion.
user (str): The user message for chat completion.
Returns:
tuple: A tuple containing the response and finish reason from the API.
Raises:
TryAgain: If the API response is empty or there are no choices in the response.
APIError: If there is an error during OpenAI inference.
Timeout: If there is a timeout during OpenAI inference.
TryAgain: If there is an attribute error during OpenAI inference.
"""
2023-07-06 00:21:08 +03:00
try:
response = await acompletion(
2023-07-06 00:21:08 +03:00
model=model,
2023-07-12 11:53:46 +03:00
deployment_id=self.deployment_id,
2023-07-06 00:21:08 +03:00
messages=[
{"role": "system", "content": system},
{"role": "user", "content": user}
],
temperature=temperature,
2023-08-05 22:50:41 -07:00
azure=self.azure
2023-07-06 00:21:08 +03:00
)
except (APIError, Timeout, TryAgain) as e:
logging.error("Error during OpenAI inference: ", e)
raise
except (RateLimitError) as e:
logging.error("Rate limit error during OpenAI inference: ", e)
raise
except (Exception) as e:
logging.error("Unknown error during OpenAI inference: ", e)
raise TryAgain from e
2023-08-03 16:05:46 -07:00
if response is None or len(response["choices"]) == 0:
2023-07-06 00:21:08 +03:00
raise TryAgain
2023-08-03 16:05:46 -07:00
resp = response["choices"][0]['message']['content']
finish_reason = response["choices"][0]["finish_reason"]
print(resp, finish_reason)
2023-07-20 10:51:21 +03:00
return resp, finish_reason