随着生成式 AI 和大语言模型(LLM)的兴起,越来越多的企业和开发者希望能在自己的环境内快速部署高效的 AI 应用。本文将详细介绍如何在某逊使用 SageMaker 部署基于 vLLM 的 DeepSeek 模型,并通过 SageMaker Endpoint 对外提供服务。同时,我们还将探讨如何利用开源项目 NextChat 构建 ChatBot 聊天应用,实现流式推理与 Reasoning 输出,并在 UI 上实现流式交互效果。
vLLM(Virtual Large Language Model)是一种高效的 LLM 推理引擎,专为大型语言模型(例如 DeepSeek、LLaMA、GPT 等)的高性能部署设计。vLLM 通过动态批处理、请求合并和缓存机制,大幅提高推理效率,降低模型推理延迟,最大限度地提升 GPU 的使用效率。
vLLM 的主要优势包括:
这些特性使 vLLM 成为在云端或本地部署生成式 AI 模型的理想选择,尤其适用于需要高性能、低延迟、实时交互场景的企业和开发团队。
NextChat 是一个基于 Next.js 构建的开源聊天应用,它能快速实现 AI ChatBot 服务。
原始的 NextChat 通常调用公开的大模型 API,但公开 API 存在网络延迟、服务不稳定、数据安全风险等问题。为了更高效、更安全,我们对 NextChat 进行了改造,使用 SageMaker Endpoint 进行推理,实现了私有化部署,性能更稳定,响应更快。
下面是实现流式响应的核心逻辑:
SageMakerRuntimeClient
初始化客户端,并设置相应参数。temperature
、top_p
等)封装成 JSON 格式的请求数据。InvokeEndpointWithResponseStreamCommand
调用 Endpoint,接收流式响应数据。ReadableStream
,解析每个 chunk 数据,识别“data: ”前缀,对 JSON 数据进行解码和拼接,并实时推送到前端。关键代码如下所示:
// API层逻辑(Node.js环境)
import {
SageMakerRuntimeClient,
InvokeEndpointWithResponseStreamCommand,
} from "@aws-sdk/client-sagemaker-runtime"; // ES Modules import
const ENDPIOINT_NAME = "your_sagemaker_endpoint";
const AWS_SAGEMAKER_PARAM = {}; // 添加你的 AWS 配置 或者采用运行容器的IAM Role
export const requestSagemakerDS = () => {
// 对话参数,参考:https://api-docs.deepseek.com/zh-cn/guides/reasoning_model
const payload = {
messages: [{"role": "user", "content": "9.11 and 9.8, which is greater?"}],
max_tokens: 2048,
stream: true,
frequency_penalty: 0,
presence_penalty: 0,
temperature: 0.5,
top_p: 1,
};
const client = new SageMakerRuntimeClient(AWS_SAGEMAKER_PARAM);
const command = new InvokeEndpointWithResponseStreamCommand({
EndpointName: ENDPIOINT_NAME,
Body: JSON.stringify(payload),
ContentType: "application/json",
});
const response = await client.send(command);
return formateOutput(response);
}
const formateOutput = (response) => {
return new ReadableStream({
async start(controller) {
let lastRawData = "";
for await (const chunk of response.Body) {
const decoded = new TextDecoder("utf-8")
.decode(chunk.PayloadPart.Bytes)
.trim();
if (decoded !== "data: [DONE]") {
if (decoded.startsWith("data: ")) {
if (lastRawData) {
controller.enqueue(new TextEncoder().encode(`${lastRawData}\n\n`));
lastRawData = decoded;
} else {
lastRawData = decoded;
}
} else {
lastRawData += decoded;
}
} else {
controller.enqueue(new TextEncoder().encode(`${lastRawData}\n\n${decoded}`));
}
}
controller.close();
},
});
};
const response: any = await requestSagemakerDS();
return new NextResponse(response, {
headers: {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache", // 可选:防止缓存
Connection: "keep-alive", // 可选:保持连接
},
});
在 API 层,我们通过设置响应头 Content-Type: "text/event-stream"
来支持 SSE(Server-Sent Events)格式的流式响应,这种格式能够让前端实时接收到数据更新,同时避免了轮询带来的性能开销:
// 外层handle调用
export const runtime = "nodejs"; // 重要
const response = await requestSagemakerDS();
return new NextResponse(response, {
headers: {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
},
});
DeepSeek 等大语言模型的推理过程中,除了最终内容输出外,还支持额外的 reasoning_content
字段,帮助用户理解模型的推理过程,进一步增强 AI 服务的透明度。例如:(\n\n 两个换行符代表一次 OutPut Event)
data: {"choices":[{"delta":{"reasoning_content":"interaction"}}]}
data: {"choices":[{"delta":{"reasoning_content":".\n"}}]}
data: {"choices":[{"delta":{"content":"Hi"}}]}
data: [DONE]
目前,主流 ChatBot 应用都采用 text/event-stream
类型的响应格式(SSE,Server-Sent Events),原因包括:
示例响应:
Content-Type: text/event-stream
Cache-Control: no-cache
Connection: keep-alive
数据流持续推送...
本文方案通过 vLLM 和 SageMaker Endpoint,在某逊快速部署 DeepSeek 模型,并结合 NextChat 的改造,开发了一个高效、安全、实时的 AI ChatBot 应用,显著提升了企业 AI 落地的能力。欢迎开发者和企业实践以上技术方案,打造更高效、更智能的 AI 服务。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。