feat: 对接 Spark 大模型

This commit is contained in:
2024-04-01 17:03:03 +08:00
parent d20b518f5b
commit 3ded5f8e7a
12 changed files with 328 additions and 67 deletions

View File

@@ -1,5 +1,5 @@
import type {ResultBlockMeta} from '~/components/aigc/drawing';
import type {ChatSession} from "~/components/aigc/chat";
import type {ChatSession} from "~/typings/llm";
export interface HistoryItem {
fid: string

View File

@@ -1,18 +0,0 @@
export const useIdGenerator = () => {
const generateUUID = () => {
// noinspection SpellCheckingInspection
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
})
}
const generateMathRandom = () => {
return Math.random().toString(36).slice(2)
}
return {
generateUUID,
generateMathRandom
}
}

31
composables/useLLM.ts Normal file
View File

@@ -0,0 +1,31 @@
import {type ChatMessage, llmModels, type LLMSpark, type MessageRole, type ModelTag} from "~/typings/llm";
import {useFetchWrapped} from "~/composables/useFetchWrapped";
export interface LLMRequestOptions {
modelTag: ModelTag
}
export const useLLM = (context: ChatMessage[], options: LLMRequestOptions): Promise<string> => new Promise((resolve, reject) => {
const {modelTag} = options
const model = llmModels.find(model => model.tag === modelTag)
if (!model) return reject('model specified is not available')
const loginState = useLoginState()
useFetchWrapped<LLMSpark.request | AuthedRequest, BaseResponse<LLMSpark.response>>(
model.endpoint,
{
token: loginState.token || '',
user_id: loginState.user.id,
prompt: JSON.stringify(context.filter(c => c.content && !c.interrupted).map(c => ({
role: c.role,
content: c.content
})))
}
).then(res => {
if (res.ret !== 200) return reject(res.msg || 'unknown error')
if (res.data.request_msg) return resolve(res.data.request_msg)
if (res.data.request_fail) return reject(res.data.request_fail?.header?.message || 'unknown error')
return reject('unknown error')
}).catch(err => {
reject(err)
})
})