-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoolbox.py
More file actions
279 lines (240 loc) · 13 KB
/
toolbox.py
File metadata and controls
279 lines (240 loc) · 13 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import json
from functools import wraps
from typing import Callable, Any, Union, List, Dict
import inspect
from langchain_anthropic import AnthropicLLM
from langchain_core.prompts import PromptTemplate
class ToolRegistry:
def __init__(self):
self.tools: Dict[str, Callable] = {}
def register(self, func: Callable) -> Callable:
self.tools[func.__name__] = func
return func
def get_tools(self) -> Dict[str, Callable]:
return self.tools
tool_registry = ToolRegistry()
def tool(func: Callable) -> Callable:
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
tool_registry.register(wrapper)
return wrapper
class ToolInvoker:
def __init__(self, func: Callable):
self.func = func
def invoke(self, *args, **kwargs) -> Any:
return self.func(*args, **kwargs)
def parse_tool_invocation(raw_invocation: str) -> Union[dict, None]:
try:
invocation = json.loads(raw_invocation)
if isinstance(invocation, dict) and 'tool' in invocation and 'tool_input' in invocation:
return invocation
except json.JSONDecodeError:
pass
return None
class ToolNotFoundException(Exception):
pass
def invoke_tools(tool_invocations: List[Dict]) -> List[Dict]:
results = []
for invocation in tool_invocations:
tool_name = invocation["tool"]
tool_input = invocation["tool_input"]
try:
tool_func = tool_registry.get_tools()[tool_name]
invoker = ToolInvoker(tool_func)
result = invoker.invoke(**tool_input)
results.append({"tool_name": tool_name, "result": result})
except KeyError:
results.append({"tool_name": tool_name, "error": f"Tool '{tool_name}' not found."})
except TypeError as e:
results.append({"tool_name": tool_name, "error": f"Invalid arguments for tool '{tool_name}': {e}"})
except Exception as e:
results.append({"tool_name": tool_name, "error": f"Error executing tool '{tool_name}': {e}"})
return results
def tool_metadata(func: Callable) -> dict:
signature = inspect.signature(func)
docs = func.__doc__ or ""
params = {}
for name, param in signature.parameters.items():
annotation = param.annotation if param.annotation != inspect.Parameter.empty else Any
description = ""
if param.default is not inspect.Parameter.empty:
description += f" (default: {param.default})"
params[name] = {"type": str(annotation), "description": description}
return_annotation = signature.return_annotation if signature.return_annotation != inspect.Parameter.empty else Any
return {
"name": func.__name__,
"description": docs,
"parameters": params,
"returns": str(return_annotation)
}
def generate_tool_metadata(as_json=True) -> Union[str, List[Dict]]:
metadata = [tool_metadata(func) for func in tool_registry.get_tools().values()]
return json.dumps(metadata, indent=2) if as_json else metadata
def generate_tool_prompt() -> str:
return f"""
Available tools:
{generate_tool_metadata()}
Analyze the user's input to determine which tool (if any) should be invoked.
If a tool is relevant, provide its name and the necessary parameters in JSON format:
{{
"tool": "<tool_name>",
"tool_input": {{<param1>: <value1>, <param2>: <value2>, ...}}
}}
It is important to provide the correct tool name and parameters to ensure the tool is invoked correctly.
Remember, you can find the list of tools here: {generate_tool_metadata()}
If no tool is relevant, respond with an empty JSON object:
{{}}
"""
def parse_tool_invocation_ollama(response: str) -> Union[dict, None]:
try:
tool_invocation = json.loads(response)
if isinstance(tool_invocation, dict) and 'tool' in tool_invocation and 'tool_input' in tool_invocation:
return [tool_invocation]
except json.JSONDecodeError:
pass
return None
def parse_tool_invocation_watsonx(response: str) -> Union[dict, None]:
try:
tool_invocation = json.loads(response)
if isinstance(tool_invocation, dict) and 'tool' in tool_invocation and 'tool_input' in tool_invocation:
return [tool_invocation]
except json.JSONDecodeError:
pass
return None
def parse_tool_invocation_llama_cpp(response: str) -> Union[dict, None]:
try:
tool_invocation = json.loads(response)
if isinstance(tool_invocation, dict) and 'tool' in tool_invocation and 'tool_input' in tool_invocation:
return [tool_invocation]
except json.JSONDecodeError:
pass
return None
def parse_tool_invocation_anthropic(response: str) -> Union[dict, None]:
try:
tool_invocation = json.loads(response)
if isinstance(tool_invocation, dict) and 'tool' in tool_invocation and 'tool_input' in tool_invocation:
return [tool_invocation]
except json.JSONDecodeError:
pass
return None
def get_tool_invocation_from_llm(client: object, model: str, prompt: str, api_type: str = "default") -> Union[dict, None]:
if api_type == "ollama":
ollama_prompt = f"{generate_tool_prompt()}\nUser: {prompt}\nAssistant:"
response = client.invoke(ollama_prompt)
tool_invocation = parse_tool_invocation_ollama(response)
elif api_type == "watsonx":
watsonx_prompt = f"{generate_tool_prompt()}\nUser: {prompt}\nAssistant:"
response = client.invoke(watsonx_prompt)
tool_invocation = parse_tool_invocation_watsonx(response)
elif api_type == "llama-cpp":
llama_cpp_prompt = f"{generate_tool_prompt()}\nUser: {prompt}\nAssistant:"
response = client.invoke(llama_cpp_prompt)
tool_invocation = parse_tool_invocation_llama_cpp(response)
elif api_type == "anthropic":
template = """Question: {question}
Answer: Let's think step by step."""
prompt_template = PromptTemplate.from_template(template)
chain = prompt_template | client
response = chain.invoke({"question": prompt})
tool_invocation = parse_tool_invocation_anthropic(response)
else:
messages = [{"role": "system", "content": generate_tool_prompt()}, {"role": "user", "content": prompt}]
response = client.chat.completions.create(model=model, messages=messages, stream=False)
try:
tool_invocation = json.loads(response.choices[0].message.content)
if isinstance(tool_invocation, dict):
tool_invocation = [tool_invocation]
except json.JSONDecodeError:
tool_invocation = None
print("Tool Invocation: ", tool_invocation)
return response, tool_invocation
def generate_final_response(tool_invocations, tool_results, client, model, user_prompt, api_type="default"):
if tool_results is not None:
if api_type == "ollama":
print("OLLAMA INVOKED")
response = client.invoke(f"Based on the data retrieved from the following tool invocations, provide an appropriate response to the user's question(s). \n **** \n The result of invoking {tool_invocations} is {tool_results}.\nUser: {user_prompt}\nAssistant:")
return response, response
elif api_type == "watsonx":
response = client.invoke(f"Based on the data retrieved from the following tool invocations, provide an appropriate response to the user's question(s). \n **** \n The result of invoking {tool_invocations} is {tool_results}.\nUser: {user_prompt}\nAssistant:")
return response, response
elif api_type == "llama-cpp":
response = client.invoke(f"Based on the data retrieved from the following tool invocations, provide an appropriate response to the user's question(s). \n **** \n The result of invoking {tool_invocations} is {tool_results}.\nUser: {user_prompt}\nAssistant:")
return response, response
elif api_type == "anthropic":
template = """Question: Based on the data retrieved from the following tool invocations, provide an appropriate response to the user's question(s).
The result of invoking {tool_invocations} is {tool_results}.
Original user question: {question}
Answer: Let's think step by step."""
prompt_template = PromptTemplate.from_template(template)
chain = prompt_template | client
response = chain.invoke({"question": user_prompt, "tool_invocations": tool_invocations, "tool_results": tool_results})
return response, response
else:
messages = [
{"role": "system", "content": f"Based on the data retrieved from the following tool invocations, provide an appropriate response to the user's question(s). \n **** \n The result of invoking {tool_invocations} is {tool_results}."},
{"role": "user", "content": user_prompt},
]
full_response = client.chat.completions.create(
model=model,
messages=messages,
stream=False,
)
return full_response, full_response.choices[0].message.content
return None, None
def handle_llm_error(error, client, model, prompt, api_type="default"):
error_message = str(error)
if api_type == "ollama":
response = client.invoke(f"The previous response contained malformed JSON. Please review the error carefully and provide a valid JSON response.\nExample of valid JSON format: {generate_tool_prompt()}\nError: {error_message}\nOriginal prompt: {prompt}\nAssistant:")
return response
elif api_type == "watsonx":
response = client.invoke(f"The previous response contained malformed JSON. Please review the error carefully and provide a valid JSON response.\nExample of valid JSON format: {generate_tool_prompt()}\nError: {error_message}\nOriginal prompt: {prompt}\nAssistant:")
return response
elif api_type == "llama-cpp":
response = client.invoke(f"The previous response contained malformed JSON. Please review the error carefully and provide a valid JSON response.\nExample of valid JSON format: {generate_tool_prompt()}\nError: {error_message}\nOriginal prompt: {prompt}\nAssistant:")
return response
elif api_type == "anthropic":
template = """Question: The previous response contained malformed JSON. Please review the error carefully and provide a valid JSON response.
Example of valid JSON format: {generate_tool_prompt()}
Error: {error}
Original prompt: {prompt}
Answer: Let's think step by step."""
prompt_template = PromptTemplate.from_template(template)
chain = prompt_template | client
response = chain.invoke({"error": error_message, "prompt": prompt})
return response
else:
messages = [
{"role": "system", "content": "The previous response contained malformed JSON. Please review the error carefully and provide a valid JSON response."},
{"role": "system", "content": f"Example of valid JSON format: {generate_tool_prompt()}"},
{"role": "user", "content": f"Error: {error_message}\nOriginal prompt: {prompt}"},
]
retry_response = client.chat.completions.create(
model=model,
messages=messages,
stream=False,
)
try:
tool_invocation = json.loads(retry_response.choices[0].message.content)
if isinstance(tool_invocation, dict):
tool_invocation = [tool_invocation]
tool_results = invoke_tools(tool_invocation)
full_response, answer = generate_final_response(tool_invocation, tool_results, client, model, prompt, api_type)
return answer
except json.JSONDecodeError:
return "Apologies, I encountered an error while processing your request. Please try rephrasing your question."
def process_user_request(client, model, prompt, api_type="default"):
response, tool_invocation = get_tool_invocation_from_llm(client, model, prompt, api_type)
if tool_invocation is not None:
try:
if isinstance(tool_invocation, str):
tool_invocation = json.loads(tool_invocation)
if isinstance(tool_invocation, dict):
tool_invocation = [tool_invocation]
tool_results = invoke_tools(tool_invocation)
full_response, answer = generate_final_response(tool_invocation, tool_results, client, model, prompt, api_type)
return answer
except json.JSONDecodeError as e:
return handle_llm_error(e, client, model, prompt, api_type)
except Exception as e:
return "Apologies, an unexpected error occurred while processing your request. Please try again later."