修改: app/api/bedrock.ts

修改:     app/client/platforms/bedrock.ts
This commit is contained in:
glay 2024-11-06 17:23:53 +08:00
parent f0c23cc6aa
commit 5d5456c1c5
2 changed files with 31 additions and 3 deletions

View File

@ -46,6 +46,7 @@ export interface ConverseRequest {
description?: string;
input_schema: any;
}[];
stream?: boolean;
}
function supportsToolUse(modelId: string): boolean {
@ -188,6 +189,30 @@ export async function handle(
throw new Error("No stream in response");
}
// If stream is false, accumulate the response and return as JSON
console.log("Body.stream==========" + body.stream);
if (body.stream === false) {
let fullResponse = {
content: "",
};
const responseStream =
response.stream as AsyncIterable<ConverseStreamOutput>;
for await (const event of responseStream) {
if (
"contentBlockDelta" in event &&
event.contentBlockDelta?.delta &&
"text" in event.contentBlockDelta.delta &&
event.contentBlockDelta.delta.text
) {
fullResponse.content += event.contentBlockDelta.delta.text;
}
}
return NextResponse.json(fullResponse);
}
// Otherwise, return streaming response
const stream = new ReadableStream({
async start(controller) {
try {

View File

@ -38,9 +38,11 @@ export class BedrockApi implements LLMApi {
}
extractMessage(res: any) {
console.log("[Response] claude response: ", res);
return res?.content?.[0]?.text;
console.log("[Response] Bedrock not stream response: ", res);
if (res.error) {
return "```\n" + JSON.stringify(res, null, 4) + "\n```";
}
return res?.content ?? res;
}
async chat(options: ChatOptions): Promise<void> {
@ -144,6 +146,7 @@ export class BedrockApi implements LLMApi {
topP: modelConfig.top_p,
stopSequences: [],
},
stream: shouldStream,
};
const conversePath = `${ApiPath.Bedrock}/converse`;