update experience for chat

This commit is contained in:
AceDataCloud committed 2023-10-03 20:13:23 +08:00
1 parent 7ca606ae20
commit 71dec1cb67
8 files changed
+151 -42

No files matched your search

+24 -6
View File
@@ -41,20 +41,38 @@ export interface IChatMessage {
error?: IError;
}
export interface IChatOptions {
stream?: (response: IChatResponse) => void;
token: string;
endpoint?: string;
export interface IChatHistory {
conversation_id: string;
messages: IChatMessage[];
}
export interface IChatRequest {
export interface IChatAskOptions {
stream?: (response: IChatAskResponse) => void;
token: string;
endpoint: string;
path: string;
}
export interface IChatHistoryOptions {
endpoint: string;
path: string;
}
export interface IChatAskRequest {
question: string;
stateful?: boolean;
conversation_id?: string;
}
export interface IChatResponse {
export interface IChatAskResponse {
answer: string;
delta_answer: string;
conversation_id?: string;
}
export interface IChatHistoryRequest {
action: string;
conversation_id: string;
}
export type IChatHistoryResponse = IChatHistory;
+20 -5
View File
@@ -1,10 +1,16 @@
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios';
import { IChatOptions, IChatRequest, IChatResponse } from './models';
import store from '@/store';
import {
IChatHistoryRequest,
IChatHistoryResponse,
IChatAskOptions,
IChatAskRequest,
IChatAskResponse,
IChatHistoryOptions
} from './models';
class ChatOperator {
async request(data: IChatRequest, options: IChatOptions): Promise<AxiosResponse<IChatResponse>> {
return await axios.post(`/chatgpt`, data, {
async ask(data: IChatAskRequest, options: IChatAskOptions): Promise<AxiosResponse<IChatAskResponse>> {
return await axios.post(options.path, data, {
headers: {
authorization: `Bearer ${options.token}`,
accept: 'application/x-ndjson',
@@ -19,12 +25,21 @@ class ChatOperator {
if (lastLine) {
const jsonData = JSON.parse(lastLine);
if (options?.stream) {
options?.stream(jsonData as IChatResponse);
options?.stream(jsonData as IChatAskResponse);
}
}
}
});
}
async history(data: IChatHistoryRequest, options: IChatHistoryOptions): Promise<AxiosResponse<IChatHistoryResponse>> {
return await axios.post(options.path, data, {
headers: {
'content-type': 'application/json'
},
baseURL: options.endpoint
});
}
}
export const chatOperator = new ChatOperator();