chatgpt调整

This commit is contained in:
xiaoqi.cxq 2023-06-13 18:32:49 +08:00
parent c1232b59db
commit 90d887519d
6 changed files with 23 additions and 141 deletions

View File

@ -42,7 +42,6 @@ import SponsorModal from './modals/SponsorModal';
import CommitMessageModal from './modals/CommitMessageModal'; import CommitMessageModal from './modals/CommitMessageModal';
import WorkspaceImgPathModal from './modals/WorkspaceImgPathModal'; import WorkspaceImgPathModal from './modals/WorkspaceImgPathModal';
import ChatGptModal from './modals/ChatGptModal'; import ChatGptModal from './modals/ChatGptModal';
import ChatGptConfigModal from './modals/ChatGptConfigModal';
// Providers // Providers
import GooglePhotoModal from './modals/providers/GooglePhotoModal'; import GooglePhotoModal from './modals/providers/GooglePhotoModal';
@ -114,7 +113,6 @@ export default {
CommitMessageModal, CommitMessageModal,
WorkspaceImgPathModal, WorkspaceImgPathModal,
ChatGptModal, ChatGptModal,
ChatGptConfigModal,
// Providers // Providers
GooglePhotoModal, GooglePhotoModal,
GoogleDriveAccountModal, GoogleDriveAccountModal,

View File

@ -1,69 +0,0 @@
<template>
<modal-inner aria-label="ChatGPT配置">
<div class="modal__content">
<div class="modal__image">
<icon-chat-gpt></icon-chat-gpt>
</div>
<p> <b>ChatGPT</b> 配置<br>官方的接口地址在国内可能无法正常访问可以自行找代理地址</p>
<form-entry label="代理地址" error="proxyHost">
<input slot="field" class="textfield" type="text" v-model.trim="proxyHost" @keydown.enter="resolve()">
<div class="form-entry__info">
<b>非必填默认是官方接口地址(https://api.openai.com):</b> https://openai.geekr.cool
</div>
</form-entry>
<form-entry label="apiKey" error="apiKey">
<input slot="field" class="textfield" type="text" v-model.trim="apiKey" @keydown.enter="resolve()">
<div class="form-entry__info">
<b>apiKey</b> 请到 <a href="https://platform.openai.com/account/api-keys" target="_blank">https://platform.openai.com/account/api-keys</a> <br>
</div>
</form-entry>
<form-entry label="采样温度" error="temperature">
<input slot="field" class="textfield" type="number" v-model.trim="temperature" @keydown.enter="resolve()">
<div class="form-entry__info">
<b>采样温度</b>介于 0 2 之间较高的值 0.8将使输出更加随机而较低的值 0.2将使输出更加集中和确定<br>
</div>
</form-entry>
</div>
<div class="modal__button-bar">
<button class="button" @click="config.reject()">取消</button>
<button class="button button--resolve" @click="resolve()">确认</button>
</div>
</modal-inner>
</template>
<script>
import modalTemplate from './common/modalTemplate';
export default modalTemplate({
data: () => ({
apiKey: null,
proxyHost: null,
temperature: 1,
}),
methods: {
resolve() {
if (!this.apiKey) {
this.setError('apiKey');
return;
}
if (this.temperature < 0 || this.temperature > 2) {
this.setError('temperature');
return;
}
if (this.proxyHost && this.proxyHost.endsWith('/')) {
this.proxyHost = this.proxyHost.substring(0, this.proxyHost.length - 1);
}
this.config.resolve({
apiKey: this.apiKey,
proxyHost: this.proxyHost,
temperature: parseFloat(this.temperature),
});
},
},
mounted() {
this.apiKey = this.config.apiKey;
this.proxyHost = this.config.proxyHost;
this.temperature = this.config.temperature || this.temperature;
},
});
</script>

View File

@ -6,17 +6,9 @@
</div> </div>
<p><b>ChatGPT内容生成</b><br>生成时长受ChatGPT服务响应与网络响应时长影响时间可能较长</p> <p><b>ChatGPT内容生成</b><br>生成时长受ChatGPT服务响应与网络响应时长影响时间可能较长</p>
<form-entry label="生成内容要求详细描述" error="content"> <form-entry label="生成内容要求详细描述" error="content">
<textarea slot="field" class="text-input" type="text" placeholder="输入内容(支持换行)" v-model.trim="content" :disabled="generating || !chatGptConfig.apiKey"></textarea> <textarea slot="field" class="text-input" type="text" placeholder="输入内容(支持换行)" v-model.trim="content" :disabled="generating"></textarea>
<div class="form-entry__info"> <div class="form-entry__info">
<span v-if="!chatGptConfig.apiKey" class="config-warning"> 使用 <a href="https://chat.forefront.ai" target="_blank">https://chat.forefront.ai</a> AIGPT-3.5 Turbo
未配置apiKey请点击 <a href="javascript:void(0)" @click="openConfig">配置</a> apiKey
</span>
<span v-else>
<span v-if="chatGptConfig.proxyHost">
<b>当前使用的接口代理</b>{{ chatGptConfig.proxyHost }}
</span>
<a href="javascript:void(0)" @click="openConfig">修改apiKey配置</a>
</span>
</div> </div>
</form-entry> </form-entry>
<div class="modal__result"> <div class="modal__result">
@ -33,7 +25,6 @@
</template> </template>
<script> <script>
import { mapGetters } from 'vuex';
import modalTemplate from './common/modalTemplate'; import modalTemplate from './common/modalTemplate';
import chatGptSvc from '../../services/chatGptSvc'; import chatGptSvc from '../../services/chatGptSvc';
import store from '../../store'; import store from '../../store';
@ -45,14 +36,9 @@ export default modalTemplate({
result: '', result: '',
xhr: null, xhr: null,
}), }),
computed: {
...mapGetters('chatgpt', [
'chatGptConfig',
]),
},
methods: { methods: {
resolve(evt) { resolve(evt) {
evt.preventDefault(); // Fixes https://github.com/mafgwo/stackedit/issues/1503 evt.preventDefault();
const { callback } = this.config; const { callback } = this.config;
this.config.resolve(); this.config.resolve();
callback(this.result); callback(this.result);
@ -74,27 +60,13 @@ export default modalTemplate({
this.result = ''; this.result = '';
try { try {
this.xhr = chatGptSvc.chat({ this.xhr = chatGptSvc.chat({
proxyHost: this.chatGptConfig.proxyHost,
apiKey: this.chatGptConfig.apiKey,
content: `${this.content}\n(使用Markdown方式输出结果)`, content: `${this.content}\n(使用Markdown方式输出结果)`,
temperature: this.chatGptConfig.temperature || 1,
}, this.process); }, this.process);
} catch (err) { } catch (err) {
this.generating = false; this.generating = false;
store.dispatch('notification/error', err); store.dispatch('notification/error', err);
} }
}, },
async openConfig() {
try {
const config = await store.dispatch('modal/open', {
type: 'chatGptConfig',
apiKey: this.chatGptConfig.apiKey,
proxyHost: this.chatGptConfig.proxyHost,
temperature: this.chatGptConfig.temperature,
});
store.dispatch('chatgpt/setCurrConfig', config);
} catch (e) { /* Cancel */ }
},
reject() { reject() {
if (this.generating) { if (this.generating) {
if (this.xhr) { if (this.xhr) {

View File

@ -2,21 +2,24 @@ import store from '../store';
export default { export default {
chat({ chat({
proxyHost,
apiKey,
content, content,
temperature,
}, callback) { }, callback) {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
const url = `${proxyHost || 'https://api.openai.com'}/v1/chat/completions`; const url = 'https://streaming.tenant-forefront-default.knative.chi.coreweave.com/free-chat';
xhr.open('POST', url); xhr.open('POST', url);
xhr.setRequestHeader('Authorization', `Bearer ${apiKey}`); xhr.setRequestHeader('Authorization', 'Bearer null');
xhr.setRequestHeader('Content-Type', 'application/json'); xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({ xhr.send(JSON.stringify({
model: 'gpt-3.5-turbo', model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content }], action: 'noauth',
temperature: temperature || 1, text: content,
stream: true, messagePersona: '607e41fe-95be-497e-8e97-010a59b2e2c0',
messages: [],
internetMode: 'auto',
hidden: false,
id: '',
parentId: '',
workspaceId: '',
})); }));
let lastRespLen = 0; let lastRespLen = 0;
xhr.onprogress = () => { xhr.onprogress = () => {
@ -25,12 +28,17 @@ export default {
responseText.split('\n\n') responseText.split('\n\n')
.filter(l => l.length > 0) .filter(l => l.length > 0)
.forEach((text) => { .forEach((text) => {
const item = text.substr(6); if (text === 'event: end') {
if (item === '[DONE]') {
callback({ done: true }); callback({ done: true });
} else { } else if (text.startsWith('event: message')) {
const item = text.split('\n')[1].substr(6);
const data = JSON.parse(item); const data = JSON.parse(item);
callback({ content: data.choices[0].delta.content }); if (data.error) {
store.dispatch('notification/error', `ChatGPT接口报错,错误信息:${data.error.message}`);
callback({ error: 'ChatGPT接口请求异常' });
} else {
callback({ content: data.delta });
}
} }
}); });
}; };

View File

@ -1,25 +0,0 @@
const chatgptConfigKey = 'chatgpt/config';
export default {
namespaced: true,
state: {
config: {
apiKey: null,
proxyHost: null,
},
},
mutations: {
setCurrConfig: (state, value) => {
state.config = value;
},
},
getters: {
chatGptConfig: state => state.config,
},
actions: {
setCurrConfig({ commit }, value) {
commit('setCurrConfig', value);
localStorage.setItem(chatgptConfigKey, JSON.stringify(value));
},
},
};

View File

@ -20,7 +20,6 @@ import userInfo from './userInfo';
import workspace from './workspace'; import workspace from './workspace';
import img from './img'; import img from './img';
import theme from './theme'; import theme from './theme';
import chatgpt from './chatgpt';
import locationTemplate from './locationTemplate'; import locationTemplate from './locationTemplate';
import emptyPublishLocation from '../data/empties/emptyPublishLocation'; import emptyPublishLocation from '../data/empties/emptyPublishLocation';
import emptySyncLocation from '../data/empties/emptySyncLocation'; import emptySyncLocation from '../data/empties/emptySyncLocation';
@ -52,7 +51,6 @@ const store = new Vuex.Store({
workspace, workspace,
img, img,
theme, theme,
chatgpt,
}, },
state: { state: {
light: false, light: false,