Update service_chat.py
Browse files114 unmodified lines11511611711811811912012112212212312412547 unmodified lines1731741751761771761771781795 unmodified lines18518618718918818919119019119219319419519619719819920020119820220320420520620720420520820921021121242 unmodified lines25525625725425525625725825926026126226326426526626726825825927126026126226324 unmodified lines2882892902912922933043053063073083093102942952962972982993003013023033042 unmodified lines307308309319310311312313114 unmodified lines
def get_tool_metadata(func_name: str, args: dict): if func_name == "tavily_search": q = args.get("query", "Search the web") q = args.get("query", "the web") depth = args.get("search_depth", "basic") if depth == "advanced": return "search", f"Deep Research: {q}" return "search", f"Searched for {q}" return "search", f"Searching: {q}"
endpoint = args.get("endpoint", "") method = args.get("method", "GET")47 unmodified lines images = 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) 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 = 8 max_turns = 4 tool_step_counter = 0 has_emitted_search_step = False search_calls_count = 0 max_search_budget = 2
try: 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.7, "temperature": 0.6, "top_p": 0.95, "max_tokens": 16384, "stream": True }
if available_tools: payload["tools"] = available_tools if active_tools: payload["tools"] = active_tools payload["tool_choice"] = "auto"
tool_calls_acc = []42 unmodified lines
valid_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 += 1 has_emitted_search_step = True search_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}" tc["id"] = f"call_{i+1}_{tool_step_counter}"
curr_messages.append({ "role": "assistant",24 unmodified lines yield json.dumps(step_add_payload) + "\n"
if fn_name == "tavily_search": search_calls_count += 1 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 8 try: 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) 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 lines step_update_payload = { "tool_event": "step_result", "step_index": tool_step_counter, "response": tool_res "response": str(tool_res) } yield json.dumps(step_update_payload) + "\n"
service_chat.py+24-33