1+ """UiPath LLM Gateway Services.
2+
3+ This module provides services for interacting with UiPath's LLM (Large Language Model) Gateway,
4+ offering both OpenAI-compatible and normalized API interfaces for chat completions and embeddings.
5+
6+ The module includes:
7+ - UiPathOpenAIService: OpenAI-compatible API for chat completions and embeddings
8+ - UiPathLlmChatService: UiPath's normalized API with advanced features like tool calling
9+ - ChatModels: Constants for available chat models
10+ - EmbeddingModels: Constants for available embedding models
11+
12+ Classes:
13+ ChatModels: Container for supported chat model identifiers
14+ EmbeddingModels: Container for supported embedding model identifiers
15+ UiPathOpenAIService: Service using OpenAI-compatible API format
16+ UiPathLlmChatService: Service using UiPath's normalized API format
17+ """
18+
119import json
220from typing import Any , Dict , List , Optional
321
1634from ._base_service import BaseService
1735
1836# Common constants
19- API_VERSION = "2024-10-21"
20- NORMALIZED_API_VERSION = "2024-08-01-preview"
37+ API_VERSION = "2024-10-21" # Standard API version for OpenAI-compatible endpoints
38+ NORMALIZED_API_VERSION = (
39+ "2024-08-01-preview" # API version for UiPath's normalized endpoints
40+ )
2141
22- # Common headers
42+ # Common headers used across all LLM Gateway requests
2343DEFAULT_LLM_HEADERS = {
2444 "X-UIPATH-STREAMING-ENABLED" : "false" ,
2545 "X-UiPath-LlmGateway-RequestingProduct" : "uipath-python-sdk" ,
2848
2949
3050class ChatModels (object ):
51+ """Available chat models for LLM Gateway services.
52+
53+ This class provides constants for the supported chat models that can be used
54+ with both UiPathOpenAIService and UiPathLlmChatService.
55+ """
56+
3157 gpt_4 = "gpt-4"
3258 gpt_4_1106_Preview = "gpt-4-1106-Preview"
3359 gpt_4_32k = "gpt-4-32k"
@@ -40,16 +66,23 @@ class ChatModels(object):
4066
4167
4268class EmbeddingModels (object ):
43- text_embedding_3_large = "text-embedding-3-large"
44- text_embedding_ada_002 = "text-embedding-ada-002"
69+ """Available embedding models for LLM Gateway services.
4570
71+ This class provides constants for the supported embedding models that can be used
72+ with the embeddings functionality.
73+ """
4674
47- API_VERSION = "2024-10-21 "
48- NORMALIZED_API_VERSION = "2024-08-01-preview "
75+ text_embedding_3_large = "text-embedding-3-large "
76+ text_embedding_ada_002 = "text-embedding-ada-002 "
4977
5078
5179class UiPathOpenAIService (BaseService ):
52- """Service calling llm gateway service."""
80+ """Service for calling UiPath's LLM Gateway using OpenAI-compatible API.
81+
82+ This service provides access to Large Language Model capabilities through UiPath's
83+ LLM Gateway, including chat completions and text embeddings. It uses the OpenAI-compatible
84+ API format and is suitable for applications that need direct OpenAI API compatibility.
85+ """
5386
5487 def __init__ (self , config : Config , execution_context : ExecutionContext ) -> None :
5588 super ().__init__ (config = config , execution_context = execution_context )
@@ -61,13 +94,35 @@ async def embeddings(
6194 embedding_model : str = EmbeddingModels .text_embedding_ada_002 ,
6295 openai_api_version : str = API_VERSION ,
6396 ):
64- """Embed the input text using llm gateway service.
97+ """Generate text embeddings using UiPath's LLM Gateway service.
98+
99+ This method converts input text into dense vector representations that can be used
100+ for semantic search, similarity calculations, and other NLP tasks.
65101
66102 Args:
67- input (str): The input text to embed.
103+ input (str): The input text to embed. Can be a single sentence, paragraph,
104+ or document that you want to convert to embeddings.
105+ embedding_model (str, optional): The embedding model to use.
106+ Defaults to EmbeddingModels.text_embedding_ada_002.
107+ Available models are defined in the EmbeddingModels class.
108+ openai_api_version (str, optional): The OpenAI API version to use.
109+ Defaults to API_VERSION.
68110
69111 Returns:
70- TextEmbedding: The embedding response.
112+ TextEmbedding: The embedding response containing the vector representation
113+ of the input text along with metadata.
114+
115+ Examples:
116+ ```python
117+ # Basic embedding
118+ embedding = await service.embeddings("Hello, world!")
119+
120+ # Using a specific model
121+ embedding = await service.embeddings(
122+ "This is a longer text to embed",
123+ embedding_model=EmbeddingModels.text_embedding_3_large
124+ )
125+ ```
71126 """
72127 endpoint = EndpointManager .get_embeddings_endpoint ().format (
73128 model = embedding_model , api_version = openai_api_version
@@ -93,29 +148,57 @@ async def chat_completions(
93148 temperature : float = 0 ,
94149 api_version : str = API_VERSION ,
95150 ):
96- """Get chat completions using llm gateway service.
151+ """Generate chat completions using UiPath's LLM Gateway service.
152+
153+ This method provides conversational AI capabilities by sending a series of messages
154+ to a language model and receiving a generated response. It supports multi-turn
155+ conversations and various OpenAI-compatible models.
97156
98157 Args:
99158 messages (List[Dict[str, str]]): List of message dictionaries with 'role' and 'content' keys.
100- The supported roles are 'system', 'user', and 'assistant'.
101-
102- Example:
103- ```
104- [
105- {"role": "system", "content": "You are a helpful Python programming assistant."},
106- {"role": "user", "content": "How do I read a file in Python?"},
107- {"role": "assistant", "content": "You can use the built-in open() function."},
108- {"role": "user", "content": "Can you show an example?"}
109- ]
110- ```
111- The conversation history can be included to provide context to the model.
112- model (str, optional): The model to use for chat completion. Defaults to ChatModels.gpt_4o_mini_2024_07_18.
113- max_tokens (int, optional): Maximum number of tokens to generate. Defaults to 50.
159+ The supported roles are 'system', 'user', and 'assistant'. System messages set
160+ the behavior/context, user messages are from the human, and assistant messages
161+ are from the AI.
162+ model (str, optional): The model to use for chat completion.
163+ Defaults to ChatModels.gpt_4o_mini_2024_07_18.
164+ Available models are defined in the ChatModels class.
165+ max_tokens (int, optional): Maximum number of tokens to generate in the response.
166+ Defaults to 50. Higher values allow longer responses.
114167 temperature (float, optional): Temperature for sampling, between 0 and 1.
115- Lower values make output more deterministic. Defaults to 0.
168+ Lower values (closer to 0) make output more deterministic and focused,
169+ higher values make it more creative and random. Defaults to 0.
170+ api_version (str, optional): The API version to use. Defaults to API_VERSION.
116171
117172 Returns:
118- ChatCompletion: The chat completion response.
173+ ChatCompletion: The chat completion response containing the generated message,
174+ usage statistics, and other metadata.
175+
176+ Examples:
177+ ```python
178+ # Simple conversation
179+ messages = [
180+ {"role": "system", "content": "You are a helpful Python programming assistant."},
181+ {"role": "user", "content": "How do I read a file in Python?"}
182+ ]
183+ response = await service.chat_completions(messages)
184+
185+ # Multi-turn conversation with more tokens
186+ messages = [
187+ {"role": "system", "content": "You are a helpful assistant."},
188+ {"role": "user", "content": "What is machine learning?"},
189+ {"role": "assistant", "content": "Machine learning is a subset of AI..."},
190+ {"role": "user", "content": "Can you give me a practical example?"}
191+ ]
192+ response = await service.chat_completions(
193+ messages,
194+ max_tokens=200,
195+ temperature=0.3
196+ )
197+ ```
198+
199+ Note:
200+ The conversation history can be included to provide context to the model.
201+ Each message should have both 'role' and 'content' keys.
119202 """
120203 endpoint = EndpointManager .get_passthrough_endpoint ().format (
121204 model = model , api_version = api_version
@@ -140,7 +223,16 @@ async def chat_completions(
140223
141224
142225class UiPathLlmChatService (BaseService ):
143- """Service for calling UiPath's normalized LLM Gateway API."""
226+ """Service for calling UiPath's normalized LLM Gateway API.
227+
228+ This service provides access to Large Language Model capabilities through UiPath's
229+ normalized LLM Gateway API. Unlike the OpenAI-compatible service, this service uses
230+ UiPath's standardized API format and supports advanced features like tool calling,
231+ function calling, and more sophisticated conversation control.
232+
233+ The normalized API provides a consistent interface across different underlying model
234+ providers and includes enhanced features for enterprise use cases.
235+ """
144236
145237 def __init__ (self , config : Config , execution_context : ExecutionContext ) -> None :
146238 super ().__init__ (config = config , execution_context = execution_context )
@@ -160,25 +252,96 @@ async def chat_completions(
160252 tool_choice : Optional [ToolChoice ] = None ,
161253 api_version : str = NORMALIZED_API_VERSION ,
162254 ):
163- """Get chat completions using UiPath's normalized LLM Gateway API.
255+ """Generate chat completions using UiPath's normalized LLM Gateway API.
256+
257+ This method provides advanced conversational AI capabilities with support for
258+ tool calling, function calling, and sophisticated conversation control parameters.
259+ It uses UiPath's normalized API format for consistent behavior across different
260+ model providers.
164261
165262 Args:
166263 messages (List[Dict[str, str]]): List of message dictionaries with 'role' and 'content' keys.
167- The supported roles are 'system', 'user', and 'assistant'.
168- model (str, optional): The model to use for chat completion. Defaults to ChatModels.gpt_4o_mini_2024_07_18.
169- max_tokens (int, optional): Maximum number of tokens to generate. Defaults to 250.
264+ The supported roles are 'system', 'user', and 'assistant'. System messages set
265+ the behavior/context, user messages are from the human, and assistant messages
266+ are from the AI.
267+ model (str, optional): The model to use for chat completion.
268+ Defaults to ChatModels.gpt_4o_mini_2024_07_18.
269+ Available models are defined in the ChatModels class.
270+ max_tokens (int, optional): Maximum number of tokens to generate in the response.
271+ Defaults to 250. Higher values allow longer responses.
170272 temperature (float, optional): Temperature for sampling, between 0 and 1.
171- Lower values make output more deterministic. Defaults to 0.
172- n (int, optional): Number of chat completion choices to generate. Defaults to 1.
173- frequency_penalty (float, optional): Penalty for token frequency. Defaults to 0.
174- presence_penalty (float, optional): Penalty for token presence. Defaults to 0.
175- top_p (float, optional): Nucleus sampling parameter. Defaults to 1.
176- tools (Optional[List[ToolDefinition]], optional): List of tool definitions. Defaults to None.
177- tool_choice (Optional[ToolChoice], optional): Tool choice configuration.
178- Can be "auto", "none", an AutoToolChoice, a RequiredToolChoice, or a SpecificToolChoice. Defaults to None.
273+ Lower values (closer to 0) make output more deterministic and focused,
274+ higher values make it more creative and random. Defaults to 0.
275+ n (int, optional): Number of chat completion choices to generate for each input.
276+ Defaults to 1. Higher values generate multiple alternative responses.
277+ frequency_penalty (float, optional): Penalty for token frequency between -2.0 and 2.0.
278+ Positive values reduce repetition of frequent tokens. Defaults to 0.
279+ presence_penalty (float, optional): Penalty for token presence between -2.0 and 2.0.
280+ Positive values encourage discussion of new topics. Defaults to 0.
281+ top_p (float, optional): Nucleus sampling parameter between 0 and 1.
282+ Controls diversity by considering only the top p probability mass. Defaults to 1.
283+ tools (Optional[List[ToolDefinition]], optional): List of tool definitions that the
284+ model can call. Tools enable the model to perform actions or retrieve information
285+ beyond text generation. Defaults to None.
286+ tool_choice (Optional[ToolChoice], optional): Controls which tools the model can call.
287+ Can be "auto" (model decides), "none" (no tools), or a specific tool choice.
288+ Defaults to None.
289+ api_version (str, optional): The normalized API version to use.
290+ Defaults to NORMALIZED_API_VERSION.
179291
180292 Returns:
181- ChatCompletion: The chat completion response.
293+ ChatCompletion: The chat completion response containing the generated message(s),
294+ tool calls (if any), usage statistics, and other metadata.
295+
296+ Examples:
297+ ```python
298+ # Basic conversation
299+ messages = [
300+ {"role": "system", "content": "You are a helpful assistant."},
301+ {"role": "user", "content": "What is the weather like today?"}
302+ ]
303+ response = await service.chat_completions(messages)
304+
305+ # Conversation with tool calling
306+ tools = [
307+ ToolDefinition(
308+ function=FunctionDefinition(
309+ name="get_weather",
310+ description="Get current weather for a location",
311+ parameters=ParametersDefinition(
312+ type="object",
313+ properties={
314+ "location": PropertyDefinition(
315+ type="string",
316+ description="City name"
317+ )
318+ },
319+ required=["location"]
320+ )
321+ )
322+ )
323+ ]
324+ response = await service.chat_completions(
325+ messages,
326+ tools=tools,
327+ tool_choice="auto",
328+ max_tokens=500
329+ )
330+
331+ # Advanced parameters for creative writing
332+ response = await service.chat_completions(
333+ messages,
334+ temperature=0.8,
335+ top_p=0.9,
336+ frequency_penalty=0.3,
337+ presence_penalty=0.2,
338+ n=3 # Generate 3 alternative responses
339+ )
340+ ```
341+
342+ Note:
343+ This service uses UiPath's normalized API format which provides consistent
344+ behavior across different underlying model providers and enhanced enterprise features.
182345 """
183346 endpoint = EndpointManager .get_normalized_endpoint ().format (
184347 model = model , api_version = api_version
@@ -227,7 +390,19 @@ async def chat_completions(
227390 return ChatCompletion .model_validate (response .json ())
228391
229392 def _convert_tool_to_uipath_format (self , tool : ToolDefinition ) -> Dict [str , Any ]:
230- """Convert an OpenAI-style tool definition directly to UiPath API format."""
393+ """Convert an OpenAI-style tool definition to UiPath API format.
394+
395+ This internal method transforms tool definitions from the standard OpenAI format
396+ to the format expected by UiPath's normalized LLM Gateway API.
397+
398+ Args:
399+ tool (ToolDefinition): The tool definition in OpenAI format containing
400+ function name, description, and parameter schema.
401+
402+ Returns:
403+ Dict[str, Any]: The tool definition converted to UiPath API format
404+ with the appropriate structure and field mappings.
405+ """
231406 parameters = {
232407 "type" : tool .function .parameters .type ,
233408 "properties" : {
0 commit comments