Skip to content

增强Qwen类模型应用Agent解析时正则表达式的匹配+修复创建模型时的bug #5257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ class QwenChatAgentOutputParserCustom(StructuredChatOutputParser):

def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
if s := re.findall(
r"\nAction:\s*(.+)\nAction\sInput:\s*(.+)", text, flags=re.DOTALL
r"\*{0,2}Action:\*{0,2}\s*(.+?)\s*\*{0,2}Action Input:\*{0,2}\s*(?:```(?:json\n)?)?({.*?})(?:```)?", text, flags=re.DOTALL
):
s = s[-1]
json_string: str = s[1]
tool_name: str = s[0].replace("[", "").replace("]", "").replace("*", "").strip()
json_string: str = s[1].replace("\n", "").replace(" ", "").replace("*", "").strip()
json_input = None
try:
json_input = json.loads(json_string)
Expand Down Expand Up @@ -135,7 +136,7 @@ def parse(self, text: str) -> Union[AgentAction, AgentFinish]:
if "command" in json_input:
json_input["query"] = json_input.pop("command")

return AgentAction(tool=s[0].strip(), tool_input=json_input, log=text)
return AgentAction(tool=tool_name.strip(), tool_input=json_input, log=text)
elif s := re.findall(r"\nFinal\sAnswer:\s*(.+)", text, flags=re.DOTALL):
s = s[-1]
return AgentFinish({"output": s}, log=text)
Expand Down Expand Up @@ -192,7 +193,7 @@ def create_structured_qwen_chat_agent(
RunnablePassthrough.assign(agent_scratchpad=itemgetter("intermediate_steps"))
| prompt
| llm.bind(
stop=["<|endoftext|>", "<|im_start|>", "<|im_end|>", "\nObservation:"]
stop=["<|endoftext|>", "<|im_start|>", "<|im_end|>", "\nObservation:", "\n**Observation:**"]
)
| output_parser
)
Expand Down
11 changes: 6 additions & 5 deletions libs/chatchat-server/chatchat/server/chat/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,21 @@ def create_models_from_config(configs, callbacks, stream, max_tokens):
models = {}
prompts = {}
for model_type, params in configs.items():
model_name = params.get("model", "").strip() or get_default_llm()
callbacks = callbacks if params.get("callbacks", False) else None
model_name = list(params.keys())[0].strip() or get_default_llm()
params_model = params[model_name]
callbacks = callbacks if params_model.get("callbacks", False) else None
# 判断是否传入 max_tokens 的值, 如果传入就按传入的赋值(api 调用且赋值), 如果没有传入则按照初始化配置赋值(ui 调用或 api 调用未赋值)
max_tokens_value = max_tokens if max_tokens is not None else params.get("max_tokens", 1000)
max_tokens_value = max_tokens if max_tokens is not None else params_model.get("max_tokens", 1000)
model_instance = get_ChatOpenAI(
model_name=model_name,
temperature=params.get("temperature", 0.5),
temperature=params_model.get("temperature", 0.5),
max_tokens=max_tokens_value,
callbacks=callbacks,
streaming=stream,
local_wrap=True,
)
models[model_type] = model_instance
prompt_name = params.get("prompt_name", "default")
prompt_name = params_model.get("prompt_name", "default")
prompt_template = get_prompt_template(type=model_type, name=prompt_name)
prompts[model_type] = prompt_template
return models, prompts
Expand Down