33
44from .._config import Config
55from .._execution_context import ExecutionContext
6- from .._utils import Endpoint
6+ from .._utils import Endpoint , EndpointManager
77from ..models .llm_gateway import (
88 ChatCompletion ,
99 SpecificToolChoice ,
@@ -54,36 +54,12 @@ class UiPathOpenAIService(BaseService):
5454 def __init__ (self , config : Config , execution_context : ExecutionContext ) -> None :
5555 super ().__init__ (config = config , execution_context = execution_context )
5656
57- @traced (name = "llm_embeddings_usage" , run_type = "uipath" )
58- async def embeddings_usage (
59- self , input : str , embedding_model : str = EmbeddingModels .text_embedding_ada_002
60- ):
61- """Embedd the input text using llm gateway service.
62-
63- Args:
64- input (str): The input text to embedd.
65- embedding_model (str, optional): The embedding model to use. Defaults to text-embedding-ada-002.
66-
67- Returns:
68- EmbeddingUsageInfo: The embedding usage information.
69- """
70- endpoint = Endpoint (
71- f"/llmgateway_/openai/deployments/{ embedding_model } /embeddings/usage"
72- )
73-
74- response = await self .request_async (
75- "POST" ,
76- endpoint ,
77- content = json .dumps ({"input" : input }),
78- params = {"api-version" : API_VERSION },
79- headers = DEFAULT_LLM_HEADERS ,
80- )
81-
82- return UsageInfo .model_validate (response .json ())
83-
8457 @traced (name = "llm_embeddings" , run_type = "uipath" )
8558 async def embeddings (
86- self , input : str , embedding_model : str = EmbeddingModels .text_embedding_ada_002
59+ self ,
60+ input : str ,
61+ embedding_model : str = EmbeddingModels .text_embedding_ada_002 ,
62+ openai_api_version : str = API_VERSION ,
8763 ):
8864 """Embed the input text using llm gateway service.
8965
@@ -93,9 +69,10 @@ async def embeddings(
9369 Returns:
9470 TextEmbedding: The embedding response.
9571 """
96- endpoint = Endpoint (
97- f"/llmgateway_/openai/deployments/ { embedding_model } /embeddings"
72+ endpoint = EndpointManager . get_embeddings_endpoint (). format (
73+ model = embedding_model , api_version = openai_api_version
9874 )
75+ endpoint = Endpoint ("/" + endpoint )
9976
10077 response = await self .request_async (
10178 "POST" ,
@@ -114,6 +91,7 @@ async def chat_completions(
11491 model : str = ChatModels .gpt_4o_mini_2024_07_18 ,
11592 max_tokens : int = 50 ,
11693 temperature : float = 0 ,
94+ api_version : str = API_VERSION ,
11795 ):
11896 """Get chat completions using llm gateway service.
11997
@@ -139,59 +117,10 @@ async def chat_completions(
139117 Returns:
140118 ChatCompletion: The chat completion response.
141119 """
142- endpoint = Endpoint (f"/llmgateway_/openai/deployments/{ model } /chat/completions" )
143-
144- request_body = {
145- "messages" : messages ,
146- "max_tokens" : max_tokens ,
147- "temperature" : temperature ,
148- }
149-
150- response = await self .request_async (
151- "POST" ,
152- endpoint ,
153- content = json .dumps (request_body ),
154- params = {"api-version" : API_VERSION },
155- headers = DEFAULT_LLM_HEADERS ,
156- )
157-
158- return ChatCompletion .model_validate (response .json ())
159-
160- @traced (name = "llm_chat_completions_usage" , run_type = "uipath" )
161- async def chat_completions_usage (
162- self ,
163- messages : List [Dict [str , str ]],
164- model : str = ChatModels .gpt_4o_mini_2024_07_18 ,
165- max_tokens : int = 50 ,
166- temperature : float = 0 ,
167- ):
168- """Get chat completions usage using llm gateway service.
169-
170- Args:
171- messages (List[Dict[str, str]]): List of message dictionaries with 'role' and 'content' keys.
172- The supported roles are 'system', 'user', and 'assistant'.
173-
174- Example:
175- ```
176- [
177- {"role": "system", "content": "You are a helpful Python programming assistant."},
178- {"role": "user", "content": "How do I read a file in Python?"},
179- {"role": "assistant", "content": "You can use the built-in open() function."},
180- {"role": "user", "content": "Can you show an example?"}
181- ]
182- ```
183- The conversation history can be included to provide context to the model.
184- model (str, optional): The model to use for chat completion. Defaults to ChatModels.gpt_4o_mini_2024_07_18.
185- max_tokens (int, optional): Maximum number of tokens to generate. Defaults to 50.
186- temperature (float, optional): Temperature for sampling, between 0 and 1.
187- Lower values make output more deterministic. Defaults to 0.
188-
189- Returns:
190- ChatCompletion: The chat completion usage response.
191- """
192- endpoint = Endpoint (
193- f"/llmgateway_/openai/deployments/{ model } /chat/completions/usage"
120+ endpoint = EndpointManager .get_passthrough_endpoint ().format (
121+ model = model , api_version = api_version
194122 )
123+ endpoint = Endpoint ("/" + endpoint )
195124
196125 request_body = {
197126 "messages" : messages ,
@@ -207,7 +136,7 @@ async def chat_completions_usage(
207136 headers = DEFAULT_LLM_HEADERS ,
208137 )
209138
210- return UsageInfo .model_validate (response .json ())
139+ return ChatCompletion .model_validate (response .json ())
211140
212141
213142class UiPathLlmChatService (BaseService ):
@@ -229,6 +158,7 @@ async def chat_completions(
229158 top_p : float = 1 ,
230159 tools : Optional [List [ToolDefinition ]] = None ,
231160 tool_choice : Optional [ToolChoice ] = None ,
161+ api_version : str = NORMALIZED_API_VERSION ,
232162 ):
233163 """Get chat completions using UiPath's normalized LLM Gateway API.
234164
@@ -250,7 +180,10 @@ async def chat_completions(
250180 Returns:
251181 ChatCompletion: The chat completion response.
252182 """
253- endpoint = Endpoint ("/llmgateway_/api/chat/completions" )
183+ endpoint = EndpointManager .get_normalized_endpoint ().format (
184+ model = model , api_version = api_version
185+ )
186+ endpoint = Endpoint ("/" + endpoint )
254187
255188 request_body = {
256189 "messages" : messages ,
0 commit comments