update api integration
This commit is contained in:
1 parent
7880b1d5d0
commit
8c1f3ab04d
15 files changed
+155
-127
No files matched your search
+11
-1
@@ -8,6 +8,7 @@
|
||||
import { defineComponent } from 'vue';
|
||||
import { ElConfigProvider } from 'element-plus';
|
||||
import zhCn from 'element-plus/dist/locale/zh-cn.mjs';
|
||||
import { applicationOperator, userOperator } from './operators';
|
||||
|
||||
export default defineComponent({
|
||||
components: {
|
||||
@@ -18,6 +19,15 @@ export default defineComponent({
|
||||
locale: zhCn
|
||||
};
|
||||
},
|
||||
async mounted() {}
|
||||
async mounted() {
|
||||
const { data: user } = await userOperator.getMe();
|
||||
console.log('suer', user);
|
||||
this.$store.dispatch('setUser', user);
|
||||
const { data: applications } = await applicationOperator.getAll({
|
||||
user_id: user.id
|
||||
});
|
||||
console.log('applicatioins', applications);
|
||||
this.$store.dispatch('setApplications', applications);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
@@ -21,12 +21,17 @@ import { defineComponent } from 'vue';
|
||||
import { ElSelect, ElOption, ElDropdown, ElButton, ElDropdownItem } from 'element-plus';
|
||||
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
|
||||
import { ROUTE_CHAT_INDEX } from '@/router/constants';
|
||||
import {
|
||||
API_ID_CHATGPT,
|
||||
API_ID_CHATGPT4,
|
||||
API_ID_CHATGPT4_BROWSING,
|
||||
API_ID_CHATGPT_16K,
|
||||
API_ID_CHATGPT_BROWSING
|
||||
} from '@/operators/chat/constants';
|
||||
|
||||
export default defineComponent({
|
||||
name: 'ModelSelector',
|
||||
components: {
|
||||
ElSelect,
|
||||
ElOption,
|
||||
ElDropdown,
|
||||
ElButton,
|
||||
ElDropdownItem,
|
||||
@@ -38,7 +43,7 @@ export default defineComponent({
|
||||
required: true
|
||||
}
|
||||
},
|
||||
emits: ['update:modelValue'],
|
||||
emits: ['update:modelValue', 'select'],
|
||||
data() {
|
||||
return {
|
||||
value: this.modelValue,
|
||||
@@ -49,15 +54,15 @@ export default defineComponent({
|
||||
options: [
|
||||
{
|
||||
label: 'AI 问答',
|
||||
value: 'chatgpt'
|
||||
value: API_ID_CHATGPT
|
||||
},
|
||||
{
|
||||
label: 'AI 问答 3.5 - 16K',
|
||||
value: 'chatgpt-16k'
|
||||
value: API_ID_CHATGPT_16K
|
||||
},
|
||||
{
|
||||
label: 'AI 问答 3.5 - 联网版',
|
||||
value: 'chatgpt-browsing'
|
||||
value: API_ID_CHATGPT_BROWSING
|
||||
}
|
||||
]
|
||||
},
|
||||
@@ -67,11 +72,11 @@ export default defineComponent({
|
||||
options: [
|
||||
{
|
||||
label: 'AI 问答 4.0',
|
||||
value: 'chatgpt4'
|
||||
value: API_ID_CHATGPT4
|
||||
},
|
||||
{
|
||||
label: 'AI 问答 4.0 - 联网版',
|
||||
value: 'chatgpt4-browsing'
|
||||
value: API_ID_CHATGPT4_BROWSING
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -79,9 +84,6 @@ export default defineComponent({
|
||||
};
|
||||
},
|
||||
watch: {
|
||||
value(val) {
|
||||
this.$emit('update:modelValue', val);
|
||||
},
|
||||
modelValue(val) {
|
||||
if (val !== this.value) {
|
||||
this.value = val;
|
||||
@@ -92,6 +94,9 @@ export default defineComponent({
|
||||
onCommandChange(command: string) {
|
||||
console.log('val', command);
|
||||
this.value = command;
|
||||
// this.$emit('update:modelValue', command);
|
||||
this.$emit('select', command);
|
||||
console.log('ss', command);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
export const API_ID_CHATGPT = '1d58971c-e3cd-4713-a3ce-854a731adb14';
|
||||
export const API_ID_CHATGPT4 = '1c4e8fa3-362b-4e0a-b0fd-9ff9fc173b77';
|
||||
|
||||
export const APIS = [
|
||||
{
|
||||
id: API_ID_CHATGPT,
|
||||
name: 'ChatGPT-3.5'
|
||||
},
|
||||
{
|
||||
id: API_ID_CHATGPT4,
|
||||
name: 'ChatGPT-4'
|
||||
}
|
||||
];
|
||||
|
||||
export const DEFAULT_API = APIS[0];
|
||||
@@ -2,4 +2,4 @@ export * from './errorCode';
|
||||
export * from './env';
|
||||
export * from './endpoint';
|
||||
export * from './image';
|
||||
export * from './api';
|
||||
// export * from './api';
|
||||
@@ -35,14 +35,6 @@ class ApplicationOperator {
|
||||
async delete(id: string): Promise<AxiosResponse<null>> {
|
||||
return await httpClient.delete(`/${this.key}/${id}`);
|
||||
}
|
||||
|
||||
async rotateCredential(id: string): Promise<AxiosResponse<IApplicationDetailResponse>> {
|
||||
return await httpClient.post(`/${this.key}/${id}/rotate-credential/`);
|
||||
}
|
||||
|
||||
async updateRemainingAmount(id: string, data: IApplication): Promise<AxiosResponse<IApplicationDetailResponse>> {
|
||||
return await httpClient.post(`/${this.key}/${id}/update-remaining-amount/`, data);
|
||||
}
|
||||
}
|
||||
|
||||
export const applicationOperator = new ApplicationOperator();
|
||||
@@ -1,5 +1,6 @@
|
||||
export const API_ID_CHATGPT = '3333';
|
||||
export const API_ID_CHATGPT4 = '3333';
|
||||
export const API_ID_CHATGPT_BROWSING = '3333';
|
||||
export const API_ID_CHATGPT4_BROWSING = '3333';
|
||||
export const API_ID_CHATGPT_16k = '3333';
|
||||
export const API_ID_CHATGPT = '1d58971c-e3cd-4713-a3ce-854a731adb14';
|
||||
export const API_ID_CHATGPT_16K = 'bf93bb28-55a7-4428-ad82-156bf410f4e2';
|
||||
export const API_ID_CHATGPT_BROWSING = '12271e2e-794f-4079-a714-f68dd0df7808';
|
||||
|
||||
export const API_ID_CHATGPT4 = '1c4e8fa3-362b-4e0a-b0fd-9ff9fc173b77';
|
||||
export const API_ID_CHATGPT4_BROWSING = 'a61de51b-d0d0-48a2-9a17-1d4986d5d497';
|
||||
@@ -1,2 +1,3 @@
|
||||
export * from './models';
|
||||
export * from './operator';
|
||||
export * from './constants';
|
||||
@@ -48,5 +48,6 @@ export interface IChatResponse {
|
||||
|
||||
export interface IChatOptions {
|
||||
stream?: (response: IChatResponse) => void;
|
||||
api_id: string;
|
||||
token: string;
|
||||
endpoint?: string;
|
||||
}
|
||||
@@ -1,35 +1,25 @@
|
||||
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios';
|
||||
import { IChatOptions, IChatRequest, IChatResponse } from './models';
|
||||
import store from '@/store';
|
||||
import { IApplication } from '@/store/models';
|
||||
|
||||
class ChatOperator {
|
||||
async request(data: IChatRequest, config?: IChatOptions): Promise<AxiosResponse<IChatResponse>> {
|
||||
const applications: IApplication[] = store.getters.applications;
|
||||
// find related application
|
||||
const application = applications?.filter((application: IApplication) => {
|
||||
return (application.api_id = config?.api_id);
|
||||
})?.[0];
|
||||
if (!application) {
|
||||
return Promise.reject('no application');
|
||||
}
|
||||
const token = application?.credential?.token;
|
||||
if (!token) {
|
||||
return Promise.reject('no token');
|
||||
}
|
||||
async request(data: IChatRequest, options: IChatOptions): Promise<AxiosResponse<IChatResponse>> {
|
||||
return await axios.post(`/chatgpt`, data, {
|
||||
headers: {
|
||||
authorization: `Bearer ${token}`,
|
||||
authorization: `Bearer ${options.token}`,
|
||||
accept: 'application/x-ndjson',
|
||||
'content-type': 'application/json'
|
||||
},
|
||||
baseURL: options.endpoint,
|
||||
responseType: 'stream',
|
||||
onDownloadProgress: (event) => {
|
||||
const response = event.target.response;
|
||||
const lines = response.split('\r\n').filter((line: string) => !!line);
|
||||
const lastLine = lines[lines.length - 1];
|
||||
if (lastLine) {
|
||||
const jsonData = JSON.parse(lastLine);
|
||||
if (config?.stream) {
|
||||
config?.stream(jsonData as IChatResponse);
|
||||
if (options?.stream) {
|
||||
options?.stream(jsonData as IChatResponse);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ const httpClient: AxiosInstance = axios.create({
|
||||
});
|
||||
|
||||
httpClient.interceptors.request.use((config) => {
|
||||
const accessToken = store.getters.accessToken;
|
||||
const accessToken = store.getters.token.access;
|
||||
if (!config.headers) {
|
||||
config.headers = {};
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export interface IUser {
|
||||
id: string;
|
||||
id?: string;
|
||||
username?: string;
|
||||
first_name?: string;
|
||||
last_name?: string;
|
||||
@@ -10,15 +10,6 @@ export interface IUser {
|
||||
province?: string;
|
||||
country?: string;
|
||||
avatar?: string;
|
||||
type: 'user';
|
||||
}
|
||||
|
||||
export interface IBot {
|
||||
id?: string;
|
||||
username?: string;
|
||||
nickname?: string;
|
||||
avatar?: string;
|
||||
type: 'bot';
|
||||
}
|
||||
|
||||
export interface IUserDetailResponse extends IUser {}
|
||||
@@ -1,6 +1,9 @@
|
||||
<template>
|
||||
<div class="page">
|
||||
<model-selector v-model="model" class="model-selector" />
|
||||
<model-selector v-model="apiId" class="model-selector" @select="onSelectModel" />
|
||||
<div>
|
||||
{{ application?.remaining_amount }}
|
||||
</div>
|
||||
<div class="main">
|
||||
<introduction v-if="messages && messages.length === 0" />
|
||||
<div v-else class="messages">
|
||||
@@ -16,18 +19,30 @@
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { IMessage, ROLE_ASSISTANT, ROLE_USER } from '@/operators/message/models';
|
||||
import { IMessage, IMessageState, ROLE_ASSISTANT, ROLE_USER } from '@/operators/message/models';
|
||||
import { defineComponent } from 'vue';
|
||||
import Message from '@/components/chat/Message.vue';
|
||||
import { IChatModel, IChatMessage, CHAT_MODEL_CHATGPT, IChatResponse } from '@/operators';
|
||||
import {
|
||||
IChatModel,
|
||||
IChatMessage,
|
||||
CHAT_MODEL_CHATGPT,
|
||||
IChatResponse,
|
||||
API_ID_CHATGPT,
|
||||
applicationOperator,
|
||||
IApplication,
|
||||
ChatMessageState
|
||||
} from '@/operators';
|
||||
import InputBox from '@/components/chat/InputBox.vue';
|
||||
import ModelSelector from '@/components/chat/ModelSelector.vue';
|
||||
import { chatOperator } from '@/operators';
|
||||
|
||||
import { ERROR_CODE_CANCELED, ERROR_CODE_UNKNOWN } from '@/constants/errorCode';
|
||||
import axios from 'axios';
|
||||
export interface IData {
|
||||
messages: IChatMessage[];
|
||||
model: IChatModel;
|
||||
application: IApplication | undefined;
|
||||
apiId: string;
|
||||
question: '';
|
||||
needApply: boolean;
|
||||
}
|
||||
|
||||
export default defineComponent({
|
||||
@@ -41,7 +56,9 @@ export default defineComponent({
|
||||
data(): IData {
|
||||
return {
|
||||
question: '',
|
||||
model: CHAT_MODEL_CHATGPT,
|
||||
needApply: false,
|
||||
apiId: API_ID_CHATGPT,
|
||||
application: undefined,
|
||||
messages: [
|
||||
{
|
||||
role: ROLE_USER,
|
||||
@@ -92,29 +109,74 @@ export default defineComponent({
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
onSubmitQuestion() {
|
||||
async onSelectModel() {
|
||||
console.log('this.apiId', this.apiId);
|
||||
const { data: applications } = await applicationOperator.getAll({
|
||||
user_id: this.$store.state.user.id,
|
||||
api_id: this.apiId
|
||||
});
|
||||
if (!applications || applications?.items?.length === 0) {
|
||||
this.needApply = true;
|
||||
return;
|
||||
}
|
||||
this.application = applications.items[0];
|
||||
console.log('applications', applications);
|
||||
},
|
||||
async onSubmitQuestion() {
|
||||
this.messages.push({
|
||||
content: this.question,
|
||||
role: ROLE_USER
|
||||
});
|
||||
this.question = '';
|
||||
await this.onFetchAnswer();
|
||||
},
|
||||
async onFetchAnswer() {
|
||||
const apiId = this.model;
|
||||
const { data: response }: IChatResponse = await chatOperator.request(
|
||||
{
|
||||
question: this.question,
|
||||
conversation_id: this.conversationId,
|
||||
stateful: true
|
||||
},
|
||||
{
|
||||
api_id: apiId,
|
||||
stream: (response: IChatResponse) => {
|
||||
console.log('response', response);
|
||||
const token = this.application?.credential?.token;
|
||||
const endpoint = this.application?.api?.endpoint;
|
||||
const question = this.messages[this.messages.length - 1].content;
|
||||
console.log('q', question);
|
||||
if (!token || !endpoint || !question) {
|
||||
console.error('no token or endpoint or question');
|
||||
return;
|
||||
}
|
||||
console.log('token', token);
|
||||
chatOperator
|
||||
.request(
|
||||
{
|
||||
question,
|
||||
conversation_id: this.conversationId,
|
||||
stateful: true
|
||||
},
|
||||
{
|
||||
token,
|
||||
endpoint,
|
||||
stream: (response: IChatResponse) => {
|
||||
console.log('response', response);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
console.log('this.response', response);
|
||||
)
|
||||
.catch((error) => {
|
||||
if (this.messages && this.messages.length > 0) {
|
||||
this.messages[this.messages.length - 1].state = ChatMessageState.FAILED;
|
||||
}
|
||||
if (axios.isCancel(error)) {
|
||||
this.messages[this.messages.length - 1].error = {
|
||||
code: ERROR_CODE_CANCELED
|
||||
};
|
||||
} else if (error?.response?.data && error?.response?.data.code) {
|
||||
const data = error?.response?.data;
|
||||
if (this.messages && this.messages.length > 0) {
|
||||
this.messages[this.messages.length - 1].error = data;
|
||||
}
|
||||
} else {
|
||||
if (this.messages && this.messages.length > 0) {
|
||||
this.messages[this.messages.length - 1].error = {
|
||||
code: ERROR_CODE_UNKNOWN
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
// console.log('this.response', response);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
+19
-2
@@ -2,14 +2,16 @@ import { createStore, ActionContext } from 'vuex';
|
||||
import createPersistedState from 'vuex-persistedstate';
|
||||
import { removeCookies } from '@/utils/cookie';
|
||||
import { ENDPOINT } from '@/constants';
|
||||
import { IApplication } from '@/operators';
|
||||
import { IApplication, IUser } from '@/operators';
|
||||
import { ISetting, IState, IToken } from './models';
|
||||
|
||||
const store = createStore({
|
||||
state(): IState {
|
||||
return {
|
||||
user: {},
|
||||
token: {
|
||||
access: undefined,
|
||||
access:
|
||||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjk2NjQ2NjMxLCJqdGkiOiI4ODc1NzhhOTgyYjU0ZjBmODQ4NmU2ODFiYTZhYmVkMCIsInVzZXJfaWQiOiJlOGRjYWUyNC0yMmQwLTQ3MTItOTIxNy0zN2MyMzc0MTIwM2EiLCJwZXJtaXNzaW9ucyI6WyJhZGRf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U1ZTg2XHU2MjRkIiwicGhvbmUiOiIxNzYwMDE1NTU2NyIsImlkY2FyZCI6IjM3MDc4MTE5OTQwNjI5MzI3MSJ9.3866OMRcmExhw_7PD4OK_3zU58azknDSUNLvFxFSvbM',
|
||||
refresh: undefined
|
||||
},
|
||||
setting: {
|
||||
@@ -20,6 +22,12 @@ const store = createStore({
|
||||
};
|
||||
},
|
||||
mutations: {
|
||||
setUser(state: IState, payload: IUser): void {
|
||||
state.user = {
|
||||
...state.user,
|
||||
...payload
|
||||
};
|
||||
},
|
||||
setToken(state: IState, payload: IToken): void {
|
||||
state.token = {
|
||||
...state.token,
|
||||
@@ -41,17 +49,26 @@ const store = createStore({
|
||||
commit('setToken', {});
|
||||
removeCookies();
|
||||
},
|
||||
setUser({ commit }: ActionContext<IState, IState>, payload: IUser) {
|
||||
commit('setUser', payload);
|
||||
},
|
||||
setToken({ commit }: ActionContext<IState, IState>, payload: IToken) {
|
||||
commit('setToken', payload);
|
||||
},
|
||||
setSetting({ commit }: ActionContext<IState, IState>, payload: ISetting) {
|
||||
commit('setSetting', payload);
|
||||
},
|
||||
setApplications({ commit }: ActionContext<IState, IState>, payload: IApplication[]) {
|
||||
commit('setApplications', payload);
|
||||
}
|
||||
},
|
||||
getters: {
|
||||
authenticated(state): boolean {
|
||||
return !!state.token.access;
|
||||
},
|
||||
user(state): IUser {
|
||||
return state.user;
|
||||
},
|
||||
token(state): IToken {
|
||||
return state.token;
|
||||
},
|
||||
|
||||
+3
-34
@@ -1,3 +1,5 @@
|
||||
import { IApplication, IUser } from '@/operators';
|
||||
|
||||
export interface IToken {
|
||||
access?: string;
|
||||
refresh?: string;
|
||||
@@ -8,42 +10,9 @@ export interface ISetting {
|
||||
endpoint?: string;
|
||||
}
|
||||
|
||||
export enum IApplicationType {
|
||||
API = 'Api',
|
||||
PROXY = 'Proxy'
|
||||
}
|
||||
|
||||
export enum ICredentialType {
|
||||
TOKEN = 'Token',
|
||||
IDENTITY = 'Identity'
|
||||
}
|
||||
|
||||
export interface ICredential {
|
||||
type: ICredentialType;
|
||||
name?: string;
|
||||
token?: string;
|
||||
username?: string;
|
||||
password?: string;
|
||||
created_at?: string;
|
||||
updated_at?: string;
|
||||
}
|
||||
|
||||
export interface IApplication {
|
||||
id?: string;
|
||||
type?: IApplicationType;
|
||||
api_id?: string;
|
||||
proxy_id?: string;
|
||||
api_key?: string;
|
||||
user_id?: string;
|
||||
remaining_amount?: number;
|
||||
used_amount?: number;
|
||||
credential?: ICredential;
|
||||
created_at?: string;
|
||||
updated_at?: string;
|
||||
}
|
||||
|
||||
export interface IState {
|
||||
token: IToken;
|
||||
user: IUser;
|
||||
setting: ISetting;
|
||||
applications: IApplication[];
|
||||
}
|
||||
@@ -12,6 +12,10 @@ export default defineConfig({
|
||||
'/api': {
|
||||
target: 'https://data.zhishuyun.com',
|
||||
changeOrigin: true
|
||||
},
|
||||
'/api/v1/me': {
|
||||
target: 'https://auth.zhishuyun.com',
|
||||
changeOrigin: true
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user