Repository navigation
Expand file tree
/
Copy pathtool_factory.py
More file actions
115 lines (94 loc) · 3.68 KB
/
Copy pathtool_factory.py
File metadata and controls
115 lines (94 loc) · 3.68 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from pprint import pprint
from src.get_tools import get_tools
from agents import FunctionTool
from run_tool_calls import run_http_tool_call
from pydantic_model_utils import make_pydantic_model_from_schema
import json
# def generate_tool_docstring(tool):
# """
# Generate a docstring for a tool function based on its metadata.
# Args:
# tool (dict): Tool metadata, should include 'name', 'description', 'parameters', 'call' (with 'method' and 'path').
# Returns:
# str: A formatted docstring for the tool function.
# """
# name = tool.get('name', 'unknown_tool')
# desc = tool.get('description', '')
# params = tool.get('parameters', {}).get('properties', {})
# param_lines = []
# for pname, pinfo in params.items():
# param_lines.append(f" {pname} ({pinfo.get('type', 'str')}): {pinfo.get('description', '')}")
# param_section = "\n".join(param_lines) if param_lines else " None"
# docstring = f'''"""
# {name}: {desc}
# Args:
# params (dict): Dictionary of parameters for the tool. Keys:
# {param_section}
# Returns:
# dict or str: The response from the HTTP request.
# """'''
# print(docstring + "\n\n")
# return docstring
tools = get_tools()
def make_on_invoke_tool(tool_def, ToolModel):
async def on_invoke_tool(ctx, args: str):
ToolModel.model_validate_json(args)
parsed = json.loads(args)
print('tool params', parsed)
return run_http_tool_call(tool_def, parsed)
return on_invoke_tool
generated_function_tools = []
for idx, tool_def in enumerate(tools):
if tool_def['call']['type'] == 'http':
ToolModel = make_pydantic_model_from_schema(tool_def['parameters'], name=tool_def['name'] + 'Params')
on_invoke_tool = make_on_invoke_tool(tool_def, ToolModel)
generated_function_tools.append(
FunctionTool(
name=tool_def['name'],
description=tool_def['description'],
params_json_schema=tool_def['parameters'],
on_invoke_tool=on_invoke_tool,
)
)
# if idx == 2:
# break
# generated_function_tools = generated_function_tools[:2]
# def get_people_tool():
# get_people_tool_definition = tools[0]
# generated_function_tool = FunctionTool(
# name=get_people_tool_definition['name'],
# description=generate_tool_docstring(get_people_tool_definition),
# params_json_schema=make_pydantic_model_from_tool(get_people_tool_definition).model_json_schema(),
# on_invoke_tool=lambda ctx, args: run_http_tool_call(get_people_tool_definition, args),
# )
# return generated_function_tool
# def double_number(number: int):
# """
# Double a number.
# Args:
# number (int): The number to double.
# Returns:
# int: The doubled number.
# """
# return number * 2
# double_number_tool_definition = {
# "name": "double_number",
# "description": "Double a number.",
# "parameters": {
# "number": {"type": "integer", "description": "The number to double."},
# }
# }
# def generate_dummy_tool():
# tool_def = double_number_tool_definition
# # tool_def = tools[0]
# return FunctionTool(
# name=tool_def['name'],
# description=generate_tool_docstring(tool_def),
# params_json_schema=make_pydantic_model_from_tool(tool_def).model_json_schema(),
# on_invoke_tool=lambda ctx, args: "Hello world",
# )
# generated_function_tools.append(generate_dummy_tool())
# generated_function_tools.append(generate_double_number_tool())
pprint(generated_function_tools, indent=4)
# if __name__ == "__main__":
# pprint(generated_function_tools, indent=4)