add aws claude-3 (#298)

# PR Checklist
- [x] Did you check if it works normally in all models? *ignore this
when it dosen't uses models*
- [x] Did you check if it works normally in all of web, local and node
hosted versions? if it dosen't, did you blocked it in those versions?
- [x] Did you added a type def?

# Description


![image](https://github.com/kwaroran/RisuAI/assets/133192207/5a515821-fc22-4cf2-98ac-e86d65ba7c86)
This commit is contained in:
kwaroran
2024-03-06 03:14:18 +09:00
committed by GitHub
2 changed files with 82 additions and 6 deletions

View File

@@ -1508,7 +1508,87 @@ 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);
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,
@@ -1646,11 +1726,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",