Skip to content

Commit 8671553

Browse files
committed
fix glm
1 parent 3c391e4 commit 8671553

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

gpt_server/model_handler/prompts.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@ def __init__(
1515
separator="\n",
1616
tools="""\n\n你可以使用以下工具提供适当的答复和支持。\n\n# 可用工具\n\n在<tools></tools> XML标签中提供了function的签名(即函数的结构信息):\n<tools>""",
1717
eotools="""\n</tools>
18-
Use the following format:
18+
## 如果使用工具,你可以在回复中插入零次、一次或多次以下命令以调用工具:
1919
20-
Question: the input question you must answer
21-
Thought: you should always think about what to do
2220
Action: the action to take, should be one of [{tool_names}]
2321
Action Input: the input to the action
24-
Observation: the result of the action
25-
... (this Thought/Action/Action Input/Observation can be repeated zero or more times)
26-
Thought: I now know the final answer
27-
Final Answer: the final answer to the original input question
2822
29-
Begin!
23+
形如:
24+
Action: get_result
25+
Action Input: {{"a": "1","b": "2"}}
26+
27+
如果回答问题已经不需要再继续使用工具,则不需要再使用Action、Action Input格式,可直接回答。
3028
""",
3129
stop_words=["<|user|>", "<|endoftext|>", "<|observation|>"],
3230
meta_instruction="你是一个名为 GLM-4 的人工智能助手。你是基于智谱AI训练的语言模型 GLM-4 模型开发的,你的任务是针对用户的问题和要求提供适当的答复和支持。",
@@ -102,15 +100,15 @@ def messages2prompt_base(self, messages, sequence_start=True, tools=None, **kwar
102100
for tool in tools:
103101
tool_names.append(tool["function"]["name"])
104102
tool_names = ",".join(tool_names)
105-
self.eotools = self.eotools.format(tool_names=tool_names)
103+
eotools = self.eotools.format(tool_names=tool_names)
106104
for tool in tools:
107105
tool_prompt += self.separator
108106
tool_prompt += f'{{"type": "function", "function": {json.dumps(tool, ensure_ascii=False)}}}'
109107
if len(messages) and messages[0]["role"] == "system":
110-
ret += f"{self.system}{messages[0]['content']}{self.tools}{tool_prompt}{self.eotools}{self.eosys}"
108+
ret += f"{self.system}{messages[0]['content']}{self.tools}{tool_prompt}{eotools}{self.eosys}"
111109
messages.pop(0)
112110
else:
113-
ret += f"{self.system}{self.meta_instruction}{self.tools}{tool_prompt}{self.eotools}{self.eosys}"
111+
ret += f"{self.system}{self.meta_instruction}{self.tools}{tool_prompt}{eotools}{self.eosys}"
114112

115113
for message in messages:
116114
role = message["role"]

gpt_server/model_handler/tool_parser.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ def extract_tool_calls(
6363
) -> ExtractedToolCallInformation:
6464
text = model_output
6565
try:
66-
66+
if "Action:" not in text:
67+
raise Exception
6768
i = text.rfind("Action:")
6869
j = text.rfind("Action Input:")
6970
name = text[i + len("Action:") : j].strip().strip(".")
@@ -324,7 +325,10 @@ def tool_parser(full_text: str, tool_parser: ToolParser, tools, ret):
324325
ret["finish_reason"] = "tool_calls"
325326
return json.dumps(ret).encode() + b"\0"
326327
else:
328+
logger.info(f"工具解析失败, tool_calls: {tool_calls}")
327329
ret["text"] = ""
330+
ret["tool_calls"] = tool_calls
331+
ret["finish_reason"] = "tool_calls"
328332
return json.dumps(ret).encode() + b"\0"
329333

330334

gpt_server/model_worker/chatglm.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ def __init__(
5050
)
5151
self.stop_words_ids = []
5252

53-
self.stop = [
54-
self.tokenizer.decode(skip_word) for skip_word in self.stop_words_ids
55-
]
53+
self.stop = ["Observation:"]
5654
logger.warning(f"{model_names[0]} 停用词: {self.stop}")
5755

5856
async def generate_stream_gate(self, params):

tests/test_openai_completion_tool_calling.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
client = OpenAI(api_key="EMPTY", base_url="http://localhost:8082/v1")
66

77

8-
def get_weather(location: str, unit: str):
8+
def get_weather(location: str, unit: str = "celsius"):
99
return f"Getting the weather for {location} in {unit}..."
1010

1111

@@ -37,8 +37,10 @@ def get_weather(location: str, unit: str):
3737
tools=tools,
3838
tool_choice="auto",
3939
)
40-
tool_call = response.choices[0].message.tool_calls[0].function
40+
41+
print("message", response.choices[0].message)
4142
print(response.choices[0].message.tool_calls)
43+
tool_call = response.choices[0].message.tool_calls[0].function
4244
print(f"Function called: {tool_call.name}")
4345
print(f"Arguments: {tool_call.arguments}")
4446
print(f"Result: {get_weather(**json.loads(tool_call.arguments))}")

0 commit comments

Comments
 (0)