if role is tool then set to user

This commit is contained in:
Toddy
2025-03-03 08:23:04 +00:00
parent 88d483c1ef
commit 7b5b6c7d4c
2 changed files with 17 additions and 3 deletions

View File

@@ -36,10 +36,17 @@ class OpenAIMessageConverter(MessageConverter):
converted_messages = []
system_instruction = None
for msg in messages:
for idx, msg in enumerate(messages):
role = msg.get("role", "")
if role not in SUPPORTED_ROLES:
role = "model"
if role == "tool":
role = "user"
else:
# 如果是最后一条消息,则认为是用户消息
if idx == len(messages) - 1:
role = "user"
else:
role = "model"
parts = []
if isinstance(msg["content"], str) and msg["content"]:

View File

@@ -57,7 +57,14 @@ def _build_tools(
function_declarations.append(function)
if function_declarations:
tools.append({"functionDeclarations": function_declarations})
# 按照 function 的 name 去重
names, functions = set(), []
for item in function_declarations:
if item.get("name") not in names:
names.add(item.get("name"))
functions.append(item)
tools.append({"functionDeclarations": functions})
return tools