(url, header, llm, temperature, top_p, check, tool_number, sampler, tools, method, figure_dir, wf, dependency_type)
| 231 | super().__init__(message) |
| 232 | |
| 233 | async def sample(url, header, llm, temperature, top_p, check, tool_number, sampler, tools, method, figure_dir, wf, dependency_type): |
| 234 | start_time = datetime.now() |
| 235 | sample_id = str(uuid.uuid4().int)[:8] |
| 236 | sub_G = sampler.sample_subgraph(tool_number, sample_method=method) |
| 237 | |
| 238 | tool_list = list(sub_G.nodes) |
| 239 | tool_edge = list(sub_G.edges) |
| 240 | seed = random.randint(0, 1000000) |
| 241 | sampled_tools_string = "Given a tool graph with tools as nodes, and invoking chains between tools as edges. The following tools (nodes) are available with their corresponding descriptions and input/outputs types:\n" |
| 242 | for k, tool in enumerate(tool_list): |
| 243 | sampled_tools_string += f"Node {k+1}:" + json.dumps(tools[tool]) + "\n" |
| 244 | |
| 245 | sampled_links_string = "These tools can be connected as follows (the directed edges are invoking chains among tools):\n" |
| 246 | for k, edge in enumerate(tool_edge): |
| 247 | sampled_links_string += f"Edge: " + edge[0] + " -> " + edge[1] + "\n" |
| 248 | prompt = """\nBased on the above tool graph, please be skillful to generate the according task steps, user request and tool invoking graph. \nRequirements: \n1. the generated user request should be somewhat clear, self-contained (user-specified text, image, video, audio, content should be contained in the request) and practical (help users solve a practical problem); \n2. the task steps must be strictly aligned with the tool graph (nodes and edges) and reasonable, the tool invoking graph must align with task steps, also with the given tool graph; \n3. the user request just can be decomposed into task steps solved by the tool invoking graph; \n4. each task step corresponds to a tool node in the tool graph and tool invoking graph, and the number of task steps must be same with the nodes. Each tool node can only be used once; \n5. if need image/audio/video resources in user request, please use files 'example.[jpg/mp4/wav/png]'; \n6. the dependencies among task steps must align with the edges of tool graph and tool invoking graph; \n7. the number and types of tool parameters in the generated tool invoking graph need to be consistent with the pre-defined input/outputs types of the tools. \nNow please generate your result (with random seed {""" + f"{seed}"+ """}) in a compact JSON format""" |
| 249 | if dependency_type == "resource": |
| 250 | prompt += """{"task_steps": [ step description of one or more steps ], "user_request": "your high-quality and self-contained synthesized request", "invoking_graph": {"nodes": [{"id": "tool name", "input": [ either user-specified text or resource file 'example.[jpg/mp4/wav/png' ] in the above user request, or the dependent tool name whose output is required by this node ]}], "links": [{"source": "tool name i", "target": "tool name j"}]}""" |
| 251 | else: |
| 252 | prompt += """{"task_steps": [ "concrete steps, format as Step x: Call xxx tool with xxx: 'xxx' and xxx: 'xxx'" ], "user_request": "your high-quality, concrete and self-contained synthesized request, with explicit parameter values", "invoking_graph": {"nodes": [{"id": "tool name", "arguments": [ {"name": "parameter name", "value": "parameter value, either user-specified text or the specific name of the tool whose result is required by this node"} ]}], "links": [{"source": "tool name i", "target": "tool name j"}]}""" |
| 253 | if check: |
| 254 | prompt += """, "check_by_teacher": "This field is filled by your strict and well-trained teacher, minor mistakes are complete intolerable to him. He evaluated whether your synthesized user request, tool invoking graph are valid and whether they are aligned with the given tool graph (strictly checked step by step according to the above requirements). Some comments from him place here (start with 'Let me check your result step by step, and evaluate the 'Executable' and 'Correct' of the tool invoking graph (Executable means that the tool invoking graph executed successfully, regardless of alignment with the given tool graph. While Correct implies that the tool invoking graph are not only 'Executable' but also strictly consistent (with strictly same nodes and same edges) with the given tool graph). After carefully evaluating, found some mistakes:' and end with a conclusion: 'Conclusion: Executable: no/yes, Correct: no/yes'.)""" |
| 255 | prompt += "}:" |
| 256 | |
| 257 | final_prompt = sampled_tools_string + sampled_links_string + prompt |
| 258 | |
| 259 | if dependency_type == "temporal": |
| 260 | final_prompt = final_prompt.replace("tool", "API") |
| 261 | |
| 262 | payload = json.dumps({ |
| 263 | "model": f"{llm}", |
| 264 | "messages": [ |
| 265 | { |
| 266 | "role": "user", |
| 267 | "content": final_prompt |
| 268 | } |
| 269 | ], |
| 270 | "temperature": temperature, |
| 271 | "top_p": top_p, |
| 272 | "frequency_penalty": 0, |
| 273 | "presence_penalty": 0, |
| 274 | "max_tokens": 2500, |
| 275 | "stream": False, |
| 276 | "stop": None |
| 277 | }) |
| 278 | try: |
| 279 | async with aiohttp.ClientSession() as session: |
| 280 | async with session.post(url, headers=header, data=payload, timeout=120) as response: |
| 281 | resp = await response.json() |
| 282 | |
| 283 | if response.status == 429: |
| 284 | raise RateLimitError(f"{resp}") |
| 285 | if response.status != 200: |
| 286 | raise Exception(f"{resp}") |
| 287 | |
| 288 | content = resp["choices"][0]["message"]["content"] |
| 289 | content = content.replace("\n", "") |
| 290 | json_start = 0 |
no test coverage detected