Update service_chat.py
Browse files114 unmodified lines11511611711811912012112212312412547 unmodified lines1731741751761771781791805 unmodified lines18618718818919019119219319419519619719819920020120220320420520620720842 unmodified lines25125225325425525625725825926026126226326426526626726826927027127227327424 unmodified lines2993003013023033043053063073083093103113123132 unmodified lines316317318319320321322114 unmodified linesdef get_tool_metadata(func_name: str, args: dict):if func_name == "tavily_search":q = args.get("query", "Search the web")depth = args.get("search_depth", "basic")if depth == "advanced":return "search", f"Deep Research: {q}"return "search", f"Searched for {q}"endpoint = args.get("endpoint", "")method = args.get("method", "GET")47 unmodified linesimages = body.get("images", [])user_id = body.get("user_id", "default_user")connectors = body.get("connectors", [])is_extended = body.get("extended", True)is_web_search = body.get("web_search", False)messages = build_context_messages(req_messages, user_prompt, images)available_tools = get_filtered_tools(connectors, web_search=is_web_search)5 unmodified lines}curr_messages = list(messages)max_turns = 8tool_step_counter = 0has_emitted_search_step = Falsetry:for _ in range(max_turns):payload = {"model": MODEL_NAME,"messages": curr_messages,"temperature": 0.7,"top_p": 0.95,"max_tokens": 16384,"stream": True}if available_tools:payload["tools"] = available_toolspayload["tool_choice"] = "auto"tool_calls_acc = []42 unmodified linesvalid_tool_calls = [tc for tc in tool_calls_acc if tc.get("function", {}).get("name")]if has_tool_call and valid_tool_calls:if not has_emitted_search_step:tool_step_counter += 1has_emitted_search_step = Truesearch_step_payload = {"tool_event": "step_add","step_index": tool_step_counter,"type": "search","title": "Searching available tools","icon": "search","query": user_prompt or "Search tools","request": {},"response": ""}yield json.dumps(search_step_payload) + "\n"for i, tc in enumerate(valid_tool_calls):if not tc.get("id"):tc["id"] = f"call_{i+1}"curr_messages.append({"role": "assistant",24 unmodified linesyield json.dumps(step_add_payload) + "\n"if fn_name == "tavily_search":q = args_dict.get("query", user_prompt or "")depth = args_dict.get("search_depth", "basic")max_r = args_dict.get("max_results")if not max_r:max_r = 30 if depth == "advanced" else 8try:tool_res, sources_list = await execute_tavily_search(q, max_r, depth)except TypeError:tool_res, sources_list = await execute_tavily_search(q, max_r)if sources_list:yield json.dumps({"sources": sources_list}) + "\n"else:2 unmodified linesstep_update_payload = {"tool_event": "step_result","step_index": tool_step_counter,"response": tool_res}yield json.dumps(step_update_payload) + "\n"114 unmodified lines11511611711811912012112212312412547 unmodified lines1731741751761771781795 unmodified lines18518618718818919019119219319419519619719819920020120220320420520620720820921021121242 unmodified lines25525625725825926026126226324 unmodified lines2882892902912922932942952962972982993003013023033042 unmodified lines307308309310311312313114 unmodified linesdef get_tool_metadata(func_name: str, args: dict):if func_name == "tavily_search":q = args.get("query", "the web")depth = args.get("search_depth", "basic")if depth == "advanced":return "search", f"Deep Research: {q}"return "search", f"Searching: {q}"endpoint = args.get("endpoint", "")method = args.get("method", "GET")47 unmodified linesimages = body.get("images", [])user_id = body.get("user_id", "default_user")connectors = body.get("connectors", [])is_web_search = bool(body.get("web_search", False))messages = build_context_messages(req_messages, user_prompt, images)available_tools = get_filtered_tools(connectors, web_search=is_web_search)5 unmodified lines}curr_messages = list(messages)max_turns = 4tool_step_counter = 0search_calls_count = 0max_search_budget = 2try:for _ in range(max_turns):active_tools = list(available_tools)if search_calls_count >= max_search_budget:active_tools = [t for t in active_tools if t.get("function", {}).get("name") != "tavily_search"]payload = {"model": MODEL_NAME,"messages": curr_messages,"temperature": 0.6,"top_p": 0.95,"max_tokens": 16384,"stream": True}if active_tools:payload["tools"] = active_toolspayload["tool_choice"] = "auto"tool_calls_acc = []42 unmodified linesvalid_tool_calls = [tc for tc in tool_calls_acc if tc.get("function", {}).get("name")]if has_tool_call and valid_tool_calls:for i, tc in enumerate(valid_tool_calls):if not tc.get("id"):tc["id"] = f"call_{i+1}_{tool_step_counter}"curr_messages.append({"role": "assistant",24 unmodified linesyield json.dumps(step_add_payload) + "\n"if fn_name == "tavily_search":search_calls_count += 1q = args_dict.get("query", user_prompt or "")depth = args_dict.get("search_depth", "basic")max_r = args_dict.get("max_results", 8)tool_res, sources_list = await execute_tavily_search(query=q,max_results=max_r,search_depth=depth)if sources_list:yield json.dumps({"sources": sources_list}) + "\n"else:2 unmodified linesstep_update_payload = {"tool_event": "step_result","step_index": tool_step_counter,"response": str(tool_res)}yield json.dumps(step_update_payload) + "\n"
service_chat.py+24-33