Compare commits

...

2 Commits

Author SHA1 Message Date
glay
afbf5eb541 修改: .env.template
修改:     app/api/auth.ts
	修改:     app/api/bedrock.ts
	修改:     app/client/api.ts
	修改:     app/client/platforms/bedrock.ts
	修改:     app/components/settings.tsx
	修改:     app/config/server.ts
	修改:     app/constant.t
2024-11-05 14:27:52 +08:00
glay
0f276f59bb 修改: app/client/platforms/bedrock.ts 2024-11-05 10:34:33 +08:00
9 changed files with 409 additions and 489 deletions

View File

@@ -66,4 +66,9 @@ ANTHROPIC_API_VERSION=
ANTHROPIC_URL=
### (optional)
WHITE_WEBDAV_ENDPOINTS=
WHITE_WEBDAV_ENDPOINTS=
### bedrock (optional)
AWS_REGION=
AWS_ACCESS_KEY=
AWS_SECRET_KEY=

View File

@@ -54,18 +54,18 @@ export function auth(req: NextRequest, modelProvider: ModelProvider) {
}
// Special handling for Bedrock
if (modelProvider === ModelProvider.Bedrock) {
const region = req.headers.get("X-Region");
const accessKeyId = req.headers.get("X-Access-Key");
const secretKey = req.headers.get("X-Secret-Key");
const region = serverConfig.awsRegion;
const accessKeyId = serverConfig.awsAccessKey;
const secretAccessKey = serverConfig.awsSecretKey;
console.log("[Auth] Bedrock credentials:", {
region,
accessKeyId: accessKeyId ? "***" : undefined,
secretKey: secretKey ? "***" : undefined,
secretKey: secretAccessKey ? "***" : undefined,
});
// Check if AWS credentials are provided
if (!region || !accessKeyId || !secretKey) {
if (!region || !accessKeyId || !secretAccessKey) {
return {
error: true,
msg: "Missing AWS credentials. Please configure Region, Access Key ID, and Secret Access Key in settings.",

View File

@@ -1,7 +1,6 @@
import { ModelProvider } from "../constant";
import { getServerSideConfig } from "../config/server";
import { prettyObject } from "../utils/format";
import { NextRequest, NextResponse } from "next/server";
import { auth } from "./auth";
import {
BedrockRuntimeClient,
ConverseStreamCommand,
@@ -16,6 +15,15 @@ import {
type ToolResultContentBlock,
} from "@aws-sdk/client-bedrock-runtime";
// 解密函数
function decrypt(str: string): string {
try {
return Buffer.from(str, "base64").toString().split("").reverse().join("");
} catch {
return "";
}
}
// Constants and Types
const ALLOWED_PATH = new Set(["converse"]);
@@ -92,26 +100,6 @@ type DocumentFormat =
| "txt"
| "md";
// Validation Functions
function validateModelId(modelId: string): string | null {
if (
modelId.startsWith("meta.llama") &&
!modelId.includes("inference-profile")
) {
return "Llama models require an inference profile. Please use the full inference profile ARN.";
}
return null;
}
function validateDocumentSize(base64Data: string): boolean {
const sizeInBytes = (base64Data.length * 3) / 4;
const maxSize = 4.5 * 1024 * 1024;
if (sizeInBytes > maxSize) {
throw new Error("Document size exceeds 4.5 MB limit");
}
return true;
}
function validateImageSize(base64Data: string): boolean {
const sizeInBytes = (base64Data.length * 3) / 4;
const maxSize = 3.75 * 1024 * 1024;
@@ -147,21 +135,6 @@ function convertContentToAWSBlock(item: ContentItem): ContentBlock | null {
}
}
if (item.type === "document" && item.document) {
validateDocumentSize(item.document.source.bytes);
return {
document: {
format: item.document.format,
name: item.document.name,
source: {
bytes: Uint8Array.from(
Buffer.from(item.document.source.bytes, "base64"),
),
},
},
};
}
if (item.type === "tool_use" && item.tool_use) {
return {
toolUse: {
@@ -373,15 +346,48 @@ export async function handle(
);
}
const authResult = auth(req, ModelProvider.Bedrock);
if (authResult.error) {
return NextResponse.json(authResult, {
status: 401,
});
const serverConfig = getServerSideConfig();
// 首先尝试使用环境变量中的凭证
let region = serverConfig.awsRegion;
let accessKeyId = serverConfig.awsAccessKey;
let secretAccessKey = serverConfig.awsSecretKey;
let sessionToken = undefined;
// 如果环境变量中没有配置,则尝试使用前端传来的加密凭证
if (!region || !accessKeyId || !secretAccessKey) {
// 解密前端传来的凭证
region = decrypt(req.headers.get("X-Region") ?? "");
accessKeyId = decrypt(req.headers.get("X-Access-Key") ?? "");
secretAccessKey = decrypt(req.headers.get("X-Secret-Key") ?? "");
sessionToken = req.headers.get("X-Session-Token")
? decrypt(req.headers.get("X-Session-Token") ?? "")
: undefined;
}
if (!region || !accessKeyId || !secretAccessKey) {
return NextResponse.json(
{
error: true,
msg: "AWS credentials not found in environment variables or request headers",
},
{
status: 401,
},
);
}
try {
const response = await handleConverseRequest(req);
const client = new BedrockRuntimeClient({
region,
credentials: {
accessKeyId,
secretAccessKey,
sessionToken,
},
});
const response = await handleConverseRequest(req, client);
return response;
} catch (e) {
console.error("[Bedrock] ", e);
@@ -396,42 +402,14 @@ export async function handle(
}
}
async function handleConverseRequest(req: NextRequest) {
const region = req.headers.get("X-Region") || "us-west-2";
const accessKeyId = req.headers.get("X-Access-Key") || "";
const secretAccessKey = req.headers.get("X-Secret-Key") || "";
const sessionToken = req.headers.get("X-Session-Token");
if (!accessKeyId || !secretAccessKey) {
return NextResponse.json(
{
error: true,
message: "Missing AWS credentials",
},
{
status: 401,
},
);
}
const client = new BedrockRuntimeClient({
region,
credentials: {
accessKeyId,
secretAccessKey,
sessionToken: sessionToken || undefined,
},
});
async function handleConverseRequest(
req: NextRequest,
client: BedrockRuntimeClient,
) {
try {
const body = (await req.json()) as ConverseRequest;
const { modelId } = body;
const validationError = validateModelId(modelId);
if (validationError) {
throw new Error(validationError);
}
console.log("[Bedrock] Invoking model:", modelId);
const command = new ConverseStreamCommand(formatRequestBody(body));
@@ -455,8 +433,9 @@ async function handleConverseRequest(req: NextRequest) {
if ("messageStart" in output && output.messageStart?.role) {
controller.enqueue(
`data: ${JSON.stringify({
type: "messageStart",
role: output.messageStart.role,
stream: {
messageStart: { role: output.messageStart.role },
},
})}\n\n`,
);
} else if (
@@ -465,9 +444,13 @@ async function handleConverseRequest(req: NextRequest) {
) {
controller.enqueue(
`data: ${JSON.stringify({
type: "contentBlockStart",
index: output.contentBlockStart.contentBlockIndex,
start: output.contentBlockStart.start,
stream: {
contentBlockStart: {
contentBlockIndex:
output.contentBlockStart.contentBlockIndex,
start: output.contentBlockStart.start,
},
},
})}\n\n`,
);
} else if (
@@ -477,15 +460,30 @@ async function handleConverseRequest(req: NextRequest) {
if ("text" in output.contentBlockDelta.delta) {
controller.enqueue(
`data: ${JSON.stringify({
type: "text",
content: output.contentBlockDelta.delta.text,
stream: {
contentBlockDelta: {
delta: { text: output.contentBlockDelta.delta.text },
contentBlockIndex:
output.contentBlockDelta.contentBlockIndex,
},
},
})}\n\n`,
);
} else if ("toolUse" in output.contentBlockDelta.delta) {
controller.enqueue(
`data: ${JSON.stringify({
type: "toolUse",
input: output.contentBlockDelta.delta.toolUse?.input,
stream: {
contentBlockDelta: {
delta: {
toolUse: {
input:
output.contentBlockDelta.delta.toolUse?.input,
},
},
contentBlockIndex:
output.contentBlockDelta.contentBlockIndex,
},
},
})}\n\n`,
);
}
@@ -495,26 +493,36 @@ async function handleConverseRequest(req: NextRequest) {
) {
controller.enqueue(
`data: ${JSON.stringify({
type: "contentBlockStop",
index: output.contentBlockStop.contentBlockIndex,
stream: {
contentBlockStop: {
contentBlockIndex:
output.contentBlockStop.contentBlockIndex,
},
},
})}\n\n`,
);
} else if ("messageStop" in output && output.messageStop) {
controller.enqueue(
`data: ${JSON.stringify({
type: "messageStop",
stopReason: output.messageStop.stopReason,
additionalModelResponseFields:
output.messageStop.additionalModelResponseFields,
stream: {
messageStop: {
stopReason: output.messageStop.stopReason,
additionalModelResponseFields:
output.messageStop.additionalModelResponseFields,
},
},
})}\n\n`,
);
} else if ("metadata" in output && output.metadata) {
controller.enqueue(
`data: ${JSON.stringify({
type: "metadata",
usage: output.metadata.usage,
metrics: output.metadata.metrics,
trace: output.metadata.trace,
stream: {
metadata: {
usage: output.metadata.usage,
metrics: output.metadata.metrics,
trace: output.metadata.trace,
},
},
})}\n\n`,
);
}
@@ -522,14 +530,17 @@ async function handleConverseRequest(req: NextRequest) {
controller.close();
} catch (error) {
const errorResponse = {
type: "error",
error:
error instanceof Error ? error.constructor.name : "UnknownError",
message: error instanceof Error ? error.message : "Unknown error",
...(error instanceof ModelStreamErrorException && {
originalStatusCode: error.originalStatusCode,
originalMessage: error.originalMessage,
}),
stream: {
error:
error instanceof Error
? error.constructor.name
: "UnknownError",
message: error instanceof Error ? error.message : "Unknown error",
...(error instanceof ModelStreamErrorException && {
originalStatusCode: error.originalStatusCode,
originalMessage: error.originalMessage,
}),
},
};
controller.enqueue(`data: ${JSON.stringify(errorResponse)}\n\n`);
controller.close();

View File

@@ -261,7 +261,7 @@ export function getHeaders(ignoreHeaders: boolean = false) {
const apiKey = isGoogle
? accessStore.googleApiKey
: isBedrock
? accessStore.awsAccessKeyId // Use AWS access key for Bedrock
? accessStore.awsAccessKey // Use AWS access key for Bedrock
: isAzure
? accessStore.azureApiKey
: isAnthropic
@@ -322,12 +322,15 @@ export function getHeaders(ignoreHeaders: boolean = false) {
const authHeader = getAuthHeader();
if (isBedrock) {
// Add AWS credentials for Bedrock
headers["X-Region"] = accessStore.awsRegion;
headers["X-Access-Key"] = accessStore.awsAccessKeyId;
headers["X-Secret-Key"] = accessStore.awsSecretAccessKey;
// 简单加密 AWS credentials
const encrypt = (str: string) =>
Buffer.from(str.split("").reverse().join("")).toString("base64");
headers["X-Region"] = encrypt(accessStore.awsRegion);
headers["X-Access-Key"] = encrypt(accessStore.awsAccessKey);
headers["X-Secret-Key"] = encrypt(accessStore.awsSecretKey);
if (accessStore.awsSessionToken) {
headers["X-Session-Token"] = accessStore.awsSessionToken;
headers["X-Session-Token"] = encrypt(accessStore.awsSessionToken);
}
} else {
const bearerToken = getBearerToken(

View File

@@ -3,22 +3,48 @@ import {
ChatOptions,
getHeaders,
LLMApi,
LLMModel,
LLMUsage,
MultimodalContent,
SpeechOptions,
} from "../api";
import { useAccessStore, useAppConfig } from "../../store";
import Locale from "../../locales";
import {
getMessageImages,
getMessageTextContent,
isVisionModel,
} from "../../utils";
useAppConfig,
usePluginStore,
useChatStore,
ChatMessageTool,
} from "../../store";
import { getMessageTextContent, isVisionModel } from "../../utils";
import { fetch } from "../../utils/stream";
import { preProcessImageContent, stream } from "../../utils/chat";
const MAX_IMAGE_SIZE = 1024 * 1024 * 4; // 4MB limit
export type MultiBlockContent = {
type: "image" | "text";
source?: {
type: string;
media_type: string;
data: string;
};
text?: string;
};
export type AnthropicMessage = {
role: (typeof ClaudeMapper)[keyof typeof ClaudeMapper];
content: string | MultiBlockContent[];
};
const ClaudeMapper = {
assistant: "assistant",
user: "user",
system: "user",
} as const;
export class BedrockApi implements LLMApi {
usage(): Promise<LLMUsage> {
throw new Error("Method not implemented.");
}
models(): Promise<LLMModel[]> {
throw new Error("Method not implemented.");
}
speech(options: SpeechOptions): Promise<ArrayBuffer> {
throw new Error("Speech not implemented for Bedrock.");
}
@@ -31,358 +57,245 @@ export class BedrockApi implements LLMApi {
return res;
}
async processDocument(
file: File,
): Promise<{ display: string; content: MultimodalContent }> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = async () => {
try {
const arrayBuffer = reader.result as ArrayBuffer;
const format = file.name.split(".").pop()?.toLowerCase();
if (!format) {
throw new Error("Could not determine file format");
}
// Format file size
const size = file.size;
let sizeStr = "";
if (size < 1024) {
sizeStr = size + " B";
} else if (size < 1024 * 1024) {
sizeStr = (size / 1024).toFixed(2) + " KB";
} else {
sizeStr = (size / (1024 * 1024)).toFixed(2) + " MB";
}
// Create display text
const displayText = `Document: ${file.name} (${sizeStr})`;
// Create actual content
const content: MultimodalContent = {
type: "document",
document: {
format: format as
| "pdf"
| "csv"
| "doc"
| "docx"
| "xls"
| "xlsx"
| "html"
| "txt"
| "md",
name: file.name,
source: {
bytes: Buffer.from(arrayBuffer).toString("base64"),
},
},
};
resolve({
display: displayText,
content: content,
});
} catch (e) {
reject(e);
}
};
reader.onerror = () => reject(reader.error);
reader.readAsArrayBuffer(file);
});
}
async processImage(url: string): Promise<MultimodalContent> {
if (url.startsWith("data:")) {
const base64Match = url.match(/^data:image\/([a-zA-Z]*);base64,([^"]*)/);
if (base64Match) {
const format = base64Match[1].toLowerCase();
const base64Data = base64Match[2];
// Check base64 size
const binarySize = atob(base64Data).length;
if (binarySize > MAX_IMAGE_SIZE) {
throw new Error(
`Image size (${(binarySize / (1024 * 1024)).toFixed(
2,
)}MB) exceeds maximum allowed size of 4MB`,
);
}
return {
type: "image_url",
image_url: {
url: url,
},
};
}
throw new Error("Invalid data URL format");
}
// For non-data URLs, fetch and convert to base64
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch image: ${response.statusText}`);
}
const blob = await response.blob();
if (blob.size > MAX_IMAGE_SIZE) {
throw new Error(
`Image size (${(blob.size / (1024 * 1024)).toFixed(
2,
)}MB) exceeds maximum allowed size of 4MB`,
);
}
const reader = new FileReader();
const base64 = await new Promise<string>((resolve, reject) => {
reader.onloadend = () => resolve(reader.result as string);
reader.onerror = () => reject(new Error("Failed to read image data"));
reader.readAsDataURL(blob);
});
return {
type: "image_url",
image_url: {
url: base64,
},
};
} catch (error) {
console.error("[Bedrock] Image processing error:", error);
throw error;
}
}
async chat(options: ChatOptions): Promise<void> {
const accessStore = useAccessStore.getState();
const visionModel = isVisionModel(options.config.model);
const shouldStream = !!options.config.stream;
const modelConfig = {
...useAppConfig.getState().modelConfig,
...options.config,
...useChatStore.getState().currentSession().mask.modelConfig,
...{
model: options.config.model,
},
};
if (
!accessStore.awsRegion ||
!accessStore.awsAccessKeyId ||
!accessStore.awsSecretAccessKey
) {
console.log("AWS credentials are not set");
let responseText = "";
const responseTexts = [responseText];
responseTexts.push(Locale.Error.Unauthorized);
responseText = responseTexts.join("\n\n");
options.onFinish(responseText);
return;
// try get base64image from local cache image_url
const messages: ChatOptions["messages"] = [];
for (const v of options.messages) {
const content = await preProcessImageContent(v.content);
messages.push({ role: v.role, content });
}
const keys = ["system", "user"];
// roles must alternate between "user" and "assistant" in claude, so add a fake assistant message between two user messages
for (let i = 0; i < messages.length - 1; i++) {
const message = messages[i];
const nextMessage = messages[i + 1];
if (keys.includes(message.role) && keys.includes(nextMessage.role)) {
messages[i] = [
message,
{
role: "assistant",
content: ";",
},
] as any;
}
}
const prompt = messages
.flat()
.filter((v) => {
if (!v.content) return false;
if (typeof v.content === "string" && !v.content.trim()) return false;
return true;
})
.map((v) => {
const { role, content } = v;
const insideRole = ClaudeMapper[role] ?? "user";
if (!visionModel || typeof content === "string") {
return {
role: insideRole,
content: getMessageTextContent(v),
};
}
return {
role: insideRole,
content: content
.filter((v) => v.image_url || v.text)
.map(({ type, text, image_url }) => {
if (type === "text") {
return {
type,
text: text!,
};
}
const { url = "" } = image_url || {};
const colonIndex = url.indexOf(":");
const semicolonIndex = url.indexOf(";");
const comma = url.indexOf(",");
const mimeType = url.slice(colonIndex + 1, semicolonIndex);
const encodeType = url.slice(semicolonIndex + 1, comma);
const data = url.slice(comma + 1);
return {
type: "image" as const,
source: {
type: encodeType,
media_type: mimeType,
data,
},
};
}),
};
});
if (prompt[0]?.role === "assistant") {
prompt.unshift({
role: "user",
content: ";",
});
}
const [tools, funcs] = usePluginStore
.getState()
.getAsTools(useChatStore.getState().currentSession().mask?.plugin || []);
const requestBody = {
modelId: options.config.model,
messages: messages.filter((msg) => msg.content.length > 0),
inferenceConfig: {
maxTokens: modelConfig.max_tokens,
temperature: modelConfig.temperature,
topP: modelConfig.top_p,
stopSequences: [],
},
toolConfig:
Array.isArray(tools) && tools.length > 0
? {
tools: tools.map((tool: any) => ({
toolSpec: {
name: tool?.function?.name,
description: tool?.function?.description,
inputSchema: {
json: tool?.function?.parameters,
},
},
})),
toolChoice: { auto: {} },
}
: undefined,
};
const conversePath = `${ApiPath.Bedrock}/converse`;
const controller = new AbortController();
options.onController?.(controller);
const headers: Record<string, string> = {
...getHeaders(),
"X-Region": accessStore.awsRegion,
"X-Access-Key": accessStore.awsAccessKeyId,
"X-Secret-Key": accessStore.awsSecretAccessKey,
};
if (shouldStream) {
let currentToolUse: ChatMessageTool | null = null;
return stream(
conversePath,
requestBody,
getHeaders(),
Array.isArray(tools)
? tools.map((tool: any) => ({
name: tool?.function?.name,
description: tool?.function?.description,
input_schema: tool?.function?.parameters,
}))
: [],
funcs,
controller,
// parseSSE
(text: string, runTools: ChatMessageTool[]) => {
const parsed = JSON.parse(text);
const event = parsed.stream;
if (accessStore.awsSessionToken) {
headers["X-Session-Token"] = accessStore.awsSessionToken;
}
try {
// Process messages to handle multimodal content
const messages = await Promise.all(
options.messages.map(async (msg) => {
if (Array.isArray(msg.content)) {
// For vision models, include both text and images
if (isVisionModel(options.config.model)) {
const images = getMessageImages(msg);
const content: MultimodalContent[] = [];
// Process documents first
for (const item of msg.content) {
// Check for document content
if (item && typeof item === "object") {
if ("file" in item && item.file instanceof File) {
try {
console.log(
"[Bedrock] Processing document:",
item.file.name,
);
const { content: docContent } =
await this.processDocument(item.file);
content.push(docContent);
} catch (e) {
console.error("[Bedrock] Failed to process document:", e);
}
} else if ("document" in item && item.document) {
// If document content is already processed, include it directly
content.push(item as MultimodalContent);
}
}
}
// Add text content if it's not a document display text
const text = getMessageTextContent(msg);
if (text && !text.startsWith("Document: ")) {
content.push({ type: "text", text });
}
// Process images with size check and error handling
for (const url of images) {
try {
const imageContent = await this.processImage(url);
content.push(imageContent);
} catch (e) {
console.error("[Bedrock] Failed to process image:", e);
// Add error message as text content
content.push({
type: "text",
text: `Error processing image: ${e}`,
});
}
}
// Only return content if there is any
if (content.length > 0) {
return { ...msg, content };
}
}
// For non-vision models, only include text
return { ...msg, content: getMessageTextContent(msg) };
if (!event) {
console.warn("[Bedrock] Unexpected event format:", parsed);
return "";
}
return msg;
}),
);
// Filter out empty messages
const filteredMessages = messages.filter((msg) => {
if (Array.isArray(msg.content)) {
return msg.content.length > 0;
}
return msg.content !== "";
});
if (event.messageStart) {
return "";
}
const requestBody = {
messages: filteredMessages,
modelId: options.config.model,
inferenceConfig: {
maxTokens: modelConfig.max_tokens,
temperature: modelConfig.temperature,
topP: modelConfig.top_p,
stopSequences: [],
if (event.contentBlockStart?.start?.toolUse) {
const { toolUseId, name } = event.contentBlockStart.start.toolUse;
currentToolUse = {
id: toolUseId,
type: "function",
function: {
name,
arguments: "",
},
};
runTools.push(currentToolUse);
return "";
}
if (event.contentBlockDelta?.delta?.text) {
return event.contentBlockDelta.delta.text;
}
if (
event.contentBlockDelta?.delta?.toolUse?.input &&
currentToolUse?.function
) {
currentToolUse.function.arguments +=
event.contentBlockDelta.delta.toolUse.input;
return "";
}
if (
event.internalServerException ||
event.modelStreamErrorException ||
event.validationException ||
event.throttlingException ||
event.serviceUnavailableException
) {
const errorMessage =
event.internalServerException?.message ||
event.modelStreamErrorException?.message ||
event.validationException?.message ||
event.throttlingException?.message ||
event.serviceUnavailableException?.message ||
"Unknown error";
throw new Error(errorMessage);
}
return "";
},
};
console.log(
"[Bedrock] Request body:",
JSON.stringify(
{
...requestBody,
messages: requestBody.messages.map((msg) => ({
...msg,
content: Array.isArray(msg.content)
? msg.content.map((c) => ({
type: c.type,
...(c.document
? {
document: {
format: c.document.format,
name: c.document.name,
},
}
: {}),
...(c.image_url ? { image_url: { url: "[BINARY]" } } : {}),
...(c.text ? { text: c.text } : {}),
}))
: msg.content,
// processToolMessage
(requestPayload: any, toolCallMessage: any, toolCallResult: any[]) => {
currentToolUse = null;
requestPayload?.messages?.splice(
requestPayload?.messages?.length,
0,
{
role: "assistant",
content: toolCallMessage.tool_calls.map(
(tool: ChatMessageTool) => ({
type: "tool_use",
id: tool.id,
name: tool?.function?.name,
input: tool?.function?.arguments
? JSON.parse(tool?.function?.arguments)
: {},
}),
),
},
...toolCallResult.map((result) => ({
role: "user",
content: [
{
type: "tool_result",
tool_use_id: result.tool_call_id,
content: result.content,
},
],
})),
},
null,
2,
),
);
},
options,
);
const shouldStream = !!options.config.stream;
const conversePath = `${ApiPath.Bedrock}/converse`;
if (shouldStream) {
let response = await fetch(conversePath, {
method: "POST",
headers: {
...headers,
"X-Stream": "true",
},
body: JSON.stringify(requestBody),
signal: controller.signal,
});
if (!response.ok) {
const error = await response.text();
throw new Error(`Bedrock API error: ${error}`);
}
let buffer = "";
const reader = response.body?.getReader();
if (!reader) {
throw new Error("No response body reader available");
}
let currentContent = "";
let isFirstMessage = true;
while (true) {
const { done, value } = await reader.read();
if (done) break;
// Convert the chunk to text and add to buffer
const chunk = new TextDecoder().decode(value);
buffer += chunk;
// Process complete messages from buffer
let newlineIndex;
while ((newlineIndex = buffer.indexOf("\n")) !== -1) {
const line = buffer.slice(0, newlineIndex).trim();
buffer = buffer.slice(newlineIndex + 1);
if (line.startsWith("data: ")) {
try {
const event = JSON.parse(line.slice(6));
if (event.type === "messageStart") {
if (isFirstMessage) {
isFirstMessage = false;
}
continue;
}
if (event.type === "text" && event.content) {
currentContent += event.content;
options.onUpdate?.(currentContent, event.content);
}
if (event.type === "messageStop") {
options.onFinish(currentContent);
return;
}
if (event.type === "error") {
throw new Error(event.message || "Unknown error");
}
} catch (e) {
console.error("[Bedrock] Failed to parse stream event:", e);
}
}
}
}
// If we reach here without a messageStop event, finish with current content
options.onFinish(currentContent);
} else {
} else {
try {
const response = await fetch(conversePath, {
method: "POST",
headers,
headers: getHeaders(),
body: JSON.stringify(requestBody),
signal: controller.signal,
});
@@ -395,23 +308,10 @@ export class BedrockApi implements LLMApi {
const responseBody = await response.json();
const content = this.extractMessage(responseBody);
options.onFinish(content);
} catch (e: any) {
console.error("[Bedrock] Chat error:", e);
throw e;
}
} catch (e) {
console.error("[Bedrock] Chat error:", e);
options.onError?.(e as Error);
}
}
async usage(): Promise<LLMUsage> {
// Bedrock usage is tracked through AWS billing
return {
used: 0,
total: 0,
};
}
async models() {
// Return empty array as models are configured through AWS console
return [];
}
}

View File

@@ -988,12 +988,12 @@ export function Settings() {
>
<PasswordInput
aria-label={Locale.Settings.Access.Bedrock.AccessKey.Title}
value={accessStore.awsAccessKeyId}
value={accessStore.awsAccessKey}
type="text"
placeholder={Locale.Settings.Access.Bedrock.AccessKey.Placeholder}
onChange={(e) => {
accessStore.update(
(access) => (access.awsAccessKeyId = e.currentTarget.value),
(access) => (access.awsAccessKey = e.currentTarget.value),
);
}}
/>
@@ -1004,12 +1004,12 @@ export function Settings() {
>
<PasswordInput
aria-label={Locale.Settings.Access.Bedrock.SecretKey.Title}
value={accessStore.awsSecretAccessKey}
value={accessStore.awsSecretKey}
type="text"
placeholder={Locale.Settings.Access.Bedrock.SecretKey.Placeholder}
onChange={(e) => {
accessStore.update(
(access) => (access.awsSecretAccessKey = e.currentTarget.value),
(access) => (access.awsSecretKey = e.currentTarget.value),
);
}}
/>

View File

@@ -13,8 +13,9 @@ declare global {
OPENAI_ORG_ID?: string; // openai only
// bedrock only
BEDROCK_URL?: string;
BEDROCK_REGION?: string;
BEDROCK_API_KEY?: string;
BEDROCK_API_SECRET?: string;
VERCEL?: string;
BUILD_MODE?: "standalone" | "export";
@@ -173,8 +174,9 @@ export const getServerSideConfig = () => {
openaiOrgId: process.env.OPENAI_ORG_ID,
isBedrock,
bedrockUrl: process.env.BEDROCK_URL,
bedrockApiKey: getApiKey(process.env.BEDROCK_API_KEY),
awsRegion: process.env.AWS_REGION,
awsAccessKey: process.env.AWS_ACCESS_KEY,
awsSecretKey: process.env.AWS_SECRET_KEY,
isStability,
stabilityUrl: process.env.STABILITY_URL,

View File

@@ -230,6 +230,10 @@ export const XAI = {
ChatPath: "v1/chat/completions",
};
export const Bedrock = {
ChatPath: "converse",
};
export const DEFAULT_INPUT_TEMPLATE = `{{input}}`; // input / time / model / lang
// export const DEFAULT_SYSTEM_TEMPLATE = `
// You are ChatGPT, a large language model trained by {{ServiceProvider}}.
@@ -312,9 +316,11 @@ const openaiModels = [
const bedrockModels = [
// Claude Models
"anthropic.claude-3-haiku-20240307-v1:0",
"anthropic.claude-3-5-haiku-20241022-v1:0",
"anthropic.claude-3-sonnet-20240229-v1:0",
"anthropic.claude-3-opus-20240229-v1:0",
"anthropic.claude-3-5-sonnet-20241022-v2:0",
"anthropic.claude-3-opus-20240229-v1:0",
// Meta Llama Models
"us.meta.llama3-2-11b-instruct-v1:0",
"us.meta.llama3-2-90b-instruct-v1:0",

View File

@@ -60,14 +60,11 @@ const DEFAULT_ACCESS_STATE = {
openaiApiKey: "",
// bedrock
bedrockUrl: DEFAULT_BEDROCK_URL,
bedrockApiKey: "",
awsRegion: "",
awsAccessKeyId: "",
awsSecretAccessKey: "",
awsAccessKey: "",
awsSecretKey: "",
awsSessionToken: "",
awsCognitoUser: false,
awsInferenceProfile: "", // Added inference profile field
// azure
azureUrl: "",
@@ -154,11 +151,7 @@ export const useAccessStore = createPersistStore(
},
isValidBedrock() {
return ensure(get(), [
"awsAccessKeyId",
"awsSecretAccessKey",
"awsRegion",
]);
return ensure(get(), ["awsAccessKey", "awsSecretKey", "awsRegion"]);
},
isValidAzure() {