From c6099eb8d9af974ffb84d5503b49c10a89c7a082 Mon Sep 17 00:00:00 2001 From: weixin_44463441 Date: Thu, 11 Jan 2024 12:02:00 +0800 Subject: [PATCH] Thu Jan 11 12:02:00 CST 2024 inscode --- .inscode | 11 +++------- src/App.vue | 16 +++++++++++++-- src/components/content/index.vue | 34 +++++++++++++++++++++++++++++++ src/js/config.js | 10 +++++---- src/js/inscodeai.js | 17 ++++++++++------ src/js/repEs.js | 35 ++++++++++++++++++++++++++++++-- tailwind.config.js | 1 + vite.config.js | 7 ++++++- 8 files changed, 108 insertions(+), 23 deletions(-) create mode 100644 src/components/content/index.vue diff --git a/.inscode b/.inscode index a69885c..a096644 100644 --- a/.inscode +++ b/.inscode @@ -1,14 +1,9 @@ -run = "cd dist; python3 -m http.server" +run = "npm i -f && npm run dev" language = "node" [deployment] -build = "npm i && npm run build" -run = "cd dist; python3 -m http.server" - -[env] -PATH = "/root/${PROJECT_DIR}/.config/npm/node_global/bin:/root/${PROJECT_DIR}/node_modules/.bin:${PATH}" -XDG_CONFIG_HOME = "/root/.config" -npm_config_prefix = "/root/${PROJECT_DIR}/.config/npm/node_global" +build = "npm i -f && npm run build" +run = "npm run preview" [debugger] program = "main.js" diff --git a/src/App.vue b/src/App.vue index 705e3fe..43c89fa 100644 --- a/src/App.vue +++ b/src/App.vue @@ -67,6 +67,9 @@
+ + +
@@ -159,12 +162,16 @@ // import { Configuration, OpenAIApi } from "openai" import OpenAI from './js/openai.js' import Config from './js/config.js' +import InsCodeAI from './js/inscodeai.js' + import SDApi from './js/sd.js' import StorageApi from './js/storage.js' import PodsApi from './js/pod.js' import RepEs from './js/repEs.js' import { MessagePlugin } from 'tdesign-vue-next'; +import Content from './components/content/index.vue' + // import MarkdownItVue from 'markdown-it-vue' // import 'markdown-it-vue/dist/markdown-it-vue.css' @@ -177,7 +184,7 @@ import 'markdown-it-vue/dist/markdown-it-vue.css' export default { name: 'LlmApp', components: { - MarkdownItVue + MarkdownItVue, Content }, data() { return { @@ -276,7 +283,8 @@ export default { }) }, initAIClient () { - this.client = new OpenAI(this.config) + // this.client = new OpenAI(this.config) + this.client = new InsCodeAI(this.config) }, initIndexClient () { if (this.config?.index_url) { @@ -329,6 +337,8 @@ export default { }).catch((err) => { this.getAnswer(null, newPrompt, currentMsg) }) + }else { + this.getAnswer(null, newPrompt, currentMsg) } } // if (this.indexClient) { @@ -408,6 +418,7 @@ export default { { onmessage: (msg, isPart) => { // _this.set(_this.message, id, currentMsg) + // console.info(msg) if (isPart) { currentMsg.message += msg } else { @@ -564,6 +575,7 @@ export default { }) }, initChat() { + this.initAIClient() this.initIndexClient() const config = this.config diff --git a/src/components/content/index.vue b/src/components/content/index.vue new file mode 100644 index 0000000..ac0bcc1 --- /dev/null +++ b/src/components/content/index.vue @@ -0,0 +1,34 @@ + + \ No newline at end of file diff --git a/src/js/config.js b/src/js/config.js index b99ef7e..ac39f01 100644 --- a/src/js/config.js +++ b/src/js/config.js @@ -36,7 +36,7 @@ Inscode文档地址:https://inscode-doc.inscode.cc/ // 外部内容库,没有时指定为null,你也可以替换为自己的知识库 // const index_url = null -const index_url = "https://api.quickapi.cloud/api/b/v1/es-api" +const index_url = "https://es-api.inscode.cc/" // 默认提示词 const default_prompt = '介绍下InsCode' @@ -63,9 +63,11 @@ export default { "prompt_template": prompt_template, "api_prompt_prefix":"结合上下文回答问题,如果问题不是技术问题,请直接回答不知道。\n问题为:", "show_profile_setting":false, - // "index_url": index_url, - // "index_name":"inscode", - // "index_type":"es", + + "search_url": index_url, + "index_name":"inscode-rag", + "index_type":"es", + "welcome_text": welcome_text, "temperature": 0.1 }, diff --git a/src/js/inscodeai.js b/src/js/inscodeai.js index af823b9..9a578c0 100644 --- a/src/js/inscodeai.js +++ b/src/js/inscodeai.js @@ -1,7 +1,10 @@ import { fetchEventSource } from '@microsoft/fetch-event-source'; import Prompt from './prompt.js' -class OpenAI { +const apiKey = process.env.INSCODE_API_KEY; +const apiUrl = 'https://inscode-api.csdn.net/api/v1/gpt/'; + +class InsCodeAI { constructor(config) { this.config = config @@ -22,7 +25,7 @@ class OpenAI { const mode = config?.mode??'chat' const token = config?.token??'empty' - const url = config.api_url + (mode === 'chat' ? '/chat/completions' : '/completions') + const url = apiUrl const stop = config?.stop??'[DONE]' const max_tokens = config?.api_max_token??512 const model = config?.model??'vicuna-13b-all-v1.1' @@ -38,7 +41,8 @@ class OpenAI { temperature: parseFloat(temperature), top_p: parseFloat(top_p), stream: true, - stop: stop_key + stop: stop_key, + apikey: apiKey, // prefix: prefix } @@ -63,12 +67,13 @@ class OpenAI { if (callback?.onclose) { callback?.onclose() } - abortController.abort(); + // abortController.abort(); return } else { - console.info(msg.data) + // console.info(msg.data) const jsonData = JSON.parse(msg.data) + // 和上面重复触发,只留一个 // if (jsonData.choices[0].finish_reason === 'stop') { // if (callback?.onclose) { @@ -126,4 +131,4 @@ class OpenAI { } -export default OpenAI \ No newline at end of file +export default InsCodeAI \ No newline at end of file diff --git a/src/js/repEs.js b/src/js/repEs.js index 3762dc8..cce9583 100644 --- a/src/js/repEs.js +++ b/src/js/repEs.js @@ -7,12 +7,12 @@ class RepEs { this.config = config this.index_url = config.index_url this.index_name = config.index_name - this.url = this.index_url + '?index_name=' + this.index_name + this.token = process.env?.INSCODE_API_KEY??'default' } query (keyword) { - const url = this.url + const url = this.index_url + '?user_token=' + this.token + '&index_name=' + this.index_name return new Promise(function(resolve,rejcet){ axios.get(url,{ params: { @@ -41,6 +41,37 @@ class RepEs { } + add (content) { + debugger + const url = this.index_url + '/' + this.index_name + return new Promise(function(resolve,rejcet){ + axios.put(url,{ + data: { + content: content, + } + }).then(res => { + + if (res?.status === 200 && res?.data?.code === 200) { + const result = [] + res?.data?.data?.hits.forEach(element => { + result.push({ + page_content: element?._source?.content + }) + }); + resolve(result) + + } + console.info(res) + + + }).catch((err) => { + console.error(err) + rejcet() + }) + }) + + } + } export default RepEs \ No newline at end of file diff --git a/tailwind.config.js b/tailwind.config.js index 8bea3f5..d2574d5 100644 --- a/tailwind.config.js +++ b/tailwind.config.js @@ -2,6 +2,7 @@ module.exports = { content: [ "./index.html", + "./src/components/*.{vue,js,ts,jsx,tsx}", "./src/*.{vue,js,ts,jsx,tsx}", "./src/**/*.{vue,js,ts,jsx,tsx}", ], diff --git a/vite.config.js b/vite.config.js index 4ad898d..e6011ca 100644 --- a/vite.config.js +++ b/vite.config.js @@ -6,5 +6,10 @@ export default defineConfig({ server: { host: true }, - plugins: [vue()] + plugins: [vue()], + define: { + 'process.env': { + INSCODE_API_KEY: process.env.INSCODE_API_KEY + } + } }) -- GitLab