File size: 4,403 Bytes
7feac49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Alibaba, Inc. and its affiliates.
from typing import List, Union

from .base import AgentKeyword, BaseAgentTemplate

keyword = AgentKeyword(
    action='✿FUNCTION✿:',
    action_input='✿ARGS✿:',
    observation='✿RESULT✿:',
)


class QwenEnAgentTemplate(BaseAgentTemplate):
    keyword = keyword

    def _get_tool_names_descs(self, tools):
        tool_names = []
        tool_descs = []
        for tool in tools:
            tool_desc = self._parse_tool(tool, 'en')
            tool_names.append(tool_desc.name_for_model)
            tool_descs.append(f'### {tool_desc.name_for_human}\n\n'
                              f'{tool_desc.name_for_model}: {tool_desc.description_for_model} '
                              f'Parameters: {tool_desc.parameters} {tool_desc.args_format}')
        return tool_names, tool_descs

    def _format_tools(self, tools: List[Union[str, dict]], system: str, user_message=None) -> str:
        tool_names, tool_descs = self._get_tool_names_descs(tools)
        return f"""{system}

# Tools

## You have access to the following tools:

""" + '\n\n'.join(tool_descs) + f"""

## When you need to call a tool, please insert the following command in your reply, which can be called zero or multiple times according to your needs:

✿FUNCTION✿: The tool to use, should be one of [{','.join(tool_names)}]
✿ARGS✿: The input of the tool
✿RESULT✿: Tool results
✿RETURN✿: Reply based on tool results. Images need to be rendered as ![](url)"""  # noqa


class QwenZhAgentTemplate(BaseAgentTemplate):
    keyword = keyword

    def _get_tool_names_descs(self, tools):
        tool_names = []
        tool_descs = []
        for tool in tools:
            tool_desc = self._parse_tool(tool, 'zh')
            tool_names.append(tool_desc.name_for_model)
            tool_descs.append(f'### {tool_desc.name_for_human}\n\n'
                              f'{tool_desc.name_for_model}: {tool_desc.description_for_model} '
                              f'输入参数:{tool_desc.parameters} {tool_desc.args_format}')
        return tool_names, tool_descs

    def _format_tools(self, tools: List[Union[str, dict]], system: str, user_message=None) -> str:
        tool_names, tool_descs = self._get_tool_names_descs(tools)
        return f"""{system}

# 工具

## 你拥有如下工具:

""" + '\n\n'.join(tool_descs) + f"""

## 你可以在回复中插入零次、一次或多次以下命令以调用工具:

✿FUNCTION✿: 工具名称,必须是[{','.join(tool_names)}]之一。
✿ARGS✿: 工具输入
✿RESULT✿: 工具结果
✿RETURN✿: 根据工具结果进行回复,需将图片用![](url)渲染出来"""  # noqa


class QwenEnParallelAgentTemplate(QwenEnAgentTemplate):

    def _format_tools(self, tools: List[Union[str, dict]], system: str, user_message=None) -> str:
        tool_names, tool_descs = self._get_tool_names_descs(tools)
        return f"""{system}

# Tools

## You have access to the following tools:

""" + '\n\n'.join(tool_descs) + f"""

## Insert the following command in your reply when you need to call N tools in parallel:

✿FUNCTION✿: The name of tool 1, should be one of [{','.join(tool_names)}]
✿ARGS✿: The input of tool 1
✿FUNCTION✿: The name of tool 2
✿ARGS✿: The input of tool 2
...
✿FUNCTION✿: The name of tool N
✿ARGS✿: The input of tool N
✿RESULT✿: The result of tool 1
✿RESULT✿: The result of tool 2
...
✿RESULT✿: he result of tool N
✿RETURN✿: Reply based on tool results. Images need to be rendered as ![](url)"""  # noqa


class QwenZhParallelAgentTemplate(QwenZhAgentTemplate):

    def _format_tools(self, tools: List[Union[str, dict]], system: str, user_message=None) -> str:
        tool_names, tool_descs = self._get_tool_names_descs(tools)
        return f"""{system}

# 工具

## 你拥有如下工具:

""" + '\n\n'.join(tool_descs) + f"""

## 你可以在回复中插入以下命令以并行调用N个工具:

✿FUNCTION✿: 工具1的名称,必须是[{','.join(tool_names)}]之一
✿ARGS✿: 工具1的输入
✿FUNCTION✿: 工具2的名称
✿ARGS✿: 工具2的输入
...
✿FUNCTION✿: 工具N的名称
✿ARGS✿: 工具N的输入
✿RESULT✿: 工具1的结果
✿RESULT✿: 工具2的结果
...
✿RESULT✿: 工具N的结果
✿RETURN✿: 根据工具结果进行回复,需将图片用![](url)渲染出来"""  # noqa