MCPcopy
hub / github.com/HKUDS/AI-Researcher / run

Method run

research_agent/inno/core.py:267–337  ·  view source on GitHub ↗
(
        self,
        agent: Agent,
        messages: List,
        context_variables: dict = {},
        model_override: str = None,
        stream: bool = False,
        debug: bool = True,
        max_turns: int = float("inf"),
        execute_tools: bool = True,
    )

Source from the content-addressed store, hash-verified

265 return partial_response
266
267 def run(
268 self,
269 agent: Agent,
270 messages: List,
271 context_variables: dict = {},
272 model_override: str = None,
273 stream: bool = False,
274 debug: bool = True,
275 max_turns: int = float("inf"),
276 execute_tools: bool = True,
277 ) -> Response:
278 if stream:
279 return self.run_and_stream(
280 agent=agent,
281 messages=messages,
282 context_variables=context_variables,
283 model_override=model_override,
284 debug=debug,
285 max_turns=max_turns,
286 execute_tools=execute_tools,
287 )
288 active_agent = agent
289 context_variables = copy.deepcopy(context_variables)
290 history = copy.deepcopy(messages)
291 init_len = len(messages)
292
293 self.logger.info("Receiveing the task:", history[-1]['content'], title="Receive Task", color="green")
294
295 while len(history) - init_len < max_turns and active_agent:
296
297 # get completion with current history, agent
298 completion = self.get_chat_completion(
299 agent=active_agent,
300 history=history,
301 context_variables=context_variables,
302 model_override=model_override,
303 stream=stream,
304 debug=debug,
305 )
306 message: Message = completion.choices[0].message
307 message.sender = active_agent.name
308 # debug_print(debug, "Received completion:", message.model_dump_json(indent=4), log_path=log_path, title="Received Completion", color="blue")
309 self.logger.pretty_print_messages(message)
310 history.append(
311 json.loads(message.model_dump_json())
312 ) # to avoid OpenAI types (?)
313
314 if not message.tool_calls or not execute_tools:
315 self.logger.info("Ending turn.", title="End Turn", color="red")
316 break
317 # if (message.tool_calls and message.tool_calls[0].function.name == "case_resolved") or not execute_tools:
318 # debug_print(debug, "Ending turn.", log_path=log_path, title="End Turn", color="red")
319 # break
320
321 # handle function calls, updating context_variables, and switching agents
322 if message.tool_calls:
323 partial_response = self.handle_tool_calls(
324 message.tool_calls, active_agent.functions, context_variables, debug, handle_mm_func=active_agent.handle_mm_func

Callers 15

agentFunction · 0.95
run_demo_loopFunction · 0.95
main_ai_researcherFunction · 0.80
run_infer.pyFile · 0.80
0_crawl_paper.pyFile · 0.80
pdf_to_twitter_videoFunction · 0.80
mainFunction · 0.80
mainFunction · 0.80
init_containerFunction · 0.80
wait_for_container_readyFunction · 0.80
init_containerMethod · 0.80

Calls 5

get_chat_completionMethod · 0.95
handle_tool_callsMethod · 0.95
ResponseClass · 0.85
infoMethod · 0.80
pretty_print_messagesMethod · 0.80

Tested by

no test coverage detected