Fix aws claude (#283)

# 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?
- [ ] Did you added a type def?

# Description
Fix aws claude things.
This commit is contained in:
kwaroran
2024-01-14 02:40:57 +09:00
committed by GitHub

View File

@@ -1438,7 +1438,7 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
const bedrock = db.claudeAws
if(bedrock){
if(bedrock && aiModel !== 'reverse_proxy'){
function getCredentialParts(key:string) {
const [accessKeyId, secretAccessKey, region] = key.split(":");
@@ -1450,28 +1450,58 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
}
const { accessKeyId, secretAccessKey, region } = getCredentialParts(apiKey);
const AMZ_HOST = "invoke-bedrock.%REGION%.amazonaws.com";
const AMZ_HOST = "bedrock-runtime.%REGION%.amazonaws.com";
const host = AMZ_HOST.replace("%REGION%", region);
const stream = false
const url = `https://${host}/model/${model}/invoke${stream ? "-with-response-stream" : ""}`
const LATEST_AWS_V2_MINOR_VERSION = 1;
let awsModel = `anthropic.claude-v2:${LATEST_AWS_V2_MINOR_VERSION}`;
const pattern = /^(claude-)?(instant-)?(v)?(\d+)(\.(\d+))?(-\d+k)?$/i;
const match = raiModel.match(pattern);
if (match) {
const [, , instant, v, major, dot, minor] = match;
if (instant) {
awsModel = "anthropic.claude-instant-v1";
}
// There's only one v1 model
else if (major === "1") {
awsModel = "anthropic.claude-v1";
}
// Try to map Anthropic API v2 models to AWS v2 models
else if (major === "2") {
if (minor === "0") {
awsModel = "anthropic.claude-v2";
} else if (!v && !dot && !minor) {
awsModel = "anthropic.claude-v2";
}
}
}
const url = `https://${host}/model/${awsModel}/invoke${stream ? "-with-response-stream" : ""}`
const params = {
prompt : "\n\nHuman: " + requestPrompt,
model: raiModel,
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,
}
const rq = new HttpRequest({
method: "POST",
protocol: "https:",
hostname: host,
path: `/model/${model}/invoke${stream ? "-with-response-stream" : ""}`,
path: `/model/${awsModel}/invoke${stream ? "-with-response-stream" : ""}`,
headers: {
["Host"]: host,
["content-type"]: "application/json",
["Content-Type"]: "application/json",
["accept"]: "application/json",
"anthropic-version": "2023-06-01",
//"anthropic-version": "2023-06-01",
},
body: JSON.stringify(params),
});
@@ -1486,15 +1516,11 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
const signed = await signer.sign(rq);
const da = await globalFetch(`${signed.protocol}//${signed.hostname}`, {
const da = await globalFetch(url, {
method: "POST",
body: params,
headers: {
["Host"]: host,
["content-type"]: "application/json",
["accept"]: "application/json",
"anthropic-version": "2023-06-01",
}
headers: signed.headers,
plainFetchForce: true
})