Add Chatdoc feature (#40)

This commit is contained in:
AceDataCloud committed 2024-01-28 07:23:59 +00:00
1 parent 60752af1fb
commit 811a2bca09
51 files changed
+2742 -25

No files matched your search

-4
View File
@@ -1,9 +1,5 @@
import { IChatModel } from './models';
export const ROLE_SYSTEM = 'system';
export const ROLE_ASSISTANT = 'assistant';
export const ROLE_USER = 'user';
export const CHAT_MODEL_NAME_CHATGPT = 'chatgpt';
export const CHAT_MODEL_NAME_CHATGPT4 = 'chatgpt4';
export const CHAT_MODEL_NAME_CHATGPT_16K = 'chatgpt-16k';
+3 -5
View File
@@ -1,13 +1,11 @@
import { ROLE_ASSISTANT, ROLE_SYSTEM, ROLE_USER } from '@/constants';
import {
CHAT_MODEL_NAME_CHATGPT,
CHAT_MODEL_NAME_CHATGPT4,
CHAT_MODEL_NAME_CHATGPT4_BROWSING,
CHAT_MODEL_NAME_CHATGPT4_VISION,
CHAT_MODEL_NAME_CHATGPT_16K,
CHAT_MODEL_NAME_CHATGPT_BROWSING,
ROLE_ASSISTANT,
ROLE_SYSTEM,
ROLE_USER
CHAT_MODEL_NAME_CHATGPT_BROWSING
} from './constants';
export type IChatModelName =
@@ -25,7 +23,7 @@ export interface IChatModel {
description: string;
}
export interface IError {
interface IError {
code: string;
detail?: string;
}
+4
View File
@@ -0,0 +1,4 @@
export const API_ID_CHATDOC_REPOSITORIES = 'e22e9e3f-5594-4c48-9284-c1847c22c65f';
export const API_ID_CHATDOC_DOCUMENTS = 'afc7917f-d89f-4dc9-95c2-863936b02cad';
export const API_ID_CHATDOC_CONVERSATIONS = 'afc7917f-d89f-4dc9-95c2-863936b02cad';
export const API_ID_CHATDOC_CHAT = '099eafc8-8167-4557-b8f2-47867ca76e24';
+2
View File
@@ -0,0 +1,2 @@
export * from './models';
export * from './operator';
+76
View File
@@ -0,0 +1,76 @@
import { ACTION_CREATE, ACTION_DELETE, ACTION_UPDATE, ROLE_ASSISTANT, ROLE_SYSTEM, ROLE_USER } from '@/constants';
export interface IError {
code: string;
detail?: string;
}
export interface IChatdocRepository {
id: string;
name?: string;
description?: string;
deleting?: boolean;
editing?: boolean;
documents?: IChatdocDocument[];
conversations?: IChatdocConversation[];
}
export interface IChatdocDocument {
id: string;
repository_id: string;
file_url: string;
file_name: string;
}
export interface IChatdocConversation {
id: string;
repository_id: string;
messages: IChatdocMessage[];
editing?: boolean;
deleting?: boolean;
}
export interface IChatdocMessage {
content?: string;
error?: IError;
state?: IChatdocMessageState;
role?: typeof ROLE_SYSTEM | typeof ROLE_ASSISTANT | typeof ROLE_USER;
}
export enum IChatdocMessageState {
PENDING = 'pending',
ANSWERING = 'answering',
FINISHED = 'finished',
FAILED = 'failed'
}
export interface IChatdocChatRequest {
repository_id: string;
messages: IChatdocMessage[];
temperature?: number;
knowledge_fallback?: boolean;
}
export interface IChatdocDocumentRequest extends IChatdocDocument {
action: typeof ACTION_CREATE | typeof ACTION_UPDATE | typeof ACTION_DELETE;
callback_url?: string;
}
export interface IChatdocDocumentResponse extends IChatdocDocument {}
export type IChatdocConversationsResponse = IChatdocConversation[];
export interface IChatdocRepositoryRequest extends IChatdocRepository {
action: typeof ACTION_CREATE | typeof ACTION_UPDATE | typeof ACTION_DELETE;
}
export interface IChatdocRepositoryResponse extends IChatdocRepository {}
export type IChatdocRepositoriesResponse = IChatdocRepository[];
export type IChatdocDocumentsResponse = IChatdocDocument[];
export interface IChatdocChatResponse {
answer: string;
delta_answer: string;
repository_id?: string;
conversation_id?: string;
}
+300
View File
@@ -0,0 +1,300 @@
import axios, { AxiosProgressEvent, AxiosResponse } from 'axios';
import {
IChatdocChatResponse,
IChatdocConversation,
IChatdocConversationsResponse,
IChatdocDocumentResponse,
IChatdocDocumentsResponse,
IChatdocRepositoriesResponse,
IChatdocRepositoryResponse
} from './models';
import { ACTION_RETRIEVE_ALL, ACTION_UPDATE, BASE_URL_API } from '@/constants';
import { ACTION_CREATE, ACTION_DELETE, ACTION_RETRIEVE, ACTION_RETRIEVE_BATCH } from '@/constants';
class ChatdocOperator {
async createRepository(
payload: {
name: string;
description?: string;
},
options: { token: string }
): Promise<AxiosResponse<IChatdocRepositoryResponse>> {
return await axios.post(
`/chatdoc/repositories`,
{
action: ACTION_CREATE,
name: payload.name,
description: payload.description
},
{
headers: {
authorization: `Bearer ${options.token}`,
accept: 'application/json',
'content-type': 'application/json'
},
baseURL: BASE_URL_API
}
);
}
async deleteRepository(id: string, options: { token: string }): Promise<AxiosResponse<IChatdocRepositoryResponse>> {
return await axios.post(
`/chatdoc/repositories`,
{
action: ACTION_DELETE,
id
},
{
headers: {
authorization: `Bearer ${options.token}`,
accept: 'application/json',
'content-type': 'application/json'
},
baseURL: BASE_URL_API
}
);
}
async getRepository(id: string, options: { token: string }): Promise<AxiosResponse<IChatdocRepositoryResponse>> {
return await axios.post(
`/chatdoc/repositories`,
{
action: ACTION_RETRIEVE,
id
},
{
headers: {
authorization: `Bearer ${options.token}`,
accept: 'application/json',
'content-type': 'application/json'
},
baseURL: BASE_URL_API
}
);
}
async getAllRepositories(options: { token: string }): Promise<AxiosResponse<IChatdocRepositoriesResponse>> {
return await axios.post(
`/chatdoc/repositories`,
{
action: ACTION_RETRIEVE_ALL
},
{
headers: {
authorization: `Bearer ${options.token}`,
accept: 'application/json',
'content-type': 'application/json'
},
baseURL: BASE_URL_API
}
);
}
async getAllDocuments(
repositoryId: string,
options: { token: string }
): Promise<AxiosResponse<IChatdocDocumentsResponse>> {
return await axios.post(
`/chatdoc/documents`,
{
action: ACTION_RETRIEVE_ALL,
repository_id: repositoryId
},
{
headers: {
authorization: `Bearer ${options.token}`,
accept: 'application/json',
'content-type': 'application/json'
},
baseURL: BASE_URL_API
}
);
}
async getAllConversations(repositoryId: string): Promise<AxiosResponse<IChatdocConversationsResponse>> {
return await axios.post(
`/chatdoc/conversations`,
{
action: ACTION_RETRIEVE_ALL,
repository_id: repositoryId
},
{
headers: {
accept: 'application/json',
'content-type': 'application/json'
},
baseURL: BASE_URL_API
}
);
}
async getRepositories(ids: string[]): Promise<AxiosResponse<IChatdocRepositoriesResponse>> {
return await axios.post(
`/chatdoc/repositories`,
{
action: ACTION_RETRIEVE_BATCH,
ids
},
{
headers: {
accept: 'application/json',
'content-type': 'application/json'
},
baseURL: BASE_URL_API
}
);
}
async createDocument(
payload: {
repositoryId: string;
fileUrl: string;
fileName: string;
},
options: { token: string }
): Promise<AxiosResponse<IChatdocDocumentResponse>> {
return await axios.post(
`/chatdoc/documents`,
{
action: ACTION_CREATE,
repository_id: payload.repositoryId,
file_url: payload.fileUrl,
file_name: payload.fileName
},
{
headers: {
authorization: `Bearer ${options.token}`,
accept: 'application/json',
'content-type': 'application/json'
},
baseURL: BASE_URL_API
}
);
}
async retrieveDocument(id: string, options: { token: string }): Promise<AxiosResponse<IChatdocDocumentResponse>> {
return await axios.post(
`/chatdoc/documents`,
{
action: ACTION_RETRIEVE,
id
},
{
headers: {
authorization: `Bearer ${options.token}`,
accept: 'application/json',
'content-type': 'application/json'
},
baseURL: BASE_URL_API
}
);
}
async retrieveConversation(id: string, options: { token: string }): Promise<AxiosResponse<IChatdocDocumentResponse>> {
return await axios.post(
`/chatdoc/conversations`,
{
action: ACTION_RETRIEVE,
id
},
{
headers: {
authorization: `Bearer ${options.token}`,
accept: 'application/json',
'content-type': 'application/json'
},
baseURL: BASE_URL_API
}
);
}
async deleteDocument(id: string, options: { token: string }): Promise<AxiosResponse<IChatdocDocumentResponse>> {
return await axios.post(
`/chatdoc/documents`,
{
action: ACTION_DELETE,
id
},
{
headers: {
authorization: `Bearer ${options.token}`,
accept: 'application/json',
'content-type': 'application/json'
},
baseURL: BASE_URL_API
}
);
}
async updateConversation(payload: IChatdocConversation) {
return await axios.post(
`/chatdoc/conversations`,
{
action: ACTION_UPDATE,
id: payload.id,
messages: payload.messages
},
{
headers: {
accept: 'application/json',
'content-type': 'application/json'
},
baseURL: BASE_URL_API
}
);
}
async deleteConversation(id: string): Promise<AxiosResponse<IChatdocDocumentResponse>> {
return await axios.post(
`/chatdoc/conversations`,
{
action: ACTION_DELETE,
id
},
{
headers: {
accept: 'application/json',
'content-type': 'application/json'
},
baseURL: BASE_URL_API
}
);
}
async chat(
payload: { repositoryId: string; question: string; conversationId?: string; knowledgeFallback?: boolean },
options: { token: string; stream: (response: IChatdocChatResponse) => void }
): Promise<AxiosResponse<IChatdocChatResponse>> {
return await axios.post(
`/chatdoc/chat`,
{
repository_id: payload.repositoryId,
question: payload.question,
conversation_id: payload.conversationId,
stateful: true
},
{
headers: {
authorization: `Bearer ${options.token}`,
accept: 'application/x-ndjson',
'content-type': 'application/json'
},
baseURL: BASE_URL_API,
responseType: 'stream',
onDownloadProgress: ({ event }: AxiosProgressEvent) => {
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 (options?.stream) {
options?.stream(jsonData as IChatdocChatResponse);
}
}
}
}
);
}
}
export const chatdocOperator = new ChatdocOperator();
+1
View File
@@ -7,3 +7,4 @@ export * from './usage';
export * from './api';
export * from './order';
export * from './distribution';
export * from './chatdoc';
+2 -2
View File
@@ -1,4 +1,4 @@
import axios, { AxiosResponse } from 'axios';
import axios, { AxiosProgressEvent, AxiosResponse } from 'axios';
import { IMidjourneyImagineRequest, IMidjourneyImagineResponse, IMidjourneyImagineTask } from './models';
import { BASE_URL_API } from '@/constants';
@@ -54,7 +54,7 @@ class MidjourneyOperator {
},
baseURL: options.endpoint,
responseType: 'stream',
onDownloadProgress: (event) => {
onDownloadProgress: ({ event }: AxiosProgressEvent) => {
const response = event.target.response;
const lines = response.split('\r\n').filter((line: string) => !!line);
const lastLine = lines[lines.length - 1];