UPDATE

上级 8e3a89c0
run = "npm i && npm run dev" run = "npm i && npm run dev"
[deployment]
build = "npm i && npm run build"
run = "npm run preview"
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
"preview": "vite preview --port 4173" "preview": "vite preview --port 4173"
}, },
"dependencies": { "dependencies": {
"@microsoft/fetch-event-source": "^2.0.1",
"axios": "^1.4.0", "axios": "^1.4.0",
"event-source-polyfill": "^1.0.31",
"guess": "^1.0.2", "guess": "^1.0.2",
"view-ui-plus": "^1.3.9", "view-ui-plus": "^1.3.9",
"vue": "^3.2.37" "vue": "^3.2.37"
......
...@@ -21,10 +21,8 @@ ...@@ -21,10 +21,8 @@
</div> </div>
</template> </template>
<script> <script>
import { EventSourcePolyfill } from 'event-source-polyfill'; import { fetchEventSource } from '@microsoft/fetch-event-source';
import { apiKey, apiUrl } from './api';
const token = 'Bearer ZXlKMGVYQWlPaUpLVjFRaUxDSmhiR2NpT2lKSVV6STFOaUo5LmV5SjBkQ0k2TkN3aVlYVmtJam9pWXpkbU16WTVOMlpqTldaak5EVmpNR0l5WldFNE5UVTNaRFkxTnpnM1lXVWlMQ0pzZFNJNklrbHVjME52WkdVaUxDSmxlSEFpT2pFMk9EVTFORGczT1Rrc0luVnlJam95TENKcWRHa2lPaUpCVUVsZlZFOUxSVTVmWXpkbU16WTVOMlpqTldaak5EVmpNR0l5WldFNE5UVTNaRFkxTnpnM1lXVXROQ0o5LmlyLTJYa1A4dFhNaFVldnlzTFhkUlJsY1VBV0ZiaWE5em9ZdGN6VlpleFk=';
const api = 'https://api.ai100.ai/ai/api/ai/chat';
export default { export default {
data() { data() {
...@@ -56,43 +54,49 @@ export default { ...@@ -56,43 +54,49 @@ export default {
text: 'AI 思考中...' text: 'AI 思考中...'
}); });
const query = { const dialog = this.dialogs.find(item => item.id === aiDialogID);
prompt: '',
question,
stream: true
}
const source = new EventSourcePolyfill( /**
`${api}?question=${query.question}&prompt=${query.prompt}&stream=${query.stream}`, * 发送请求,InsCode 已经集成了 GPT 能力
* 在 vite.config.js 中已通过环境变量写入了 apiKey(该 key 是动态写入使用者的,在 IDE 中是作者,发布到社区是运行该作品的用户)
* 发布到社区后,将消耗运行者的额度
* 注意:如果部署应用,任何人通过部署后的域名访问应用时,都将消耗部署者的额度
*/
const body = {
messages: [
{ {
headers: { role: 'user',
Accept: '*/*', content: question
Authorization: token
} }
],
apikey: apiKey
} }
)
source.onopen = event => { const source = fetchEventSource(apiUrl, {
console.log("onopen", event); method: 'POST',
const dialog = this.dialogs.find(item => item.id === aiDialogID); headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body),
onopen: (response) => {
dialog.text = ''; dialog.text = '';
}; },
source.onmessage = event => { onmessage: (msg) => {
if (event.data === "[DONE]") { const data = JSON.parse(msg.data);
source.close(); const finish = data.choices[0].finish_reason === 'stop';
const content = data.choices[0].delta.content;
if (finish) {
this.loading = false; this.loading = false;
} } else if (content) {
if (event.data) { const text = content;
const data = JSON.parse(event.data);
const text = data.message.content.parts.join("");
console.log(text);
const dialog = this.dialogs.find(item => item.id === aiDialogID);
dialog.text += text; dialog.text += text;
} }
}; },
source.onerror = event => { onerror: (err) => {
console.log("error", event); console.log("error", err);
}; }
});
}, },
handleNewChat() { handleNewChat() {
this.dialogs = []; this.dialogs = [];
......
export const apiKey = process.env.INSCODE_API_KEY;
export const apiUrl = 'https://test-inscode-api.csdn.net/api/v1/gpt/';
\ No newline at end of file
...@@ -6,5 +6,10 @@ export default defineConfig({ ...@@ -6,5 +6,10 @@ export default defineConfig({
server: { server: {
host: true host: true
}, },
plugins: [vue()] plugins: [vue()],
define: {
'process.env': {
INSCODE_API_KEY: process.env.INSCODE_API_KEY
}
}
}) })
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册