integrate api

This commit is contained in:
AceDataCloud committed 2023-09-30 20:32:00 +08:00
1 parent ddd3910e7d
commit 7880b1d5d0
10 files changed
+293 -34

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
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';
+2
View File
@@ -0,0 +1,2 @@
export * from './models';
export * from './operator';
+52
View File
@@ -0,0 +1,52 @@
export interface IError {
code: string;
detail?: string;
}
export enum ChatMessageState {
PENDING = 'pending',
ANSWERING = 'answering',
FINISHED = 'finished',
FAILED = 'failed'
}
export const ROLE_SYSTEM = 'system';
export const ROLE_ASSISTANT = 'assistant';
export const ROLE_USER = 'user';
export interface IChatMessage {
state?: ChatMessageState;
content: string;
role?: typeof ROLE_SYSTEM | typeof ROLE_ASSISTANT | typeof ROLE_USER;
error?: IError;
}
export const CHAT_MODEL_CHATGPT = 'chatgpt';
export const CHAT_MODEL_CHATGPT4 = 'chatgpt4';
export const CHAT_MODEL_CHATGPT_16K = 'chatgpt-16k';
export const CHAT_MODEL_CHATGPT_BROWSING = 'chatgpt-browsing';
export const CHAT_MODEL_CHATGPT4_BROWSING = 'chatgpt4-browsing';
export type IChatModel =
| typeof CHAT_MODEL_CHATGPT
| typeof CHAT_MODEL_CHATGPT4
| typeof CHAT_MODEL_CHATGPT_16K
| typeof CHAT_MODEL_CHATGPT_BROWSING
| typeof CHAT_MODEL_CHATGPT4_BROWSING;
export interface IChatRequest {
question: string;
stateful?: boolean;
conversation_id?: string;
}
export interface IChatResponse {
answer: string;
delta_answer: string;
conversation_id?: string;
}
export interface IChatOptions {
stream?: (response: IChatResponse) => void;
api_id: string;
}
+40
View File
@@ -0,0 +1,40 @@
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');
}
return await axios.post(`/chatgpt`, data, {
headers: {
authorization: `Bearer ${token}`,
accept: 'application/x-ndjson',
'content-type': 'application/json'
},
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);
}
}
}
});
}
}
export const chatOperator = new ChatOperator();
+1
View File
@@ -2,3 +2,4 @@ export * from './instance';
export * from './application';
export * from './user';
export * from './api';
export * from './chat';