mirror of
https://github.com/NCBM/plyngent.git
synced 2026-07-23 05:55:16 +08:00
core/lmproto+agent: use normal stream API for chat loop
Accept async-def returning async iterator; stream via chat_completions(..., stream=True); typed StreamToolCallDelta merge; update scripted test clients to emit real chunks.
This commit is contained in:
@@ -1,15 +1,29 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from msgspec import UNSET
|
||||||
|
|
||||||
from plyngent.lmproto.openai_compatible.client import merge_stream_tool_calls
|
from plyngent.lmproto.openai_compatible.client import merge_stream_tool_calls
|
||||||
|
from plyngent.lmproto.openai_compatible.model import StreamFunctionDelta, StreamToolCallDelta
|
||||||
|
|
||||||
|
|
||||||
def test_merge_stream_tool_calls_accumulates_arguments() -> None:
|
def test_merge_stream_tool_calls_accumulates_arguments() -> None:
|
||||||
lines = [
|
deltas = [
|
||||||
b'{"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"c1","type":"function","function":{"name":"add","arguments":""}}]}}]}',
|
StreamToolCallDelta(
|
||||||
b'{"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\\"a\\":"}}]}}]}',
|
index=0,
|
||||||
b'{"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"1}"}}]},"finish_reason":"tool_calls"}]}',
|
id="c1",
|
||||||
|
type="function",
|
||||||
|
function=StreamFunctionDelta(name="add", arguments=""),
|
||||||
|
),
|
||||||
|
StreamToolCallDelta(
|
||||||
|
index=0,
|
||||||
|
function=StreamFunctionDelta(name=UNSET, arguments='{"a":'),
|
||||||
|
),
|
||||||
|
StreamToolCallDelta(
|
||||||
|
index=0,
|
||||||
|
function=StreamFunctionDelta(name=UNSET, arguments="1}"),
|
||||||
|
),
|
||||||
]
|
]
|
||||||
calls = merge_stream_tool_calls(lines)
|
calls = merge_stream_tool_calls(deltas)
|
||||||
assert len(calls) == 1
|
assert len(calls) == 1
|
||||||
assert calls[0].id == "c1"
|
assert calls[0].id == "c1"
|
||||||
assert calls[0].function.name == "add"
|
assert calls[0].function.name == "add"
|
||||||
@@ -17,9 +31,19 @@ def test_merge_stream_tool_calls_accumulates_arguments() -> None:
|
|||||||
|
|
||||||
|
|
||||||
def test_merge_stream_tool_calls_multiple_indices() -> None:
|
def test_merge_stream_tool_calls_multiple_indices() -> None:
|
||||||
lines = [
|
deltas = [
|
||||||
b'{"choices":[{"delta":{"tool_calls":[{"index":0,"id":"a","type":"function","function":{"name":"f1","arguments":"{}"}}]}}]}',
|
StreamToolCallDelta(
|
||||||
b'{"choices":[{"delta":{"tool_calls":[{"index":1,"id":"b","type":"function","function":{"name":"f2","arguments":"[]"}}]}}]}',
|
index=0,
|
||||||
|
id="a",
|
||||||
|
type="function",
|
||||||
|
function=StreamFunctionDelta(name="f1", arguments="{}"),
|
||||||
|
),
|
||||||
|
StreamToolCallDelta(
|
||||||
|
index=1,
|
||||||
|
id="b",
|
||||||
|
type="function",
|
||||||
|
function=StreamFunctionDelta(name="f2", arguments="[]"),
|
||||||
|
),
|
||||||
]
|
]
|
||||||
calls = merge_stream_tool_calls(lines)
|
calls = merge_stream_tool_calls(deltas)
|
||||||
assert {c.function.name for c in calls} == {"f1", "f2"}
|
assert {c.function.name for c in calls} == {"f1", "f2"}
|
||||||
|
|||||||
Reference in New Issue
Block a user