Skip to content

集成 AWS Bedrock 作为新的 LLM 服务提供商 #6430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ DISABLE_FAST_LINK=
# (optional)
# Default: Empty
# To control custom models, use + to add a custom model, use - to hide a model, use name=displayName to customize model name, separated by comma.
CUSTOM_MODELS=
CUSTOM_MODELS=-all,+gpt-4o-2024-11-20@openai=gpt-4o,+gpt-4o-mini@openai,+us.anthropic.claude-3-5-sonnet-20241022-v2:0@bedrock=sonnet

# (optional)
# Default: Empty
Expand All @@ -81,3 +81,22 @@ SILICONFLOW_API_KEY=

### siliconflow Api url (optional)
SILICONFLOW_URL=

# --- AWS Bedrock Section ---
# Ensure these lines for keys either have placeholder values like below,
# are commented out entirely, or removed if they shouldn't be in the template.

# AWS Access Key for Bedrock API (Example: Use placeholder or comment out)
# AWS_ACCESS_KEY_ID=

# AWS Secret Access Key for Bedrock API (Example: Use placeholder or comment out)
# AWS_SECRET_ACCESS_KEY=

# AWS Region for Bedrock API (e.g. us-east-1, us-west-2)
AWS_REGION=

# Enable AWS Bedrock models (set to "true" to enable)
ENABLE_AWS_BEDROCK=

# Custom endpoint URL for AWS Bedrock (optional)
AWS_BEDROCK_ENDPOINT=
3 changes: 3 additions & 0 deletions app/api/[provider]/[...path]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { handle as deepseekHandler } from "../../deepseek";
import { handle as siliconflowHandler } from "../../siliconflow";
import { handle as xaiHandler } from "../../xai";
import { handle as chatglmHandler } from "../../glm";
import { handle as bedrockHandler } from "../../bedrock";
import { handle as proxyHandler } from "../../proxy";

