mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
feat: 增加fastgpt适配:支持think、variables参数
This commit is contained in:
parent
8b151d07c2
commit
d4d9758caa
2 changed files with 73 additions and 0 deletions
|
|
@ -168,6 +168,15 @@ LLM:
|
||||||
base_url: http://homeassistant.local:8123
|
base_url: http://homeassistant.local:8123
|
||||||
agent_id: conversation.chatgpt
|
agent_id: conversation.chatgpt
|
||||||
api_key: 你的home assistant api访问令牌
|
api_key: 你的home assistant api访问令牌
|
||||||
|
FastgptLLM:
|
||||||
|
# 定义LLM API类型
|
||||||
|
type: fastgpt
|
||||||
|
# 如果使用fastgpt,配置文件里prompt(提示词)是无效的,需要在fastgpt控制台设置提示词
|
||||||
|
base_url: https://host/api/v1
|
||||||
|
api_key: fastgpt-xxx
|
||||||
|
variables:
|
||||||
|
k: "v"
|
||||||
|
k2: "v2"
|
||||||
TTS:
|
TTS:
|
||||||
# 当前支持的type为edge、doubao,可自行适配
|
# 当前支持的type为edge、doubao,可自行适配
|
||||||
EdgeTTS:
|
EdgeTTS:
|
||||||
|
|
|
||||||
64
core/providers/llm/fastgpt/fastgpt.py
Normal file
64
core/providers/llm/fastgpt/fastgpt.py
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
import json
|
||||||
|
from config.logger import setup_logging
|
||||||
|
import requests
|
||||||
|
from core.providers.llm.base import LLMProviderBase
|
||||||
|
|
||||||
|
TAG = __name__
|
||||||
|
logger = setup_logging()
|
||||||
|
|
||||||
|
class LLMProvider(LLMProviderBase):
|
||||||
|
def __init__(self, config):
|
||||||
|
self.api_key = config["api_key"]
|
||||||
|
self.base_url = config.get("base_url")
|
||||||
|
self.detail = config.get("detail", False)
|
||||||
|
self.variables = config.get("variables", {})
|
||||||
|
|
||||||
|
def response(self, session_id, dialogue):
|
||||||
|
try:
|
||||||
|
# 取最后一条用户消息
|
||||||
|
last_msg = next(m for m in reversed(dialogue) if m["role"] == "user")
|
||||||
|
|
||||||
|
# 发起流式请求
|
||||||
|
with requests.post(
|
||||||
|
f"{self.base_url}/chat/completions",
|
||||||
|
headers={"Authorization": f"Bearer {self.api_key}"},
|
||||||
|
json={
|
||||||
|
"stream": True,
|
||||||
|
"chatId": session_id,
|
||||||
|
"detail": self.detail,
|
||||||
|
"variables": self.variables,
|
||||||
|
"messages": [
|
||||||
|
{
|
||||||
|
"role": "user",
|
||||||
|
"content": last_msg["content"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
stream=True
|
||||||
|
) as r:
|
||||||
|
for line in r.iter_lines():
|
||||||
|
if line:
|
||||||
|
try:
|
||||||
|
if line.startswith(b'data: '):
|
||||||
|
if line[6:].decode('utf-8') == '[DONE]':
|
||||||
|
break
|
||||||
|
|
||||||
|
data = json.loads(line[6:])
|
||||||
|
if 'choices' in data and len(data['choices']) > 0:
|
||||||
|
delta = data['choices'][0].get('delta', {})
|
||||||
|
if delta and 'content' in delta and delta['content'] is not None:
|
||||||
|
content = delta['content']
|
||||||
|
if '<think>' in content:
|
||||||
|
continue
|
||||||
|
if '</think>' in content:
|
||||||
|
continue
|
||||||
|
yield content
|
||||||
|
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
continue
|
||||||
|
except Exception as e:
|
||||||
|
continue
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.bind(tag=TAG).error(f"Error in response generation: {e}")
|
||||||
|
yield "【服务响应异常】"
|
||||||
Loading…
Reference in a new issue