mirror of
https://github.com/qodo-ai/pr-agent.git
synced 2025-07-04 04:40:38 +08:00
Fix LangChainOpenAIHandler for Azure
This commit is contained in:
@ -20,35 +20,13 @@ class LangChainOpenAIHandler(BaseAiHandler):
|
|||||||
# Initialize OpenAIHandler specific attributes here
|
# Initialize OpenAIHandler specific attributes here
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.azure = get_settings().get("OPENAI.API_TYPE", "").lower() == "azure"
|
self.azure = get_settings().get("OPENAI.API_TYPE", "").lower() == "azure"
|
||||||
try:
|
|
||||||
if self.azure:
|
# Create a default unused chat object to trigger early validation
|
||||||
# using a partial function so we can set the deployment_id later to support fallback_deployments
|
self._create_chat(self.deployment_id)
|
||||||
# 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:
|
|
||||||
# for llms that compatible with openai, should use custom api base
|
|
||||||
openai_api_base = get_settings().get("OPENAI.API_BASE", None)
|
|
||||||
if openai_api_base is None or len(openai_api_base) == 0:
|
|
||||||
self._chat = ChatOpenAI(openai_api_key=get_settings().openai.key)
|
|
||||||
else:
|
|
||||||
self._chat = ChatOpenAI(openai_api_key=get_settings().openai.key, openai_api_base=openai_api_base)
|
|
||||||
except AttributeError as e:
|
|
||||||
if getattr(e, "name"):
|
|
||||||
raise ValueError(f"OpenAI {e.name} is required") from e
|
|
||||||
else:
|
|
||||||
raise e
|
|
||||||
|
|
||||||
def chat(self, messages: list, model: str, temperature: float):
|
def chat(self, messages: list, model: str, temperature: float):
|
||||||
if self.azure:
|
chat = self._create_chat(self.deployment_id)
|
||||||
# we must set the deployment_id only here (instead of the __init__ method) to support fallback_deployments
|
return chat.invoke(input=messages, model=model, temperature=temperature)
|
||||||
return self._chat.invoke(input = messages, model=model, temperature=temperature, deployment_name=self.deployment_id)
|
|
||||||
else:
|
|
||||||
return self._chat.invoke(input = messages, model=model, temperature=temperature)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def deployment_id(self):
|
def deployment_id(self):
|
||||||
@ -71,3 +49,28 @@ class LangChainOpenAIHandler(BaseAiHandler):
|
|||||||
except (Exception) as e:
|
except (Exception) as e:
|
||||||
get_logger().error("Unknown error during OpenAI inference: ", e)
|
get_logger().error("Unknown error during OpenAI inference: ", e)
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
def _create_chat(self, deployment_id=None):
|
||||||
|
try:
|
||||||
|
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
|
||||||
|
return AzureChatOpenAI(
|
||||||
|
openai_api_key=get_settings().openai.key,
|
||||||
|
openai_api_version=get_settings().openai.api_version,
|
||||||
|
azure_deployment=deployment_id,
|
||||||
|
azure_endpoint=get_settings().openai.api_base,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
# for llms that compatible with openai, should use custom api base
|
||||||
|
openai_api_base = get_settings().get("OPENAI.API_BASE", None)
|
||||||
|
if openai_api_base is None or len(openai_api_base) == 0:
|
||||||
|
return ChatOpenAI(openai_api_key=get_settings().openai.key)
|
||||||
|
else:
|
||||||
|
return ChatOpenAI(openai_api_key=get_settings().openai.key, openai_api_base=openai_api_base)
|
||||||
|
except AttributeError as e:
|
||||||
|
if getattr(e, "name"):
|
||||||
|
raise ValueError(f"OpenAI {e.name} is required") from e
|
||||||
|
else:
|
||||||
|
raise e
|
||||||
|
|
||||||
|
@ -30,4 +30,6 @@ gunicorn==20.1.0
|
|||||||
# pinecone-datasets @ git+https://github.com/mrT23/pinecone-datasets.git@main
|
# pinecone-datasets @ git+https://github.com/mrT23/pinecone-datasets.git@main
|
||||||
# lancedb==0.5.1
|
# lancedb==0.5.1
|
||||||
# uncomment this to support language LangChainOpenAIHandler
|
# uncomment this to support language LangChainOpenAIHandler
|
||||||
# langchain==0.0.349
|
# langchain==0.2.0
|
||||||
|
# langchain-core==0.2.28
|
||||||
|
# langchain-openai==0.1.20
|
||||||
|
Reference in New Issue
Block a user