refac: tools handling

This commit is contained in:
Timothy Jaeryang Baek 2025-07-07 11:42:52 +04:00
parent 05a9b72670
commit cfcfefb20c

View file

@ -249,9 +249,6 @@ async def chat_completion_tools_handler(
else f"{tool_function_name}"
)
if tool.get("metadata", {}).get("citation", False) or tool.get(
"direct", False
):
# Citation is enabled for this tool
sources.append(
{
@ -265,9 +262,9 @@ async def chat_completion_tools_handler(
"parameters": tool_function_params,
}
],
"tool_result": True,
}
)
else:
# Citation is not enabled for this tool
body["messages"] = add_or_update_user_message(
f"\nTool `{tool_name}` Output: {tool_result}",
@ -931,7 +928,9 @@ async def process_chat_payload(request, form_data, user, metadata, model):
citation_idx_map = {}
for source in sources:
if "document" in source:
is_tool_result = source.get("tool_result", False)
if "document" in source and not is_tool_result:
for document_text, document_metadata in zip(
source["document"], source["metadata"]
):
@ -952,18 +951,17 @@ async def process_chat_payload(request, form_data, user, metadata, model):
)
context_string = context_string.strip()
prompt = get_last_user_message(form_data["messages"])
prompt = get_last_user_message(form_data["messages"])
if prompt is None:
raise Exception("No user message found")
if (
request.app.state.config.RELEVANCE_THRESHOLD == 0
and context_string.strip() == ""
):
if context_string == "":
if request.app.state.config.RELEVANCE_THRESHOLD == 0:
log.debug(
f"With a 0 relevancy threshold for RAG, the context cannot be empty"
)
else:
# Workaround for Ollama 2.0+ system prompt issue
# TODO: replace with add_or_update_system_message
if model.get("owned_by") == "ollama":