From 3b383845e5ff3fff7b46cfbed2efb41eecc1eed8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E9=98=B3?= <3311118881@qq.com> Date: Thu, 26 Sep 2024 21:15:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90AI=E7=BB=AD=E5=86=99=E3=80=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E3=80=81=E9=97=AE=E7=AD=94=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/Ai.ts | 3 + .../Create/components/VditorMD/index.tsx | 2 +- src/pages/Create/index.tsx | 92 ++++++++++++++++--- src/types/env.d.ts | 3 + vite.config.js | 9 +- 5 files changed, 91 insertions(+), 18 deletions(-) create mode 100644 src/api/Ai.ts diff --git a/src/api/Ai.ts b/src/api/Ai.ts new file mode 100644 index 0000000..0951c61 --- /dev/null +++ b/src/api/Ai.ts @@ -0,0 +1,3 @@ +import Request from '@/utils/request' + +// export \ No newline at end of file diff --git a/src/pages/Create/components/VditorMD/index.tsx b/src/pages/Create/components/VditorMD/index.tsx index e1ed942..58bf70b 100644 --- a/src/pages/Create/components/VditorMD/index.tsx +++ b/src/pages/Create/components/VditorMD/index.tsx @@ -208,7 +208,7 @@ const VditorEditor = ({ value, getValue }: VditorProps) => { // 监听 value 变化并更新编辑器内容 useEffect(() => { - if (vd && value) { + if (vd && value !== undefined && value !== vd.getValue()) { vd.setValue(value); } }, [value, vd]); diff --git a/src/pages/Create/index.tsx b/src/pages/Create/index.tsx index 17bcce9..92732cf 100644 --- a/src/pages/Create/index.tsx +++ b/src/pages/Create/index.tsx @@ -10,21 +10,6 @@ import { Article } from '@/types/app/article'; import { getArticleDataAPI } from '@/api/Article' import { useSearchParams } from 'react-router-dom'; -const items: MenuProps['items'] = [ - { - key: '1', - label: 'AI 续写', - }, - { - key: '2', - label: 'AI 优化', - }, - { - key: '3', - label: 'AI 生成', - }, -]; - const CreatePage = () => { const [params] = useSearchParams() const id = +params.get('id')! @@ -58,6 +43,83 @@ const CreatePage = () => { setData({ ...data, content }) }, [content]) + + // 解析接口数据 + const parsingData = async (command: string) => { + const res = await fetch(`/ai/v1/chat/completions`, { + method: "POST", + headers: { + "Content-Type": "application/json", + "Authorization": `Bearer ${import.meta.env.VITE_AI_APIPassword}` + }, + body: JSON.stringify({ + model: import.meta.env.VITE_AI_MODEL, + messages: [{ + role: "user", + content: `${command}${content}` + }], + stream: true + }) + }); + + const reader = res.body.getReader(); + const decoder = new TextDecoder("utf-8"); + + let receivedText = ""; + + while (true) { + const { done, value } = await reader.read(); + if (done) break; + receivedText += decoder.decode(value, { stream: true }); + + // 处理每一块数据 + const lines = receivedText.split("\n"); + for (let i = 0; i < lines.length - 1; i++) { + const line = lines[i].trim(); + if (line.startsWith("data:")) { + const jsonString = line.substring(5).trim(); + if (jsonString !== "[DONE]") { + const data = JSON.parse(jsonString); + console.log("Received chunk:", data.choices[0].delta.content); + setContent((content) => content + data.choices[0].delta.content); + // 在这里处理每一块数据 + } else { + console.log("Stream finished."); + return; + } + } + } + + // 保留最后一行未处理的数据 + receivedText = lines[lines.length - 1]; + } + } + + // AI功能 + const items: MenuProps['items'] = [ + { + key: '1', + label: 'AI 续写', + onClick: async () => { + parsingData("帮我续写:") + }, + }, + { + key: '2', + label: 'AI 优化', + onClick: async () => { + parsingData("帮我优化该文章,意思不变:") + }, + }, + { + key: '3', + label: 'AI 生成', + onClick: async () => { + parsingData("") + }, + }, + ]; + return ( <> diff --git a/src/types/env.d.ts b/src/types/env.d.ts index ed5c3ca..0966eb1 100644 --- a/src/types/env.d.ts +++ b/src/types/env.d.ts @@ -4,6 +4,9 @@ interface ImportMetaEnv { readonly VITE_BAIDU_TONGJI_SITE_ID: string; readonly VITE_BAIDU_TONGJI_ACCESS_TOKEN: string; readonly VITE_BAIDU_TONGJI_REFRESH_TOKEN: string; + + readonly VITE_AI_APIPassword: string; + readonly VITE_AI_MODEL: string; } interface ImportMeta { diff --git a/vite.config.js b/vite.config.js index f5c97ec..9367841 100644 --- a/vite.config.js +++ b/vite.config.js @@ -24,15 +24,20 @@ export default defineConfig({ server: { proxy: { '/api': { - target: 'https://openapi.baidu.com/', // 你的后端服务器地址 + target: 'https://openapi.baidu.com/', changeOrigin: true, rewrite: (path) => path.replace(/^\/api/, ''), }, '/qiniu': { - target: 'https://rsf.qiniuapi.com/', // 你的后端服务器地址 + target: 'https://rsf.qiniuapi.com/', changeOrigin: true, rewrite: (path) => path.replace(/^\/qiniu/, ''), }, + '/ai': { + target: 'https://spark-api-open.xf-yun.com/', + changeOrigin: true, + rewrite: (path) => path.replace(/^\/ai/, ''), + }, }, }, })