diff --git a/config.yaml b/config.yaml index de0ca5eb0..9e22febb2 100644 --- a/config.yaml +++ b/config.yaml @@ -131,8 +131,6 @@ LLM: vLLm: type: vllm base_url: http://localhost:8000 - max_tokens: 256 - temperature: 0.7 model_name: "Qwen/Qwen2.5-1.5B-Instruct" TTS: diff --git a/core/providers/llm/vllm/vllm.py b/core/providers/llm/vllm/vllm.py index a0a9c751b..30f8e797b 100644 --- a/core/providers/llm/vllm/vllm.py +++ b/core/providers/llm/vllm/vllm.py @@ -8,72 +8,57 @@ logger = logging.getLogger(__name__) class LLMProvider(LLMProviderBase): def __init__(self, config): """ - 初始化 vllm 提供器 + 初始化 vllm 聊天提供器 - 配置项包含: + 配置项: - model_name: 模型名称或路径(必填) - base_url: vllm 服务地址,默认为 "http://localhost:8000" - - max_tokens: 最大生成 token 数量,默认为 256 - - temperature: 采样温度,默认为 0.7 + - max_tokens: 生成文本的最大 token 数量(可选) + - temperature: 采样温度(可选) """ self.model_name = config.get("model_name") if not self.model_name: raise ValueError("配置中必须指定 model_name") + 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): """ - 根据对话生成 prompt,并调用 vllm 的 completions 接口,通过 HTTP POST 请求获取生成结果, - 返回生成的文本作为生成器 + 根据对话消息构造请求(消息格式:{"role": "xxx", "content": "xxx"}), + 调用 vllm 的聊天接口,并返回生成的响应文本。 参数: - 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: - # 拼接对话日志,形成完整的 prompt - 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 示例,构造请求体参数 + # 直接使用 dialogue 列表作为 chat 接口的 messages 字段 payload = { "model": self.model_name, - "prompt": prompt, - "max_tokens": self.max_tokens, - "temperature": self.temperature + "messages": dialogue } - - url = f"{self.base_url}/v1/completions" + + url = f"{self.base_url}/v1/chat/completions" headers = {"Content-Type": "application/json"} response = requests.post(url, json=payload, headers=headers) response.raise_for_status() - json_response = response.json() - # 根据返回格式解析生成文本 - # 如果返回结果遵循 OpenAI 的 completions 格式,则通常在 "choices" 列表中返回生成文本 + # 假设响应格式类似 OpenAI chat completions 的结构 + # 从 "choices" 数组中取第一个候选消息的 "message" 字段的 "content" if "choices" in json_response and json_response["choices"]: - generated_text = json_response["choices"][0].get("text", "") - yield generated_text - elif "response" in json_response: - yield json_response["response"] - elif "text" in json_response: - yield json_response["text"] + generated_message = json_response["choices"][0].get("message", {}) + content = generated_message.get("content", "") + yield content else: logger.error("无法解析 vllm 响应: %s", json_response) yield "【vllm 服务响应异常】" except Exception as e: - logger.error("vllm 请求生成响应时发生错误: %s", e) + logger.error("vllm 聊天响应生成请求错误: %s", e) yield "【vllm 服务响应异常】" \ No newline at end of file