Skip to content

Commit

Permalink
fix: 进一步优化 AI 效果
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Oct 11, 2023
1 parent ad93890 commit b2ccb55
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 45 deletions.
14 changes: 5 additions & 9 deletions src/components/publish/form/CommonCategories.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@
<script setup lang="ts">
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { useVueI18n } from "~/src/composables/useVueI18n.ts"
import { nextTick, reactive, ref } from "vue"
import { watch } from "vue"
import { nextTick, reactive, ref, watch } from "vue"
import { CategoryAIResult, prompt } from "~/src/utils/ai/prompt.ts"
import { useChatGPT } from "~/src/composables/useChatGPT.ts"
import { HtmlUtil, JsonUtil, StrUtil } from "zhi-common"
import { JsonUtil, StrUtil } from "zhi-common"
import { ElMessage } from "element-plus"
import { AiConstants } from "~/src/utils/ai/AiConstants.ts"
const logger = createAppLogger("common-categories")
const { t } = useVueI18n()
Expand Down Expand Up @@ -113,12 +111,10 @@ const cateMethods = {
formData.isLoading = true
const inputWord = prompt.categoryPrompt.content
const { chat } = useChatGPT()
const { chat, getChatInput } = useChatGPT()
const chatText = await chat(inputWord, {
name: "categories",
systemMessage:
formData.md?.substring(0, AiConstants.MAX_INPUT_TOKEN_LENGTH) ??
HtmlUtil.parseHtml(formData.html, AiConstants.MAX_INPUT_TOKEN_LENGTH, true),
systemMessage: getChatInput(formData?.md, formData.html),
})
if (StrUtil.isEmptyString(chatText)) {
ElMessage.error("请求错误,请在偏好设置配置请求地址和ChatGPT key!")
Expand Down Expand Up @@ -215,4 +211,4 @@ const cateMethods = {
.recomm-cate
margin 0 10px
</style>
</style>
16 changes: 6 additions & 10 deletions src/components/publish/form/PublishCategories.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@

<script setup lang="ts">
import { CategoryTypeEnum } from "zhi-blog-api"
import { reactive, toRaw } from "vue"
import { reactive, toRaw, watch } from "vue"
import { ICategoryConfig, IMultiCategoriesConfig } from "~/src/types/ICategoryConfig.ts"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { useVueI18n } from "~/src/composables/useVueI18n.ts"
import { watch } from "vue"
import { CategoryAIResult, prompt, TagAIResult } from "~/src/utils/ai/prompt.ts"
import { CategoryAIResult, prompt } from "~/src/utils/ai/prompt.ts"
import { useChatGPT } from "~/src/composables/useChatGPT.ts"
import { HtmlUtil, JsonUtil, StrUtil } from "zhi-common"
import { JsonUtil, StrUtil } from "zhi-common"
import { ElMessage } from "element-plus"
import { AiConstants } from "~/src/utils/ai/AiConstants.ts"
const logger = createAppLogger("publish-categories")
const { t } = useVueI18n()
Expand Down Expand Up @@ -108,12 +106,10 @@ const fetchCate = async () => {
formData.isLoading = true
const inputWord = prompt.categoryPrompt.content
const { chat } = useChatGPT()
const { chat, getChatInput } = useChatGPT()
const chatText = await chat(inputWord, {
name: "categories",
systemMessage:
formData.md?.substring(0, AiConstants.MAX_INPUT_TOKEN_LENGTH) ??
HtmlUtil.parseHtml(formData.html, AiConstants.MAX_INPUT_TOKEN_LENGTH, true),
systemMessage: getChatInput(formData?.md, formData.html),
})
if (StrUtil.isEmptyString(chatText)) {
ElMessage.error("请求错误,请在偏好设置配置请求地址和ChatGPT key!")
Expand Down Expand Up @@ -184,4 +180,4 @@ const fetchCate = async () => {
.recomm-cate
margin 0 10px
</style>
</style>
9 changes: 3 additions & 6 deletions src/components/publish/form/PublishDescription.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ import { useVueI18n } from "~/src/composables/useVueI18n.ts"
import { reactive, watch } from "vue"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { ElMessage } from "element-plus"
import { HtmlUtil, JsonUtil, SmartUtil, StrUtil } from "zhi-common"
import { JsonUtil, StrUtil } from "zhi-common"
import { prompt, ShortDescAIResult } from "~/src/utils/ai/prompt.ts"
import { useChatGPT } from "~/src/composables/useChatGPT.ts"
import { AiConstants } from "~/src/utils/ai/AiConstants.ts"
const logger = createAppLogger("publish-description")
const { t } = useVueI18n()
Expand Down Expand Up @@ -98,12 +97,10 @@ const handleMakeDesc = async () => {
try {
// if (formData.useAi) {
const inputWord = prompt.shortDescPrompt.content
const { chat } = useChatGPT()
const { chat, getChatInput } = useChatGPT()
const chatText = await chat(inputWord, {
name: "desc",
systemMessage:
formData.md?.substring(0, AiConstants.MAX_INPUT_TOKEN_LENGTH) ??
HtmlUtil.parseHtml(formData.html, AiConstants.MAX_INPUT_TOKEN_LENGTH, true),
systemMessage: getChatInput(formData?.md, formData.html),
})
if (StrUtil.isEmptyString(chatText)) {
ElMessage.error("请求错误,请在偏好设置配置请求地址和ChatGPT key!")
Expand Down
9 changes: 3 additions & 6 deletions src/components/publish/form/PublishTags.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ import { useVueI18n } from "~/src/composables/useVueI18n.ts"
import { nextTick, reactive, ref, watch } from "vue"
import { ElMessage } from "element-plus"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { HtmlUtil, JsonUtil, StrUtil } from "zhi-common"
import { JsonUtil, StrUtil } from "zhi-common"
import { prompt, TagAIResult } from "~/src/utils/ai/prompt.ts"
import { useChatGPT } from "~/src/composables/useChatGPT.ts"
import { AiConstants } from "~/src/utils/ai/AiConstants.ts"
const logger = createAppLogger("publish-tags")
const { t } = useVueI18n()
Expand Down Expand Up @@ -116,12 +115,10 @@ const tagMethods = {
formData.isTagLoading = true
const inputWord = prompt.tagPrompt.content
const { chat } = useChatGPT()
const { chat, getChatInput } = useChatGPT()
const chatText = await chat(inputWord, {
name: "tags",
systemMessage:
formData.md?.substring(0, AiConstants.MAX_INPUT_TOKEN_LENGTH) ??
HtmlUtil.parseHtml(formData.html, AiConstants.MAX_INPUT_TOKEN_LENGTH, true),
systemMessage: getChatInput(formData?.md, formData.html),
})
if (StrUtil.isEmptyString(chatText)) {
ElMessage.error("请求错误,请在偏好设置配置请求地址和ChatGPT key!")
Expand Down
23 changes: 10 additions & 13 deletions src/components/publish/form/PublishTitle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@
<script setup lang="ts">
import { reactive, watch } from "vue"
import { useVueI18n } from "~/src/composables/useVueI18n.ts"
import { HtmlUtil, JsonUtil, StrUtil } from "zhi-common"
import { JsonUtil, StrUtil } from "zhi-common"
import { ElMessage } from "element-plus"
import { useChatGPT } from "~/src/composables/useChatGPT.ts"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { prompt, TitleAIResult } from "~/src/utils/ai/prompt.ts"
import { AiConstants } from "~/src/utils/ai/AiConstants.ts"
const logger = createAppLogger("publish-title")
const { t } = useVueI18n()
Expand Down Expand Up @@ -77,12 +76,12 @@ watch(
}
)
// watch(
// () => props.md,
// (newValue) => {
// formData.md = newValue
// }
// )
watch(
() => props.md,
(newValue) => {
formData.md = newValue
}
)
watch(
() => props.html,
Expand All @@ -101,13 +100,11 @@ const handleMakeTitle = async () => {
try {
formData.isLoading = true
const inputWord = prompt.titlePrompt.content
const { chat } = useChatGPT()
debugger
const { chat, getChatInput } = useChatGPT()
const chatText = await chat(inputWord, {
name: "title",
systemMessage:
formData.md?.substring(0, AiConstants.MAX_INPUT_TOKEN_LENGTH) ??
HtmlUtil.parseHtml(formData.html, AiConstants.MAX_INPUT_TOKEN_LENGTH, true),
systemMessage: getChatInput(formData?.md, formData.html),
})
if (StrUtil.isEmptyString(chatText)) {
ElMessage.error("请求错误,请在偏好设置配置请求地址和ChatGPT key!")
Expand Down
16 changes: 15 additions & 1 deletion src/composables/useChatGPT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
*/

import { usePublishPreferenceSetting } from "~/src/stores/usePublishPreferenceSetting.ts"
import { StrUtil } from "zhi-common"
import { HtmlUtil, StrUtil } from "zhi-common"
import { ChatGPTAPI, ChatGPTUnofficialProxyAPI, SendMessageOptions } from "chatgpt"
import { Utils } from "~/src/utils/utils.ts"
import { isDev } from "~/src/utils/constants.ts"
import { createAppLogger } from "~/src/utils/appLogger.ts"
import { AiConstants } from "~/src/utils/ai/AiConstants.ts"

/**
* 创建一个用于与 ChatGPT 服务进行交互的钩子
Expand Down Expand Up @@ -96,7 +97,20 @@ const useChatGPT = () => {
}
}

/**
* 获取聊天输入
*
* @param input1 可能为空的输入1,优先级高
* @param input2 不为空的输入2,优先级低
*/
const getChatInput = (input1: string, input2: string) => {
const md = input1.substring(0, AiConstants.MAX_INPUT_TOKEN_LENGTH)
const html = HtmlUtil.parseHtml(input2, AiConstants.MAX_INPUT_TOKEN_LENGTH, true)
return Utils.emptyOrDefault(md, html)
}

return {
getChatInput,
chat,
}
}
Expand Down

0 comments on commit b2ccb55

Please sign in to comment.