From 9b3fbb73b09f40f9a94ff4cb0ec43f1dd3cfc1fa Mon Sep 17 00:00:00 2001 From: bangonicdd Date: Tue, 5 Mar 2024 23:35:46 +0900 Subject: [PATCH 1/2] add aws claude-3 --- src/lib/Setting/Pages/BotSettings.svelte | 6 +- src/ts/process/request.ts | 90 +++++++++++++++++++++++- 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/src/lib/Setting/Pages/BotSettings.svelte b/src/lib/Setting/Pages/BotSettings.svelte index 90998860..2f1faf4a 100644 --- a/src/lib/Setting/Pages/BotSettings.svelte +++ b/src/lib/Setting/Pages/BotSettings.svelte @@ -147,11 +147,7 @@ Mancer {language.apiKey} {/if} -{#if $DataBase.aiModel.startsWith('claude-3') || $DataBase.subModel.startsWith('claude-3')} - Claude {language.apiKey} - -{/if} -{#if $DataBase.aiModel.startsWith('claude-1') || $DataBase.subModel.startsWith('claude-1') || $DataBase.subModel.startsWith('claude-2') || $DataBase.subModel.startsWith('claude-2')} +{#if $DataBase.aiModel.startsWith('claude-') || $DataBase.subModel.startsWith('claude-')} Claude {language.apiKey} {#if $DataBase.useExperimental} diff --git a/src/ts/process/request.ts b/src/ts/process/request.ts index af28e7c3..72ce969c 100644 --- a/src/ts/process/request.ts +++ b/src/ts/process/request.ts @@ -1505,7 +1505,95 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model' delete body.system } + const bedrock = db.claudeAws + if(bedrock && aiModel !== 'reverse_proxy'){ + function getCredentialParts(key:string) { + const [accessKeyId, secretAccessKey, region] = key.split(":"); + + if (!accessKeyId || !secretAccessKey || !region) { + throw new Error("The key assigned to this request is invalid."); + } + + return { accessKeyId, secretAccessKey, region }; + } + const { accessKeyId, secretAccessKey, region } = getCredentialParts(apiKey); + + const AMZ_HOST = "bedrock-runtime.%REGION%.amazonaws.com"; + const host = AMZ_HOST.replace("%REGION%", region); + const stream = false + const CLAUDE_3_COMPAT_MODEL = "anthropic.claude-3-sonnet-20240229-v1:0"; + + // AWS currently only supports one v3 model. + const awsModel = CLAUDE_3_COMPAT_MODEL; + const url = `https://${host}/model/${awsModel}/invoke${stream ? "-with-response-stream" : ""}` + + const params = { + messages : claudeChat, + system: systemPrompt.trim(), + max_tokens: maxTokens, + // stop_sequences: ["user:", "assistant:", "system:"], + temperature: temperature, + top_p: db.top_p, + top_k: db.top_k, + anthropic_version: "bedrock-2023-05-31", + } + + const rq = new HttpRequest({ + method: "POST", + protocol: "https:", + hostname: host, + path: `/model/${awsModel}/invoke${stream ? "-with-response-stream" : ""}`, + headers: { + ["Host"]: host, + ["Content-Type"]: "application/json", + ["accept"]: "application/json", + }, + body: JSON.stringify(params), + }); + + const signer = new SignatureV4({ + sha256: Sha256, + credentials: { accessKeyId, secretAccessKey }, + region, + service: "bedrock", + }); + + const signed = await signer.sign(rq); + + console.log(` + ### Params + ${JSON.stringify(body)} + + ### Signed Headers + ${JSON.stringify(signed.headers)} + `); + + const res = await globalFetch(url, { + method: "POST", + body: params, + headers: signed.headers, + plainFetchForce: true + }) + + if(!res.ok){ + return { + type: 'fail', + result: JSON.stringify(res.data) + } + } + if(res.data.error){ + return { + type: 'fail', + result: JSON.stringify(res.data.error) + } + } + return { + type: 'success', + result: res.data.content[0].text + + } + } const res = await globalFetch(replacerURL, { body: body, @@ -1643,11 +1731,11 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model' const url = `https://${host}/model/${awsModel}/invoke${stream ? "-with-response-stream" : ""}` const params = { prompt : requestPrompt.startsWith("\n\nHuman: ") ? requestPrompt : "\n\nHuman: " + requestPrompt, - //model: raiModel, max_tokens_to_sample: maxTokens, stop_sequences: ["\n\nHuman:", "\n\nSystem:", "\n\nAssistant:"], temperature: temperature, top_p: db.top_p, + //top_k: db.top_k, } const rq = new HttpRequest({ method: "POST", From 359216c6a1f9a81904530d2d37e537835d1f06ea Mon Sep 17 00:00:00 2001 From: bangonicdd Date: Wed, 6 Mar 2024 00:15:04 +0900 Subject: [PATCH 2/2] Remove test log --- src/ts/process/request.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/ts/process/request.ts b/src/ts/process/request.ts index 72ce969c..c6092d12 100644 --- a/src/ts/process/request.ts +++ b/src/ts/process/request.ts @@ -1561,14 +1561,6 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model' const signed = await signer.sign(rq); - console.log(` - ### Params - ${JSON.stringify(body)} - - ### Signed Headers - ${JSON.stringify(signed.headers)} - `); - const res = await globalFetch(url, { method: "POST", body: params,