mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
添加vllm支持
This commit is contained in:
parent
d30ae930eb
commit
6573559ef5
2 changed files with 22 additions and 39 deletions
|
|
@ -131,8 +131,6 @@ LLM:
|
||||||
vLLm:
|
vLLm:
|
||||||
type: vllm
|
type: vllm
|
||||||
base_url: http://localhost:8000
|
base_url: http://localhost:8000
|
||||||
max_tokens: 256
|
|
||||||
temperature: 0.7
|
|
||||||
model_name: "Qwen/Qwen2.5-1.5B-Instruct"
|
model_name: "Qwen/Qwen2.5-1.5B-Instruct"
|
||||||
|
|
||||||
TTS:
|
TTS:
|
||||||
|
|
|
||||||
|
|
@ -8,72 +8,57 @@ logger = logging.getLogger(__name__)
|
||||||
class LLMProvider(LLMProviderBase):
|
class LLMProvider(LLMProviderBase):
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
"""
|
"""
|
||||||
初始化 vllm 提供器
|
初始化 vllm 聊天提供器
|
||||||
|
|
||||||
配置项包含:
|
配置项:
|
||||||
- model_name: 模型名称或路径(必填)
|
- model_name: 模型名称或路径(必填)
|
||||||
- base_url: vllm 服务地址,默认为 "http://localhost:8000"
|
- base_url: vllm 服务地址,默认为 "http://localhost:8000"
|
||||||
- max_tokens: 最大生成 token 数量,默认为 256
|
- max_tokens: 生成文本的最大 token 数量(可选)
|
||||||
- temperature: 采样温度,默认为 0.7
|
- temperature: 采样温度(可选)
|
||||||
"""
|
"""
|
||||||
self.model_name = config.get("model_name")
|
self.model_name = config.get("model_name")
|
||||||
if not self.model_name:
|
if not self.model_name:
|
||||||
raise ValueError("配置中必须指定 model_name")
|
raise ValueError("配置中必须指定 model_name")
|
||||||
|
|
||||||
self.base_url = config.get("base_url", "http://localhost:8000")
|
self.base_url = config.get("base_url", "http://localhost:8000")
|
||||||
self.max_tokens = config.get("max_tokens", 256)
|
|
||||||
self.temperature = config.get("temperature", 0.7)
|
|
||||||
|
|
||||||
def response(self, session_id, dialogue):
|
def response(self, session_id, dialogue):
|
||||||
"""
|
"""
|
||||||
根据对话生成 prompt,并调用 vllm 的 completions 接口,通过 HTTP POST 请求获取生成结果,
|
根据对话消息构造请求(消息格式:{"role": "xxx", "content": "xxx"}),
|
||||||
返回生成的文本作为生成器
|
调用 vllm 的聊天接口,并返回生成的响应文本。
|
||||||
|
|
||||||
参数:
|
参数:
|
||||||
- session_id: 会话 ID(用于日志记录)
|
- session_id: 会话 ID(用于日志记录)
|
||||||
- dialogue: 对话列表,每个元素为字典,包含 "role" 与 "content" 字段
|
- dialogue: 消息列表,每个元素为字典,例如:
|
||||||
|
{"role": "system", "content": "You are a helpful assistant."},
|
||||||
|
{"role": "user", "content": "Who won the world series in 2020?"}
|
||||||
|
|
||||||
返回:
|
返回:
|
||||||
生成器,每个元素为生成的文本
|
一个生成器,产生生成的文本(取第一个候选消息的 "content")。
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# 拼接对话日志,形成完整的 prompt
|
# 直接使用 dialogue 列表作为 chat 接口的 messages 字段
|
||||||
prompt = ""
|
|
||||||
for msg in dialogue:
|
|
||||||
if msg["role"] == "system":
|
|
||||||
prompt += f"System: {msg['content']}\n"
|
|
||||||
elif msg["role"] == "user":
|
|
||||||
prompt += f"User: {msg['content']}\n"
|
|
||||||
elif msg["role"] == "assistant":
|
|
||||||
prompt += f"Assistant: {msg['content']}\n"
|
|
||||||
|
|
||||||
# 根据 vllm 接口的 curl 示例,构造请求体参数
|
|
||||||
payload = {
|
payload = {
|
||||||
"model": self.model_name,
|
"model": self.model_name,
|
||||||
"prompt": prompt,
|
"messages": dialogue
|
||||||
"max_tokens": self.max_tokens,
|
|
||||||
"temperature": self.temperature
|
|
||||||
}
|
}
|
||||||
|
|
||||||
url = f"{self.base_url}/v1/completions"
|
url = f"{self.base_url}/v1/chat/completions"
|
||||||
headers = {"Content-Type": "application/json"}
|
headers = {"Content-Type": "application/json"}
|
||||||
|
|
||||||
response = requests.post(url, json=payload, headers=headers)
|
response = requests.post(url, json=payload, headers=headers)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
json_response = response.json()
|
json_response = response.json()
|
||||||
|
|
||||||
# 根据返回格式解析生成文本
|
# 假设响应格式类似 OpenAI chat completions 的结构
|
||||||
# 如果返回结果遵循 OpenAI 的 completions 格式,则通常在 "choices" 列表中返回生成文本
|
# 从 "choices" 数组中取第一个候选消息的 "message" 字段的 "content"
|
||||||
if "choices" in json_response and json_response["choices"]:
|
if "choices" in json_response and json_response["choices"]:
|
||||||
generated_text = json_response["choices"][0].get("text", "")
|
generated_message = json_response["choices"][0].get("message", {})
|
||||||
yield generated_text
|
content = generated_message.get("content", "")
|
||||||
elif "response" in json_response:
|
yield content
|
||||||
yield json_response["response"]
|
|
||||||
elif "text" in json_response:
|
|
||||||
yield json_response["text"]
|
|
||||||
else:
|
else:
|
||||||
logger.error("无法解析 vllm 响应: %s", json_response)
|
logger.error("无法解析 vllm 响应: %s", json_response)
|
||||||
yield "【vllm 服务响应异常】"
|
yield "【vllm 服务响应异常】"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("vllm 请求生成响应时发生错误: %s", e)
|
logger.error("vllm 聊天响应生成请求错误: %s", e)
|
||||||
yield "【vllm 服务响应异常】"
|
yield "【vllm 服务响应异常】"
|
||||||
Loading…
Reference in a new issue