Implement StepFun Realtime service and enhance AssistantConfig
- Add new fields to AssistantConfig for realtime interface configuration, including types, values, and secrets. - Introduce StepFunRealtimeService to handle speech-to-speech processing via WebSocket, integrating STT, LLM, and TTS functionalities. - Refactor pipeline execution to support a new realtime mode, allowing direct text input processing and immediate responses. - Update model resource testing to include validation for StepFun Realtime connections. - Enhance service factory to create realtime services based on configuration settings. - Modify README documentation to reflect new realtime capabilities and usage instructions.
This commit is contained in:
@@ -2,11 +2,15 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import io
|
||||
import json
|
||||
import time
|
||||
import wave
|
||||
from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit
|
||||
|
||||
import httpx
|
||||
from websockets.asyncio.client import connect as websocket_connect
|
||||
|
||||
import config
|
||||
from schemas import ModelResourceTestResult
|
||||
@@ -57,6 +61,8 @@ async def test_model_resource(
|
||||
message="讯飞连接参数有效",
|
||||
detail="鉴权字段和连接参数完整,请在语音测试页验证签名及音频链路",
|
||||
)
|
||||
if interface_type == "stepfun-realtime":
|
||||
return await _test_stepfun_realtime(values, secrets)
|
||||
if capability == "Realtime":
|
||||
return ModelResourceTestResult(
|
||||
ok=False,
|
||||
@@ -150,3 +156,61 @@ async def test_model_resource(
|
||||
message="无法连接到模型服务",
|
||||
detail=str(exc)[:300],
|
||||
)
|
||||
|
||||
|
||||
async def _test_stepfun_realtime(
|
||||
values: dict, secrets: dict
|
||||
) -> ModelResourceTestResult:
|
||||
api_url = str(values.get("apiUrl") or "")
|
||||
model_id = str(values.get("modelId") or "")
|
||||
api_key = str(secrets.get("apiKey") or "")
|
||||
parts = urlsplit(api_url)
|
||||
query = dict(parse_qsl(parts.query))
|
||||
query["model"] = model_id
|
||||
url = urlunsplit(
|
||||
(parts.scheme, parts.netloc, parts.path, urlencode(query), parts.fragment)
|
||||
)
|
||||
started = time.perf_counter()
|
||||
|
||||
try:
|
||||
async with websocket_connect(
|
||||
url,
|
||||
additional_headers={"Authorization": f"Bearer {api_key}"},
|
||||
open_timeout=TEST_TIMEOUT_SECONDS,
|
||||
close_timeout=2,
|
||||
) as websocket:
|
||||
raw_message = await asyncio.wait_for(
|
||||
websocket.recv(), timeout=TEST_TIMEOUT_SECONDS
|
||||
)
|
||||
event = json.loads(raw_message)
|
||||
if event.get("type") != "session.created":
|
||||
return ModelResourceTestResult(
|
||||
ok=False,
|
||||
latency_ms=round((time.perf_counter() - started) * 1000),
|
||||
message="Realtime 连接返回了意外事件",
|
||||
detail=str(event.get("type") or event)[:300],
|
||||
)
|
||||
return ModelResourceTestResult(
|
||||
ok=True,
|
||||
latency_ms=round((time.perf_counter() - started) * 1000),
|
||||
message="Realtime 连接成功",
|
||||
detail="StepFun 返回 session.created",
|
||||
)
|
||||
except TimeoutError:
|
||||
return ModelResourceTestResult(
|
||||
ok=False,
|
||||
latency_ms=round((time.perf_counter() - started) * 1000),
|
||||
message="Realtime 连接超时",
|
||||
detail=f"服务未在 {TEST_TIMEOUT_SECONDS:g} 秒内创建 session",
|
||||
)
|
||||
except Exception as exc:
|
||||
detail = str(exc)
|
||||
for secret in secrets.values():
|
||||
if secret:
|
||||
detail = detail.replace(str(secret), "***")
|
||||
return ModelResourceTestResult(
|
||||
ok=False,
|
||||
latency_ms=round((time.perf_counter() - started) * 1000),
|
||||
message="无法连接到 StepFun Realtime",
|
||||
detail=detail[:300],
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user