async function handle(
Expand Down Expand Up @@ -50,6 +51,8 @@ async function handle(
return chatglmHandler(req, { params });
case ApiPath.SiliconFlow:
return siliconflowHandler(req, { params });
case ApiPath.Bedrock:
return bedrockHandler(req, { params });
case ApiPath.OpenAI:
return openaiHandler(req, { params });
default:
Expand Down
18 changes: 9 additions & 9 deletions app/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@ export function auth(req: NextRequest, modelProvider: ModelProvider) {
// if user does not provide an api key, inject system api key
if (!apiKey) {
const serverConfig = getServerSideConfig();

// const systemApiKey =
// modelProvider === ModelProvider.GeminiPro
// ? serverConfig.googleApiKey
// : serverConfig.isAzure
// ? serverConfig.azureApiKey
// : serverConfig.apiKey;

let systemApiKey: string | undefined;

switch (modelProvider) {
Expand Down Expand Up @@ -104,6 +96,11 @@ export function auth(req: NextRequest, modelProvider: ModelProvider) {
case ModelProvider.SiliconFlow:
systemApiKey = serverConfig.siliconFlowApiKey;
break;
case ModelProvider.Bedrock:
console.log(
"[Auth] Using AWS credentials for Bedrock, no API key override.",
);
return { error: false };
case ModelProvider.GPT:
Comment on lines +99 to 104
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Verify AWS credentials before returning.
This block immediately returns { error: false } for Bedrock, skipping an explicit credential validation step. Consider ensuring that AWS credential checks (e.g., presence of required environment variables) have definitively passed before returning success.

🧰 Tools
🪛 Biome (1.9.4)

[error] 104-104: Useless case clause.

because the default clause is present:

Unsafe fix: Remove the useless case.

(lint/complexity/noUselessSwitchCase)

default:
if (req.nextUrl.pathname.includes("azure/deployments")) {
Expand All @@ -117,7 +114,10 @@ export function auth(req: NextRequest, modelProvider: ModelProvider) {
console.log("[Auth] use system api key");
req.headers.set("Authorization", `Bearer ${systemApiKey}`);
} else {
console.log("[Auth] admin did not provide an api key");
console.log(
"[Auth] admin did not provide an api key for provider:",
modelProvider,
);
}
} else {
console.log("[Auth] use user api key");
Expand Down
249 changes: 249 additions & 0 deletions app/api/bedrock/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
import { ModelProvider, Bedrock as BedrockConfig } from "@/app/constant";
import { getServerSideConfig } from "@/app/config/server";
import { prettyObject } from "@/app/utils/format";
import { NextRequest, NextResponse } from "next/server";
import { auth } from "../auth";
import {
BedrockRuntimeClient,
InvokeModelWithResponseStreamCommand,
InvokeModelCommand,
} from "@aws-sdk/client-bedrock-runtime";

const ALLOWED_PATH = new Set([BedrockConfig.ChatPath]);

// Helper to get AWS Credentials
function getAwsCredentials() {
const config = getServerSideConfig();
if (!config.isBedrock) {
throw new Error(
"AWS Bedrock is not configured properly (ENABLE_AWS_BEDROCK is not true)",
);
}
if (!config.bedrockAccessKeyId) {
throw new Error("AWS Bedrock Access Key ID is missing or empty.");
}
if (!config.bedrockSecretAccessKey) {
throw new Error("AWS Bedrock Secret Access Key is missing or empty.");
}
return {
accessKeyId: config.bedrockAccessKeyId as string,
secretAccessKey: config.bedrockSecretAccessKey as string,
};
}

export async function handle(
req: NextRequest,
{ params }: { params: { path: string[] } },
) {
console.log("[Bedrock Route] params ", params);

if (req.method === "OPTIONS") {
return NextResponse.json({ body: "OK" }, { status: 200 });
}

const subpath = params.path.join("/");

if (!ALLOWED_PATH.has(subpath)) {
console.log("[Bedrock Route] forbidden path ", subpath);
return NextResponse.json(
{ error: true, msg: "you are not allowed to request " + subpath },
{ status: 403 },
);
}

// Auth check specifically for Bedrock (might not need header API key)
const authResult = auth(req, ModelProvider.Bedrock);
if (authResult.error) {
return NextResponse.json(authResult, { status: 401 });
}

try {
const config = getServerSideConfig();

const bedrockRegion = config.bedrockRegion as string;
const bedrockEndpoint = config.bedrockEndpoint;

const client = new BedrockRuntimeClient({
region: bedrockRegion,
credentials: getAwsCredentials(),
endpoint: bedrockEndpoint || undefined,
});

const body = await req.json();
console.log(
"[Bedrock] Request - Model:",
body.model,
"Stream:",
body.stream,
"Messages count:",
body.messages.length,
);

const {
messages,
model,
stream = false,
temperature = 0.7,
max_tokens,
} = body;

// --- Payload formatting for Claude on Bedrock ---
const isClaudeModel = model.includes("anthropic.claude");
if (!isClaudeModel) {
return NextResponse.json(
{ error: true, msg: "Unsupported Bedrock model: " + model },
{ status: 400 },
);
}

const systemPrompts = messages.filter((msg: any) => msg.role === "system");
const userAssistantMessages = messages.filter(
(msg: any) => msg.role !== "system",
);

const payload = {
anthropic_version: "bedrock-2023-05-31",
max_tokens: max_tokens || 4096,
temperature: temperature,
messages: userAssistantMessages.map((msg: any) => ({
role: msg.role, // 'user' or 'assistant'
content:
typeof msg.content === "string"
? [{ type: "text", text: msg.content }]
: msg.content, // Assuming MultimodalContent format is compatible
})),
...(systemPrompts.length > 0 && {
system: systemPrompts.map((msg: any) => msg.content).join("\n"),
}),
};
// --- End Payload Formatting ---

if (stream) {
const command = new InvokeModelWithResponseStreamCommand({
modelId: model,
contentType: "application/json",
accept: "application/json",
body: JSON.stringify(payload),
});
const response = await client.send(command);

if (!response.body) {
throw new Error("Empty response stream from Bedrock");
}
const responseBody = response.body;

const encoder = new TextEncoder();
const decoder = new TextDecoder();
const readableStream = new ReadableStream({
async start(controller) {
try {
for await (const event of responseBody) {
if (event.chunk?.bytes) {
const chunkData = JSON.parse(decoder.decode(event.chunk.bytes));
let responseText = "";
let finishReason: string | null = null;

if (
chunkData.type === "content_block_delta" &&
chunkData.delta.type === "text_delta"
) {
responseText = chunkData.delta.text || "";
} else if (chunkData.type === "message_stop") {
finishReason =
chunkData["amazon-bedrock-invocationMetrics"]
?.outputTokenCount > 0
? "stop"
: "length"; // Example logic
}

// Format as OpenAI SSE chunk
const sseData = {
id: `chatcmpl-${nanoid()}`,
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1000),
model: model,
choices: [
{
index: 0,
delta: { content: responseText },
finish_reason: finishReason,
},
],
};
controller.enqueue(
encoder.encode(`data: ${JSON.stringify(sseData)}\n\n`),
);

if (finishReason) {
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
break; // Exit loop after stop message
}
}
}
} catch (error) {
console.error("[Bedrock] Streaming error:", error);
controller.error(error);
} finally {
controller.close();
}
},
});

return new NextResponse(readableStream, {
headers: {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
},
});
} else {
Comment on lines +121 to +199
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Handle partial chunk scenarios.
While streaming data, a partial chunk may be returned from Bedrock if the chunk boundaries don't align with JSON structures. Consider buffering incomplete segments before parsing to prevent JSON parse errors.

// Non-streaming response
const command = new InvokeModelCommand({
modelId: model,
contentType: "application/json",
accept: "application/json",
body: JSON.stringify(payload),
});
const response = await client.send(command);
const responseBody = JSON.parse(new TextDecoder().decode(response.body));

// Format response to match OpenAI
const formattedResponse = {
id: `chatcmpl-${nanoid()}`,
object: "chat.completion",
created: Math.floor(Date.now() / 1000),
model: model,
choices: [
{
index: 0,
message: {
role: "assistant",
content: responseBody.content?.[0]?.text ?? "",
},
finish_reason: "stop", // Assuming stop for non-streamed
},
],
usage: {
prompt_tokens:
responseBody["amazon-bedrock-invocationMetrics"]?.inputTokenCount ??
-1,
completion_tokens:
responseBody["amazon-bedrock-invocationMetrics"]
?.outputTokenCount ?? -1,
total_tokens:
(responseBody["amazon-bedrock-invocationMetrics"]
?.inputTokenCount ?? 0) +
(responseBody["amazon-bedrock-invocationMetrics"]
?.outputTokenCount ?? 0) || -1,
},
};
return NextResponse.json(formattedResponse);
}
} catch (e) {
console.error("[Bedrock] API Handler Error:", e);
return NextResponse.json(prettyObject(e), { status: 500 });
}
}

// Need nanoid for unique IDs
import { nanoid } from "nanoid";
Loading