2023-12-14 09:05:53 +02:00
|
|
|
try:
|
2024-01-04 16:08:10 +02:00
|
|
|
from langchain.chat_models import ChatOpenAI, AzureChatOpenAI
|
2023-12-14 09:05:53 +02:00
|
|
|
from langchain.schema import SystemMessage, HumanMessage
|
2024-06-03 23:58:31 +08:00
|
|
|
except: # we don't enforce langchain as a dependency, so if it's not installed, just move on
|
2023-12-14 09:05:53 +02:00
|
|
|
pass
|
2023-12-12 23:52:50 +08:00
|
|
|
|
|
|
|
from pr_agent.algo.ai_handlers.base_ai_handler import BaseAiHandler
|
2023-12-12 23:03:49 +08:00
|
|
|
from pr_agent.config_loader import get_settings
|
|
|
|
from pr_agent.log import get_logger
|
|
|
|
|
2023-12-12 23:52:50 +08:00
|
|
|
from openai.error import APIError, RateLimitError, Timeout, TryAgain
|
2024-01-04 16:08:10 +02:00
|
|
|
from retry import retry
|
|
|
|
import functools
|
2023-12-12 23:52:50 +08:00
|
|
|
|
|
|
|
OPENAI_RETRIES = 5
|
|
|
|
|
2024-06-03 23:58:31 +08:00
|
|
|
|
2023-12-12 23:28:58 +08:00
|
|
|
class LangChainOpenAIHandler(BaseAiHandler):
|
2023-12-12 23:03:49 +08:00
|
|
|
def __init__(self):
|
|
|
|
# Initialize OpenAIHandler specific attributes here
|
2024-01-04 16:08:10 +02:00
|
|
|
super().__init__()
|
|
|
|
self.azure = get_settings().get("OPENAI.API_TYPE", "").lower() == "azure"
|
2023-12-12 23:03:49 +08:00
|
|
|
try:
|
2024-01-04 16:08:10 +02:00
|
|
|
if self.azure:
|
|
|
|
# using a partial function so we can set the deployment_id later to support fallback_deployments
|
|
|
|
# but still need to access the other settings now so we can raise a proper exception if they're missing
|
|
|
|
self._chat = functools.partial(
|
|
|
|
lambda **kwargs: AzureChatOpenAI(**kwargs),
|
|
|
|
openai_api_key=get_settings().openai.key,
|
|
|
|
openai_api_base=get_settings().openai.api_base,
|
|
|
|
openai_api_version=get_settings().openai.api_version,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self._chat = ChatOpenAI(openai_api_key=get_settings().openai.key)
|
2023-12-12 23:03:49 +08:00
|
|
|
except AttributeError as e:
|
2024-01-04 16:08:10 +02:00
|
|
|
if getattr(e, "name"):
|
|
|
|
raise ValueError(f"OpenAI {e.name} is required") from e
|
|
|
|
else:
|
|
|
|
raise e
|
2024-06-03 23:58:31 +08:00
|
|
|
|
2023-12-12 23:28:58 +08:00
|
|
|
@property
|
|
|
|
def chat(self):
|
2024-01-04 16:08:10 +02:00
|
|
|
if self.azure:
|
|
|
|
# we must set the deployment_id only here (instead of the __init__ method) to support fallback_deployments
|
|
|
|
return self._chat(deployment_name=self.deployment_id)
|
|
|
|
else:
|
|
|
|
return self._chat
|
2023-12-12 23:28:58 +08:00
|
|
|
|
2023-12-12 23:03:49 +08:00
|
|
|
@property
|
|
|
|
def deployment_id(self):
|
|
|
|
"""
|
|
|
|
Returns the deployment ID for the OpenAI API.
|
|
|
|
"""
|
|
|
|
return get_settings().get("OPENAI.DEPLOYMENT_ID", None)
|
2024-06-03 23:58:31 +08:00
|
|
|
|
2023-12-12 23:52:50 +08:00
|
|
|
@retry(exceptions=(APIError, Timeout, TryAgain, AttributeError, RateLimitError),
|
|
|
|
tries=OPENAI_RETRIES, delay=2, backoff=2, jitter=(1, 3))
|
2023-12-12 23:03:49 +08:00
|
|
|
async def chat_completion(self, model: str, system: str, user: str, temperature: float = 0.2):
|
|
|
|
try:
|
2024-06-03 23:58:31 +08:00
|
|
|
messages = [SystemMessage(content=system), HumanMessage(content=user)]
|
|
|
|
|
2023-12-12 23:03:49 +08:00
|
|
|
# get a chat completion from the formatted messages
|
2023-12-12 23:28:58 +08:00
|
|
|
resp = self.chat(messages, model=model, temperature=temperature)
|
2024-06-03 23:58:31 +08:00
|
|
|
finish_reason = "completed"
|
2023-12-12 23:03:49 +08:00
|
|
|
return resp.content, finish_reason
|
2024-06-03 23:58:31 +08:00
|
|
|
|
2023-12-12 23:03:49 +08:00
|
|
|
except (Exception) as e:
|
|
|
|
get_logger().error("Unknown error during OpenAI inference: ", e)
|
2024-06-03 23:58:31 +08:00
|
|
|
raise e
|