Skip to content

Commit

Permalink
add responding
Browse files Browse the repository at this point in the history
  • Loading branch information
Germey committed Oct 1, 2023
1 parent 814e6b5 commit 50e2da1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 49 deletions.
1 change: 1 addition & 0 deletions src/components/chat/Message.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export default defineComponent({
.content {
border-radius: 10px;
padding: 8px 15px;
max-width: 800px;
// background-color: aqua;
}
}
Expand Down
78 changes: 30 additions & 48 deletions src/pages/chat/Conversation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ import {
IChatResponse,
API_ID_CHATGPT,
applicationOperator,
IApplication
IApplication,
IChatMessageState
} 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';
import ApiStatus from '@/components/common/ApiStatus.vue';
import { ROUTE_CHAT_CONVERSATION } from '@/router';
export interface IData {
messages: IChatMessage[];
Expand All @@ -61,48 +63,7 @@ export default defineComponent({
initializing: false,
applied: undefined,
application: undefined,
messages: [
{
role: ROLE_USER,
content: 'hello'
},
{
role: ROLE_ASSISTANT,
content: '## ok \n\n hello, how can I assist you? \n ```python\nprint("hello")\n```'
},
{
role: ROLE_USER,
content: 'hello'
},
{
role: ROLE_ASSISTANT,
content: '## ok \n\n hello, how can I assist you? \n ```python\nprint("hello")\n```'
},
{
role: ROLE_USER,
content: 'hello'
},
{
role: ROLE_ASSISTANT,
content: '## ok \n\n hello, how can I assist you? \n ```python\nprint("hello")\n```'
},
{
role: ROLE_USER,
content: 'hello'
},
{
role: ROLE_ASSISTANT,
content: '## ok \n\n hello, how can I assist you? \n ```python\nprint("hello")\n```'
},
{
role: ROLE_USER,
content: 'hello'
},
{
role: ROLE_ASSISTANT,
content: '## ok \n\n hello, how can I assist you? \n ```python\nprint("hello")\n```'
}
]
messages: []
};
},
computed: {
Expand All @@ -111,6 +72,7 @@ export default defineComponent({
}
},
mounted() {
console.log('mounted');
this.onLoadModel();
},
methods: {
Expand All @@ -133,18 +95,23 @@ export default defineComponent({
role: ROLE_USER
});
this.question = '';
await this.onFetchAnswer();
},
async onFetchAnswer() {
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);
this.messages.push({
content: '',
role: ROLE_ASSISTANT,
state: IChatMessageState.PENDING
});
// request server to get answer
chatOperator
.request(
{
Expand All @@ -156,13 +123,24 @@ export default defineComponent({
token,
endpoint,
stream: (response: IChatResponse) => {
console.log('response', response);
this.messages[this.messages.length - 1].content = response.answer;
if (!this.conversationId) {
this.$router.push({
name: ROUTE_CHAT_CONVERSATION,
params: {
id: response.conversation_id
}
});
}
}
}
)
.then(() => {
this.messages[this.messages.length - 1].state = IChatMessageState.FINISHED;
})
.catch((error) => {
if (this.messages && this.messages.length > 0) {
this.messages[this.messages.length - 1].state = ChatMessageState.FAILED;
this.messages[this.messages.length - 1].state = IChatMessageState.FAILED;
}
if (axios.isCancel(error)) {
this.messages[this.messages.length - 1].error = {
Expand All @@ -181,7 +159,6 @@ export default defineComponent({
}
}
});
// console.log('this.response', response);
}
}
});
Expand All @@ -200,6 +177,11 @@ export default defineComponent({
width: 100%;
overflow-y: scroll;
padding: 15px 0;
.messages {
.message {
margin-bottom: 15px;
}
}
}
.bottom {
width: 100%;
Expand Down
13 changes: 12 additions & 1 deletion src/router/chat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ROUTE_AUTH_CALLBACK, ROUTE_AUTH_LOGIN, ROUTE_CHAT_CONVERSATION, ROUTE_CHAT_INDEX } from './constants';
import {
ROUTE_AUTH_CALLBACK,
ROUTE_AUTH_LOGIN,
ROUTE_CHAT_CONVERSATION,
ROUTE_CHAT_CONVERSATION_NEW,
ROUTE_CHAT_INDEX
} from './constants';

export default {
path: '/chat',
Expand All @@ -9,6 +15,11 @@ export default {
name: ROUTE_CHAT_INDEX,
component: () => import('@/pages/chat/Index.vue')
},
{
path: 'conversation',
name: ROUTE_CHAT_CONVERSATION_NEW,
component: () => import('@/pages/chat/Conversation.vue')
},
{
path: 'conversation/:id',
name: ROUTE_CHAT_CONVERSATION,
Expand Down
1 change: 1 addition & 0 deletions src/router/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const ROUTE_AUTH_CALLBACK = 'auth-callback';

export const ROUTE_CHAT_INDEX = 'chat-index';
export const ROUTE_CHAT_CONVERSATION = 'chat-conversation';
export const ROUTE_CHAT_CONVERSATION_NEW = 'chat-conversation-new';

export const ROUTE_PAINT_INDEX = 'paint-index';
export const ROUTE_PAINT_HISTORY = 'paint-history';
Expand Down

0 comments on commit 50e2da1

Please sign in to comment.