|
| 1 | +from loguru import logger |
| 2 | +from typing import Any, Dict, List, Tuple, Union, Optional |
| 3 | +import json |
| 4 | +import uuid |
| 5 | + |
| 6 | +from gpt_server.model_handler.react.v1.prompts.qwen_prompt import ( |
| 7 | + TOOL_SYSTEM_PROMPT_CN, |
| 8 | + TOOl_CHOICE_SYSTEM_PROMPT_CN, |
| 9 | + TOOL_CHOICE_SUFFIX_PROMPT, |
| 10 | + TOOL_SUFFIX_PROMPT, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +def qwen_tool_formatter( |
| 15 | + tools: List[Dict[str, Any]], tool_choice_info: Optional[dict] = None |
| 16 | +) -> str: |
| 17 | + tool_chooce_suffix_prompt = "" |
| 18 | + logger.info(f"tool_choice_info: {tool_choice_info}") |
| 19 | + tool_system_prompt = TOOL_SYSTEM_PROMPT_CN |
| 20 | + if tool_choice_info: |
| 21 | + tool_chooce_suffix_prompt = TOOL_CHOICE_SUFFIX_PROMPT |
| 22 | + tools = [tools[tool_choice_info["tool_choice_idx"]]] |
| 23 | + logger.info(f"tools 已被替换为tool_choic: {tools}") |
| 24 | + tool_system_prompt = TOOl_CHOICE_SYSTEM_PROMPT_CN |
| 25 | + |
| 26 | + tool_names = [] |
| 27 | + param_text_list = [] |
| 28 | + for tool in tools: |
| 29 | + tool = tool["function"] |
| 30 | + tool_name = tool["name"] |
| 31 | + description = tool["description"] |
| 32 | + parameters = tool["parameters"] |
| 33 | + param_text = ( |
| 34 | + """### {tool_name}\n\n{tool_name}: {description} 输入参数: {parameters} \n""" |
| 35 | + + TOOL_SUFFIX_PROMPT |
| 36 | + + tool_chooce_suffix_prompt |
| 37 | + ) |
| 38 | + param_text_str = param_text.format( |
| 39 | + tool_name=tool_name, |
| 40 | + description=description, |
| 41 | + parameters=parameters, |
| 42 | + ) |
| 43 | + param_text_list.append(param_text_str) |
| 44 | + |
| 45 | + tool_names.append(tool_name) |
| 46 | + |
| 47 | + tool_text = "\n\n".join(param_text_list).strip() |
| 48 | + return tool_system_prompt.format( |
| 49 | + tool_text=tool_text, |
| 50 | + tool_names=", ".join(tool_names), |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +def qwen_tool_extractor(content: str) -> Union[str, List[Tuple[str, str]]]: |
| 55 | + output = json.loads(content) |
| 56 | + reason = output["reason"] |
| 57 | + final_answer = reason.get("final_answer", None) |
| 58 | + if final_answer: # 最终回答 |
| 59 | + return output |
| 60 | + else: # 工具 |
| 61 | + tool_name = reason["action"] |
| 62 | + tool_input = reason["action_input"] |
| 63 | + tool_calls = [] |
| 64 | + tool_call = { |
| 65 | + "index": 0, |
| 66 | + "id": "call_{}".format(uuid.uuid4().hex), |
| 67 | + "function": {"name": tool_name, "arguments": tool_input}, |
| 68 | + } |
| 69 | + tool_calls.append(tool_call) |
| 70 | + |
| 71 | + return tool_calls |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + import json |
| 76 | + |
| 77 | + tools_str = """[{'type': 'function', 'function': {'name': 'track', 'description': '追踪指定股票的实时价格', 'parameters': {'type': 'object', 'properties': {'symbol': {'description': '需要追踪的股票代码', 'type': 'integer'}}, 'required': ['symbol']}}}, {'type': 'function', 'function': {'name': 'text-to-speech', 'description': '将文本转换为语音', 'parameters': {'type': 'object', 'properties': {'text': {'description': '需要转换成语音的文本', 'type': 'string'}, 'voice': {'description': '要使用的语音类型(男声、女声等', 'default': '男声', 'type': 'string'}, 'speed': {'description': '语音的速度(快、中等、慢等', 'default': '中等', 'type': 'string'}}, 'required': ['text']}}}]""" |
| 78 | + tools_str = tools_str.replace("'", '"') |
| 79 | + tools = json.loads(tools_str) |
| 80 | + res = qwen_tool_formatter(tools=tools) |
| 81 | + print(res) |
| 82 | + |
| 83 | + out = """{ |
| 84 | + "thought":"你应该时刻思考自己该做什么", |
| 85 | + "reason":{ |
| 86 | + "action":"track", |
| 87 | + "action_input":{"a":"1"} |
| 88 | + } |
| 89 | +}""" |
| 90 | + r = qwen_tool_extractor(out) |
| 91 | + print("\n\n") |
| 92 | + print(r) |
0 commit comments