Add antiClaudeOverload option

This commit is contained in:
kwaroran
2024-04-06 05:45:16 +09:00
parent ad951bd045
commit b61baaa4ae
4 changed files with 63 additions and 3 deletions

View File

@@ -118,6 +118,7 @@ export const languageEnglish = {
emotionPrompt: "This option is used to set the prompt that is used to detect emotion. if it is blank, it will use the default prompt.",
removePunctuationHypa: "If enabled, it will remove punctuation before executing HypaMemory.",
additionalParams: "Additional parameters that would be added to the request body. if you want to exclude some parameters, you can put `{{none}}` to the value. if you want to add a header instead of body, you can put `header::` in front of the key like `header::Authorization`. if you want value as json, you can put `json::` in front of the value like `json::{\"key\":\"value\"}`. otherwise, type of the value would be determined automatically.",
antiClaudeOverload: "If Claude overload happens, RisuAI would try to prevent it by continuing with same prompt, making it less likely to happen. works only for streamed responses. this could not work for non-official api endpoints.",
},
setup: {
chooseProvider: "Choose AI Provider",
@@ -557,4 +558,5 @@ export const languageEnglish = {
noWaitForTranslate: "No Wait for Translate",
updateRealm: "Update to RisuRealm",
updateRealmDesc: "You are trying to update your character to RisuRealm. this will update your character to RisuRealm, and you can't revert it back.",
antiClaudeOverload: "Anti-Claude Overload",
}

View File

@@ -80,6 +80,11 @@
<div class="flex items-center mt-4">
<Check bind:check={$DataBase.allowAllExtentionFiles} name="Allow all in file select"/>
</div>
<div class="flex items-center mt-4">
<Check bind:check={$DataBase.antiClaudeOverload} name={language.antiClaudeOverload}>
<Help key="experimental"/><Help key="antiClaudeOverload"/>
</Check>
</div>
<div class="flex items-center mt-4">
<Check bind:check={$DataBase.putUserOpen} name={language.oaiRandomUser}>
<Help key="experimental"/><Help key="oaiRandomUser"/>

View File

@@ -9,7 +9,7 @@ import { sleep } from "../util";
import { createDeep } from "./deepai";
import { hubURL } from "../characterCards";
import { NovelAIBadWordIds, stringlizeNAIChat } from "./models/nai";
import { strongBan, tokenizeNum } from "../tokenizer";
import { strongBan, tokenize, tokenizeNum } from "../tokenizer";
import { runGGUFModel } from "./models/local";
import { risuChatParser } from "../parser";
import { SignatureV4 } from "@smithy/signature-v4";
@@ -1740,13 +1740,16 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
result: await textifyReadableStream(res.body)
}
}
let rerequesting = false
let breakError = ''
const stream = new ReadableStream<StreamResponseChunk>({
async start(controller){
let text = ''
let reader = res.body.getReader()
const decoder = new TextDecoder()
const parser = createParser((e) => {
const parser = createParser(async (e) => {
if(e.type === 'event'){
switch(e.event){
case 'content_block_delta': {
@@ -1760,6 +1763,42 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
}
case 'error': {
if(e.data){
const errormsg:string = JSON.parse(e.data).error?.message
if(errormsg && errormsg.toLocaleLowerCase().includes('overload') && db.antiClaudeOverload){
console.log('Overload detected, retrying...')
reader.cancel()
rerequesting = true
await sleep(2000)
body.max_tokens -= await tokenize(text)
if(body.max_tokens < 0){
body.max_tokens = 0
}
if(body.messages.at(-1)?.role !== 'assistant'){
body.messages.push({
role: 'assistant',
content: ''
})
}
body.messages[body.messages.length-1].content += text
const res = await fetchNative(replacerURL, {
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
"x-api-key": apiKey,
"anthropic-version": "2023-06-01",
"accept": "application/json",
},
method: "POST",
chatId: arg.chatId
})
if(res.status !== 200){
breakError = 'Error: ' + await textifyReadableStream(res.body)
break
}
reader = res.body.getReader()
rerequesting = false
break
}
text += "Error:" + JSON.parse(e.data).error?.message
controller.enqueue({
"0": text
@@ -1770,10 +1809,22 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model'
}
}
})
const reader = res.body.getReader()
while(true){
if(rerequesting){
if(breakError){
controller.enqueue({
"0": breakError
})
break
}
await sleep(1000)
continue
}
const {done, value} = await reader.read()
if(done){
if(rerequesting){
continue
}
break
}
parser.feed(decoder.decode(value))

View File

@@ -381,6 +381,7 @@ export function setDatabase(data:Database){
data.enabledModules ??= []
data.additionalParams ??= []
data.heightMode ??= 'normal'
data.antiClaudeOverload ??= false
changeLanguage(data.language)
DataBase.set(data)
@@ -614,6 +615,7 @@ export interface Database{
heightMode:string
useAdvancedEditor:boolean
noWaitForTranslate:boolean
antiClaudeOverload:boolean
}
export interface customscript{