Skip to content

Commit

Permalink
Merge pull request #1412 from terwer/dev
Browse files Browse the repository at this point in the history
feat: support stream for short desc
  • Loading branch information
terwer authored Nov 8, 2024
2 parents e6a59f1 + b70e350 commit 0a46ea6
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 22 deletions.
1 change: 1 addition & 0 deletions auto-imports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// @ts-nocheck
// noinspection JSUnusedGlobalSymbols
// Generated by unplugin-auto-import
// biome-ignore lint: disable
export {}
declare global {
const ElMessage: typeof import('element-plus/es')['ElMessage']
Expand Down
13 changes: 12 additions & 1 deletion src/ai/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,20 @@ const shortDescPrompt = <AiPrompt>{
key: "desc",
content:
"请为这篇文章生成简明扼要的摘要,只处理文本,尽量返回中文摘要。" +
"摘要长度不超过255个中文字符或512个英文字符。" +
"摘要长度不超过1024个中文字符或2048个英文字符。" +
"输出为 JSON 格式,键名为 desc,结果需放在 {} 内。" +
"完整结果必须是合法JSON,不得包含非法 JSON 字符。",
}
const shortDescPromptStream = <AiPrompt>{
title: "自动提取摘要",
description: "从文章内容生成文章摘要",
key: "desc",
content:
"请为这篇文章生成简明扼要的摘要,只处理文本,尽量返回中文摘要。" +
"摘要长度不超过1024个中文字符或2048个英文字符。" +
"输出为文本格式,内容为语义化的摘要。" +
"完整结果必须是文本摘要,不得包含非法字符。",
}
export type ShortDescAIResult = {
desc: string
}
Expand Down Expand Up @@ -106,6 +116,7 @@ export type CategoryAIResult = {
const prompt = {
titlePrompt,
shortDescPrompt,
shortDescPromptStream,
tagPrompt,
categoryPrompt,
}
Expand Down
56 changes: 37 additions & 19 deletions src/components/publish/form/PublishDescription.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,45 @@ const emit = defineEmits(["emitSyncDesc"])

const handleMakeDesc = async () => {
formData.isDescLoading = true
const isStream = true
try {
// if (formData.useAi) {
const inputWord = prompt.shortDescPrompt.content
const { chat, getChatInput } = useChatGPT()
const chatText = await chat(inputWord, {
name: "desc",
systemMessage: getChatInput(formData?.md, formData.html),
})
if (StrUtil.isEmptyString(chatText)) {
ElMessage.error("请求错误,请在偏好设置配置请求地址和ChatGPT key!")
return
if (isStream) {
const inputWord = prompt.shortDescPromptStream.content
const { chat, getChatInput } = useChatGPT()
formData.desc = ""
const chatResp = await chat(inputWord, {
name: "desc",
systemMessage: getChatInput(formData?.md, formData.html),
stream: true,
onProgress: (partialResponse) => {
formData.desc = partialResponse.text
logger.debug("partialResponse=>", partialResponse.text)
},
timeoutMs: 2 * 60 * 1000,
})
logger.debug("chatResp=>", chatResp)
// formData.desc = chatResp.text
} else {
const inputWord = prompt.shortDescPrompt.content
const { chat, getChatInput } = useChatGPT()
const chatText = await chat(inputWord, {
name: "desc",
systemMessage: getChatInput(formData?.md, formData.html),
})
if (StrUtil.isEmptyString(chatText)) {
ElMessage.error("请求错误,请在偏好设置配置请求地址和ChatGPT key!")
return
}
const resJson = JsonUtil.safeParse<ShortDescAIResult>(chatText, {} as ShortDescAIResult)
if (StrUtil.isEmptyString(resJson?.desc)) {
throw new Error("文档信息量太少,未能抽取有效信息")
}
formData.desc = resJson.desc
logger.info("使用AI智能生成的摘要结果 =>", {
inputWord: inputWord,
chatText: chatText,
})
}
const resJson = JsonUtil.safeParse<ShortDescAIResult>(chatText, {} as ShortDescAIResult)
if (StrUtil.isEmptyString(resJson?.desc)) {
throw new Error("文档信息量太少,未能抽取有效信息")
}
formData.desc = resJson.desc
logger.info("使用AI智能生成的摘要结果 =>", {
inputWord: inputWord,
chatText: chatText,
})

// 自部署无监督摘要
// if (StrUtil.isEmptyString(formData.html)) {
Expand Down
21 changes: 19 additions & 2 deletions src/composables/useChatGPT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ const useChatGPT = () => {
debug: isDev,
// workaround for https://github.com/transitive-bullshit/chatgpt-api/issues/592
fetch: self.fetch.bind(self),
completionParams: {
model: pref.value.experimentalAIApiModel,
max_tokens: pref.value.experimentalAIApiMaxTokens,
temperature: pref.value.experimentalAIApiTemperature,
},
})
}
} catch (e) {
Expand All @@ -89,16 +94,28 @@ const useChatGPT = () => {
* const chatResponse = await chat('你好,ChatGPT!');
* console.log(chatResponse); // ChatGPT 生成的响应
*/
const chat = async (q: string, opts?: SendMessageOptions): Promise<string> => {
const chat = async (q: string, opts?: SendMessageOptions): Promise<string | any> => {
try {
const api = await getAPI()
// 使用 ChatGPTAPI 实例进行聊天操作
opts.completionParams = {
...opts.completionParams,
model: pref.value.experimentalAIApiModel,
max_tokens: pref.value.experimentalAIApiMaxTokens,
temperature: pref.value.experimentalAIApiTemperature,
}

logger.debug("chat q =>", { q, opts })
const res = await api.sendMessage(q, opts)
logger.debug("chat res =>", res)
return res.text
if (opts.stream) {
return res
} else {
return res.text
}
} catch (e) {
logger.error("Chat encountered an error:", e)
ElMessage.error("Chat encountered an error:" + e)
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/models/publishPreferenceCfg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ class PublishPreferenceCfg extends PreferenceConfig {
*/
public experimentalAIProxyUrl?: string

/**
* AI 模型
*/
public experimentalAIApiModel?: string

/**
* AI token 数目
*/
public experimentalAIApiMaxTokens?: number

/**
* AI 温度
*/
public experimentalAIApiTemperature?: number

// 文档菜单
/**
* 是否展示文档快捷菜单
Expand Down
3 changes: 3 additions & 0 deletions src/stores/usePreferenceSettingStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ const usePreferenceSettingStore = () => {
prefConfig.value.experimentalAIProxyUrl = snAiCfg.apiProxy
prefConfig.value.experimentalAICode = snAiCfg.apiKey
prefConfig.value.experimentalAIBaseUrl = snAiCfg.apiBaseURL
prefConfig.value.experimentalAIApiModel = snAiCfg.apiModel
prefConfig.value.experimentalAIApiMaxTokens = snAiCfg.apiMaxTokens
prefConfig.value.experimentalAIApiTemperature = snAiCfg.apiTemperature
logger.info("use siyuan-note ai config")
} else {
prefConfig.value.experimentalUseSiyuanNoteAIConfig = false
Expand Down

0 comments on commit 0a46ea6

Please sign in to comment.