支持gemini

This commit is contained in:
IndieKKY
2024-01-20 12:20:09 +08:00
parent 771a802728
commit 5078baca1b
7 changed files with 104 additions and 49 deletions

View File

@@ -18,3 +18,16 @@ export const handleChatCompleteTask = async (task: Task) => {
throw new Error(`${task.resp.error.code as string??''} ${task.resp.error.message as string ??''}`)
}
}
export const handleGeminiChatCompleteTask = async (task: Task) => {
const data = task.def.data
const resp = await fetch('https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-goog-api-key': task.def.extra.geminiApiKey,
},
body: JSON.stringify(data),
})
task.resp = await resp.json()
}

View File

@@ -1,5 +1,5 @@
import {TASK_EXPIRE_TIME} from '../const'
import {handleChatCompleteTask} from './openaiService'
import {handleChatCompleteTask, handleGeminiChatCompleteTask} from './openaiService'
export const tasksMap = new Map<string, Task>()
@@ -11,6 +11,9 @@ export const handleTask = async (task: Task) => {
case 'chatComplete':
await handleChatCompleteTask(task)
break
case 'geminiChatComplete':
await handleGeminiChatCompleteTask(task)
break
default:
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
throw new Error(`任务类型不支持: ${task.def.type}`)