11import os
22from enum import Enum
33from typing import Any , Union
4-
4+ import logging
5+ import json
56from llama_index .llms .azure_openai import AzureOpenAI # type: ignore
6- from uipath .utils import EndpointManager
7+ from openai import PermissionDeniedError
8+ from uipath ._cli ._runtime ._contracts import UiPathErrorCategory
9+
10+ from .._cli ._runtime ._exception import UiPathLlamaIndexRuntimeError
11+ from llama_index .core .base .llms .types import CompletionResponse
12+
13+ logger = logging .getLogger (__name__ )
714
815
916class OpenAIModel (Enum ):
@@ -43,11 +50,57 @@ def __init__(
4350 defaults = {
4451 "model" : model_value ,
4552 "deployment_name" : model_value ,
46- "azure_endpoint" : f"{ base_url } /{ EndpointManager .get_passthrough_endpoint ().format (model = model , api_version = api_version )} " ,
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 } " ,
4755 "api_key" : os .environ .get ("UIPATH_ACCESS_TOKEN" ),
4856 "api_version" : api_version ,
4957 "is_chat_model" : True ,
5058 "default_headers" : default_headers_dict ,
5159 }
60+ print ("endpoint" , defaults ["azure_endpoint" ])
5261 final_kwargs = {** defaults , ** kwargs }
5362 super ().__init__ (** final_kwargs )
63+
64+ def _is_license_error (self , e : PermissionDeniedError ) -> bool :
65+ """Check if the error is a license-related 403 error."""
66+ if e .status_code == 403 and e .response :
67+ try :
68+ response_body = e .response .json ()
69+ if isinstance (response_body , dict ):
70+ 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."
79+
80+ return CompletionResponse (
81+ text = default_message ,
82+ raw = {"id" : "license-fallback" , "object" : "text_completion" , "model" : self .model }
83+ )
84+
85+ async def acomplete (self , prompt , ** kwargs ):
86+ """Override acomplete to handle license errors universally."""
87+ try :
88+ return await super ().acomplete (prompt , ** kwargs )
89+ 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
94+
95+ async def _achat (self , messages , ** kwargs ):
96+ try :
97+ return await super ()._achat (messages , ** kwargs )
98+ 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
0 commit comments