-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtool_factory.py
More file actions
95 lines (75 loc) · 3.2 KB
/
tool_factory.py
File metadata and controls
95 lines (75 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""Factory functions for creating tools from agent resources."""
from logging import getLogger
from langchain_core.language_models import BaseChatModel
from langchain_core.tools import BaseTool
from uipath.agent.models.agent import (
AgentContextResourceConfig,
AgentEscalationResourceConfig,
AgentIntegrationToolResourceConfig,
AgentInternalToolResourceConfig,
AgentIxpExtractionResourceConfig,
AgentIxpVsEscalationResourceConfig,
AgentProcessToolResourceConfig,
BaseAgentResourceConfig,
LowCodeAgentDefinition,
)
from uipath_langchain.chat.hitl import REQUIRE_CONVERSATIONAL_CONFIRMATION
from .context_tool import create_context_tool
from .escalation_tool import create_escalation_tool
from .extraction_tool import create_ixp_extraction_tool
from .integration_tool import create_integration_tool
from .internal_tools import create_internal_tool
from .ixp_escalation_tool import create_ixp_escalation_tool
from .process_tool import create_process_tool
logger = getLogger(__name__)
async def create_tools_from_resources(
agent: LowCodeAgentDefinition, llm: BaseChatModel
) -> list[BaseTool]:
tools: list[BaseTool] = []
logger.info("Creating tools for agent '%s' from resources", agent.name)
for resource in agent.resources:
if not resource.is_enabled:
logger.info(
"Skipping disabled resource '%s' of type '%s'",
resource.name,
type(resource).__name__,
)
continue
logger.info(
"Creating tool for resource '%s' of type '%s'",
resource.name,
type(resource).__name__,
)
tool = await _build_tool_for_resource(resource, llm)
if tool is not None:
if isinstance(tool, list):
tools.extend(tool)
else:
tools.append(tool)
if agent.is_conversational:
props = getattr(resource, "properties", None)
if props and getattr(
props, REQUIRE_CONVERSATIONAL_CONFIRMATION, False
):
if tool.metadata is None:
tool.metadata = {}
tool.metadata[REQUIRE_CONVERSATIONAL_CONFIRMATION] = True
return tools
async def _build_tool_for_resource(
resource: BaseAgentResourceConfig, llm: BaseChatModel
) -> BaseTool | list[BaseTool] | None:
if isinstance(resource, AgentProcessToolResourceConfig):
return create_process_tool(resource)
elif isinstance(resource, AgentContextResourceConfig):
return create_context_tool(resource)
elif isinstance(resource, AgentEscalationResourceConfig):
return create_escalation_tool(resource)
elif isinstance(resource, AgentIntegrationToolResourceConfig):
return create_integration_tool(resource)
elif isinstance(resource, AgentInternalToolResourceConfig):
return create_internal_tool(resource, llm)
elif isinstance(resource, AgentIxpExtractionResourceConfig):
return create_ixp_extraction_tool(resource)
elif isinstance(resource, AgentIxpVsEscalationResourceConfig):
return create_ixp_escalation_tool(resource)
return None