修改: app/api/bedrock.ts

修改:     app/client/platforms/bedrock.ts
	修改:     app/constant.ts
This commit is contained in:
glay 2024-11-22 06:33:39 +08:00
parent f60c237b16
commit bd68df1d9b
3 changed files with 309 additions and 78 deletions

View File

@ -7,32 +7,117 @@ function parseEventData(chunk: Uint8Array): any {
const decoder = new TextDecoder(); const decoder = new TextDecoder();
const text = decoder.decode(chunk); const text = decoder.decode(chunk);
try { try {
return JSON.parse(text); const parsed = JSON.parse(text);
// AWS Bedrock wraps the response in a 'body' field
if (typeof parsed.body === "string") {
try {
return JSON.parse(parsed.body);
} catch (e) {
return { output: parsed.body };
}
}
return parsed.body || parsed;
} catch (e) { } catch (e) {
console.error("Error parsing event data:", e);
try { try {
// Handle base64 encoded responses
const base64Match = text.match(/:"([A-Za-z0-9+/=]+)"/); const base64Match = text.match(/:"([A-Za-z0-9+/=]+)"/);
if (base64Match) { if (base64Match) {
const decoded = Buffer.from(base64Match[1], "base64").toString("utf-8"); const decoded = Buffer.from(base64Match[1], "base64").toString("utf-8");
return JSON.parse(decoded); try {
return JSON.parse(decoded);
} catch (e) {
return { output: decoded };
}
} }
// Handle event-type responses
const eventMatch = text.match(/:event-type[^\{]+({.*})/); const eventMatch = text.match(/:event-type[^\{]+({.*})/);
if (eventMatch) { if (eventMatch) {
return JSON.parse(eventMatch[1]); try {
return JSON.parse(eventMatch[1]);
} catch (e) {
return { output: eventMatch[1] };
}
} }
} catch (innerError) {}
// Handle plain text responses
if (text.trim()) {
// Clean up any malformed JSON characters
const cleanText = text.replace(/[\x00-\x1F\x7F-\x9F]/g, "");
return { output: cleanText };
}
} catch (innerError) {
console.error("Error in fallback parsing:", innerError);
}
} }
return null; return null;
} }
async function* transformBedrockStream(stream: ReadableStream) { async function* transformBedrockStream(
stream: ReadableStream,
modelId: string,
) {
const reader = stream.getReader(); const reader = stream.getReader();
let buffer = "";
try { try {
while (true) { while (true) {
const { done, value } = await reader.read(); const { done, value } = await reader.read();
if (done) break; if (done) {
if (buffer) {
yield `data: ${JSON.stringify({
delta: { text: buffer },
})}\n\n`;
}
break;
}
const parsed = parseEventData(value); const parsed = parseEventData(value);
if (parsed) { if (!parsed) continue;
console.log("Parsed response:", JSON.stringify(parsed, null, 2));
// Handle Titan models
if (modelId.startsWith("amazon.titan")) {
const text = parsed.outputText || "";
if (text) {
yield `data: ${JSON.stringify({
delta: { text },
})}\n\n`;
}
}
// Handle LLaMA3 models
else if (modelId.startsWith("us.meta.llama3")) {
let text = "";
if (parsed.generation) {
text = parsed.generation;
} else if (parsed.output) {
text = parsed.output;
} else if (typeof parsed === "string") {
text = parsed;
}
if (text) {
// Clean up any control characters or invalid JSON characters
text = text.replace(/[\x00-\x1F\x7F-\x9F]/g, "");
yield `data: ${JSON.stringify({
delta: { text },
})}\n\n`;
}
}
// Handle Mistral models
else if (modelId.startsWith("mistral.mistral")) {
const text =
parsed.output || parsed.outputs?.[0]?.text || parsed.completion || "";
if (text) {
yield `data: ${JSON.stringify({
delta: { text },
})}\n\n`;
}
}
// Handle Claude models
else if (modelId.startsWith("anthropic.claude")) {
if (parsed.type === "content_block_delta") { if (parsed.type === "content_block_delta") {
if (parsed.delta?.type === "text_delta") { if (parsed.delta?.type === "text_delta") {
yield `data: ${JSON.stringify({ yield `data: ${JSON.stringify({
@ -66,6 +151,8 @@ async function* transformBedrockStream(stream: ReadableStream) {
function validateRequest(body: any, modelId: string): void { function validateRequest(body: any, modelId: string): void {
if (!modelId) throw new Error("Model ID is required"); if (!modelId) throw new Error("Model ID is required");
const bodyContent = body.body || body;
if (modelId.startsWith("anthropic.claude")) { if (modelId.startsWith("anthropic.claude")) {
if ( if (
!body.anthropic_version || !body.anthropic_version ||
@ -82,13 +169,14 @@ function validateRequest(body: any, modelId: string): void {
} else if (typeof body.prompt !== "string") { } else if (typeof body.prompt !== "string") {
throw new Error("prompt is required for Claude 2 and earlier"); throw new Error("prompt is required for Claude 2 and earlier");
} }
} else if (modelId.startsWith("meta.llama")) { } else if (modelId.startsWith("us.meta.llama3")) {
if (!body.prompt) throw new Error("Llama requires a prompt"); if (!bodyContent.prompt) {
throw new Error("prompt is required for LLaMA3 models");
}
} else if (modelId.startsWith("mistral.mistral")) { } else if (modelId.startsWith("mistral.mistral")) {
if (!Array.isArray(body.messages)) if (!bodyContent.prompt) throw new Error("Mistral requires a prompt");
throw new Error("Mistral requires a messages array");
} else if (modelId.startsWith("amazon.titan")) { } else if (modelId.startsWith("amazon.titan")) {
if (!body.inputText) throw new Error("Titan requires inputText"); if (!bodyContent.inputText) throw new Error("Titan requires inputText");
} }
} }
@ -114,14 +202,35 @@ async function requestBedrock(req: NextRequest) {
throw new Error("Failed to decrypt AWS credentials"); throw new Error("Failed to decrypt AWS credentials");
} }
const endpoint = `https://bedrock-runtime.${awsRegion}.amazonaws.com/model/${modelId}/invoke-with-response-stream`; // Construct the base endpoint
const baseEndpoint = `https://bedrock-runtime.${awsRegion}.amazonaws.com`;
// Set up timeout
const timeoutId = setTimeout(() => controller.abort(), 10 * 60 * 1000); const timeoutId = setTimeout(() => controller.abort(), 10 * 60 * 1000);
try { try {
// Determine the endpoint and request body based on model type
let endpoint;
let requestBody;
let additionalHeaders = {};
const bodyText = await req.clone().text(); const bodyText = await req.clone().text();
if (!bodyText) {
throw new Error("Request body is empty");
}
const bodyJson = JSON.parse(bodyText); const bodyJson = JSON.parse(bodyText);
validateRequest(bodyJson, modelId); validateRequest(bodyJson, modelId);
const canonicalBody = JSON.stringify(bodyJson);
// For all other models, use standard endpoint
endpoint = `${baseEndpoint}/model/${modelId}/invoke-with-response-stream`;
requestBody = JSON.stringify(bodyJson.body || bodyJson);
console.log("Request to AWS Bedrock:", {
endpoint,
modelId,
body: requestBody,
});
const headers = await sign({ const headers = await sign({
method: "POST", method: "POST",
@ -130,14 +239,17 @@ async function requestBedrock(req: NextRequest) {
accessKeyId: decryptedAccessKey, accessKeyId: decryptedAccessKey,
secretAccessKey: decryptedSecretKey, secretAccessKey: decryptedSecretKey,
sessionToken: decryptedSessionToken, sessionToken: decryptedSessionToken,
body: canonicalBody, body: requestBody,
service: "bedrock", service: "bedrock",
}); });
const res = await fetch(endpoint, { const res = await fetch(endpoint, {
method: "POST", method: "POST",
headers, headers: {
body: canonicalBody, ...headers,
...additionalHeaders,
},
body: requestBody,
redirect: "manual", redirect: "manual",
// @ts-ignore // @ts-ignore
duplex: "half", duplex: "half",
@ -146,15 +258,20 @@ async function requestBedrock(req: NextRequest) {
if (!res.ok) { if (!res.ok) {
const error = await res.text(); const error = await res.text();
console.error("AWS Bedrock error response:", error);
try { try {
const errorJson = JSON.parse(error); const errorJson = JSON.parse(error);
throw new Error(errorJson.message || error); throw new Error(errorJson.message || error);
} catch { } catch {
throw new Error(error); throw new Error(error || "Failed to get response from Bedrock");
} }
} }
const transformedStream = transformBedrockStream(res.body!); if (!res.body) {
throw new Error("Empty response from Bedrock");
}
const transformedStream = transformBedrockStream(res.body, modelId);
const stream = new ReadableStream({ const stream = new ReadableStream({
async start(controller) { async start(controller) {
try { try {
@ -163,6 +280,7 @@ async function requestBedrock(req: NextRequest) {
} }
controller.close(); controller.close();
} catch (err) { } catch (err) {
console.error("Stream error:", err);
controller.error(err); controller.error(err);
} }
}, },
@ -177,6 +295,7 @@ async function requestBedrock(req: NextRequest) {
}, },
}); });
} catch (e) { } catch (e) {
console.error("Request error:", e);
throw e; throw e;
} finally { } finally {
clearTimeout(timeoutId); clearTimeout(timeoutId);
@ -202,6 +321,7 @@ export async function handle(
try { try {
return await requestBedrock(req); return await requestBedrock(req);
} catch (e) { } catch (e) {
console.error("Handler error:", e);
return NextResponse.json( return NextResponse.json(
{ error: true, msg: e instanceof Error ? e.message : "Unknown error" }, { error: true, msg: e instanceof Error ? e.message : "Unknown error" },
{ status: 500 }, { status: 500 },

View File

@ -1,4 +1,11 @@
import { ChatOptions, LLMApi, SpeechOptions } from "../api"; import {
ChatOptions,
LLMApi,
SpeechOptions,
RequestMessage,
MultimodalContent,
MessageRole,
} from "../api";
import { import {
useAppConfig, useAppConfig,
usePluginStore, usePluginStore,
@ -15,6 +22,8 @@ const ClaudeMapper = {
system: "user", system: "user",
} as const; } as const;
type ClaudeRole = keyof typeof ClaudeMapper;
interface ToolDefinition { interface ToolDefinition {
function?: { function?: {
name: string; name: string;
@ -28,44 +37,131 @@ export class BedrockApi implements LLMApi {
throw new Error("Speech not implemented for Bedrock."); throw new Error("Speech not implemented for Bedrock.");
} }
extractMessage(res: any) { extractMessage(res: any, modelId: string = "") {
if (res?.content?.[0]?.text) return res.content[0].text; try {
if (res?.messages?.[0]?.content?.[0]?.text) // Handle Titan models
return res.messages[0].content[0].text; if (modelId.startsWith("amazon.titan")) {
if (res?.delta?.text) return res.delta.text; if (res?.delta?.text) return res.delta.text;
return ""; return res?.outputText || "";
}
// Handle LLaMA models
if (modelId.startsWith("us.meta.llama3")) {
if (res?.delta?.text) return res.delta.text;
if (res?.generation) return res.generation;
if (typeof res?.output === "string") return res.output;
if (typeof res === "string") return res;
return "";
}
// Handle Mistral models
if (modelId.startsWith("mistral.mistral")) {
if (res?.delta?.text) return res.delta.text;
return res?.outputs?.[0]?.text || res?.output || res?.completion || "";
}
// Handle Claude models and fallback cases
if (res?.content?.[0]?.text) return res.content[0].text;
if (res?.messages?.[0]?.content?.[0]?.text)
return res.messages[0].content[0].text;
if (res?.delta?.text) return res.delta.text;
if (res?.completion) return res.completion;
if (res?.generation) return res.generation;
if (res?.outputText) return res.outputText;
if (res?.output) return res.output;
if (typeof res === "string") return res;
return "";
} catch (e) {
console.error("Error extracting message:", e);
return "";
}
} }
async chat(options: ChatOptions) { formatRequestBody(
const visionModel = isVisionModel(options.config.model); messages: RequestMessage[],
const isClaude3 = options.config.model.startsWith("anthropic.claude-3"); systemMessage: string,
modelConfig: any,
) {
const model = modelConfig.model;
const modelConfig = { // Handle Titan models
...useAppConfig.getState().modelConfig, if (model.startsWith("amazon.titan")) {
...useChatStore.getState().currentSession().mask.modelConfig, const allMessages = systemMessage
model: options.config.model, ? [
}; { role: "system" as MessageRole, content: systemMessage },
...messages,
let systemMessage = ""; ]
const messages = []; : messages;
for (const msg of options.messages) { const inputText = allMessages
const content = await preProcessImageContent(msg.content); .map((m) => `${m.role}: ${getMessageTextContent(m)}`)
if (msg.role === "system") { .join("\n");
systemMessage = getMessageTextContent(msg); return {
} else { body: {
messages.push({ role: msg.role, content }); inputText,
} textGenerationConfig: {
maxTokenCount: modelConfig.max_tokens,
temperature: modelConfig.temperature,
stopSequences: [],
},
},
};
} }
// Handle LLaMA3 models - simplified format
if (model.startsWith("us.meta.llama3")) {
const allMessages = systemMessage
? [
{ role: "system" as MessageRole, content: systemMessage },
...messages,
]
: messages;
const prompt = allMessages
.map((m) => `${m.role}: ${getMessageTextContent(m)}`)
.join("\n");
return {
contentType: "application/json",
accept: "application/json",
body: {
prompt,
},
};
}
// Handle Mistral models
if (model.startsWith("mistral.mistral")) {
const allMessages = systemMessage
? [
{ role: "system" as MessageRole, content: systemMessage },
...messages,
]
: messages;
const prompt = allMessages
.map((m) => `${m.role}: ${getMessageTextContent(m)}`)
.join("\n");
return {
body: {
prompt,
temperature: modelConfig.temperature || 0.7,
max_tokens: modelConfig.max_tokens || 4096,
},
};
}
// Handle Claude models (existing implementation)
const isClaude3 = model.startsWith("anthropic.claude-3");
const formattedMessages = messages const formattedMessages = messages
.filter( .filter(
(v) => v.content && (typeof v.content !== "string" || v.content.trim()), (v) => v.content && (typeof v.content !== "string" || v.content.trim()),
) )
.map((v) => { .map((v) => {
const { role, content } = v; const { role, content } = v;
const insideRole = ClaudeMapper[role] ?? "user"; const insideRole = ClaudeMapper[role as ClaudeRole] ?? "user";
if (!visionModel || typeof content === "string") { if (!isVisionModel(model) || typeof content === "string") {
return { return {
role: insideRole, role: insideRole,
content: [{ type: "text", text: getMessageTextContent(v) }], content: [{ type: "text", text: getMessageTextContent(v) }],
@ -74,7 +170,7 @@ export class BedrockApi implements LLMApi {
return { return {
role: insideRole, role: insideRole,
content: content content: (content as MultimodalContent[])
.filter((v) => v.image_url || v.text) .filter((v) => v.image_url || v.text)
.map(({ type, text, image_url }) => { .map(({ type, text, image_url }) => {
if (type === "text") return { type, text: text! }; if (type === "text") return { type, text: text! };
@ -96,17 +192,40 @@ export class BedrockApi implements LLMApi {
}; };
}); });
const requestBody = { return {
anthropic_version: "bedrock-2023-05-31", anthropic_version: "bedrock-2023-05-31",
max_tokens: modelConfig.max_tokens, max_tokens: modelConfig.max_tokens,
messages: formattedMessages, messages: formattedMessages,
...(systemMessage && { system: systemMessage }), ...(systemMessage && { system: systemMessage }),
...(modelConfig.temperature !== undefined && { temperature: modelConfig.temperature,
temperature: modelConfig.temperature,
}),
...(modelConfig.top_p !== undefined && { top_p: modelConfig.top_p }),
...(isClaude3 && { top_k: 5 }), ...(isClaude3 && { top_k: 5 }),
}; };
}
async chat(options: ChatOptions) {
const modelConfig = {
...useAppConfig.getState().modelConfig,
...useChatStore.getState().currentSession().mask.modelConfig,
model: options.config.model,
};
let systemMessage = "";
const messages = [];
for (const msg of options.messages) {
const content = await preProcessImageContent(msg.content);
if (msg.role === "system") {
systemMessage = getMessageTextContent(msg);
} else {
messages.push({ role: msg.role, content });
}
}
const requestBody = this.formatRequestBody(
messages,
systemMessage,
modelConfig,
);
// console.log("Request body:", JSON.stringify(requestBody, null, 2));
const controller = new AbortController(); const controller = new AbortController();
options.onController?.(controller); options.onController?.(controller);
@ -121,7 +240,8 @@ export class BedrockApi implements LLMApi {
try { try {
const apiEndpoint = "/api/bedrock/chat"; const apiEndpoint = "/api/bedrock/chat";
const headers = { const headers = {
"Content-Type": "application/json", "Content-Type": requestBody.contentType || "application/json",
Accept: requestBody.accept || "application/json",
"X-Region": accessStore.awsRegion, "X-Region": accessStore.awsRegion,
"X-Access-Key": accessStore.awsAccessKey, "X-Access-Key": accessStore.awsAccessKey,
"X-Secret-Key": accessStore.awsSecretKey, "X-Secret-Key": accessStore.awsSecretKey,
@ -154,6 +274,7 @@ export class BedrockApi implements LLMApi {
(text: string, runTools: ChatMessageTool[]) => { (text: string, runTools: ChatMessageTool[]) => {
try { try {
const chunkJson = JSON.parse(text); const chunkJson = JSON.parse(text);
// console.log("Received chunk:", JSON.stringify(chunkJson, null, 2));
if (chunkJson?.content_block?.type === "tool_use") { if (chunkJson?.content_block?.type === "tool_use") {
index += 1; index += 1;
currentToolArgs = ""; currentToolArgs = "";
@ -193,8 +314,11 @@ export class BedrockApi implements LLMApi {
runTools[index].function!.arguments = currentToolArgs; runTools[index].function!.arguments = currentToolArgs;
} catch (e) {} } catch (e) {}
} }
return this.extractMessage(chunkJson); const message = this.extractMessage(chunkJson, modelConfig.model);
// console.log("Extracted message:", message);
return message;
} catch (e) { } catch (e) {
console.error("Error parsing chunk:", e);
return ""; return "";
} }
}, },
@ -251,10 +375,13 @@ export class BedrockApi implements LLMApi {
}); });
const resJson = await res.json(); const resJson = await res.json();
const message = this.extractMessage(resJson); // console.log("Response:", JSON.stringify(resJson, null, 2));
const message = this.extractMessage(resJson, modelConfig.model);
// console.log("Extracted message:", message);
options.onFinish(message, res); options.onFinish(message, res);
} }
} catch (e) { } catch (e) {
console.error("Chat error:", e);
options.onError?.(e as Error); options.onError?.(e as Error);
} }
} }

View File

@ -330,40 +330,24 @@ const bedrockModels = [
// Amazon Titan Models // Amazon Titan Models
"amazon.titan-text-express-v1", "amazon.titan-text-express-v1",
"amazon.titan-text-lite-v1", "amazon.titan-text-lite-v1",
"amazon.titan-text-agile-v1", "amazon.titan-tg1-large",
// Cohere Models
"cohere.command-light-text-v14",
"cohere.command-r-plus-v1:0",
"cohere.command-r-v1:0",
"cohere.command-text-v14",
// Claude Models // Claude Models
"anthropic.claude-3-haiku-20240307-v1:0", "anthropic.claude-3-haiku-20240307-v1:0",
"anthropic.claude-3-5-haiku-20241022-v1:0", "anthropic.claude-3-5-haiku-20241022-v1:0",
"anthropic.claude-3-sonnet-20240229-v1:0", "anthropic.claude-3-sonnet-20240229-v1:0",
"anthropic.claude-3-5-sonnet-20241022-v2:0", "anthropic.claude-3-5-sonnet-20241022-v2:0",
"anthropic.claude-3-opus-20240229-v1:0", "anthropic.claude-3-opus-20240229-v1:0",
"anthropic.claude-2.1",
"anthropic.claude-v2",
"anthropic.claude-v1",
"anthropic.claude-instant-v1",
// Meta Llama Models // Meta Llama Models
"meta.llama2-13b-chat-v1", "us.meta.llama3-1-8b-instruct-v1:0",
"meta.llama2-70b-chat-v1", "us.meta.llama3-1-70b-instruct-v1:0",
"meta.llama3-8b-instruct-v1:0", "us.meta.llama3-2-1b-instruct-v1:0",
"meta.llama3-2-11b-instruct-v1:0", "us.meta.llama3-2-3b-instruct-v1:0",
"meta.llama3-2-90b-instruct-v1:0", "us.meta.llama3-2-11b-instruct-v1:0",
"us.meta.llama3-2-90b-instruct-v1:0",
// Mistral Models // Mistral Models
"mistral.mistral-7b-instruct-v0:2", "mistral.mistral-7b-instruct-v0:2",
"mistral.mistral-large-2402-v1:0", "mistral.mistral-large-2402-v1:0",
"mistral.mistral-large-2407-v1:0", "mistral.mistral-large-2407-v1:0",
// AI21 Models
"ai21.j2-mid-v1",
"ai21.j2-ultra-v1",
]; ];
const googleModels = [ const googleModels = [