Readd Gemini stream

This commit is contained in:
Kwaroran
2024-12-14 20:10:07 +09:00
parent f4b78480f3
commit 7d4030cf7a
2 changed files with 68 additions and 12 deletions

View File

@@ -1609,9 +1609,65 @@ async function requestGoogleCloudVertex(arg:RequestDataArgumentExtended):Promise
else if(arg.modelInfo.format === LLMFormat.VertexAIGemini){
url =`https://${REGION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/${arg.modelInfo.internalID}:streamGenerateContent`
}
else if(arg.modelInfo.format === LLMFormat.GoogleCloud && arg.useStreaming){
url = `https://generativelanguage.googleapis.com/v1beta/models/${arg.modelInfo.internalID}:streamGenerateContent?key=${db.google.accessToken}`
}
else{
url = `https://generativelanguage.googleapis.com/v1beta/models/${arg.modelInfo.internalID}:generateContent?key=${db.google.accessToken}`
}
if(arg.modelInfo.format === LLMFormat.GoogleCloud && arg.useStreaming){
headers['Content-Type'] = 'application/json'
const f = await fetchNative(url, {
headers: headers,
body: JSON.stringify(body),
method: 'POST',
chatId: arg.chatId,
})
if(f.status !== 200){
return {
type: 'fail',
result: await textifyReadableStream(f.body)
}
}
let fullResult:string = ''
const stream = new TransformStream<Uint8Array, StreamResponseChunk>( {
async transform(chunk, control) {
fullResult += new TextDecoder().decode(chunk)
try {
let reformatted = fullResult
if(reformatted.endsWith(',')){
reformatted = fullResult.slice(0, -1) + ']'
}
if(!reformatted.endsWith(']')){
reformatted = fullResult + ']'
}
const data = JSON.parse(reformatted)
let r = ''
for(const d of data){
r += d.candidates[0].content.parts[0].text
}
control.enqueue({
'0': r
})
} catch (error) {
console.log(error)
}
}
},)
return {
type: 'streaming',
result: f.body.pipeThrough(stream)
}
}
const res = await globalFetch(url, {
headers: headers,
body: body,