|
1 | 1 | """Context tool creation for semantic index retrieval.""" |
2 | 2 |
|
| 3 | +import uuid |
3 | 4 | from typing import Any |
4 | 5 |
|
5 | 6 | from langchain_core.documents import Document |
6 | 7 | from langchain_core.tools import StructuredTool |
| 8 | +from langgraph.types import interrupt |
7 | 9 | from pydantic import BaseModel, Field |
8 | | -from uipath.agent.models.agent import AgentContextResourceConfig |
| 10 | +from uipath.agent.models.agent import ( |
| 11 | + AgentContextResourceConfig, |
| 12 | + AgentContextRetrievalMode, |
| 13 | +) |
9 | 14 | from uipath.eval.mocks import mockable |
| 15 | +from uipath.platform.common import CreateBatchTransform, CreateDeepRag |
| 16 | +from uipath.platform.context_grounding import ( |
| 17 | + BatchTransformOutputColumn, |
| 18 | + BatchTransformResponse, |
| 19 | + CitationMode, |
| 20 | + DeepRagResponse, |
| 21 | +) |
10 | 22 |
|
11 | 23 | from uipath_langchain.retrievers import ContextGroundingRetriever |
12 | 24 |
|
|
16 | 28 |
|
17 | 29 | def create_context_tool(resource: AgentContextResourceConfig) -> StructuredTool: |
18 | 30 | tool_name = sanitize_tool_name(resource.name) |
| 31 | + retrieval_mode = resource.settings.retrieval_mode.lower() |
| 32 | + if retrieval_mode == AgentContextRetrievalMode.DEEP_RAG.value.lower(): |
| 33 | + return handle_deep_rag(tool_name, resource) |
| 34 | + elif retrieval_mode == AgentContextRetrievalMode.BATCH_TRANSFORM.value.lower(): |
| 35 | + return handle_batch_transform(tool_name, resource) |
| 36 | + else: |
| 37 | + return handle_semantic_search(tool_name, resource) |
| 38 | + |
| 39 | + |
| 40 | +def handle_semantic_search( |
| 41 | + tool_name: str, resource: AgentContextResourceConfig |
| 42 | +) -> StructuredTool: |
19 | 43 | retriever = ContextGroundingRetriever( |
20 | 44 | index_name=resource.index_name, |
21 | 45 | folder_path=resource.folder_path, |
@@ -52,3 +76,128 @@ async def context_tool_fn(query: str) -> dict[str, Any]: |
52 | 76 | coroutine=context_tool_fn, |
53 | 77 | output_type=output_model, |
54 | 78 | ) |
| 79 | + |
| 80 | + |
| 81 | +def handle_deep_rag( |
| 82 | + tool_name: str, resource: AgentContextResourceConfig |
| 83 | +) -> StructuredTool: |
| 84 | + ensure_valid_fields(resource) |
| 85 | + # needed for type checking |
| 86 | + assert resource.settings.query is not None |
| 87 | + assert resource.settings.query.value is not None |
| 88 | + |
| 89 | + index_name = resource.index_name |
| 90 | + prompt = resource.settings.query.value |
| 91 | + if not resource.settings.citation_mode: |
| 92 | + raise ValueError("Citation mode is required for Deep RAG") |
| 93 | + citation_mode = CitationMode(resource.settings.citation_mode.value) |
| 94 | + |
| 95 | + input_model = None |
| 96 | + output_model = DeepRagResponse |
| 97 | + |
| 98 | + @mockable( |
| 99 | + name=resource.name, |
| 100 | + description=resource.description, |
| 101 | + input_schema=input_model, |
| 102 | + output_schema=output_model.model_json_schema(), |
| 103 | + example_calls=[], # Examples cannot be provided for context. |
| 104 | + ) |
| 105 | + async def context_tool_fn() -> dict[str, Any]: |
| 106 | + # TODO: add glob pattern support |
| 107 | + return interrupt( |
| 108 | + CreateDeepRag( |
| 109 | + name=f"task-{uuid.uuid4()}", |
| 110 | + index_name=index_name, |
| 111 | + prompt=prompt, |
| 112 | + citation_mode=citation_mode, |
| 113 | + ) |
| 114 | + ) |
| 115 | + |
| 116 | + return StructuredToolWithOutputType( |
| 117 | + name=tool_name, |
| 118 | + description=resource.description, |
| 119 | + args_schema=input_model, |
| 120 | + coroutine=context_tool_fn, |
| 121 | + output_type=output_model, |
| 122 | + ) |
| 123 | + |
| 124 | + |
| 125 | +def handle_batch_transform( |
| 126 | + tool_name: str, resource: AgentContextResourceConfig |
| 127 | +) -> StructuredTool: |
| 128 | + ensure_valid_fields(resource) |
| 129 | + |
| 130 | + # needed for type checking |
| 131 | + assert resource.settings.query is not None |
| 132 | + assert resource.settings.query.value is not None |
| 133 | + |
| 134 | + index_name = resource.index_name |
| 135 | + prompt = resource.settings.query.value |
| 136 | + |
| 137 | + index_folder_path = resource.folder_path |
| 138 | + if not resource.settings.web_search_grounding: |
| 139 | + raise ValueError("Web search grounding field is required for Batch Transform") |
| 140 | + enable_web_search_grounding = ( |
| 141 | + resource.settings.web_search_grounding.value.lower() == "enabled" |
| 142 | + ) |
| 143 | + |
| 144 | + batch_transform_output_columns: list[BatchTransformOutputColumn] = [] |
| 145 | + if (output_columns := resource.settings.output_columns) is None or not len( |
| 146 | + output_columns |
| 147 | + ): |
| 148 | + raise ValueError( |
| 149 | + "Batch transform requires at least one output column to be specified in settings.output_columns" |
| 150 | + ) |
| 151 | + |
| 152 | + for column in output_columns: |
| 153 | + batch_transform_output_columns.append( |
| 154 | + BatchTransformOutputColumn( |
| 155 | + name=column.name, |
| 156 | + description=column.description, |
| 157 | + ) |
| 158 | + ) |
| 159 | + |
| 160 | + class BatchTransformSchemaModel(BaseModel): |
| 161 | + destination_path: str = Field( |
| 162 | + ..., |
| 163 | + description="The relative file path destination for the modified csv file", |
| 164 | + ) |
| 165 | + |
| 166 | + input_model = BatchTransformSchemaModel |
| 167 | + output_model = BatchTransformResponse |
| 168 | + |
| 169 | + @mockable( |
| 170 | + name=resource.name, |
| 171 | + description=resource.description, |
| 172 | + input_schema=input_model.model_json_schema(), |
| 173 | + output_schema=output_model.model_json_schema(), |
| 174 | + example_calls=[], # Examples cannot be provided for context. |
| 175 | + ) |
| 176 | + async def context_tool_fn(destination_path: str) -> dict[str, Any]: |
| 177 | + # TODO: storage_bucket_folder_path_prefix support |
| 178 | + return interrupt( |
| 179 | + CreateBatchTransform( |
| 180 | + name=f"task-{uuid.uuid4()}", |
| 181 | + index_name=index_name, |
| 182 | + prompt=prompt, |
| 183 | + destination_path=destination_path, |
| 184 | + index_folder_path=index_folder_path, |
| 185 | + enable_web_search_grounding=enable_web_search_grounding, |
| 186 | + output_columns=batch_transform_output_columns, |
| 187 | + ) |
| 188 | + ) |
| 189 | + |
| 190 | + return StructuredToolWithOutputType( |
| 191 | + name=tool_name, |
| 192 | + description=resource.description, |
| 193 | + args_schema=input_model, |
| 194 | + coroutine=context_tool_fn, |
| 195 | + output_type=output_model, |
| 196 | + ) |
| 197 | + |
| 198 | + |
| 199 | +def ensure_valid_fields(resource_config: AgentContextResourceConfig): |
| 200 | + if not resource_config.settings.query: |
| 201 | + raise ValueError("Query object is required") |
| 202 | + if not resource_config.settings.query.value: |
| 203 | + raise ValueError("Query prompt is required") |
0 commit comments