mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
feat: support gemini on the openrouter
This commit is contained in:
parent
e5ef658191
commit
2e0596de33
4 changed files with 67 additions and 11 deletions
|
|
@ -300,6 +300,7 @@ class ConnectionHandler:
|
|||
return
|
||||
|
||||
# 不需要头部处理或没有头部时,直接处理原始消息
|
||||
|
||||
self.asr_audio_queue.put(message)
|
||||
|
||||
async def _process_mqtt_audio_message(self, message):
|
||||
|
|
|
|||
|
|
@ -33,12 +33,19 @@ class LLMProvider(LLMProviderBase):
|
|||
self.timeout = int(timeout) if timeout else 300
|
||||
|
||||
param_defaults = {
|
||||
"max_tokens": (500, int),
|
||||
"max_tokens": (1000, int),
|
||||
"temperature": (0.7, lambda x: round(float(x), 1)),
|
||||
"top_p": (1.0, lambda x: round(float(x), 1)),
|
||||
"frequency_penalty": (0, lambda x: round(float(x), 1)),
|
||||
}
|
||||
|
||||
provider = config.get("provider", None)
|
||||
allow_fallback = config.get("allow_fallback", False)
|
||||
reasoning_effort = config.get("reasoning_effort", None)
|
||||
self.provider = provider
|
||||
self.allow_fallback = allow_fallback
|
||||
self.reasoning_effort = reasoning_effort
|
||||
|
||||
for param, (default, converter) in param_defaults.items():
|
||||
value = config.get(param)
|
||||
try:
|
||||
|
|
@ -109,8 +116,17 @@ class LLMProvider(LLMProviderBase):
|
|||
"max_output_tokens": self.max_tokens,
|
||||
"temperature": self.temperature,
|
||||
"top_p": self.top_p,
|
||||
"reasoning": {
|
||||
"effort": self.reasoning_effort if self.reasoning_effort else "none"
|
||||
},
|
||||
"extra_body": {
|
||||
"provider": {
|
||||
"allow_fallbacks": self.allow_fallback,
|
||||
"order": [self.provider] if self.provider else None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# 只有在有 tools 时才添加 tools 参数
|
||||
if tools:
|
||||
request_params["tools"] = tools
|
||||
|
|
|
|||
|
|
@ -63,6 +63,9 @@ class VADProvider(VADProviderBase):
|
|||
|
||||
def is_vad(self, conn, opus_packet):
|
||||
try:
|
||||
now_ms = time.time() * 1000
|
||||
conn._vad_frame_count = getattr(conn, '_vad_frame_count', 0) + 1
|
||||
|
||||
pcm_frame = self.decoder.decode(opus_packet, 960)
|
||||
conn.client_audio_buffer.extend(pcm_frame)
|
||||
|
||||
|
|
@ -92,6 +95,10 @@ class VADProvider(VADProviderBase):
|
|||
|
||||
conn.last_is_voice = is_voice
|
||||
|
||||
# record first voice frame time (when is_voice becomes True)
|
||||
if is_voice and not hasattr(conn, '_vad_first_voice_time'):
|
||||
conn._vad_first_voice_time = now_ms
|
||||
|
||||
# update sliding window
|
||||
conn.client_voice_window.append(is_voice)
|
||||
client_have_voice = (
|
||||
|
|
@ -101,8 +108,18 @@ class VADProvider(VADProviderBase):
|
|||
# 检测语音开始(边缘检测)
|
||||
if not conn.client_have_voice and client_have_voice:
|
||||
conn._latency_voice_start_time = time.time() * 1000
|
||||
logger.bind(tag=TAG).info(f"🎤 [延迟追踪] 用户开始说话")
|
||||
# calculate delay from first voice frame to VAD confirmation
|
||||
first_voice_time = getattr(conn, '_vad_first_voice_time', None)
|
||||
if first_voice_time:
|
||||
voice_to_confirm = conn._latency_voice_start_time - first_voice_time
|
||||
logger.bind(tag=TAG).info(
|
||||
f"🎤 [延迟追踪] VAD检测到开始说话 | "
|
||||
f"首个声音帧到确认: {voice_to_confirm:.0f}ms"
|
||||
)
|
||||
else:
|
||||
logger.bind(tag=TAG).info(f"🎤 [延迟追踪] VAD检测到开始说话")
|
||||
|
||||
# 检测语音结束(需要确保开始时间已设置)
|
||||
if conn.client_have_voice and not client_have_voice:
|
||||
stop_duration = time.time() * 1000 - conn.last_activity_time
|
||||
if stop_duration >= self.silence_threshold_ms:
|
||||
|
|
@ -110,10 +127,22 @@ class VADProvider(VADProviderBase):
|
|||
conn.client_voice_stop = True
|
||||
# 记录用户说完的时间(端到端延迟的起点)
|
||||
conn._latency_voice_end_time = time.time() * 1000
|
||||
voice_duration = conn._latency_voice_end_time - conn._latency_voice_start_time
|
||||
logger.bind(tag=TAG).info(
|
||||
f"🎤 [延迟追踪] 用户说完,语音时长: {voice_duration:.0f}ms"
|
||||
)
|
||||
|
||||
# 安全检查:确保时间戳已设置
|
||||
start_time = getattr(conn, '_latency_voice_start_time', None)
|
||||
|
||||
if start_time:
|
||||
voice_duration = conn._latency_voice_end_time - start_time
|
||||
logger.bind(tag=TAG).info(
|
||||
f"🎤 [延迟追踪] VAD检测到说话结束 | "
|
||||
f"语音时长: {voice_duration:.0f}ms"
|
||||
)
|
||||
|
||||
# reset for next utterance
|
||||
conn._latency_voice_start_time = None
|
||||
if hasattr(conn, '_vad_first_voice_time'):
|
||||
del conn._vad_first_voice_time
|
||||
|
||||
if client_have_voice:
|
||||
conn.client_have_voice = True
|
||||
conn.last_activity_time = time.time() * 1000
|
||||
|
|
|
|||
|
|
@ -477,9 +477,9 @@ VAD:
|
|||
SileroVAD:
|
||||
type: silero
|
||||
model_dir: models/snakers4_silero-vad
|
||||
threshold: 0.6 # high threshold to confirm voice
|
||||
threshold_low: 0.4 # low threshold to confirm silence
|
||||
min_silence_duration_ms: 200 # silence detection duration (ms)
|
||||
threshold: 0.5 # high threshold to confirm voice
|
||||
threshold_low: 0.35 # low threshold to confirm silence
|
||||
min_silence_duration_ms: 400 # silence detection duration (ms)
|
||||
frame_window_threshold: 3 # sliding window threshold
|
||||
|
||||
LLM:
|
||||
|
|
@ -539,11 +539,20 @@ LLM:
|
|||
model_name: meta-llama/llama-4-scout-17b-16e-instruct
|
||||
base_url: https://api.groq.com/openai/v1
|
||||
api_key: ${GROQ_API_KEY}
|
||||
max_tokens: 1000
|
||||
OpenaiLLM:
|
||||
type: openai
|
||||
model_name: gpt-4o-mini
|
||||
base_url: https://api.openai.com/v1
|
||||
api_key: ${OPENAI_API_KEY}
|
||||
OpenRouterLLM:
|
||||
type: openai
|
||||
model_name: google/gemini-2.5-flash
|
||||
base_url: https://openrouter.ai/api/v1
|
||||
api_key: ${OPENROUTER_API_KEY}
|
||||
provider: vertex-ai-global
|
||||
allow_fallback: true
|
||||
reasoning_effort: none
|
||||
OllamaLLM:
|
||||
# 定义LLM API类型
|
||||
type: ollama
|
||||
|
|
@ -1085,7 +1094,8 @@ TTS:
|
|||
selected_module:
|
||||
VAD: SileroVAD
|
||||
ASR: GroqASR
|
||||
LLM: GroqLLM
|
||||
# LLM: GroqLLM
|
||||
LLM: OpenRouterLLM
|
||||
VLLM: ChatGLMVLLM
|
||||
TTS: FishDualStreamTTS
|
||||
# TODO: temporarily, fish speech doesn't reuse the wesocket connection, need to discuss further.
|
||||
|
|
|
|||
Loading…
Reference in a new issue