2023-09-09 17:35:45 +03:00
|
|
|
import os
|
2023-07-06 00:21:08 +03:00
|
|
|
|
2023-11-28 23:07:46 +09:00
|
|
|
import boto3
|
2023-08-07 13:26:28 +03:00
|
|
|
import litellm
|
2023-07-06 00:21:08 +03:00
|
|
|
import openai
|
2023-08-07 13:26:28 +03:00
|
|
|
from litellm import acompletion
|
2024-03-06 12:13:54 +02:00
|
|
|
from tenacity import retry, retry_if_exception_type, stop_after_attempt
|
2023-12-14 07:44:13 +08:00
|
|
|
from pr_agent.algo.ai_handlers.base_ai_handler import BaseAiHandler
|
2023-08-01 14:43:26 +03:00
|
|
|
from pr_agent.config_loader import get_settings
|
2023-10-16 14:56:00 +03:00
|
|
|
from pr_agent.log import get_logger
|
|
|
|
|
2023-08-07 13:26:28 +03:00
|
|
|
OPENAI_RETRIES = 5
|
|
|
|
|
2023-07-06 00:21:08 +03:00
|
|
|
|
2023-12-14 07:44:13 +08:00
|
|
|
class LiteLLMAIHandler(BaseAiHandler):
|
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-11-07 09:13:08 +00:00
|
|
|
self.azure = False
|
2023-11-28 20:11:40 +09:00
|
|
|
self.aws_bedrock_client = None
|
2024-03-06 12:13:54 +02:00
|
|
|
self.api_base = None
|
|
|
|
self.repetition_penalty = None
|
2023-11-07 09:13:08 +00:00
|
|
|
if get_settings().get("OPENAI.KEY", None):
|
2023-08-01 14:43:26 +03:00
|
|
|
openai.api_key = get_settings().openai.key
|
2023-08-03 16:05:46 -07:00
|
|
|
litellm.openai_key = get_settings().openai.key
|
2023-11-07 09:13:08 +00:00
|
|
|
if get_settings().get("litellm.use_client"):
|
|
|
|
litellm_token = get_settings().get("litellm.LITELLM_TOKEN")
|
|
|
|
assert litellm_token, "LITELLM_TOKEN is required"
|
|
|
|
os.environ["LITELLM_TOKEN"] = litellm_token
|
|
|
|
litellm.use_client = True
|
|
|
|
if get_settings().get("OPENAI.ORG", None):
|
|
|
|
litellm.organization = get_settings().openai.org
|
|
|
|
if get_settings().get("OPENAI.API_TYPE", None):
|
|
|
|
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):
|
|
|
|
litellm.api_version = get_settings().openai.api_version
|
|
|
|
if get_settings().get("OPENAI.API_BASE", None):
|
|
|
|
litellm.api_base = get_settings().openai.api_base
|
|
|
|
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
|
|
|
|
if get_settings().get("REPLICATE.KEY", None):
|
|
|
|
litellm.replicate_key = get_settings().replicate.key
|
|
|
|
if get_settings().get("HUGGINGFACE.KEY", None):
|
|
|
|
litellm.huggingface_key = get_settings().huggingface.key
|
2024-03-06 12:13:54 +02:00
|
|
|
if get_settings().get("HUGGINGFACE.API_BASE", None) and 'huggingface' in get_settings().config.model:
|
|
|
|
litellm.api_base = get_settings().huggingface.api_base
|
|
|
|
self.api_base = get_settings().huggingface.api_base
|
|
|
|
if get_settings().get("HUGGINGFACE.REPITITION_PENALTY", None):
|
|
|
|
self.repetition_penalty = float(get_settings().huggingface.repetition_penalty)
|
2023-11-07 09:13:08 +00:00
|
|
|
if get_settings().get("VERTEXAI.VERTEX_PROJECT", None):
|
|
|
|
litellm.vertex_project = get_settings().vertexai.vertex_project
|
|
|
|
litellm.vertex_location = get_settings().get(
|
|
|
|
"VERTEXAI.VERTEX_LOCATION", None
|
|
|
|
)
|
2023-11-28 20:11:40 +09:00
|
|
|
if get_settings().get("AWS.BEDROCK_REGION", None):
|
2023-11-28 20:59:21 +09:00
|
|
|
litellm.AmazonAnthropicConfig.max_tokens_to_sample = 2000
|
2023-11-28 20:11:40 +09:00
|
|
|
self.aws_bedrock_client = boto3.client(
|
|
|
|
service_name="bedrock-runtime",
|
|
|
|
region_name=get_settings().aws.bedrock_region,
|
|
|
|
)
|
2023-07-06 00:21:08 +03:00
|
|
|
|
2023-08-07 16:17:06 +03:00
|
|
|
@property
|
|
|
|
def deployment_id(self):
|
|
|
|
"""
|
|
|
|
Returns the deployment ID for the OpenAI API.
|
|
|
|
"""
|
|
|
|
return get_settings().get("OPENAI.DEPLOYMENT_ID", None)
|
|
|
|
|
2024-03-06 12:13:54 +02:00
|
|
|
@retry(
|
|
|
|
retry=retry_if_exception_type((openai.APIError, openai.APIConnectionError, openai.Timeout)), # No retry on RateLimitError
|
|
|
|
stop=stop_after_attempt(OPENAI_RETRIES)
|
|
|
|
)
|
2023-08-21 09:07:21 +03:00
|
|
|
async def chat_completion(self, model: str, system: str, user: str, temperature: float = 0.2):
|
2023-07-06 00:21:08 +03:00
|
|
|
try:
|
2024-02-25 09:58:58 +02:00
|
|
|
resp, finish_reason = None, None
|
2023-08-07 22:42:53 +03:00
|
|
|
deployment_id = self.deployment_id
|
2023-10-06 08:12:11 +03:00
|
|
|
if self.azure:
|
2023-10-06 08:31:31 +03:00
|
|
|
model = 'azure/' + model
|
2023-10-16 14:56:00 +03:00
|
|
|
messages = [{"role": "system", "content": system}, {"role": "user", "content": user}]
|
2023-11-28 20:11:40 +09:00
|
|
|
kwargs = {
|
|
|
|
"model": model,
|
|
|
|
"deployment_id": deployment_id,
|
|
|
|
"messages": messages,
|
|
|
|
"temperature": temperature,
|
|
|
|
"force_timeout": get_settings().config.ai_timeout,
|
2024-03-06 12:13:54 +02:00
|
|
|
"api_base" : self.api_base,
|
2023-11-28 20:11:40 +09:00
|
|
|
}
|
|
|
|
if self.aws_bedrock_client:
|
|
|
|
kwargs["aws_bedrock_client"] = self.aws_bedrock_client
|
2024-03-06 12:13:54 +02:00
|
|
|
if self.repetition_penalty:
|
|
|
|
kwargs["repetition_penalty"] = self.repetition_penalty
|
2024-02-24 16:47:23 +02:00
|
|
|
|
2024-02-25 10:45:15 +02:00
|
|
|
get_logger().debug("Prompts", artifact={"system": system, "user": user})
|
2024-03-06 12:13:54 +02:00
|
|
|
|
|
|
|
if get_settings().config.verbosity_level >= 2:
|
|
|
|
get_logger().info(f"\nSystem prompt:\n{system}")
|
|
|
|
get_logger().info(f"\nUser prompt:\n{user}")
|
|
|
|
|
2023-11-28 20:11:40 +09:00
|
|
|
response = await acompletion(**kwargs)
|
2024-03-06 12:13:54 +02:00
|
|
|
except (openai.APIError, openai.Timeout) as e:
|
2023-10-16 14:56:00 +03:00
|
|
|
get_logger().error("Error during OpenAI inference: ", e)
|
2023-07-06 00:21:08 +03:00
|
|
|
raise
|
2024-03-06 12:13:54 +02:00
|
|
|
except (openai.RateLimitError) as e:
|
2023-10-16 14:56:00 +03:00
|
|
|
get_logger().error("Rate limit error during OpenAI inference: ", e)
|
2023-07-20 15:01:12 +03:00
|
|
|
raise
|
2023-07-20 15:02:34 +03:00
|
|
|
except (Exception) as e:
|
2023-10-16 14:56:00 +03:00
|
|
|
get_logger().error("Unknown error during OpenAI inference: ", e)
|
2024-03-06 12:13:54 +02:00
|
|
|
raise openai.APIError from e
|
2023-08-03 16:05:46 -07:00
|
|
|
if response is None or len(response["choices"]) == 0:
|
2024-03-06 12:13:54 +02:00
|
|
|
raise openai.APIError
|
2024-02-25 09:58:58 +02:00
|
|
|
else:
|
|
|
|
resp = response["choices"][0]['message']['content']
|
|
|
|
finish_reason = response["choices"][0]["finish_reason"]
|
|
|
|
# usage = response.get("usage")
|
|
|
|
get_logger().debug(f"\nAI response:\n{resp}")
|
2024-02-25 10:45:15 +02:00
|
|
|
get_logger().debug("Full_response", artifact=response)
|
2024-02-24 16:47:23 +02:00
|
|
|
|
2024-03-06 12:13:54 +02:00
|
|
|
if get_settings().config.verbosity_level >= 2:
|
|
|
|
get_logger().info(f"\nAI response:\n{resp}")
|
|
|
|
|
2023-12-14 07:44:13 +08:00
|
|
|
return resp, finish_reason
|