core
是 QSGpt
工厂函数,包含了 sdk
暴露给外部调用的所有方法
import { createApp } from 'vue'
import { setupStore } from '@/stores'
import App from '@/App.vue'
import axios from '@/config/http/http'
import { setupGlobDirectives } from '@/directives'
import UseAnswerStore from '@/stores/modules/answer'
import UseModelStore from '@/stores/modules/model'
export default class QSGpt {
gptContainer //gpt要挂载的dom
gptConfig //gpt的基础配置信息
_isCreate = false //是否挂载
constructor({ gptDom, gptConfig }) {
this.gptContainer = gptDom
this.gptConfig = gptConfig
this._validate()
}
// 挂载gpt到dom中,并且初始化vue3的加载
async _create() {
const div = document.createElement('div')
div.id = 'global-float-banner'
this.gptContainer.appendChild(div)
const app = createApp(App)
setupStore(app)
setupGlobDirectives(app)
app.config.globalProperties.$http = new axios(this.gptConfig.key)
app.config.globalProperties.$gptConfig = this.gptConfig
app.mount(div)
this._isCreate = true
}
init() {
if (this._isCreate) {
throw new Error('请勿重复挂载')
}
this._create()
}
_validate() {
if (!this.gptContainer) {
throw new Error('请传入要挂载的gptDom')
}
if (!this.gptConfig) {
throw new Error('请传入gptConfig')
} else {
if (!this.gptConfig.key) {
throw new Error('gptConfig中未配置key')
}
}
}
// 传入过滤回答的方法,可以用来加载自定义dom
addAnswerFilter(
answerFilter,
answerProps = {
type: 'type',
dom: 'dom',
params: 'params',
}
) {
if (!this._isCreate) {
throw new Error('请先执行init')
}
const answerStore = UseAnswerStore()
answerStore.setAnswerFilter(answerFilter, answerProps)
}
// 传入过滤历史回答的方法,可以在不访问业务接口的情况下加载自定义dom
addHistoryFilter(
historyFilter,
historyProps = {
type: 'type',
domParams: 'domParams',
}
) {
if (!this._isCreate) {
throw new Error('请先执行init')
}
const answerStore = UseAnswerStore()
answerStore.setHistoryFilter(historyFilter, historyProps)
}
// 设置异步loading
setAnswerLoading(loading, text = '思考中...') {
const answerStore = UseAnswerStore()
answerStore.setAnswerLoading(loading, text)
}
// 获取最近一条聊天记录中的图片
getLastImageFile() {
const modelStore = UseModelStore()
return modelStore.imageFile
}
}