Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 28 additions & 72 deletions danyapi/api/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,6 @@ class ResponsesRequest(BaseModel):
search: bool | None = None


def _find_tool_marker(text: str, start: int = 0) -> int:
markers = ('{"tool_calls"', "<tool_calls", "<calls", "<_calls", "<invoke", "tool_calls:")
best = -1
for m in markers:
pos = text.find(m, start)
if pos != -1 and (best == -1 or pos < best):
best = pos
return best


IMAGE_SIZE_RE = re.compile(r"^(\d{2,5})\s*[*x\u00d7,]\s*(\d{2,5})$", re.IGNORECASE)
MIN_IMAGE_DIM = 16
MAX_IMAGE_DIM = 8192
Expand Down Expand Up @@ -2502,17 +2492,9 @@ async def _stream_openai(
if c_diff:
if tool_mode:
content_buf += c_diff
if not tool_hidden:
search_from = content_shown_len
marker_pos = _find_tool_marker(content_buf, search_from)
if marker_pos < 0:
delta["content"] = content_buf[search_from:]
content_shown_len = len(content_buf)
else:
if marker_pos > search_from:
delta["content"] = content_buf[search_from:marker_pos]
content_shown_len = marker_pos
tool_hidden = True
visible, content_shown_len, tool_hidden = toolemu.tool_visible(content_buf, content_shown_len, tool_hidden, tool_schemas)
if visible:
delta["content"] = visible
else:
delta["content"] = c_diff
if r_diff:
Expand Down Expand Up @@ -2564,17 +2546,9 @@ async def _stream_openai(
if c_diff:
if tool_mode:
content_buf += c_diff
if not tool_hidden:
search_from = content_shown_len
marker_pos = _find_tool_marker(content_buf, search_from)
if marker_pos < 0:
delta2["content"] = content_buf[search_from:]
content_shown_len = len(content_buf)
else:
if marker_pos > search_from:
delta2["content"] = content_buf[search_from:marker_pos]
content_shown_len = marker_pos
tool_hidden = True
visible, content_shown_len, tool_hidden = toolemu.tool_visible(content_buf, content_shown_len, tool_hidden, tool_schemas)
if visible:
delta2["content"] = visible
else:
delta2["content"] = c_diff
if r_diff:
Expand Down Expand Up @@ -2671,26 +2645,17 @@ async def _stream_openai(
)
if tool_mode:
content_buf += cont_rec.content
if not tool_hidden:
search_from = content_shown_len
marker_pos = _find_tool_marker(content_buf, search_from)
if marker_pos < 0:
c_visible = cont_rec.content
content_shown_len = len(content_buf)
else:
c_visible = content_buf[search_from:marker_pos]
content_shown_len = marker_pos
tool_hidden = True
if c_visible:
yield _sse(
{
"id": chunk_id,
"object": "chat.completion.chunk",
"created": created,
"model": model,
"choices": [{"index": 0, "delta": {"content": c_visible}, "finish_reason": None}],
}
)
c_visible, content_shown_len, tool_hidden = toolemu.tool_visible(content_buf, content_shown_len, tool_hidden, tool_schemas)
if c_visible:
yield _sse(
{
"id": chunk_id,
"object": "chat.completion.chunk",
"created": created,
"model": model,
"choices": [{"index": 0, "delta": {"content": c_visible}, "finish_reason": None}],
}
)
else:
yield _sse(
{
Expand Down Expand Up @@ -2768,26 +2733,17 @@ async def _stream_openai(
)
if tool_mode:
content_buf += rec.content
if not tool_hidden:
search_from = content_shown_len
marker_pos = _find_tool_marker(content_buf, search_from)
if marker_pos < 0:
r_visible = rec.content
content_shown_len = len(content_buf)
else:
r_visible = content_buf[search_from:marker_pos]
content_shown_len = marker_pos
tool_hidden = True
if r_visible:
yield _sse(
{
"id": chunk_id,
"object": "chat.completion.chunk",
"created": created,
"model": model,
"choices": [{"index": 0, "delta": {"content": r_visible}, "finish_reason": None}],
}
)
r_visible, content_shown_len, tool_hidden = toolemu.tool_visible(content_buf, content_shown_len, tool_hidden, tool_schemas)
if r_visible:
yield _sse(
{
"id": chunk_id,
"object": "chat.completion.chunk",
"created": created,
"model": model,
"choices": [{"index": 0, "delta": {"content": r_visible}, "finish_reason": None}],
}
)
else:
yield _sse(
{
Expand Down
31 changes: 4 additions & 27 deletions danyapi/qwen/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,6 @@ def _is_retryable_http(exc: HTTPException) -> bool:
"tokenlimit",
)

_TOOL_MARKERS = ('{"tool_calls"', "<tool_calls>")


def _tool_marker_pos(text: str) -> int:
found = -1
for marker in _TOOL_MARKERS:
pos = text.find(marker)
if pos != -1 and (found == -1 or pos < found):
found = pos
return found


def _append_image_markdown(prompt: str, messages: list[Any] | None) -> str:
if not messages:
Expand Down Expand Up @@ -597,7 +586,7 @@ async def stream_openai(
rec: QwenStreamReconstructor | None = None
content_buf = ""
content_shown_len = 0
tool_marker_pos = -1
tool_hidden = False
role_sent = False
stop_response_id: str | None = None
had_cached_session = bool(existing_sid) and account.sessions.get(existing_sid) is not None
Expand Down Expand Up @@ -660,7 +649,7 @@ async def stream_openai(
got_content = False
role_sent = False
content_shown_len = 0
tool_marker_pos = -1
tool_hidden = False
stopped = False
try:
async for chunk in resp.aiter_bytes():
Expand Down Expand Up @@ -691,15 +680,9 @@ async def stream_openai(
if c_diff:
if tool_mode:
content_buf += c_diff
if tool_marker_pos == -1:
tool_marker_pos = _tool_marker_pos(content_buf)
if tool_marker_pos == -1:
shown = content_buf[content_shown_len:]
else:
shown = content_buf[content_shown_len:tool_marker_pos]
shown, content_shown_len, tool_hidden = toolemu.tool_visible(content_buf, content_shown_len, tool_hidden, tool_schemas)
if shown:
delta["content"] = shown
content_shown_len += len(shown)
else:
delta["content"] = c_diff
if r_diff:
Expand Down Expand Up @@ -747,15 +730,9 @@ async def stream_openai(
if c_diff:
if tool_mode:
content_buf += c_diff
if tool_marker_pos == -1:
tool_marker_pos = _tool_marker_pos(content_buf)
if tool_marker_pos == -1:
shown = content_buf[content_shown_len:]
else:
shown = content_buf[content_shown_len:tool_marker_pos]
shown, content_shown_len, tool_hidden = toolemu.tool_visible(content_buf, content_shown_len, tool_hidden, tool_schemas)
if shown:
delta2["content"] = shown
content_shown_len += len(shown)
else:
delta2["content"] = c_diff
if r_diff:
Expand Down
Loading
Loading