1
+ import logging
1
2
import os
2
3
from enum import Enum
3
4
from typing import Any , Union
4
- import logging
5
- import json
5
+
6
6
from llama_index .llms .azure_openai import AzureOpenAI # type: ignore
7
7
from openai import PermissionDeniedError
8
8
from uipath ._cli ._runtime ._contracts import UiPathErrorCategory
9
+ from uipath .utils import EndpointManager
9
10
10
11
from .._cli ._runtime ._exception import UiPathLlamaIndexRuntimeError
11
- from llama_index .core .base .llms .types import CompletionResponse
12
12
13
13
logger = logging .getLogger (__name__ )
14
14
@@ -25,7 +25,6 @@ class OpenAIModel(Enum):
25
25
TEXT_DAVINCI_003 = "text-davinci-003"
26
26
27
27
28
- # Define your custom AzureOpenAI class with default settings
29
28
class UiPathOpenAI (AzureOpenAI ):
30
29
def __init__ (
31
30
self ,
@@ -50,57 +49,44 @@ def __init__(
50
49
defaults = {
51
50
"model" : model_value ,
52
51
"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 )} " ,
55
53
"api_key" : os .environ .get ("UIPATH_ACCESS_TOKEN" ),
56
54
"api_version" : api_version ,
57
55
"is_chat_model" : True ,
58
56
"default_headers" : default_headers_dict ,
59
57
}
60
- print ("endpoint" , defaults ["azure_endpoint" ])
61
58
final_kwargs = {** defaults , ** kwargs }
62
59
super ().__init__ (** final_kwargs )
63
60
64
- def _is_license_error (self , e : PermissionDeniedError ) -> bool :
65
- """Check if the error is a license-related 403 error ."""
61
+ def _handle_permission_denied_error (self , e : PermissionDeniedError ) -> None :
62
+ """Handle PermissionDeniedError and convert license errors to UiPathLlamaIndexRuntimeError ."""
66
63
if e .status_code == 403 and e .response :
67
64
try :
68
65
response_body = e .response .json ()
69
66
if isinstance (response_body , dict ):
70
67
title = response_body .get ("title" , "" ).lower ()
71
- return title == "license not available"
72
- except Exception :
73
- pass
74
- return False
68
+ if title == "license not available" :
69
+ raise UiPathLlamaIndexRuntimeError (
70
+ code = "LICENSE_NOT_AVAILABLE" ,
71
+ title = response_body .get ("title" , "License Not Available" ),
72
+ detail = response_body .get (
73
+ "detail" , "License not available for LLM usage"
74
+ ),
75
+ category = UiPathErrorCategory .DEPLOYMENT ,
76
+ ) from e
77
+ except Exception as parse_error :
78
+ logger .warning (f"Failed to parse 403 response JSON: { parse_error } " )
75
79
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."
79
-
80
- return CompletionResponse (
81
- text = default_message ,
82
- raw = {"id" : "license-fallback" , "object" : "text_completion" , "model" : self .model }
83
- )
80
+ raise e
84
81
85
- async def acomplete (self , prompt , ** kwargs ):
86
- """Override acomplete to handle license errors universally."""
82
+ async def _achat (self , messages , ** kwargs ):
87
83
try :
88
- return await super ().acomplete ( prompt , ** kwargs )
84
+ return await super ()._achat ( messages , ** kwargs )
89
85
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
86
+ self ._handle_permission_denied_error (e )
94
87
95
- async def _achat (self , messages , ** kwargs ):
88
+ def _chat (self , messages , ** kwargs ):
96
89
try :
97
- return await super ()._achat (messages , ** kwargs )
90
+ return super ()._chat (messages , ** kwargs )
98
91
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
92
+ self ._handle_permission_denied_error (e )
0 commit comments