refac
This commit is contained in:
@@ -107,6 +107,7 @@ from open_webui.utils.filter import (
|
|||||||
)
|
)
|
||||||
from open_webui.utils.code_interpreter import execute_code_jupyter
|
from open_webui.utils.code_interpreter import execute_code_jupyter
|
||||||
from open_webui.utils.payload import apply_system_prompt_to_body
|
from open_webui.utils.payload import apply_system_prompt_to_body
|
||||||
|
from open_webui.utils.response import normalize_usage
|
||||||
from open_webui.utils.mcp.client import MCPClient
|
from open_webui.utils.mcp.client import MCPClient
|
||||||
|
|
||||||
|
|
||||||
@@ -3121,10 +3122,11 @@ async def process_chat_response(
|
|||||||
else:
|
else:
|
||||||
choices = data.get("choices", [])
|
choices = data.get("choices", [])
|
||||||
|
|
||||||
# 17421
|
# 17421 - Normalize usage data to standard format
|
||||||
usage = data.get("usage", {}) or {}
|
usage = data.get("usage", {}) or {}
|
||||||
usage.update(data.get("timings", {})) # llama.cpp
|
usage.update(data.get("timings", {})) # llama.cpp
|
||||||
if usage:
|
if usage:
|
||||||
|
usage = normalize_usage(usage)
|
||||||
await event_emitter(
|
await event_emitter(
|
||||||
{
|
{
|
||||||
"type": "chat:completion",
|
"type": "chat:completion",
|
||||||
|
|||||||
@@ -6,6 +6,47 @@ from open_webui.utils.misc import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def normalize_usage(usage: dict) -> dict:
|
||||||
|
"""
|
||||||
|
Normalize usage statistics to standard format.
|
||||||
|
Handles OpenAI, Ollama, and llama.cpp formats.
|
||||||
|
|
||||||
|
Adds standardized token fields to the original data:
|
||||||
|
- input_tokens: Number of tokens in the prompt
|
||||||
|
- output_tokens: Number of tokens generated
|
||||||
|
- total_tokens: Sum of input and output tokens
|
||||||
|
"""
|
||||||
|
if not usage:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
# Map various field names to standard names
|
||||||
|
input_tokens = (
|
||||||
|
usage.get("input_tokens") # Already standard
|
||||||
|
or usage.get("prompt_tokens") # OpenAI
|
||||||
|
or usage.get("prompt_eval_count") # Ollama
|
||||||
|
or usage.get("prompt_n") # llama.cpp
|
||||||
|
or 0
|
||||||
|
)
|
||||||
|
|
||||||
|
output_tokens = (
|
||||||
|
usage.get("output_tokens") # Already standard
|
||||||
|
or usage.get("completion_tokens") # OpenAI
|
||||||
|
or usage.get("eval_count") # Ollama
|
||||||
|
or usage.get("predicted_n") # llama.cpp
|
||||||
|
or 0
|
||||||
|
)
|
||||||
|
|
||||||
|
total_tokens = usage.get("total_tokens") or (input_tokens + output_tokens)
|
||||||
|
|
||||||
|
# Add standardized fields to original data
|
||||||
|
result = dict(usage)
|
||||||
|
result["input_tokens"] = int(input_tokens)
|
||||||
|
result["output_tokens"] = int(output_tokens)
|
||||||
|
result["total_tokens"] = int(total_tokens)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
def convert_ollama_tool_call_to_openai(tool_calls: list) -> list:
|
def convert_ollama_tool_call_to_openai(tool_calls: list) -> list:
|
||||||
openai_tool_calls = []
|
openai_tool_calls = []
|
||||||
for tool_call in tool_calls:
|
for tool_call in tool_calls:
|
||||||
|
|||||||
Reference in New Issue
Block a user