Files
hack/tools/scripts/llm-key-hunter/results/_cache/content/chenchen0103_acebench.json
T

1 line
34 KiB
JSON

{"repo": "chenchen0103/acebench", "files": {"model_inference/multi_turn/APIModel_agent.py": {"56dd66cf6439b0d9655ee1b353e4cd745c6f664e": {"t": 1785745443.036218, "n": 10535, "h": "cc7ede689f3d4ebb", "c": "from openai import OpenAI\nimport os\nimport re \nimport ast\nfrom model_inference.prompt_en import TRAVEL_PROMPT_EN, BASE_PROMPT_EN\nfrom model_inference.prompt_zh import TRAVEL_PROMPT_ZH, BASE_PROMPT_ZH\n\n\nMULTI_TURN_AGENT_PROMPT_SYSTEM_ZH = \"\"\"你是一个AI系统,你的角色为system,请根据给定的API说明和对话历史1..t,为角色system生成在步骤t+1中生成相应的内容。\n1 如果上一步提供的信息完整,能够正常进行api的调用,你应该调用的API请求,API请求以[ApiName(key1='value1', key2='value2', ...)]的格式输出,不要在输出中输出任何其他解释或提示或API调用的结果。\n如果API参数描述中没有特殊说明,则该参数为非必选参数(用户输入中提及的参数需要包含在输出中,如果未提及,则不需要包含在输出中)。\\n如果API参数描述未指定取值格式要求,则该参数取值使用用户原文。\n2 如果你得到的信息不完整,需要向user提问,以获得完整的信息。你不能扮演user去回答一些文职的问题,应该及时像user询问。\n\n请注意,如果需要进行api调用,请严格遵守调用规则[ApiName(key1='value1', key2='value2', ...)],此时不得输出其他文本内容。\n\n角色说明:\nuser: 用户 \nagent: 进行API请求调用的AI系统角色 \nexecution: 执行api调用并返回结果\n\n你需要遵循的规则如下:\\n\n\"\"\"\n\nMULTI_TURN_AGENT_PROMPT_USER_ZH = \"\"\"下面是你可使用的api列表:\\n {functions}\\n\\n对话历史1..t:\\n{history}\"\"\"\n\nMULTI_TURN_AGENT_PROMPT_SYSTEM_EN = \"\"\"You are an AI system with the role name \"system.\" Based on the provided API specifications and conversation history from steps 1 to t, generate the appropriate content for step t+1 for the \"system\" role.\n1. If the information provided in the previous step is complete and the API call can be executed normally, you should generate the API request. The API request should be output in the format [ApiName(key1='value1', key2='value2', ...)]. Do not include any other explanations, prompts, or API call results in the output.\n - If the API parameter description does not specify otherwise, the parameter is optional (parameters mentioned in the user input need to be included in the output; if not mentioned, they do not need to be included).\n - If the API parameter description does not specify the required format for the value, use the user's original text for the parameter value.\n2. If the information you received is incomplete, you need to ask the user for more information to obtain the complete details. You should not pretend to be the user to answer some clerical questions; instead, promptly ask the user for clarification.\n\nPlease note that if an API call is required, strictly adhere to the call format rules [ApiName(key1='value1', key2='value2', ...)] and do not output any other text content.\n\nRole Descriptions:\nuser: User\nagent: The AI system role that makes API requests\nexecution: Executes the API call and returns the result\n\nThe rules you need to follow are as follows:\\n\n\"\"\"\n\nMULTI_TURN_AGENT_PROMPT_USER_EN = \"\"\"Below is the list of APIs you can use:\\n {functions}\\n\\nConversation history 1..t:\\n{history}\"\"\"\n\nclass APIAgent_turn():\n\n def __init__(self, model_name, time, functions, involved_class, temperature=0.001, top_p=1, max_tokens=1000, language=\"zh\") -> None:\n self.model_name = model_name.lower()\n \n if \"gpt\" in self.model_name:\n api_key = os.getenv(\"GPT_AGENT_API_KEY\")\n base_url = os.getenv(\"GPT_AGENT_BASE_URL\")\n elif \"deepseek\" in self.model_name:\n api_key = os.getenv(\"DEEPSEEK_API_KEY\")\n base_url = os.getenv(\"DEEPSEEK_BASE_URL\")\n elif \"qwen\" in self.model_name:\n api_key = os.getenv(\"QWEN_API_KEY\")\n base_url = os.getenv(\"QWEN_BASE_URL\")\n elif \"kimi\" in self.model_name:\n api_key = os.getenv(\"KIMI_API_KEY\")\n base_url = os.getenv(\"KIMI_BASE_URL\")\n else:\n raise ValueError(f\"Unknown model name: {self.model_name}\")\n\n self.client = OpenAI(base_url=base_url, api_key=api_key)\n self.temperature = temperature\n self.top_p = top_p\n self.max_tokens = max_tokens\n self.time = time\n self.functions = functions\n self.involved_class = involved_class\n self.language = language\n self.model_name = model_name\n\n def decode_function_list(self, result):\n func = result\n if \" \" == func[0]:\n func = func[1:]\n if not func.startswith(\"[\"):\n func = \"[\" + func\n if not func.endswith(\"]\"):\n func = func + \"]\"\n decoded_output = self.ast_parse(func)\n return self.decoded_output_to_execution_list(decoded_output)\n\n def ast_parse(self, input_str, language=\"Python\"):\n if language == \"Python\":\n cleaned_input = input_str.strip(\"[]'\") \n parsed = ast.parse(cleaned_input, mode=\"eval\")\n extracted = []\n \n \n if isinstance(parsed.body, ast.Call):\n extracted.append(self.resolve_ast_call(parsed.body))\n elif isinstance(parsed.body, (ast.Tuple, ast.List)): \n for elem in parsed.body.elts:\n if isinstance(elem, ast.Call):\n extracted.append(self.resolve_ast_call(elem))\n else:\n return False\n return extracted\n \n def resolve_ast_call(self,elem):\n # Handle nested attributes for deeply nested module paths\n func_parts = []\n func_part = elem.func\n while isinstance(func_part, ast.Attribute):\n func_parts.append(func_part.attr)\n func_part = func_part.value\n if isinstance(func_part, ast.Name):\n func_parts.append(func_part.id)\n func_name = \".\".join(reversed(func_parts))\n args_dict = {}\n for arg in elem.keywords:\n output = self.resolve_ast_by_type(arg.value)\n args_dict[arg.arg] = output\n return {func_name: args_dict}\n\n\n def resolve_ast_by_type(self,value):\n if isinstance(value, ast.Constant):\n if value.value is Ellipsis:\n output = \"...\"\n else:\n output = value.value\n elif isinstance(value, ast.UnaryOp):\n output = -value.operand.value\n elif isinstance(value, ast.List):\n output = [self.resolve_ast_by_type(v) for v in value.elts]\n elif isinstance(value, ast.Dict):\n output = {\n self.resolve_ast_by_type(k): self.resolve_ast_by_type(v)\n for k, v in zip(value.keys, value.values)\n }\n elif isinstance(\n value, ast.NameConstant\n ): # Added this condition to handle boolean values\n output = value.value\n elif isinstance(\n value, ast.BinOp\n ): # Added this condition to handle function calls as arguments\n output = eval(ast.unparse(value))\n elif isinstance(value, ast.Name):\n output = value.id\n elif isinstance(value, ast.Call):\n if len(value.keywords) == 0:\n output = ast.unparse(value)\n else:\n output = self.resolve_ast_call(value)\n elif isinstance(value, ast.Tuple):\n output = tuple(self.resolve_ast_by_type(v) for v in value.elts)\n elif isinstance(value, ast.Lambda):\n output = eval(ast.unparse(value.body[0].value))\n elif isinstance(value, ast.Ellipsis):\n output = \"...\"\n elif isinstance(value, ast.Subscript):\n try:\n output = ast.unparse(value.body[0].value)\n except:\n output = ast.unparse(value.value) + \"[\" + ast.unparse(value.slice) + \"]\"\n else:\n raise Exception(f\"Unsupported AST type: {type(value)}\")\n return output\n \n \n def decoded_output_to_execution_list(self,decoded_output):\n\n execution_list = []\n for function_call in decoded_output:\n for key, value in function_call.items():\n args_str = \", \".join(\n f\"{k}={self.parse_nested_value(v)}\" for k, v in value.items()\n )\n execution_list.append(f\"{key}({args_str})\")\n return execution_list\n \n def parse_nested_value(self,value):\n\n if isinstance(value, dict):\n func_name = list(value.keys())[0]\n args = value[func_name]\n args_str = \", \".join(f\"{k}={self.parse_nested_value(v)}\" for k, v in args.items())\n return f\"{func_name}({args_str})\"\n return repr(value)\n\n\n\n def respond(self, history) -> None:\n\n current_message = {}\n\n if self.language == \"zh\":\n system_prompt = MULTI_TURN_AGENT_PROMPT_SYSTEM_ZH\n user_prompt = MULTI_TURN_AGENT_PROMPT_USER_ZH.format(functions = self.functions, history = history)\n if \"Travel\" in self.involved_class:\n system_prompt += TRAVEL_PROMPT_ZH\n if \"BaseApi\" in self.involved_class:\n system_prompt += BASE_PROMPT_ZH\n elif self.language == \"en\":\n system_prompt = MULTI_TURN_AGENT_PROMPT_SYSTEM_EN\n user_prompt = MULTI_TURN_AGENT_PROMPT_USER_EN.format(functions = self.functions, history = history)\n if \"Travel\" in self.involved_class:\n system_prompt += TRAVEL_PROMPT_EN\n if \"BaseApi\" in self.involved_class:\n system_prompt += BASE_PROMPT_EN\n\n if \"o1\" not in self.model_name:\n message = [\n {\n \"role\": \"system\",\n \"content\": system_prompt,\n },\n {\n \"role\": \"user\",\n \"content\": user_prompt,\n },\n ]\n\n response = self.client.chat.completions.create(\n messages=message,\n model=self.model_name,\n temperature=self.temperature,\n max_tokens=self.max_tokens,\n top_p=self.top_p,\n )\n response = response.choices[0].message.content\n else:\n message = [{\n \"role\": \"user\",\n \"content\": system_prompt+\"\\n\\n\"+user_prompt,\n }]\n response = self.client.chat.completions.create(\n messages=message,\n model=self.model_name,\n )\n response = response.choices[0].message.content\n\n current_message[\"sender\"] = \"agent\"\n\n match = re.search(r\"\\[.*\\]\", response)\n if match:\n try:\n self.decode_function_list(response)\n current_message[\"recipient\"] = \"execution\"\n current_message[\"message\"] = response\n except Exception as e:\n current_message[\"recipient\"] = \"user\"\n current_message[\"message\"] = response\n else:\n current_message[\"recipient\"] = \"user\"\n current_message[\"message\"] = response\n\n return current_message"}, "1bffa5d90fea486d87283154f4860f56612aaac4": {"t": 1785753277.7218575, "n": 10535, "h": "cc7ede689f3d4ebb", "c": "from openai import OpenAI\nimport os\nimport re \nimport ast\nfrom model_inference.prompt_en import TRAVEL_PROMPT_EN, BASE_PROMPT_EN\nfrom model_inference.prompt_zh import TRAVEL_PROMPT_ZH, BASE_PROMPT_ZH\n\n\nMULTI_TURN_AGENT_PROMPT_SYSTEM_ZH = \"\"\"你是一个AI系统,你的角色为system,请根据给定的API说明和对话历史1..t,为角色system生成在步骤t+1中生成相应的内容。\n1 如果上一步提供的信息完整,能够正常进行api的调用,你应该调用的API请求,API请求以[ApiName(key1='value1', key2='value2', ...)]的格式输出,不要在输出中输出任何其他解释或提示或API调用的结果。\n如果API参数描述中没有特殊说明,则该参数为非必选参数(用户输入中提及的参数需要包含在输出中,如果未提及,则不需要包含在输出中)。\\n如果API参数描述未指定取值格式要求,则该参数取值使用用户原文。\n2 如果你得到的信息不完整,需要向user提问,以获得完整的信息。你不能扮演user去回答一些文职的问题,应该及时像user询问。\n\n请注意,如果需要进行api调用,请严格遵守调用规则[ApiName(key1='value1', key2='value2', ...)],此时不得输出其他文本内容。\n\n角色说明:\nuser: 用户 \nagent: 进行API请求调用的AI系统角色 \nexecution: 执行api调用并返回结果\n\n你需要遵循的规则如下:\\n\n\"\"\"\n\nMULTI_TURN_AGENT_PROMPT_USER_ZH = \"\"\"下面是你可使用的api列表:\\n {functions}\\n\\n对话历史1..t:\\n{history}\"\"\"\n\nMULTI_TURN_AGENT_PROMPT_SYSTEM_EN = \"\"\"You are an AI system with the role name \"system.\" Based on the provided API specifications and conversation history from steps 1 to t, generate the appropriate content for step t+1 for the \"system\" role.\n1. If the information provided in the previous step is complete and the API call can be executed normally, you should generate the API request. The API request should be output in the format [ApiName(key1='value1', key2='value2', ...)]. Do not include any other explanations, prompts, or API call results in the output.\n - If the API parameter description does not specify otherwise, the parameter is optional (parameters mentioned in the user input need to be included in the output; if not mentioned, they do not need to be included).\n - If the API parameter description does not specify the required format for the value, use the user's original text for the parameter value.\n2. If the information you received is incomplete, you need to ask the user for more information to obtain the complete details. You should not pretend to be the user to answer some clerical questions; instead, promptly ask the user for clarification.\n\nPlease note that if an API call is required, strictly adhere to the call format rules [ApiName(key1='value1', key2='value2', ...)] and do not output any other text content.\n\nRole Descriptions:\nuser: User\nagent: The AI system role that makes API requests\nexecution: Executes the API call and returns the result\n\nThe rules you need to follow are as follows:\\n\n\"\"\"\n\nMULTI_TURN_AGENT_PROMPT_USER_EN = \"\"\"Below is the list of APIs you can use:\\n {functions}\\n\\nConversation history 1..t:\\n{history}\"\"\"\n\nclass APIAgent_turn():\n\n def __init__(self, model_name, time, functions, involved_class, temperature=0.001, top_p=1, max_tokens=1000, language=\"zh\") -> None:\n self.model_name = model_name.lower()\n \n if \"gpt\" in self.model_name:\n api_key = os.getenv(\"GPT_AGENT_API_KEY\")\n base_url = os.getenv(\"GPT_AGENT_BASE_URL\")\n elif \"deepseek\" in self.model_name:\n api_key = os.getenv(\"DEEPSEEK_API_KEY\")\n base_url = os.getenv(\"DEEPSEEK_BASE_URL\")\n elif \"qwen\" in self.model_name:\n api_key = os.getenv(\"QWEN_API_KEY\")\n base_url = os.getenv(\"QWEN_BASE_URL\")\n elif \"kimi\" in self.model_name:\n api_key = os.getenv(\"KIMI_API_KEY\")\n base_url = os.getenv(\"KIMI_BASE_URL\")\n else:\n raise ValueError(f\"Unknown model name: {self.model_name}\")\n\n self.client = OpenAI(base_url=base_url, api_key=api_key)\n self.temperature = temperature\n self.top_p = top_p\n self.max_tokens = max_tokens\n self.time = time\n self.functions = functions\n self.involved_class = involved_class\n self.language = language\n self.model_name = model_name\n\n def decode_function_list(self, result):\n func = result\n if \" \" == func[0]:\n func = func[1:]\n if not func.startswith(\"[\"):\n func = \"[\" + func\n if not func.endswith(\"]\"):\n func = func + \"]\"\n decoded_output = self.ast_parse(func)\n return self.decoded_output_to_execution_list(decoded_output)\n\n def ast_parse(self, input_str, language=\"Python\"):\n if language == \"Python\":\n cleaned_input = input_str.strip(\"[]'\") \n parsed = ast.parse(cleaned_input, mode=\"eval\")\n extracted = []\n \n \n if isinstance(parsed.body, ast.Call):\n extracted.append(self.resolve_ast_call(parsed.body))\n elif isinstance(parsed.body, (ast.Tuple, ast.List)): \n for elem in parsed.body.elts:\n if isinstance(elem, ast.Call):\n extracted.append(self.resolve_ast_call(elem))\n else:\n return False\n return extracted\n \n def resolve_ast_call(self,elem):\n # Handle nested attributes for deeply nested module paths\n func_parts = []\n func_part = elem.func\n while isinstance(func_part, ast.Attribute):\n func_parts.append(func_part.attr)\n func_part = func_part.value\n if isinstance(func_part, ast.Name):\n func_parts.append(func_part.id)\n func_name = \".\".join(reversed(func_parts))\n args_dict = {}\n for arg in elem.keywords:\n output = self.resolve_ast_by_type(arg.value)\n args_dict[arg.arg] = output\n return {func_name: args_dict}\n\n\n def resolve_ast_by_type(self,value):\n if isinstance(value, ast.Constant):\n if value.value is Ellipsis:\n output = \"...\"\n else:\n output = value.value\n elif isinstance(value, ast.UnaryOp):\n output = -value.operand.value\n elif isinstance(value, ast.List):\n output = [self.resolve_ast_by_type(v) for v in value.elts]\n elif isinstance(value, ast.Dict):\n output = {\n self.resolve_ast_by_type(k): self.resolve_ast_by_type(v)\n for k, v in zip(value.keys, value.values)\n }\n elif isinstance(\n value, ast.NameConstant\n ): # Added this condition to handle boolean values\n output = value.value\n elif isinstance(\n value, ast.BinOp\n ): # Added this condition to handle function calls as arguments\n output = eval(ast.unparse(value))\n elif isinstance(value, ast.Name):\n output = value.id\n elif isinstance(value, ast.Call):\n if len(value.keywords) == 0:\n output = ast.unparse(value)\n else:\n output = self.resolve_ast_call(value)\n elif isinstance(value, ast.Tuple):\n output = tuple(self.resolve_ast_by_type(v) for v in value.elts)\n elif isinstance(value, ast.Lambda):\n output = eval(ast.unparse(value.body[0].value))\n elif isinstance(value, ast.Ellipsis):\n output = \"...\"\n elif isinstance(value, ast.Subscript):\n try:\n output = ast.unparse(value.body[0].value)\n except:\n output = ast.unparse(value.value) + \"[\" + ast.unparse(value.slice) + \"]\"\n else:\n raise Exception(f\"Unsupported AST type: {type(value)}\")\n return output\n \n \n def decoded_output_to_execution_list(self,decoded_output):\n\n execution_list = []\n for function_call in decoded_output:\n for key, value in function_call.items():\n args_str = \", \".join(\n f\"{k}={self.parse_nested_value(v)}\" for k, v in value.items()\n )\n execution_list.append(f\"{key}({args_str})\")\n return execution_list\n \n def parse_nested_value(self,value):\n\n if isinstance(value, dict):\n func_name = list(value.keys())[0]\n args = value[func_name]\n args_str = \", \".join(f\"{k}={self.parse_nested_value(v)}\" for k, v in args.items())\n return f\"{func_name}({args_str})\"\n return repr(value)\n\n\n\n def respond(self, history) -> None:\n\n current_message = {}\n\n if self.language == \"zh\":\n system_prompt = MULTI_TURN_AGENT_PROMPT_SYSTEM_ZH\n user_prompt = MULTI_TURN_AGENT_PROMPT_USER_ZH.format(functions = self.functions, history = history)\n if \"Travel\" in self.involved_class:\n system_prompt += TRAVEL_PROMPT_ZH\n if \"BaseApi\" in self.involved_class:\n system_prompt += BASE_PROMPT_ZH\n elif self.language == \"en\":\n system_prompt = MULTI_TURN_AGENT_PROMPT_SYSTEM_EN\n user_prompt = MULTI_TURN_AGENT_PROMPT_USER_EN.format(functions = self.functions, history = history)\n if \"Travel\" in self.involved_class:\n system_prompt += TRAVEL_PROMPT_EN\n if \"BaseApi\" in self.involved_class:\n system_prompt += BASE_PROMPT_EN\n\n if \"o1\" not in self.model_name:\n message = [\n {\n \"role\": \"system\",\n \"content\": system_prompt,\n },\n {\n \"role\": \"user\",\n \"content\": user_prompt,\n },\n ]\n\n response = self.client.chat.completions.create(\n messages=message,\n model=self.model_name,\n temperature=self.temperature,\n max_tokens=self.max_tokens,\n top_p=self.top_p,\n )\n response = response.choices[0].message.content\n else:\n message = [{\n \"role\": \"user\",\n \"content\": system_prompt+\"\\n\\n\"+user_prompt,\n }]\n response = self.client.chat.completions.create(\n messages=message,\n model=self.model_name,\n )\n response = response.choices[0].message.content\n\n current_message[\"sender\"] = \"agent\"\n\n match = re.search(r\"\\[.*\\]\", response)\n if match:\n try:\n self.decode_function_list(response)\n current_message[\"recipient\"] = \"execution\"\n current_message[\"message\"] = response\n except Exception as e:\n current_message[\"recipient\"] = \"user\"\n current_message[\"message\"] = response\n else:\n current_message[\"recipient\"] = \"user\"\n current_message[\"message\"] = response\n\n return current_message"}, "c9391d09d1ae111e7d9a3d3c5cb30da2751ffa6d": {"t": 1785753279.292285, "n": 10391, "h": "29202174608ee87a", "c": "from openai import OpenAI\nimport os\nimport re \nimport ast\nfrom model_inference.prompt_en import TRAVEL_PROMPT_EN, BASE_PROMPT_EN\nfrom model_inference.prompt_zh import TRAVEL_PROMPT_ZH, BASE_PROMPT_ZH\n\n\nMULTI_TURN_AGENT_PROMPT_SYSTEM_ZH = \"\"\"你是一个AI系统,你的角色为system,请根据给定的API说明和对话历史1..t,为角色system生成在步骤t+1中生成相应的内容。\n1 如果上一步提供的信息完整,能够正常进行api的调用,你应该调用的API请求,API请求以[ApiName(key1='value1', key2='value2', ...)]的格式输出,不要在输出中输出任何其他解释或提示或API调用的结果。\n如果API参数描述中没有特殊说明,则该参数为非必选参数(用户输入中提及的参数需要包含在输出中,如果未提及,则不需要包含在输出中)。\\n如果API参数描述未指定取值格式要求,则该参数取值使用用户原文。\n2 如果你得到的信息不完整,需要向user提问,以获得完整的信息。你不能扮演user去回答一些文职的问题,应该及时像user询问。\n\n请注意,如果需要进行api调用,请严格遵守调用规则[ApiName(key1='value1', key2='value2', ...)],此时不得输出其他文本内容。\n\n角色说明:\nuser: 用户 \nagent: 进行API请求调用的AI系统角色 \nexecution: 执行api调用并返回结果\n\n你需要遵循的规则如下:\\n\n\"\"\"\n\nMULTI_TURN_AGENT_PROMPT_USER_ZH = \"\"\"下面是你可使用的api列表:\\n {functions}\\n\\n对话历史1..t:\\n{history}\"\"\"\n\nMULTI_TURN_AGENT_PROMPT_SYSTEM_EN = \"\"\"You are an AI system with the role name \"system.\" Based on the provided API specifications and conversation history from steps 1 to t, generate the appropriate content for step t+1 for the \"system\" role.\n1. If the information provided in the previous step is complete and the API call can be executed normally, you should generate the API request. The API request should be output in the format [ApiName(key1='value1', key2='value2', ...)]. Do not include any other explanations, prompts, or API call results in the output.\n - If the API parameter description does not specify otherwise, the parameter is optional (parameters mentioned in the user input need to be included in the output; if not mentioned, they do not need to be included).\n - If the API parameter description does not specify the required format for the value, use the user's original text for the parameter value.\n2. If the information you received is incomplete, you need to ask the user for more information to obtain the complete details. You should not pretend to be the user to answer some clerical questions; instead, promptly ask the user for clarification.\n\nPlease note that if an API call is required, strictly adhere to the call format rules [ApiName(key1='value1', key2='value2', ...)] and do not output any other text content.\n\nRole Descriptions:\nuser: User\nagent: The AI system role that makes API requests\nexecution: Executes the API call and returns the result\n\nThe rules you need to follow are as follows:\\n\n\"\"\"\n\nMULTI_TURN_AGENT_PROMPT_USER_EN = \"\"\"Below is the list of APIs you can use:\\n {functions}\\n\\nConversation history 1..t:\\n{history}\"\"\"\n\nclass APIAgent_turn():\n\n def __init__(self, model_name, time, functions, involved_class, temperature=0.001, top_p=1, max_tokens=1000, language=\"zh\") -> None:\n self.model_name = model_name \n \n if \"gpt\" in self.model_name:\n api_key = os.getenv(\"GPT_AGENT_API_KEY\")\n base_url = os.getenv(\"GPT_AGENT_BASE_URL\")\n elif \"deepseek\" in self.model_name:\n api_key = os.getenv(\"DEEPSEEK_API_KEY\")\n base_url = os.getenv(\"DEEPSEEK_BASE_URL\")\n elif \"qwen\" in self.model_name:\n api_key = os.getenv(\"QWEN_API_KEY\")\n base_url = os.getenv(\"QWEN_BASE_URL\")\n else:\n raise ValueError(f\"Unknown model name: {self.model_name}\")\n\n self.client = OpenAI(base_url=base_url, api_key=api_key)\n self.temperature = temperature\n self.top_p = top_p\n self.max_tokens = max_tokens\n self.time = time\n self.functions = functions\n self.involved_class = involved_class\n self.language = language\n self.model_name = model_name\n\n def decode_function_list(self, result):\n func = result\n if \" \" == func[0]:\n func = func[1:]\n if not func.startswith(\"[\"):\n func = \"[\" + func\n if not func.endswith(\"]\"):\n func = func + \"]\"\n decoded_output = self.ast_parse(func)\n return self.decoded_output_to_execution_list(decoded_output)\n\n def ast_parse(self, input_str, language=\"Python\"):\n if language == \"Python\":\n cleaned_input = input_str.strip(\"[]'\") \n parsed = ast.parse(cleaned_input, mode=\"eval\")\n extracted = []\n \n \n if isinstance(parsed.body, ast.Call):\n extracted.append(self.resolve_ast_call(parsed.body))\n elif isinstance(parsed.body, (ast.Tuple, ast.List)): \n for elem in parsed.body.elts:\n if isinstance(elem, ast.Call):\n extracted.append(self.resolve_ast_call(elem))\n else:\n return False\n return extracted\n \n def resolve_ast_call(self,elem):\n # Handle nested attributes for deeply nested module paths\n func_parts = []\n func_part = elem.func\n while isinstance(func_part, ast.Attribute):\n func_parts.append(func_part.attr)\n func_part = func_part.value\n if isinstance(func_part, ast.Name):\n func_parts.append(func_part.id)\n func_name = \".\".join(reversed(func_parts))\n args_dict = {}\n for arg in elem.keywords:\n output = self.resolve_ast_by_type(arg.value)\n args_dict[arg.arg] = output\n return {func_name: args_dict}\n\n\n def resolve_ast_by_type(self,value):\n if isinstance(value, ast.Constant):\n if value.value is Ellipsis:\n output = \"...\"\n else:\n output = value.value\n elif isinstance(value, ast.UnaryOp):\n output = -value.operand.value\n elif isinstance(value, ast.List):\n output = [self.resolve_ast_by_type(v) for v in value.elts]\n elif isinstance(value, ast.Dict):\n output = {\n self.resolve_ast_by_type(k): self.resolve_ast_by_type(v)\n for k, v in zip(value.keys, value.values)\n }\n elif isinstance(\n value, ast.NameConstant\n ): # Added this condition to handle boolean values\n output = value.value\n elif isinstance(\n value, ast.BinOp\n ): # Added this condition to handle function calls as arguments\n output = eval(ast.unparse(value))\n elif isinstance(value, ast.Name):\n output = value.id\n elif isinstance(value, ast.Call):\n if len(value.keywords) == 0:\n output = ast.unparse(value)\n else:\n output = self.resolve_ast_call(value)\n elif isinstance(value, ast.Tuple):\n output = tuple(self.resolve_ast_by_type(v) for v in value.elts)\n elif isinstance(value, ast.Lambda):\n output = eval(ast.unparse(value.body[0].value))\n elif isinstance(value, ast.Ellipsis):\n output = \"...\"\n elif isinstance(value, ast.Subscript):\n try:\n output = ast.unparse(value.body[0].value)\n except:\n output = ast.unparse(value.value) + \"[\" + ast.unparse(value.slice) + \"]\"\n else:\n raise Exception(f\"Unsupported AST type: {type(value)}\")\n return output\n \n \n def decoded_output_to_execution_list(self,decoded_output):\n\n execution_list = []\n for function_call in decoded_output:\n for key, value in function_call.items():\n args_str = \", \".join(\n f\"{k}={self.parse_nested_value(v)}\" for k, v in value.items()\n )\n execution_list.append(f\"{key}({args_str})\")\n return execution_list\n \n def parse_nested_value(self,value):\n\n if isinstance(value, dict):\n func_name = list(value.keys())[0]\n args = value[func_name]\n args_str = \", \".join(f\"{k}={self.parse_nested_value(v)}\" for k, v in args.items())\n return f\"{func_name}({args_str})\"\n return repr(value)\n\n\n\n def respond(self, history) -> None:\n\n current_message = {}\n\n if self.language == \"zh\":\n system_prompt = MULTI_TURN_AGENT_PROMPT_SYSTEM_ZH\n user_prompt = MULTI_TURN_AGENT_PROMPT_USER_ZH.format(functions = self.functions, history = history)\n if \"Travel\" in self.involved_class:\n system_prompt += TRAVEL_PROMPT_ZH\n if \"BaseApi\" in self.involved_class:\n system_prompt += BASE_PROMPT_ZH\n elif self.language == \"en\":\n system_prompt = MULTI_TURN_AGENT_PROMPT_SYSTEM_EN\n user_prompt = MULTI_TURN_AGENT_PROMPT_USER_EN.format(functions = self.functions, history = history)\n if \"Travel\" in self.involved_class:\n system_prompt += TRAVEL_PROMPT_EN\n if \"BaseApi\" in self.involved_class:\n system_prompt += BASE_PROMPT_EN\n\n if \"o1\" not in self.model_name:\n message = [\n {\n \"role\": \"system\",\n \"content\": system_prompt,\n },\n {\n \"role\": \"user\",\n \"content\": user_prompt,\n },\n ]\n\n response = self.client.chat.completions.create(\n messages=message,\n model=self.model_name,\n temperature=self.temperature,\n max_tokens=self.max_tokens,\n top_p=self.top_p,\n )\n response = response.choices[0].message.content\n else:\n message = [{\n \"role\": \"user\",\n \"content\": system_prompt+\"\\n\\n\"+user_prompt,\n }]\n response = self.client.chat.completions.create(\n messages=message,\n model=self.model_name,\n )\n response = response.choices[0].message.content\n\n current_message[\"sender\"] = \"agent\"\n\n match = re.search(r\"\\[.*\\]\", response)\n if match:\n try:\n self.decode_function_list(response)\n current_message[\"recipient\"] = \"execution\"\n current_message[\"message\"] = response\n except Exception as e:\n current_message[\"recipient\"] = \"user\"\n current_message[\"message\"] = response\n else:\n current_message[\"recipient\"] = \"user\"\n current_message[\"message\"] = response\n\n return current_message"}}}}