Skip to content

Commit

Permalink
Refine chatdoc and base code interface (#2)
Browse files Browse the repository at this point in the history
Co-authored-by: Germey <germey@acedata.cloud>
  • Loading branch information
Germey and Germey authored Mar 17, 2024
1 parent dfe7b89 commit 69d08af
Show file tree
Hide file tree
Showing 21 changed files with 352 additions and 168 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix chatdoc issues",
"packageName": "@acedatacloud/hub",
"email": "germey@acedata.cloud",
"dependentChangeType": "patch"
}
1 change: 1 addition & 0 deletions src/components/chatdoc/UploadDocument.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export default defineComponent({
ElMessage.success(this.$t('chatdoc.message.createDocumentSuccess'));
this.dialogVisible = false;
this.$store.dispatch('chatdoc/getDocuments', { repositoryId: this.$route.params.repositoryId });
this.$store.dispatch('chatdoc/getApplication');
})
.catch(() => {
ElMessage.error(this.$t('chatdoc.message.createDocumentError'));
Expand Down
4 changes: 1 addition & 3 deletions src/components/common/Price.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<template>
<div class="price-text">
<span v-if="price > 0" class="unfree"
>¥{{ price }} {{ unit ? ` / ${unit}` : '' }}{{ start ? $t('common.message.startPrice') : ''
}}{{ $t('common.message.startPrice') }}
>${{ price }} {{ unit ? ` / ${unit}` : '' }}{{ start ? $t('common.message.startPrice') : '' }}
</span>
<span v-else class="free">
{{ $t('common.message.free') }}
Expand All @@ -12,7 +11,6 @@

<script>
import { defineComponent } from 'vue';
export default defineComponent({
name: 'ApiPrice',
props: {
Expand Down
1 change: 0 additions & 1 deletion src/constants/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ export const ACTION_UPDATE = 'update';
export const ACTION_DELETE = 'delete';
export const ACTION_RETRIEVE = 'retrieve';
export const ACTION_RETRIEVE_BATCH = 'retrieve_batch';
export const ACTION_RETRIEVE_ALL = 'retrieve_all';
2 changes: 2 additions & 0 deletions src/i18n/zh/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import order from './order';
import distribution from './distribution';
import user from './user';
import chatdoc from './chatdoc';
import service from './service';

export default {
chat,
service,
chatdoc,
user,
order,
Expand Down
7 changes: 7 additions & 0 deletions src/i18n/zh/service/button.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
learnMore: 'Learn More',
apply: 'Apply Now',
introduction: 'Service Introduction',
apis: 'API Description',
pricing: 'Pricing'
};
6 changes: 6 additions & 0 deletions src/i18n/zh/service/field.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
price: 'Price',
introduction: 'Introduction',
apis: 'API',
proxies: 'Proxy'
};
13 changes: 13 additions & 0 deletions src/i18n/zh/service/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import message from './message';
import unit from './unit';
import button from './button';
import field from './field';
import title from './title';

export default {
message,
unit,
button,
field,
title
};
12 changes: 12 additions & 0 deletions src/i18n/zh/service/message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
free: 'Free',
around: 'Around',
welcomeCredits: 'Welcome Credits',
appliedCount: 'Number of Applications',
indexTitle: 'Your smart choice for digital services',
indexSubtitle: 'Empower your work and life with standardized excellence in services.',
fromPrice: 'From ',
packages: 'We offer a variety of packages for credits, the more packages you buy, the more discounts you get.',
offer: 'Offer',
partnersTitle: 'Trusted by leading partners'
};
5 changes: 5 additions & 0 deletions src/i18n/zh/service/title.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
apis: 'API',
proxies: 'Proxy',
packages: 'Packages'
};
12 changes: 12 additions & 0 deletions src/i18n/zh/service/unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
credit: 'Credit',
Credit: 'Credit',
credits: 'Credits',
Credits: 'Credits',
call: 'Call',
Call: 'Call',
calls: 'Calls',
Calls: 'Calls',
MB: 'MB',
GB: 'GB'
};
54 changes: 31 additions & 23 deletions src/models/chatdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,40 @@ export interface IChatdocRepository {
conversations?: IChatdocConversation[];
}

export interface IChatdocRepositoryRequest extends IChatdocRepository {
action: typeof ACTION_CREATE | typeof ACTION_UPDATE | typeof ACTION_DELETE;
}

export interface IChatdocRepositoryResponse extends IChatdocRepository {}
export type IChatdocRepositoriesResponse = {
items: IChatdocRepository[];
count: number;
};

export interface IChatdocDocument {
id: string;
repository_id: string;
file_url: string;
file_name: string;
}

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 IChatdocDocumentsResponse = {
items: IChatdocDocument[];
count: number;
};

export interface IChatdocConversation {
id: string;
repository_id: string;
messages: IChatdocMessage[];
question: string;
editing?: boolean;
deleting?: boolean;
}
Expand All @@ -44,36 +67,21 @@ export enum IChatdocMessageState {
FAILED = 'failed'
}

export interface IChatdocChatRequest {
export interface IChatdocConversationRequest {
id: string;
repository_id: string;
messages: IChatdocMessage[];
question: string;
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 = {
items: IChatdocRepository[];
export type IChatdocConversationsResponse = {
items: IChatdocConversation[];
count: number;
};
export type IChatdocDocumentsResponse = IChatdocDocument[];

export interface IChatdocChatResponse {
export interface IChatdocConversationResponse {
id: string;
answer: string;
delta_answer: string;
repository_id?: string;
conversation_id?: string;
delta_answer?: string;
}
2 changes: 1 addition & 1 deletion src/operators/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getBaseUrlAuth } from '@/utils';

class AuthOperator {
async refreshToken(payload: IToken): Promise<AxiosResponse<ITokenResponse>> {
return httpClient.post('/token/refresh/', payload);
return httpClient.post('/auth/refresh/', payload);
}
}

Expand Down
22 changes: 13 additions & 9 deletions src/operators/chatdoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
IChatdocRepositoriesResponse,
IChatdocRepositoryResponse
} from '@/models';
import { ACTION_RETRIEVE_ALL, ACTION_UPDATE, BASE_URL_API } from '@/constants';
import { ACTION_UPDATE, BASE_URL_API } from '@/constants';
import { ACTION_CREATE, ACTION_DELETE, ACTION_RETRIEVE, ACTION_RETRIEVE_BATCH } from '@/constants';

class ChatdocOperator {
Expand Down Expand Up @@ -77,7 +77,7 @@ class ChatdocOperator {
return await axios.post(
`/chatdoc/repositories`,
{
action: ACTION_RETRIEVE_ALL
action: ACTION_RETRIEVE_BATCH
},
{
headers: {
Expand All @@ -97,7 +97,7 @@ class ChatdocOperator {
return await axios.post(
`/chatdoc/documents`,
{
action: ACTION_RETRIEVE_ALL,
action: ACTION_RETRIEVE_BATCH,
repository_id: repositoryId
},
{
Expand All @@ -111,15 +111,19 @@ class ChatdocOperator {
);
}

async getAllConversations(repositoryId: string): Promise<AxiosResponse<IChatdocConversationsResponse>> {
async getAllConversations(
repositoryId: string,
options: { token: string }
): Promise<AxiosResponse<IChatdocConversationsResponse>> {
return await axios.post(
`/chatdoc/conversations`,
{
action: ACTION_RETRIEVE_ALL,
action: ACTION_RETRIEVE_BATCH,
repository_id: repositoryId
},
{
headers: {
authorization: `Bearer ${options.token}`,
accept: 'application/json',
'content-type': 'application/json'
},
Expand Down Expand Up @@ -273,16 +277,16 @@ class ChatdocOperator {
);
}

async chat(
payload: { repositoryId: string; question: string; conversationId?: string; knowledgeFallback?: boolean },
async chatConversation(
payload: { repositoryId: string; question: string; id?: string; knowledgeFallback?: boolean },
options: { token: string; stream: (response: IChatdocChatResponse) => void }
): Promise<AxiosResponse<IChatdocChatResponse>> {
return await axios.post(
`/chatdoc/chat`,
`/chatdoc/conversations`,
{
repository_id: payload.repositoryId,
question: payload.question,
conversation_id: payload.conversationId,
id: payload.id,
stateful: true
},
{
Expand Down
10 changes: 5 additions & 5 deletions src/operators/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ export interface IInviteesQuery {

class UserOperator {
async getMe(): Promise<AxiosResponse<IUserDetailResponse>> {
return httpClient.get('/me');
return httpClient.get('/users/me');
}

async getInvitees(query: IInviteesQuery): Promise<AxiosResponse<IUserListResponse>> {
return httpClient.get('/me/invitees', {
return httpClient.get('/users/me/invitees', {
params: query
});
}

async updateMe(data: IUser): Promise<AxiosResponse<IUserDetailResponse>> {
return httpClient.put('/me', data);
return httpClient.put('/users/me', data);
}

async getVerify(): Promise<AxiosResponse<IUserDetailResponse>> {
return httpClient.get('/verify');
return httpClient.get('/users/verify');
}

async updateVerify(data: IUser): Promise<AxiosResponse<IUserDetailResponse>> {
return httpClient.put('/verify', data);
return httpClient.put('/users/verify', data);
}
}

Expand Down
30 changes: 12 additions & 18 deletions src/pages/chatdoc/Conversation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ import { log } from '@/utils';
import { ROUTE_CHATDOC_CONVERSATION } from '@/router';
import axios from 'axios';
import { isJSONString } from '@/utils/is';
import { Status } from '@/models';
import { IService, Status } from '@/models';
import ApplicationStatus from '@/components/application/Status.vue';
import { IChatdocChatResponse, IChatdocConversation, IChatdocMessageState, IChatdocRepository } from '@/models';
import { IChatdocConversationResponse, IChatdocConversation, IChatdocMessageState, IChatdocRepository } from '@/models';
export default defineComponent({
name: 'ChatdocChat',
name: 'ChatdocConversation',
components: {
Layout,
Conversations,
Expand Down Expand Up @@ -92,7 +92,7 @@ export default defineComponent({
repository(): IChatdocRepository | undefined {
return this.$store.state?.chatdoc?.repositories?.find((repository) => repository.id === this.repositoryId);
},
conversations() {
conversations(): IChatdocConversation[] | undefined {
return this.repository?.conversations;
},
conversationId(): string | undefined {
Expand All @@ -107,29 +107,23 @@ export default defineComponent({
initializing() {
return this.$store.state.chatdoc.status.getApplication === Status.Request;
},
service() {
service(): IService | undefined {
return this.$store.state.chatdoc.service;
}
},
async mounted() {
console.log('start get conversations');
this.loading = true;
this.$store
.dispatch('chatdoc/getConversations', { repositoryId: this.repositoryId })
.then(() => {
this.loading = false;
})
.catch(() => {
this.loading = false;
});
this.$store.dispatch('chatdoc/getConversations', { repositoryId: this.repositoryId }).finally(() => {
this.loading = false;
});
},
methods: {
async onSubmit() {
this.messages.push({
content: this.question,
role: ROLE_USER
});
console.debug('onSubmit', this.question);
await this.onFetchAnswer();
},
async onScrollDown() {
Expand Down Expand Up @@ -169,21 +163,21 @@ export default defineComponent({
// request server to get answer
this.answering = true;
chatdocOperator
.chat(
.chatConversation(
{
repositoryId: this.repositoryId,
question,
conversationId: this.conversationId
id: this.conversationId
},
{
token,
stream: (response: IChatdocChatResponse) => {
stream: (response: IChatdocConversationResponse) => {
this.messages[this.messages.length - 1] = {
role: ROLE_ASSISTANT,
content: response.answer,
state: IChatdocMessageState.ANSWERING
};
conversationId = response.conversation_id;
conversationId = response.id;
this.onScrollDown();
}
}
Expand Down
Loading

0 comments on commit 69d08af

Please sign in to comment.