-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathtools.py
More file actions
57 lines (51 loc) · 2.1 KB
/
Copy pathtools.py
File metadata and controls
57 lines (51 loc) · 2.1 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
#
# Copyright (C) 2017-2025 Dremio Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from langchain_core.tools.structured import StructuredTool
from langchain_core.tools.base import create_schema_from_function
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from dremioai.tools.tools import Tool, get_tools, ToolType, system_prompt
from typing import Type, List
from asyncio import run
def instantiate(tool_class: Type[Tool]) -> StructuredTool:
tool_instance = tool_class()
args_schema = create_schema_from_function(
tool_class.__name__, tool_instance.invoke, parse_docstring=True
)
return StructuredTool.from_function(
func=lambda *args, **kw: run(tool_instance.invoke(*args, **kw)),
name=tool_class.__name__,
description=tool_instance.invoke.__doc__[:1024],
args_schema=args_schema,
coroutine=tool_instance.invoke,
strict=False,
)
def discover_tools(For: ToolType = None) -> List[StructuredTool]:
return [instantiate(tool) for tool in get_tools(For=For)]
def discover_prompt(with_prompt: str = None) -> ChatPromptTemplate:
if with_prompt is None:
with_prompt = system_prompt()
return ChatPromptTemplate.from_messages(
[
("system", with_prompt),
(
"system",
"You must respond in Markdown or tables in tab separated values format",
),
MessagesPlaceholder("chat_history", optional=True),
MessagesPlaceholder("messages"),
MessagesPlaceholder("agent_scratchpad", optional=True),
]
)