Assistant node, and the Control tab as one conversation (openpanel V5)
run_turn takes its tools, system prompt and tool caller; the chat passes the MCP tools as before. The assistant node offers one tool per wired target, reads its other inputs as context, streams its reply a sentence at a time, warms the provider on `listening` and says so when the provider is not there. Completions share the per-server HTTP client. wren replies when nothing reads `unhandled`, stt shows a note when nothing was heard. /ai/control lists one conversation per entry node with the messages to watch.
This commit is contained in:
@@ -118,26 +118,32 @@ async def stream_completion(
|
||||
model: str,
|
||||
messages: list[dict[str, Any]],
|
||||
tools: list[dict[str, Any]],
|
||||
timeout: float = 120.0,
|
||||
timeout: float | httpx.Timeout = 120.0,
|
||||
extra: dict[str, Any] | None = None,
|
||||
) -> AsyncIterator[str | dict[str, Any]]:
|
||||
"""``POST {api_base}/chat/completions`` streamed.
|
||||
|
||||
Yields each piece of the answer's text as it arrives, then the whole
|
||||
message. The timeout is per chunk, so a long answer is not cut off.
|
||||
``extra`` is added to the request as it is, for what one provider
|
||||
understands and another does not, such as Ollama's ``keep_alive``.
|
||||
"""
|
||||
body: dict[str, Any] = {"model": model, "messages": messages, "stream": True}
|
||||
body: dict[str, Any] = {
|
||||
**(extra or {}),
|
||||
"model": model,
|
||||
"messages": messages,
|
||||
"stream": True,
|
||||
}
|
||||
if tools:
|
||||
body["tools"] = tools
|
||||
assembler = Assembler()
|
||||
async with (
|
||||
httpx.AsyncClient(timeout=timeout) as http,
|
||||
http.stream(
|
||||
"POST",
|
||||
api_base.rstrip("/") + "/chat/completions",
|
||||
json=body,
|
||||
headers=_headers(api_key),
|
||||
) as response,
|
||||
):
|
||||
async with _client(api_base).stream(
|
||||
"POST",
|
||||
api_base.rstrip("/") + "/chat/completions",
|
||||
json=body,
|
||||
headers=_headers(api_key),
|
||||
timeout=timeout,
|
||||
) as response:
|
||||
if response.is_error:
|
||||
await response.aread()
|
||||
raise ProviderError(_detail(response))
|
||||
@@ -160,24 +166,24 @@ async def list_models(api_base: str, api_key: str, timeout: float = 15.0) -> lis
|
||||
return sorted(str(m["id"]) for m in data if isinstance(m, dict) and "id" in m)
|
||||
|
||||
|
||||
#: One client per speech server, so its connection stays open between calls
|
||||
#: One client per server, so its connection stays open between calls
|
||||
#: (`parrot serve` keeps an idle one 600 s). A new connection over a lossy
|
||||
#: link can cost a second, which a spoken reply cannot spare.
|
||||
_speech: dict[str, httpx.AsyncClient] = {}
|
||||
_clients: dict[str, httpx.AsyncClient] = {}
|
||||
|
||||
|
||||
def _speech_client(api_base: str) -> httpx.AsyncClient:
|
||||
"""The client for this speech server. Used from the engine's loop only."""
|
||||
http = _speech.get(api_base)
|
||||
def _client(api_base: str) -> httpx.AsyncClient:
|
||||
"""The client for this server. Used from the engine's loop only."""
|
||||
http = _clients.get(api_base)
|
||||
if http is None:
|
||||
limits = httpx.Limits(keepalive_expiry=300)
|
||||
http = _speech[api_base] = httpx.AsyncClient(limits=limits)
|
||||
http = _clients[api_base] = httpx.AsyncClient(limits=limits)
|
||||
return http
|
||||
|
||||
|
||||
async def warm(api_base: str) -> None:
|
||||
"""Open the connection to a speech server ahead of the call that needs it."""
|
||||
await _speech_client(api_base).get(api_base.rstrip("/") + "/models", timeout=10)
|
||||
await _client(api_base).get(api_base.rstrip("/") + "/models", timeout=10)
|
||||
|
||||
|
||||
async def transcribe(
|
||||
@@ -190,7 +196,7 @@ async def transcribe(
|
||||
timeout: float = 30.0,
|
||||
) -> str:
|
||||
"""``POST {api_base}/audio/transcriptions``: a whole clip, and what it says."""
|
||||
response = await _speech_client(api_base).post(
|
||||
response = await _client(api_base).post(
|
||||
api_base.rstrip("/") + "/audio/transcriptions",
|
||||
files={"file": (filename, audio, media_type)},
|
||||
data={"model": model, "response_format": "json"},
|
||||
@@ -214,7 +220,7 @@ async def speak(
|
||||
MP3 rather than WAV: a seventh of the bytes, which over a slow link to
|
||||
the server is most of the wait for a sentence.
|
||||
"""
|
||||
response = await _speech_client(api_base).post(
|
||||
response = await _client(api_base).post(
|
||||
api_base.rstrip("/") + "/audio/speech",
|
||||
json={"model": model, "input": text, "voice": voice, "response_format": "mp3"},
|
||||
timeout=timeout,
|
||||
|
||||
Reference in New Issue
Block a user