from transformers import AutoModelForCausalLM, AutoTokenizerimport jsonfrom datetime import datetime model_name = "AIDC-AI/Marco-DeepResearch-8B" tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)model = AutoModelForCausalLM.from_pretrained( model_name, torch_dtype="auto", device_map="auto", trust_remote_code=True,) # 1. Define tools (OpenAI function calling format)tools = [ { "type": "function", "function": { "name": "search", "description": "Search the web via Google to find relevant information and URLs.", "parameters": { "type": "object", "properties": { "querys": { "type": "array", "items": {"type": "string"}, "description": "Search queries for finding relevant information. Supports single or multiple queries." } }, "required": ["querys"] } } }, { "type": "function", "function": { "name": "visit", "description": "Read webpage content to extract specific information, verify claims, or understand context.", "parameters": { "type": "object", "properties": { "urls": { "type": "array", "items": {"type": "string"}, "description": "URL(s) to visit. Supports single or multiple urls." }, "goal": { "type": "string", "description": "The specific information to retrieve. Be precise, not vague." } }, "required": ["urls", "goal"] } } }] # 2. Build system promptROLE_PROMPT = """You are an expert web researcher. Your task is to find accurate, complete answers through iterative search, extraction, and verification. ## Core Principles 1) Strategic Planning - Decompose complex questions into targeted sub-tasks - Choose the right tool for each step - Refine your approach based on what you learn 2) Precise Execution - Define clear objectives before using any tool - Provide sufficient detail for accurate results - Avoid vague or overly broad requests 3) Rigorous Verification - Cross-check important facts across multiple sources - Resolve conflicts by gathering additional evidence - Only conclude when evidence is sufficient and consistent ## Output Format In each turn, you can either call a tool or provide the final answer. **Call a tool:**<think>your reasoning process</think><tool_call>{"name": "tool_name", "arguments": {"param1": "value1", "param2": "value2"}}</tool_call> **Provide final answer (when you have gathered enough information):**<think>your reasoning and analysis</think><answer>the direct answer to the question</answer> Note: All reasoning should be in <think>, <answer> should contain only the final answer.""" current_date = datetime.now().strftime("%Y-%m-%d")tools_json = "\n".join([json.dumps(t, ensure_ascii=False) for t in tools]) system_prompt = f"""{ROLE_PROMPT} Current date: {current_date} # Tools You may call one or more functions to assist with the user query. You are provided with function signatures within <tools></tools> XML tags:<tools>{tools_json}</tools> For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:<tool_call>{{"name": <function-name>, "arguments": <args-json-object>}}</tool_call>""" # 3. Build messages and generatemessages = [ {"role": "system", "content": system_prompt}, {"role": "user", "content": "Who won the 2026 Turing Award?"},] text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)inputs = tokenizer([text], return_tensors="pt").to(model.device)outputs = model.generate(**inputs, max_new_tokens=16384, temperature=0.7, top_p=0.95)response = tokenizer.decode(outputs[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True)print(response)