-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
196 lines (183 loc) · 6.03 KB
/
tools.py
File metadata and controls
196 lines (183 loc) · 6.03 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import os
import json
from langchain_community.tools.tavily_search import TavilySearchResults
from dotenv import load_dotenv # Import load_dotenv
load_dotenv()
def _get_work_dir_root():
work_dir_root = os.environ.get("WORKDIR_ROOT", './data/llm_result')
return work_dir_root
WORKDIR = _get_work_dir_root()
def read_file(filename):
if not isinstance(filename, str):
return "filename must be a string"
filename = os.path.join(WORKDIR, filename)
if not os.path.exists(filename):
return f"{filename} does not exist"
try:
with open(filename, 'r') as f:
return f.read()
except Exception as e:
return f"Error reading file: {e}"
def append_to_file(filename, content):
if not isinstance(filename, str):
return "filename must be a string"
if not isinstance(content, str):
return "content must be a string"
filename = os.path.join(WORKDIR, filename)
try:
if not os.path.exists(filename):
# 创建文件并写入内容
with open(filename, 'w') as f:
f.write(content)
return "File created and content appended"
else:
with open(filename, 'a') as f:
f.write(content)
return "Append successful"
except Exception as e:
return f"Error appending to file: {e}"
def write_to_file(filename, content):
if not isinstance(filename, str):
return "filename must be a string"
if not isinstance(content, str):
return "content must be a string"
filename = os.path.join(WORKDIR, filename)
if not os.path.exists(WORKDIR):
os.makedirs(WORKDIR)
try:
with open(filename, 'w') as f:
f.write(content)
return "Write successful"
except Exception as e:
return f"Error writing to file: {e}"
def search(query):
if not isinstance(query, str):
return "query must be a string"
# Debugging: Check if the API key is loaded
tavily_api_key = os.environ.get("TAVILY_API_KEY")
if not tavily_api_key:
return "Error: TAVILY_API_KEY is not set in the environment."
tavily = TavilySearchResults(max_results=5)
try:
ret = tavily.invoke(input=query)
# If 'ret' is a string, return it directly
if isinstance(ret, str):
return ret
# If 'ret' is a list, process accordingly
elif isinstance(ret, list):
content_list = [obj.get('content', '') for obj in ret]
return "\n".join(content_list)
else:
return f"Unexpected return type from tavily.invoke: {type(ret)}"
except Exception as err:
return f"Search error: {err}"
def calculator(expression):
if not isinstance(expression, str):
return "expression must be a string"
try:
# For safety, limit allowed characters
allowed_chars = "0123456789+-*/(). "
if not all(c in allowed_chars for c in expression):
return "Invalid characters in expression"
result = eval(expression)
return str(result)
except Exception as e:
return f"Error evaluating expression: {e}"
tools_info = [
{
"name": "read_file",
"description": "Reads the content of a specified file. The file should exist before reading.",
"args": [
{
"name": "filename",
"type": "string",
"description": "Name of the file to read."
}
]
},
{
"name": "append_to_file",
"description": "Appends content to a specified file. If the file does not exist, it will be created.",
"args": [
{
"name": "filename",
"type": "string",
"description": "Name of the file."
},
{
"name": "content",
"type": "string",
"description": "Content to append to the file."
}
]
},
{
"name": "write_to_file",
"description": "Writes content to a specified file, overwriting any existing content.",
"args": [
{
"name": "filename",
"type": "string",
"description": "Name of the file."
},
{
"name": "content",
"type": "string",
"description": "Content to write to the file."
}
]
},
{
"name": "search",
"description": "Performs a search using the provided query and returns relevant content.",
"args": [
{
"name": "query",
"type": "string",
"description": "Search query to look up."
}
]
},
{
"name": "calculator",
"description": "Evaluates a mathematical expression and returns the result.",
"args": [
{
"name": "expression",
"type": "string",
"description": "Mathematical expression to evaluate."
}
]
},
{
"name": "finish",
"description": "use this function when you think you have already complete the target",
"args": [{
"name":"answer",
"type":"string",
"description":"the goal is final answer"
}]
}
]
tools_map = {
"read_file": read_file,
"append_to_file": append_to_file,
"write_to_file": write_to_file,
"search": search,
"calculator":calculator
}
def gen_tools_desc():
tools_desc = []
for idx, i in enumerate(tools_info):
args_desc = []
for info in i['args']:
args_desc.append({
"name": info["name"],
"description": info["description"],
"type": info["type"]
})
args_desc_json = json.dumps(args_desc, ensure_ascii=False)
tool_desc = f"{idx+1}. {i['name']}: {i['description']}, args: {args_desc_json}"
tools_desc.append(tool_desc)
tools_prompt = "\n".join(tools_desc)
return tools_prompt