Add claude caching and fix gpt4o chatgpt

This commit is contained in:
kwaroran
2024-08-15 06:16:48 +09:00
parent 5d66e35a61
commit 146ed8c5b0
4 changed files with 79 additions and 24 deletions

View File

@@ -133,6 +133,8 @@ export const languageEnglish = {
triggerLLMPrompt: "A prompt that would be sent to the model. you can use multi turns and roles by using `@@role user`, `@@role system`, `@@role assistant`. for example, \n\`\`\`\n@@role system\nrespond as hello\n@@role assistant\nhello\n@@role user\nhi\n\`\`\`", triggerLLMPrompt: "A prompt that would be sent to the model. you can use multi turns and roles by using `@@role user`, `@@role system`, `@@role assistant`. for example, \n\`\`\`\n@@role system\nrespond as hello\n@@role assistant\nhello\n@@role user\nhi\n\`\`\`",
legacyTranslation: "If enabled, it will use the old translation method, which preprocess markdown and quotes before translations instead of postprocessing after translations.", legacyTranslation: "If enabled, it will use the old translation method, which preprocess markdown and quotes before translations instead of postprocessing after translations.",
luaHelp: "You can use Lua scripts as a trigger script. you can define onInput, onOutput, onStart functions. onInput is called when user sends a message, onOutput is called when character sends a message, onStart is called when the chat starts. for more information, see the documentation.", luaHelp: "You can use Lua scripts as a trigger script. you can define onInput, onOutput, onStart functions. onInput is called when user sends a message, onOutput is called when character sends a message, onStart is called when the chat starts. for more information, see the documentation.",
claudeCachingExperimental: "Caching in Claude is experimental feature that can reduce the cost of the model, but it can also increase the cost if you use it without reroll. since this is a experimental feature, it can be unstable and behavior can be changed in the future.",
}, },
setup: { setup: {
chooseProvider: "Choose AI Provider", chooseProvider: "Choose AI Provider",
@@ -680,4 +682,5 @@ export const languageEnglish = {
parameters: "Parameters", parameters: "Parameters",
sizeAndSpeed: "Size and Speed", sizeAndSpeed: "Size and Speed",
useLegacyGUI: "Use Legacy GUI", useLegacyGUI: "Use Legacy GUI",
claudeCachingExperimental: "Claude Caching",
} }

View File

@@ -100,6 +100,11 @@
<Help key="experimental"/><Help key="antiClaudeOverload"/> <Help key="experimental"/><Help key="antiClaudeOverload"/>
</Check> </Check>
</div> </div>
<div class="flex items-center mt-4">
<Check bind:check={$DataBase.claudeCachingExperimental} name={language.claudeCachingExperimental}>
<Help key="experimental"/><Help key="claudeCachingExperimental"/>
</Check>
</div>
<div class="flex items-center mt-4"> <div class="flex items-center mt-4">
<Check bind:check={$DataBase.putUserOpen} name={language.oaiRandomUser}> <Check bind:check={$DataBase.putUserOpen} name={language.oaiRandomUser}>
<Help key="experimental"/><Help key="oaiRandomUser"/> <Help key="experimental"/><Help key="oaiRandomUser"/>

View File

@@ -179,6 +179,7 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
case 'gpt4om': case 'gpt4om':
case 'gpt4om-2024-07-18': case 'gpt4om-2024-07-18':
case 'gpt4o-2024-08-06': case 'gpt4o-2024-08-06':
case 'gpt4o-chatgpt':
case 'reverse_proxy':{ case 'reverse_proxy':{
let formatedChat:OpenAIChatExtra[] = [] let formatedChat:OpenAIChatExtra[] = []
for(let i=0;i<formated.length;i++){ for(let i=0;i<formated.length;i++){
@@ -1665,7 +1666,8 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
interface Claude3TextBlock { interface Claude3TextBlock {
type: 'text', type: 'text',
text: string text: string,
cache_control?: {"type": "ephemeral"}
} }
interface Claude3ImageBlock { interface Claude3ImageBlock {
@@ -1675,13 +1677,14 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
media_type: string, media_type: string,
data: string data: string
} }
cache_control?: {"type": "ephemeral"}
} }
type Claude3ContentBlock = Claude3TextBlock|Claude3ImageBlock type Claude3ContentBlock = Claude3TextBlock|Claude3ImageBlock
interface Claude3Chat { interface Claude3Chat {
role: 'user'|'assistant' role: 'user'|'assistant'
content: string|Claude3ContentBlock[] content: Claude3ContentBlock[]
} }
let claudeChat: Claude3Chat[] = [] let claudeChat: Claude3Chat[] = []
@@ -1732,13 +1735,16 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
} }
} }
} }
else{
content += "\n\n" + chat.content
}
claudeChat[claudeChat.length-1].content = content claudeChat[claudeChat.length-1].content = content
} }
else{ else{
let formatedChat:Claude3Chat = chat let formatedChat:Claude3Chat = {
role: chat.role,
content: [{
type: 'text',
text: chat.content
}]
}
if(multimodals && multimodals.length > 0){ if(multimodals && multimodals.length > 0){
formatedChat.content = [{ formatedChat.content = [{
type: 'text', type: 'text',
@@ -1799,7 +1805,6 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
} }
} }
} }
console.log(claudeChat)
if(claudeChat.length === 0 && systemPrompt === ''){ if(claudeChat.length === 0 && systemPrompt === ''){
return { return {
type: 'fail', type: 'fail',
@@ -1809,16 +1814,47 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
if(claudeChat.length === 0 && systemPrompt !== ''){ if(claudeChat.length === 0 && systemPrompt !== ''){
claudeChat.push({ claudeChat.push({
role: 'user', role: 'user',
content: systemPrompt content: [{
type: 'text',
text: 'Start'
}]
}) })
systemPrompt = '' systemPrompt = ''
} }
if(claudeChat[0].role !== 'user'){ if(claudeChat[0].role !== 'user'){
claudeChat.unshift({ claudeChat.unshift({
role: 'user', role: 'user',
content: 'Start' content: [{
type: 'text',
text: 'Start'
}]
}) })
} }
if(db.claudeCachingExperimental){
for(let i = 0;i<4;i++){
const ind = claudeChat.findLastIndex((v) => {
if(v.role !== 'user'){
return false
}
if(v.content.length === 0){
return false
}
if(v.content[0].cache_control){ // if it already has cache control, skip
return false
}
return true
})
console.log(ind)
if(ind === -1){
break
}
claudeChat[ind].content[0].cache_control = {
type: 'ephemeral'
}
}
}
let body = { let body = {
model: raiModel, model: raiModel,
messages: claudeChat, messages: claudeChat,
@@ -1929,16 +1965,25 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
} }
} }
if(useStreaming){
const res = await fetchNative(replacerURL, { let headers:{
body: JSON.stringify(body), [key:string]:string
headers: { } = {
"Content-Type": "application/json", "Content-Type": "application/json",
"x-api-key": apiKey, "x-api-key": apiKey,
"anthropic-version": "2023-06-01", "anthropic-version": "2023-06-01",
"accept": "application/json", "accept": "application/json",
}, }
if(db.claudeCachingExperimental){
headers['anthropic-beta'] = 'prompt-caching-2024-07-31'
}
if(useStreaming){
const res = await fetchNative(replacerURL, {
body: JSON.stringify(body),
headers: headers,
method: "POST", method: "POST",
chatId: arg.chatId chatId: arg.chatId
}) })
@@ -1986,10 +2031,16 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
if(body.messages.at(-1)?.role !== 'assistant'){ if(body.messages.at(-1)?.role !== 'assistant'){
body.messages.push({ body.messages.push({
role: 'assistant', role: 'assistant',
content: '' content: [{
type: 'text',
text: ''
}]
}) })
} }
body.messages[body.messages.length-1].content += text const block = body.messages[body.messages.length-1].content[0]
if(block.type === 'text'){
block.text += text
}
const res = await fetchNative(replacerURL, { const res = await fetchNative(replacerURL, {
body: JSON.stringify(body), body: JSON.stringify(body),
headers: { headers: {
@@ -2058,12 +2109,7 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
} }
const res = await globalFetch(replacerURL, { const res = await globalFetch(replacerURL, {
body: body, body: body,
headers: { headers: headers,
"Content-Type": "application/json",
"x-api-key": apiKey,
"anthropic-version": "2023-06-01",
"accept": "application/json"
},
method: "POST", method: "POST",
chatId: arg.chatId chatId: arg.chatId
}) })

View File

@@ -709,6 +709,7 @@ export interface Database{
comfyConfig: ComfyConfig comfyConfig: ComfyConfig
comfyUiUrl: string comfyUiUrl: string
useLegacyGUI: boolean useLegacyGUI: boolean
claudeCachingExperimental: boolean
} }
export interface customscript{ export interface customscript{