MCPcopy
hub / github.com/microsoft/JARVIS / inference

Function inference

taskbench/inference.py:173–214  ·  view source on GitHub ↗
(input, url, header, temperature, top_p, tool_string, wf, llm, demos, reformat, reformat_by, dependency_type, log_detail = False)

Source from the content-addressed store, hash-verified

171 loop.close()
172
173async def inference(input, url, header, temperature, top_p, tool_string, wf, llm, demos, reformat, reformat_by, dependency_type, log_detail = False):
174 user_request = input["user_request"]
175 if dependency_type == "resource":
176 prompt = """\n# GOAL #: Based on the above tools, I want you generate task steps and task nodes to solve the # USER REQUEST #. The format must in a strict JSON format, like: {"task_steps": [ step description of one or more steps ], "task_nodes": [{"task": "tool name must be from # TOOL LIST #", "arguments": [ a concise list of arguments for the tool. Either original text, or user-mentioned filename, or tag '<node-j>' (start from 0) to refer to the output of the j-th node. ]}]} """
177 prompt += """\n\n# REQUIREMENTS #: \n1. the generated task steps and task nodes can resolve the given user request # USER REQUEST # perfectly. Task name must be selected from # TASK LIST #; \n2. the task steps should strictly aligned with the task nodes, and the number of task steps should be same with the task nodes; \n3. the dependencies among task steps should align with the argument dependencies of the task nodes; \n4. the tool arguments should be align with the input-type field of # TASK LIST #;"""
178 else:
179 prompt = """\n# GOAL #:\nBased on the above tools, I want you generate task steps and task nodes to solve the # USER REQUEST #. The format must in a strict JSON format, like: {"task_steps": [ "concrete steps, format as Step x: Call xxx tool with xxx: 'xxx' and xxx: 'xxx'" ], "task_nodes": [{"task": "task name must be from # TASK LIST #", "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"} ]}], "task_links": [{"source": "task name i", "target": "task name j"}]}"""
180 prompt += """\n\n# REQUIREMENTS #: \n1. the generated task steps and task nodes can resolve the given user request # USER REQUEST # perfectly. Task name must be selected from # TASK LIST #; \n2. the task steps should strictly aligned with the task nodes, and the number of task steps should be same with the task nodes; \n3. The task links (task_links) should reflect the temporal dependencies among task nodes, i.e. the order in which the APIs are invoked;"""
181
182 if len(demos) > 0:
183 prompt += "\n"
184 for demo in demos:
185 prompt += f"""\n# EXAMPLE #:\n# USER REQUEST #: {demo["user_request"]}\n# RESULT #: {json.dumps(demo["result"])}"""
186
187 prompt += """\n\n# USER REQUEST #: {{user_request}}\nnow please generate your result in a strict JSON format:\n# RESULT #:"""
188
189 final_prompt = tool_string + prompt.replace("{{user_request}}", user_request)
190 payload = json.dumps({
191 "model": f"{llm}",
192 "messages": [
193 {
194 "role": "user",
195 "content": final_prompt
196 }
197 ],
198 "temperature": temperature,
199 "top_p": top_p,
200 "frequency_penalty": 0,
201 "presence_penalty": 1.05,
202 "max_tokens": 2000,
203 "stream": False,
204 "stop": None
205 })
206 try:
207 result = await get_response(url, header, payload, input['id'], reformat, reformat_by, dependency_type, log_detail)
208 except Exception as e:
209 logger.info(f"Failed #id {input['id']}: {type(e)} {e}")
210 raise e
211 logger.info(f"Success #id {input['id']}")
212 input["result"] = result
213 wf.write(json.dumps(input) + "\n")
214 wf.flush()
215
216async def get_response(url, header, payload, id, reformat, reformat_by, dependency_type, log_detail=False):
217 async with aiohttp.ClientSession() as session:

Callers 2

inference_wrapperFunction · 0.85

Calls 1

get_responseFunction · 0.85

Tested by

no test coverage detected