Add claude batching

This commit is contained in:
Kwaroran
2025-05-25 18:20:54 +09:00
parent d2bc3c4210
commit 0b3a45e41e
5 changed files with 123 additions and 1 deletions

View File

@@ -1129,5 +1129,6 @@ export const languageEnglish = {
promptInfoEmptyToggle: "No custom toggles are currently active.", promptInfoEmptyToggle: "No custom toggles are currently active.",
promptInfoEmptyText: "No prompt text has been saved.", promptInfoEmptyText: "No prompt text has been saved.",
escapeOutput: "Escape Output", escapeOutput: "Escape Output",
claudeBatching: "Claude Batching",
} }

View File

@@ -996,4 +996,5 @@ export const languageKorean = {
"promptInfoEmptyToggle": "활성화된 커스텀 토글이 없습니다.", "promptInfoEmptyToggle": "활성화된 커스텀 토글이 없습니다.",
"promptInfoEmptyText": "저장된 프롬프트 텍스트가 없습니다.", "promptInfoEmptyText": "저장된 프롬프트 텍스트가 없습니다.",
"escapeOutput": "출력 이스케이프", "escapeOutput": "출력 이스케이프",
"claudeBatching": "Claude 배칭",
} }

View File

@@ -220,6 +220,11 @@
<Check bind:check={DBState.db.antiServerOverloads} name={language.antiServerOverload}> <Check bind:check={DBState.db.antiServerOverloads} name={language.antiServerOverload}>
</Check> </Check>
</div> </div>
<div class="flex items-center mt-4">
<Check bind:check={DBState.db.claudeBatching} name={language.claudeBatching}>
<Help key="experimental" />
</Check>
</div>
<div class="flex items-center mt-4"> <div class="flex items-center mt-4">
<Check bind:check={DBState.db.useTokenizerCaching} name={language.useTokenizerCaching}> <Check bind:check={DBState.db.useTokenizerCaching} name={language.useTokenizerCaching}>
</Check> </Check>

View File

@@ -3336,6 +3336,120 @@ async function requestClaude(arg:RequestDataArgumentExtended):Promise<requestDat
} }
} }
if(db.claudeBatching){
if(body.stream !== undefined){
delete body.stream
}
const id = v4()
const resp = await fetchNative(replacerURL + '/batches', {
"body": JSON.stringify({
"requests": [{
"custom_id": id,
"params": body,
}]
}),
"method": "POST",
signal: arg.abortSignal,
headers: headers
})
if(resp.status !== 200){
return {
type: 'fail',
result: await textifyReadableStream(resp.body)
}
}
const r = (await resp.json())
if(!r.id){
return {
type: 'fail',
result: 'No results URL returned from Claude batch request'
}
}
const resultsUrl = replacerURL + `/batches/${r.id}/results`
const statusUrl = replacerURL + `/batches/${r.id}`
let received = false
while(!received){
try {
await sleep(3000)
if(arg?.abortSignal?.aborted){
return {
type: 'fail',
result: 'Request aborted'
}
}
const statusRes = await fetchNative(statusUrl, {
"method": "GET",
"headers": {
"x-api-key": apiKey,
"anthropic-version": "2023-06-01",
},
"signal": arg.abortSignal,
})
if(statusRes.status !== 200){
return {
type: 'fail',
result: await textifyReadableStream(statusRes.body)
}
}
const statusData = await statusRes.json()
if(statusData.processing_status !== 'ended'){
continue
}
const batchRes = await fetchNative(resultsUrl, {
"method": "GET",
"headers": {
"x-api-key": apiKey,
"anthropic-version": "2023-06-01",
},
"signal": arg.abortSignal,
})
if(batchRes.status !== 200){
return {
type: 'fail',
result: await textifyReadableStream(batchRes.body)
}
}
//since jsonl
const batchTextData = (await batchRes.text()).split('\n').filter((v) => v.trim() !== ''). map((v) => {
try {
return JSON.parse(v)
} catch (error) {
return null
}
}).filter((v) => v !== null)
for(const batchData of batchTextData){
const type = batchData?.result?.type
console.log('Claude batch result type:', type)
if(batchData?.result?.type === 'succeeded'){
return {
type: 'success',
result: batchData.result.message.content?.[0]?.text ?? ''
}
}
if(batchData?.result?.type === 'errored'){
return {
type: 'fail',
result: JSON.stringify(batchData.result.error),
}
}
}
} catch (error) {
console.error('Error while waiting for Claude batch results:', error)
}
}
}
if(db.claudeRetrivalCaching){ if(db.claudeRetrivalCaching){

View File

@@ -562,7 +562,7 @@ export function setDatabase(data:Database){
data.customModels ??= [] data.customModels ??= []
//@ts-ignore //@ts-ignore
if(!__NODE__ && !window.__TAURI_INTERNALS__){ if(!globalThis.__NODE__ && !window.__TAURI_INTERNALS__){
//this is intended to forcely reduce the size of the database in web //this is intended to forcely reduce the size of the database in web
data.promptInfoInsideChat = false data.promptInfoInsideChat = false
} }
@@ -1032,6 +1032,7 @@ export interface Database{
mcpURLs:string[] mcpURLs:string[]
promptInfoInsideChat:boolean promptInfoInsideChat:boolean
promptTextInfoInsideChat:boolean promptTextInfoInsideChat:boolean
claudeBatching:boolean
} }
interface SeparateParameters{ interface SeparateParameters{