1+ import logging
12import os
23from enum import Enum
34from typing import Any , Union
4- import logging
5- import json
5+
66from llama_index .llms .azure_openai import AzureOpenAI # type: ignore
77from openai import PermissionDeniedError
88from uipath ._cli ._runtime ._contracts import UiPathErrorCategory
9+ from uipath .utils import EndpointManager
910
1011from .._cli ._runtime ._exception import UiPathLlamaIndexRuntimeError
11- from llama_index .core .base .llms .types import CompletionResponse
1212
1313logger = logging .getLogger (__name__ )
1414
@@ -25,7 +25,6 @@ class OpenAIModel(Enum):
2525 TEXT_DAVINCI_003 = "text-davinci-003"
2626
2727
28- # Define your custom AzureOpenAI class with default settings
2928class UiPathOpenAI (AzureOpenAI ):
3029 def __init__ (
3130 self ,
@@ -50,8 +49,7 @@ def __init__(
5049 defaults = {
5150 "model" : model_value ,
5251 "deployment_name" : model_value ,
53- # "azure_endpoint": f"{base_url}/{EndpointManager.get_passthrough_endpoint().format(model=model, api_version=api_version)}",
54- "azure_endpoint" : f"{ base_url } /llm/openai/deployments/{ model_value } /chat/completions?api-version={ api_version } " ,
52+ "azure_endpoint" : f"{ base_url } /{ EndpointManager .get_passthrough_endpoint ().format (model = model , api_version = api_version )} " ,
5553 "api_key" : os .environ .get ("UIPATH_ACCESS_TOKEN" ),
5654 "api_version" : api_version ,
5755 "is_chat_model" : True ,
@@ -61,46 +59,33 @@ def __init__(
6159 final_kwargs = {** defaults , ** kwargs }
6260 super ().__init__ (** final_kwargs )
6361
64- def _is_license_error (self , e : PermissionDeniedError ) -> bool :
65- """Check if the error is a license-related 403 error ."""
62+ def _handle_permission_denied_error (self , e : PermissionDeniedError ) -> None :
63+ """Handle PermissionDeniedError and convert license errors to UiPathLlamaIndexRuntimeError ."""
6664 if e .status_code == 403 and e .response :
6765 try :
6866 response_body = e .response .json ()
6967 if isinstance (response_body , dict ):
7068 title = response_body .get ("title" , "" ).lower ()
71- return title == "license not available"
72- except Exception :
73- pass
74- return False
75-
76- def _create_license_fallback (self ) -> CompletionResponse :
77- """Create a fallback response for license errors."""
78- default_message = "I apologize, but I'm currently unable to process your request due to licensing limitations. Please contact your UiPath administrator to configure LLM licensing for this Agent Hub instance."
69+ if title == "license not available" :
70+ raise UiPathLlamaIndexRuntimeError (
71+ code = "LICENSE_NOT_AVAILABLE" ,
72+ title = response_body .get ("title" , "License Not Available" ),
73+ detail = response_body .get ("detail" , "License not available for LLM usage" ),
74+ category = UiPathErrorCategory .DEPLOYMENT ,
75+ ) from e
76+ except Exception as parse_error :
77+ logger .warning (f"Failed to parse 403 response JSON: { parse_error } " )
7978
80- return CompletionResponse (
81- text = default_message ,
82- raw = {"id" : "license-fallback" , "object" : "text_completion" , "model" : self .model }
83- )
79+ raise e
8480
85- async def acomplete (self , prompt , ** kwargs ):
86- """Override acomplete to handle license errors universally."""
81+ async def _achat (self , messages , ** kwargs ):
8782 try :
88- return await super ().acomplete ( prompt , ** kwargs )
83+ return await super ()._achat ( messages , ** kwargs )
8984 except PermissionDeniedError as e :
90- if self ._is_license_error (e ):
91- logger .warning ("UiPath Agent Hub license not available - returning fallback response" )
92- return self ._create_license_fallback ()
93- raise
85+ self ._handle_permission_denied_error (e )
9486
95- async def _achat (self , messages , ** kwargs ):
87+ def _chat (self , messages , ** kwargs ):
9688 try :
97- return await super ()._achat (messages , ** kwargs )
89+ return super ()._chat (messages , ** kwargs )
9890 except PermissionDeniedError as e :
99- if self ._is_license_error (e ):
100- raise UiPathLlamaIndexRuntimeError (
101- code = "LICENSE_NOT_AVAILABLE" ,
102- title = "License Not Available" ,
103- detail = "License not available for LLM usage" ,
104- category = UiPathErrorCategory .DEPLOYMENT ,
105- ) from e
106- raise
91+ self ._handle_permission_denied_error (e )
0 commit comments