|
4 | 4 |
|
5 | 5 | GLM4_TOOL_SUFFIX_PROMPT = "在调用上述函数时,请使用 Json 格式表示调用的参数。" |
6 | 6 |
|
7 | | -GLM4_TOOL_PROMPT = ( |
8 | | - "你是一个名为 GLM-4 的人工智能助手。你是基于智谱AI训练的语言模型 GLM-4 模型开发的,你的任务是针对用户的问题和要求提供适当的答复和支持," |
9 | | - "{tool_text}" |
10 | | -) |
| 7 | +GLM4_TOOL_PROMPT = """"你是一个名为 GLM-4 的人工智能助手。你是基于智谱AI训练的语言模型 GLM-4 模型开发的,你的任务是针对用户的问题和要求提供适当的答复和支持。 |
| 8 | +
|
| 9 | +# 可用工具 |
| 10 | +{tool_text} |
| 11 | +Use the following format: |
| 12 | +
|
| 13 | +Question: the input question you must answer |
| 14 | +Thought: you should always think about what to do |
| 15 | +Action: the action to take, should be one of [{tool_names}] |
| 16 | +Action Input: the input to the action |
| 17 | +Observation: the result of the action |
| 18 | +... (this Thought/Action/Action Input/Observation can be repeated zero or more times) |
| 19 | +Thought: I now know the final answer |
| 20 | +Final Answer: the final answer to the original input question |
| 21 | +
|
| 22 | +Begin! |
| 23 | +
|
| 24 | +Question: |
| 25 | +""" |
11 | 26 |
|
12 | 27 |
|
13 | 28 | def glm4_tool_formatter(tools: List[Dict[str, Any]]) -> str: |
14 | | - tool_text = "" |
| 29 | + tool_text = "\n" |
| 30 | + tool_names = [] |
15 | 31 | for tool in tools: |
16 | 32 | tool = tool["function"] |
17 | 33 | tool_name = tool["name"] |
18 | | - tool_text += f"\n\n## {tool_name}\n\n{json.dumps(tool, ensure_ascii=False, indent=4)}\n{GLM4_TOOL_SUFFIX_PROMPT}" |
19 | | - return GLM4_TOOL_PROMPT.format(tool_text=tool_text) |
| 34 | + tool_text += f"## {tool_name}\n\n{json.dumps(tool, ensure_ascii=False, indent=4)}\n{GLM4_TOOL_SUFFIX_PROMPT}\n\n" |
| 35 | + tool_names.append(tool_name) |
| 36 | + return GLM4_TOOL_PROMPT.format( |
| 37 | + tool_text=tool_text, tool_names=", ".join(tool_names) |
| 38 | + ).strip() |
20 | 39 |
|
21 | 40 |
|
22 | 41 | def glm4_tool_extractor(content: str) -> Union[str, List[Tuple[str, str]]]: |
23 | | - lines = content.strip().split("\n") |
24 | | - if len(lines) != 2: |
25 | | - return content |
26 | | - tool_name = lines[0].strip() |
27 | | - tool_input = lines[1].strip() |
| 42 | + i = content.rfind("Action:") |
| 43 | + j = content.rfind("Action Input:") |
| 44 | + tool_name = content[i + len("Action:") : j].strip().strip(".") |
| 45 | + tool_input = content[j + len("Action Input:") :].strip() |
28 | 46 | try: |
29 | | - json.loads(tool_input) |
| 47 | + tool_input_obj = json.loads(tool_input) |
30 | 48 | except json.JSONDecodeError: |
31 | 49 | return content |
32 | | - tool_calls = [ |
33 | | - { |
34 | | - "id": "call_{}".format(uuid.uuid4().hex), |
35 | | - "function": {"name": tool_name, "arguments": tool_input}, |
36 | | - } |
37 | | - ] |
| 50 | + tool_calls = [] |
| 51 | + tool_call = { |
| 52 | + "index": 0, |
| 53 | + "id": "call_{}".format(uuid.uuid4().hex), |
| 54 | + "function": {"name": tool_name, "arguments": tool_input}, |
| 55 | + } |
| 56 | + tool_calls.append(tool_call) |
38 | 57 |
|
39 | 58 | return tool_calls |
40 | 59 |
|
|
0 commit comments