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,57 +49,44 @@ 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 ,
5856 "default_headers" : default_headers_dict ,
5957 }
60- print ("endpoint" , defaults ["azure_endpoint" ])
6158 final_kwargs = {** defaults , ** kwargs }
6259 super ().__init__ (** final_kwargs )
6360
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 ."""
6663 if e .status_code == 403 and e .response :
6764 try :
6865 response_body = e .response .json ()
6966 if isinstance (response_body , dict ):
7067 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 } " )
7579
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
8481
85- async def acomplete (self , prompt , ** kwargs ):
86- """Override acomplete to handle license errors universally."""
82+ async def _achat (self , messages , ** kwargs ):
8783 try :
88- return await super ().acomplete ( prompt , ** kwargs )
84+ return await super ()._achat ( messages , ** kwargs )
8985 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 )
9487
95- async def _achat (self , messages , ** kwargs ):
88+ def _chat (self , messages , ** kwargs ):
9689 try :
97- return await super ()._achat (messages , ** kwargs )
90+ return super ()._chat (messages , ** kwargs )
9891 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