Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c5fcfc5da9 | ||
|
|
9d8e262304 | ||
|
|
73ed6d7f38 |
@@ -1,7 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
import json
|
|
||||||
import math
|
import math
|
||||||
import re
|
import re
|
||||||
import secrets
|
import secrets
|
||||||
@@ -233,9 +232,10 @@ def extract_responses_text(response: Any) -> str:
|
|||||||
text = getattr(entry, "text", None)
|
text = getattr(entry, "text", None)
|
||||||
if isinstance(text, str):
|
if isinstance(text, str):
|
||||||
parts.append(text)
|
parts.append(text)
|
||||||
text = getattr(item, "text", None)
|
else:
|
||||||
if isinstance(text, str):
|
text = getattr(item, "text", None)
|
||||||
parts.append(text)
|
if isinstance(text, str):
|
||||||
|
parts.append(text)
|
||||||
|
|
||||||
if parts:
|
if parts:
|
||||||
return "\n".join(parts)
|
return "\n".join(parts)
|
||||||
@@ -243,16 +243,6 @@ def extract_responses_text(response: Any) -> str:
|
|||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
def dump_openai_response(response: Any) -> str:
|
|
||||||
if hasattr(response, "model_dump"):
|
|
||||||
return json.dumps(response.model_dump(), indent=2, default=str)
|
|
||||||
if hasattr(response, "to_dict"):
|
|
||||||
return json.dumps(response.to_dict(), indent=2, default=str)
|
|
||||||
if isinstance(response, dict):
|
|
||||||
return json.dumps(response, indent=2, default=str)
|
|
||||||
return str(response)
|
|
||||||
|
|
||||||
|
|
||||||
_THINK_BLOCK_RE = re.compile(r"<think>.*?</think>", flags=re.IGNORECASE | re.DOTALL)
|
_THINK_BLOCK_RE = re.compile(r"<think>.*?</think>", flags=re.IGNORECASE | re.DOTALL)
|
||||||
_THINK_START_RE = re.compile(r"<think>.*$", flags=re.IGNORECASE | re.DOTALL)
|
_THINK_START_RE = re.compile(r"<think>.*$", flags=re.IGNORECASE | re.DOTALL)
|
||||||
|
|
||||||
|
|||||||
@@ -14,14 +14,6 @@ def make_chat_completion(content: Any) -> SimpleNamespace:
|
|||||||
return ns(choices=[ns(message=ns(content=content))])
|
return ns(choices=[ns(message=ns(content=content))])
|
||||||
|
|
||||||
|
|
||||||
def make_response_with_output_text(text: str) -> SimpleNamespace:
|
|
||||||
return ns(output_text=text)
|
|
||||||
|
|
||||||
|
|
||||||
def make_response_with_output_items(*items: Any) -> SimpleNamespace:
|
|
||||||
return ns(output=list(items))
|
|
||||||
|
|
||||||
|
|
||||||
class FakeTensor:
|
class FakeTensor:
|
||||||
def __init__(self, array: np.ndarray):
|
def __init__(self, array: np.ndarray):
|
||||||
self._array = np.asarray(array, dtype=np.float32)
|
self._array = np.asarray(array, dtype=np.float32)
|
||||||
|
|||||||
+11
-17
@@ -2,7 +2,6 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import base64
|
import base64
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
import json
|
|
||||||
import sys
|
import sys
|
||||||
import types
|
import types
|
||||||
|
|
||||||
@@ -183,23 +182,18 @@ def test_extract_responses_text_variants() -> None:
|
|||||||
assert client.extract_responses_text(ns(output_text="", output=[])) == ""
|
assert client.extract_responses_text(ns(output_text="", output=[])) == ""
|
||||||
|
|
||||||
|
|
||||||
def test_dump_openai_response_branches() -> None:
|
def test_extract_responses_text_no_duplicate_when_item_has_content_and_text() -> None:
|
||||||
class WithModelDump:
|
# An output item that has BOTH a content list and a top-level text attribute
|
||||||
def model_dump(self):
|
# should only yield text from the content list; the item-level text field
|
||||||
return {"a": 1}
|
# must NOT be appended a second time (regression guard for the duplicate-text bug).
|
||||||
|
item_with_both = ns(content=[ns(text="the-answer")], text="the-answer")
|
||||||
|
response = ns(output=[item_with_both])
|
||||||
|
result = client.extract_responses_text(response)
|
||||||
|
assert result == "the-answer", (
|
||||||
|
f"Expected 'the-answer' but got {result!r} — "
|
||||||
|
"item.text was appended even though content was already processed"
|
||||||
|
)
|
||||||
|
|
||||||
class WithToDict:
|
|
||||||
def to_dict(self):
|
|
||||||
return {"b": 2}
|
|
||||||
|
|
||||||
class Plain:
|
|
||||||
def __str__(self) -> str:
|
|
||||||
return "plain-response"
|
|
||||||
|
|
||||||
assert json.loads(client.dump_openai_response(WithModelDump())) == {"a": 1}
|
|
||||||
assert json.loads(client.dump_openai_response(WithToDict())) == {"b": 2}
|
|
||||||
assert json.loads(client.dump_openai_response({"c": 3})) == {"c": 3}
|
|
||||||
assert client.dump_openai_response(Plain()) == "plain-response"
|
|
||||||
|
|
||||||
|
|
||||||
def test_strip_think_content_case_insensitive_and_multiple() -> None:
|
def test_strip_think_content_case_insensitive_and_multiple() -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user