feat: aws sonnet 3.7 + legacy (#772)

# PR Checklist
- [ ] Have you checked if it works normally in all models? *Ignore this
if it doesn't use models.*
- [ ] Have you checked if it works normally in all web, local, and node
hosted versions? If it doesn't, have you blocked it in those versions?
- [ ] Have you added type definitions?

# Description
This PR aims to enable the use of the newly added claude 3.7 sonnet
model (and some legacys (¬‿¬)) on AWS.
This commit is contained in:
kwaroran
2025-03-05 05:38:29 +09:00
committed by GitHub
2 changed files with 101 additions and 12 deletions

View File

@@ -2705,18 +2705,17 @@ async function requestClaude(arg:RequestDataArgumentExtended):Promise<requestDat
const host = AMZ_HOST.replace("%REGION%", region);
const stream = false; // todo?
const awsModel = "us." + arg.modelInfo.internalID;
const awsModel = !arg.modelInfo.internalID.includes("claude-v2") ? "us." + arg.modelInfo.internalID : arg.modelInfo.internalID;
const url = `https://${host}/model/${awsModel}/invoke${stream ? "-with-response-stream" : ""}`
const params = {
messages : claudeChat,
system: systemPrompt.trim(),
max_tokens: maxTokens,
// stop_sequences: null,
temperature: arg.temperature,
top_p: db.top_p,
top_k: db.top_k,
anthropic_version: "bedrock-2023-05-31",
let params = {...body}
params.anthropic_version = "bedrock-2023-05-31"
delete params.model
delete params.stream
if (params.thinking?.type === "enabled"){
params.temperature = 1.0
delete params.top_k
delete params.top_p
}
const rq = new HttpRequest({
@@ -2761,10 +2760,49 @@ async function requestClaude(arg:RequestDataArgumentExtended):Promise<requestDat
result: JSON.stringify(res.data.error)
}
}
const contents = res?.data?.content
if(!contents || contents.length === 0){
return {
type: 'fail',
result: JSON.stringify(res.data)
}
}
let resText = ''
let thinking = false
for(const content of contents){
if(content.type === 'text'){
if(thinking){
resText += "</Thoughts>\n\n"
thinking = false
}
resText += content.text
}
if(content.type === 'thinking'){
if(!thinking){
resText += "<Thoughts>\n"
thinking = true
}
resText += content.thinking ?? ''
}
if(content.type === 'redacted_thinking'){
if(!thinking){
resText += "<Thoughts>\n"
thinking = true
}
resText += '\n{{redacted_thinking}}\n'
}
}
if(arg.extractJson && db.jsonSchemaEnabled){
return {
type: 'success',
result: extractJSON(resText, db.jsonSchema)
}
}
return {
type: 'success',
result: res.data.content[0].text
result: resText
}
}