-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathtool_llama3.1.py
144 lines (106 loc) · 4 KB
/
tool_llama3.1.py
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
import json
import re, sys
from datetime import datetime
from binding import PATH_BINDS
import tool_definition
from tool_definition import dispatch_tool
# https://llama.meta.com/docs/model-cards-and-prompt-formats/llama3_1/#user-defined-custom-tool-calling
FUNCTION_CALL_START = "<function="
FUNCTION_CALL_CLOSE = "</function>"
def gen_prompt_for_tool(func: dict) -> str:
params = {}
for p in func['parameters']:
params[p['name']] = {
"param_type": p['type'],
"description": p['description'],
"required": p['required']
}
desc = {
"name": func['name'],
"description": func['description'],
"parameters": params
}
return f"Use the function '{func['name']}' to: {func['description']}" + '\n' + json.dumps(desc, ensure_ascii=False, indent=2)
SYS_PROMPT_TEMPLATE1 = """
Environment: ipython
Tools: brave_search, wolfram_alpha
Cutting Knowledge Date: December 2023
Today Date: {date_of_today}
You are a helpful Assistant."""
SYS_PROMPT_TEMPLATE = """
You have access to the following functions:
{tools_def}
If a you choose to call a function ONLY reply in the following format:
<{start_tag}={function_name}>{parameters}{end_tag}
where
start_tag => `<function`
parameters => a JSON dict with the function argument name as key and function argument value as value.
end_tag => `</function>`
Here is an example,
<function=example_function_name>{"example_name": "example_value"}</function>
Reminder:
- Function calls MUST follow the specified format
- Required parameters MUST be specified
- Only call one function at a time
- Put the entire function call reply on one line"
- Always add your sources when using search results to answer the user query
You are a helpful Assistant."""
def build_system_prompt(functions: list[dict]):
prompt = '\n\n'.join([gen_prompt_for_tool(f) for f in functions])
s = SYS_PROMPT_TEMPLATE.replace('{tools_def}', prompt)
s = s.replace('{date_of_today}', datetime.now().strftime('%d %b %Y'))
return s
import chatllm
from chatllm import ChatLLM
def call_internal_tool(s: str) -> str:
print(f"[Use Builtin Tool]{s}")
return "Error: not implemented"
def call_functions(s: str) -> str:
try:
r = []
for tool_name, code in parse_function_calls(s):
print(f'[Use Tool] {tool_name}')
observation = dispatch_tool(tool_name, code)
r.append(observation.text)
return '\n\n'.join(r)
except Exception as e:
print(f"error occurs: {e}")
return "failed to call the function"
def parse_function_calls(s: str) -> list[tuple[str, dict]] | None:
try:
matches = re.findall(r'<function=([^>]*)>(.*)' + FUNCTION_CALL_CLOSE, s)
if matches is None: return None
return [(match[0], json.loads(match[1])) for match in matches]
except:
return None
class ToolChatLLM(ChatLLM):
chunk_acc = ''
def callback_print(self, s: str) -> None:
if self.chunk_acc == '':
if FUNCTION_CALL_START.startswith(s):
self.chunk_acc = s
else:
super().callback_print(s)
return
self.chunk_acc = self.chunk_acc + s
if len(self.chunk_acc) < len(FUNCTION_CALL_START):
return
if not self.chunk_acc.startswith(FUNCTION_CALL_START):
super().callback_print(self.chunk_acc)
self.chunk_acc = ''
return
def callback_end(self) -> None:
s = self.chunk_acc
self.chunk_acc = ''
super().callback_end()
if len(s) < 1: return
if parse_function_calls(s) is not None:
rsp = call_functions(s)
self.tool_input(rsp)
else:
super().callback_print(s)
def call_tool(self, s: str) -> None:
rsp = call_internal_tool(s.strip())
self.tool_input(rsp)
if __name__ == '__main__':
chatllm.demo_simple(sys.argv[1:] + ['-s', build_system_prompt(tool_definition._TOOL_DESCRIPTIONS)], ToolChatLLM, lib_path=PATH_BINDS)