|
1 | | -# UiPath LLMs and Embeddings |
2 | | - |
3 | | -This guide covers the UiPath-integrated Large Language Models (LLMs) and embedding models available in the UiPath LlamaIndex SDK. |
4 | | - |
5 | | -## Overview |
6 | | - |
7 | | -The UiPath LlamaIndex SDK provides pre-configured LLM and embedding classes that integrate seamlessly with UiPath. These classes handle authentication, routing, and configuration automatically, allowing you to focus on building your agents. |
8 | | - |
9 | | -## Prerequisites |
10 | | - |
11 | | -Before using these classes, ensure you have: |
12 | | - |
13 | | -- Authenticated with UiPath using `uipath auth` |
14 | | -- Set up your environment variables (automatically configured after authentication) |
15 | | - |
16 | | -## UiPathOpenAI |
17 | | - |
18 | | -The `UiPathOpenAI` class is a pre-configured Azure OpenAI client that routes requests through UiPath. |
19 | | - |
20 | | -### Available Models |
21 | | - |
22 | | -The following OpenAI models are available through the `OpenAIModel` enum: |
23 | | - |
24 | | -- `GPT_4_1_2025_04_14` |
25 | | -- `GPT_4_1_MINI_2025_04_14` |
26 | | -- `GPT_4_1_NANO_2025_04_14` |
27 | | -- `GPT_4O_2024_05_13` |
28 | | -- `GPT_4O_2024_08_06` |
29 | | -- `GPT_4O_2024_11_20` |
30 | | -- `GPT_4O_MINI_2024_07_18` (default) |
31 | | -- `O3_MINI_2025_01_31` |
32 | | -- `TEXT_DAVINCI_003` |
33 | | - |
34 | | -### Basic Usage |
35 | | - |
36 | | -```python |
37 | | -from uipath_llamaindex.llms import UiPathOpenAI |
38 | | -from llama_index.core.llms import ChatMessage |
39 | | - |
40 | | -# Create an LLM instance with default settings |
41 | | -llm = UiPathOpenAI() |
42 | | - |
43 | | -# Create chat messages |
44 | | -messages = [ |
45 | | - ChatMessage( |
46 | | - role="system", content="You are a pirate with colorful personality." |
47 | | - ), |
48 | | - ChatMessage(role="user", content="Hello"), |
49 | | -] |
50 | | - |
51 | | -# Generate a response |
52 | | -response = llm.chat(messages) |
53 | | -print(response) |
54 | | -``` |
55 | | - |
56 | | -### Custom Model Configuration |
57 | | - |
58 | | -```python |
59 | | -from uipath_llamaindex.llms import UiPathOpenAI, OpenAIModel |
60 | | - |
61 | | -# Use a specific model |
62 | | -llm = UiPathOpenAI(model=OpenAIModel.GPT_4O_2024_11_20) |
63 | | - |
64 | | -# Or use a model string directly |
65 | | -llm = UiPathOpenAI(model="gpt-4o-2024-11-20") |
66 | | -``` |
67 | | - |
68 | | -## UiPathOpenAIEmbedding |
69 | | - |
70 | | -The `UiPathOpenAIEmbedding` class provides text embedding capabilities using OpenAI's embedding models through UiPath. |
71 | | - |
72 | | -### Available Embedding Models |
73 | | - |
74 | | -The following embedding models are available through the `OpenAIEmbeddingModel` enum: |
75 | | - |
76 | | -- `TEXT_EMBEDDING_ADA_002` (default) |
77 | | -- `TEXT_EMBEDDING_3_LARGE` |
78 | | - |
79 | | -### Basic Usage |
80 | | - |
81 | | -```python |
82 | | -from uipath_llamaindex.embeddings import UiPathOpenAIEmbedding |
83 | | - |
84 | | -# Create an embedding model instance |
85 | | -embed_model = UiPathOpenAIEmbedding() |
86 | | - |
87 | | -# Get embeddings for a single text |
88 | | -result = embed_model.get_text_embedding("the quick brown fox jumps over the lazy dog") |
89 | | -print(f"Embedding dimension: {len(result)}") |
90 | | -``` |
91 | | - |
92 | | -### Batch Embeddings |
93 | | - |
94 | | -```python |
95 | | -from uipath_llamaindex.embeddings import UiPathOpenAIEmbedding |
96 | | - |
97 | | -embed_model = UiPathOpenAIEmbedding() |
98 | | - |
99 | | -# Get embeddings for multiple texts |
100 | | -texts = [ |
101 | | - "Hello world", |
102 | | - "How are you?", |
103 | | - "This is a test" |
104 | | -] |
105 | | - |
106 | | -embeddings = embed_model.get_text_embedding_batch(texts) |
107 | | -print(f"Number of embeddings: {len(embeddings)}") |
108 | | -``` |
109 | | - |
110 | | - |
111 | | -## Integration with LlamaIndex |
112 | | - |
113 | | -Both classes integrate seamlessly with LlamaIndex components: |
114 | | - |
115 | | -### Using with Agents |
116 | | - |
117 | | -```python |
118 | | -from llama_index.core.agent import ReActAgent |
119 | | -from llama_index.core.tools import FunctionTool |
120 | | -from uipath_llamaindex.llms import UiPathOpenAI |
121 | | - |
122 | | -def multiply(a: int, b: int) -> int: |
123 | | - """Multiply two integers and returns the result.""" |
124 | | - return a * b |
125 | | - |
126 | | -multiply_tool = FunctionTool.from_defaults(fn=multiply) |
127 | | - |
128 | | -# Create agent with UiPath LLM |
129 | | -agent = ReActAgent.from_tools( |
130 | | - [multiply_tool], |
131 | | - llm=UiPathOpenAI(model=OpenAIModel.GPT_4O_2024_11_20) |
132 | | -) |
133 | | - |
134 | | -response = agent.chat("What is 21 multiplied by 2?") |
135 | | -``` |
136 | | - |
137 | | -### Using with VectorStoreIndex |
138 | | - |
139 | | -```python |
140 | | -from llama_index.core import VectorStoreIndex, Document |
141 | | -from uipath_llamaindex.llms import UiPathOpenAI |
142 | | -from uipath_llamaindex.embeddings import UiPathOpenAIEmbedding |
143 | | - |
144 | | -# Create documents |
145 | | -documents = [ |
146 | | - Document(text="This is a sample document about artificial intelligence."), |
147 | | - Document(text="Machine learning is a subset of AI that focuses on algorithms."), |
148 | | -] |
149 | | - |
150 | | -# Create index with UiPath models |
151 | | -index = VectorStoreIndex.from_documents( |
152 | | - documents, |
153 | | - embed_model=UiPathOpenAIEmbedding() |
154 | | -) |
155 | | - |
156 | | -# Create query engine with UiPath LLM |
157 | | -query_engine = index.as_query_engine( |
158 | | - llm=UiPathOpenAI(model=OpenAIModel.GPT_4O_2024_11_20) |
159 | | -) |
160 | | - |
161 | | -response = query_engine.query("What is machine learning?") |
162 | | -``` |
| 1 | +# LLMs and Embeddings |
| 2 | + |
| 3 | +UiPath provides pre-configured LLM and embedding classes that handle authentication, routing, and configuration automatically, allowing you to focus on building your agents. |
| 4 | +You do not need to add tokens from OpenAI, usage of these models will consume `Agent Units` on your account. |
| 5 | + |
| 6 | +## UiPathOpenAI |
| 7 | + |
| 8 | +The `UiPathOpenAI` class is a pre-configured Azure OpenAI client that routes requests through UiPath. |
| 9 | + |
| 10 | +### Available Models |
| 11 | + |
| 12 | +The following OpenAI models are available through the `OpenAIModel` enum: |
| 13 | + |
| 14 | +- `GPT_4_1_2025_04_14` |
| 15 | +- `GPT_4_1_MINI_2025_04_14` |
| 16 | +- `GPT_4_1_NANO_2025_04_14` |
| 17 | +- `GPT_4O_2024_05_13` |
| 18 | +- `GPT_4O_2024_08_06` |
| 19 | +- `GPT_4O_2024_11_20` |
| 20 | +- `GPT_4O_MINI_2024_07_18` (default) |
| 21 | +- `O3_MINI_2025_01_31` |
| 22 | +- `TEXT_DAVINCI_003` |
| 23 | + |
| 24 | +### Basic Usage |
| 25 | + |
| 26 | +```python |
| 27 | +from uipath_llamaindex.llms import UiPathOpenAI |
| 28 | +from llama_index.core.llms import ChatMessage |
| 29 | + |
| 30 | +# Create an LLM instance with default settings |
| 31 | +llm = UiPathOpenAI() |
| 32 | + |
| 33 | +# Create chat messages |
| 34 | +messages = [ |
| 35 | + ChatMessage( |
| 36 | + role="system", content="You are a pirate with colorful personality." |
| 37 | + ), |
| 38 | + ChatMessage(role="user", content="Hello"), |
| 39 | +] |
| 40 | + |
| 41 | +# Generate a response |
| 42 | +response = llm.chat(messages) |
| 43 | +print(response) |
| 44 | +``` |
| 45 | + |
| 46 | +### Custom Model Configuration |
| 47 | + |
| 48 | +```python |
| 49 | +from uipath_llamaindex.llms import UiPathOpenAI, OpenAIModel |
| 50 | + |
| 51 | +# Use a specific model |
| 52 | +llm = UiPathOpenAI(model=OpenAIModel.GPT_4O_2024_11_20) |
| 53 | + |
| 54 | +# Or use a model string directly |
| 55 | +llm = UiPathOpenAI(model="gpt-4o-2024-11-20") |
| 56 | +``` |
| 57 | + |
| 58 | +## UiPathOpenAIEmbedding |
| 59 | + |
| 60 | +The `UiPathOpenAIEmbedding` class provides text embedding capabilities using OpenAI's embedding models through UiPath. |
| 61 | + |
| 62 | +### Available Embedding Models |
| 63 | + |
| 64 | +The following embedding models are available through the `OpenAIEmbeddingModel` enum: |
| 65 | + |
| 66 | +- `TEXT_EMBEDDING_ADA_002` (default) |
| 67 | +- `TEXT_EMBEDDING_3_LARGE` |
| 68 | + |
| 69 | +### Basic Usage |
| 70 | + |
| 71 | +```python |
| 72 | +from uipath_llamaindex.embeddings import UiPathOpenAIEmbedding |
| 73 | + |
| 74 | +# Create an embedding model instance |
| 75 | +embed_model = UiPathOpenAIEmbedding() |
| 76 | + |
| 77 | +# Get embeddings for a single text |
| 78 | +result = embed_model.get_text_embedding("the quick brown fox jumps over the lazy dog") |
| 79 | +print(f"Embedding dimension: {len(result)}") |
| 80 | +``` |
| 81 | + |
| 82 | +### Batch Embeddings |
| 83 | + |
| 84 | +```python |
| 85 | +from uipath_llamaindex.embeddings import UiPathOpenAIEmbedding |
| 86 | + |
| 87 | +embed_model = UiPathOpenAIEmbedding() |
| 88 | + |
| 89 | +# Get embeddings for multiple texts |
| 90 | +texts = [ |
| 91 | + "Hello world", |
| 92 | + "How are you?", |
| 93 | + "This is a test" |
| 94 | +] |
| 95 | + |
| 96 | +embeddings = embed_model.get_text_embedding_batch(texts) |
| 97 | +print(f"Number of embeddings: {len(embeddings)}") |
| 98 | +``` |
| 99 | + |
| 100 | + |
| 101 | +## Integration with LlamaIndex |
| 102 | + |
| 103 | +Both classes integrate seamlessly with LlamaIndex components: |
| 104 | + |
| 105 | +### Using with Agents |
| 106 | + |
| 107 | +```python |
| 108 | +from llama_index.core.agent import ReActAgent |
| 109 | +from llama_index.core.tools import FunctionTool |
| 110 | +from uipath_llamaindex.llms import UiPathOpenAI |
| 111 | + |
| 112 | +def multiply(a: int, b: int) -> int: |
| 113 | + """Multiply two integers and returns the result.""" |
| 114 | + return a * b |
| 115 | + |
| 116 | +multiply_tool = FunctionTool.from_defaults(fn=multiply) |
| 117 | + |
| 118 | +# Create agent with UiPath LLM |
| 119 | +agent = ReActAgent.from_tools( |
| 120 | + [multiply_tool], |
| 121 | + llm=UiPathOpenAI(model=OpenAIModel.GPT_4O_2024_11_20) |
| 122 | +) |
| 123 | + |
| 124 | +response = agent.chat("What is 21 multiplied by 2?") |
| 125 | +``` |
| 126 | + |
| 127 | +### Using with VectorStoreIndex |
| 128 | + |
| 129 | +```python |
| 130 | +from llama_index.core import VectorStoreIndex, Document |
| 131 | +from uipath_llamaindex.llms import UiPathOpenAI |
| 132 | +from uipath_llamaindex.embeddings import UiPathOpenAIEmbedding |
| 133 | + |
| 134 | +# Create documents |
| 135 | +documents = [ |
| 136 | + Document(text="This is a sample document about artificial intelligence."), |
| 137 | + Document(text="Machine learning is a subset of AI that focuses on algorithms."), |
| 138 | +] |
| 139 | + |
| 140 | +# Create index with UiPath models |
| 141 | +index = VectorStoreIndex.from_documents( |
| 142 | + documents, |
| 143 | + embed_model=UiPathOpenAIEmbedding() |
| 144 | +) |
| 145 | + |
| 146 | +# Create query engine with UiPath LLM |
| 147 | +query_engine = index.as_query_engine( |
| 148 | + llm=UiPathOpenAI(model=OpenAIModel.GPT_4O_2024_11_20) |
| 149 | +) |
| 150 | + |
| 151 | +response = query_engine.query("What is machine learning?") |
| 152 | +``` |
| 153 | + |
| 154 | +/// warning |
| 155 | +Please note that that you may get errors related to data residency, as some models are not available on all regions. |
| 156 | + |
| 157 | +Example: `[Enforced Region] No model configuration found for product uipath-python-sdk in EU`. |
| 158 | + |
| 159 | +/// |
0 commit comments