Skip to content

Commit

Permalink
update api integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Germey committed Sep 30, 2023
1 parent 1d6b00c commit f9c6fef
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 127 deletions.
12 changes: 11 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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>
27 changes: 16 additions & 11 deletions src/components/chat/ModelSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -38,7 +43,7 @@ export default defineComponent({
required: true
}
},
emits: ['update:modelValue'],
emits: ['update:modelValue', 'select'],
data() {
return {
value: this.modelValue,
Expand All @@ -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
}
]
},
Expand All @@ -67,21 +72,18 @@ 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
}
]
}
]
};
},
watch: {
value(val) {
this.$emit('update:modelValue', val);
},
modelValue(val) {
if (val !== this.value) {
this.value = val;
Expand All @@ -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);
}
}
});
Expand Down
15 changes: 0 additions & 15 deletions src/constants/api.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from './errorCode';
export * from './env';
export * from './endpoint';
export * from './image';
export * from './api';
// export * from './api';
8 changes: 0 additions & 8 deletions src/operators/application/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
11 changes: 6 additions & 5 deletions src/operators/chat/constants.ts
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions src/operators/chat/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './models';
export * from './operator';
export * from './constants';
3 changes: 2 additions & 1 deletion src/operators/chat/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ export interface IChatResponse {

export interface IChatOptions {
stream?: (response: IChatResponse) => void;
api_id: string;
token: string;
endpoint?: string;
}
24 changes: 7 additions & 17 deletions src/operators/chat/operator.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/operators/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
}
Expand Down
11 changes: 1 addition & 10 deletions src/operators/user/models.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface IUser {
id: string;
id?: string;
username?: string;
first_name?: string;
last_name?: string;
Expand All @@ -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 {}
104 changes: 83 additions & 21 deletions src/pages/chat/Conversation.vue
Original file line number Diff line number Diff line change
@@ -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">
Expand All @@ -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({
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
}
});
Expand Down
Loading

0 comments on commit f9c6fef

Please sign in to comment.