Spaces:
Running
Running
dboa9 commited on
Commit ·
cfec061
1
Parent(s): c640df3
Fix usage calc for array content; normalize message content to text only
Browse files
app.py
CHANGED
|
@@ -480,14 +480,13 @@ MODEL_ROUTING = {
|
|
| 480 |
|
| 481 |
|
| 482 |
def _message_content_to_str(content: Union[str, List[Dict[str, Any]]]) -> str:
|
| 483 |
-
"""Normalize message content to string (OpenAI can send string or array of parts)."""
|
| 484 |
if isinstance(content, str):
|
| 485 |
return content
|
| 486 |
if isinstance(content, list):
|
| 487 |
return " ".join(
|
| 488 |
-
p.get("text", "")
|
| 489 |
-
|
| 490 |
-
if isinstance(p, dict)
|
| 491 |
)
|
| 492 |
return str(content)
|
| 493 |
|
|
@@ -667,9 +666,9 @@ async def chat_completions(
|
|
| 667 |
"finish_reason": "stop"
|
| 668 |
}],
|
| 669 |
"usage": {
|
| 670 |
-
"prompt_tokens": len(" ".join(m.content for m in request.messages).split()),
|
| 671 |
"completion_tokens": len(response_text.split()),
|
| 672 |
-
"total_tokens": len(" ".join(m.content for m in request.messages).split()) + len(response_text.split()),
|
| 673 |
}
|
| 674 |
}
|
| 675 |
|
|
|
|
| 480 |
|
| 481 |
|
| 482 |
def _message_content_to_str(content: Union[str, List[Dict[str, Any]]]) -> str:
|
| 483 |
+
"""Normalize message content to string (OpenAI can send string or array of parts, e.g. vision/R1)."""
|
| 484 |
if isinstance(content, str):
|
| 485 |
return content
|
| 486 |
if isinstance(content, list):
|
| 487 |
return " ".join(
|
| 488 |
+
p.get("text", "") for p in content
|
| 489 |
+
if isinstance(p, dict) and isinstance(p.get("text"), str)
|
|
|
|
| 490 |
)
|
| 491 |
return str(content)
|
| 492 |
|
|
|
|
| 666 |
"finish_reason": "stop"
|
| 667 |
}],
|
| 668 |
"usage": {
|
| 669 |
+
"prompt_tokens": len(" ".join(_message_content_to_str(m.content) for m in request.messages).split()),
|
| 670 |
"completion_tokens": len(response_text.split()),
|
| 671 |
+
"total_tokens": len(" ".join(_message_content_to_str(m.content) for m in request.messages).split()) + len(response_text.split()),
|
| 672 |
}
|
| 673 |
}
|
| 674 |
|