@@ -61,10 +61,25 @@ export class Bot extends EventEmitter {
61
61
}
62
62
63
63
async chat ( message : string ) {
64
- this . chatState = ChatState . CHATTING ;
65
- const messages = [ ...this . prompts , ...this . history , { role : "user" , content : message } ] ;
66
- if ( this . config . model_name . startsWith ( 'coze:' ) ) {
67
- return getCozeChatCompletions ( this . tube , messages , this . config , { ...this . options , custom_variables : this . customParams } ,
64
+ try {
65
+ this . chatState = ChatState . CHATTING ;
66
+ const prompts = this . prompts . length > 0 ? [ ...this . prompts ] : [ {
67
+ role : 'system' ,
68
+ content : `[Output]\nOutput with json format, starts with '{'\n[Example]\n{"answer": "My answer"}` ,
69
+ } ] ;
70
+ const messages = [ ...prompts , ...this . history , { role : "user" , content : message } ] ;
71
+ if ( this . config . model_name . startsWith ( 'coze:' ) ) {
72
+ return await getCozeChatCompletions ( this . tube , messages , this . config , { ...this . options , custom_variables : this . customParams } ,
73
+ ( content ) => { // on complete
74
+ this . chatState = ChatState . FINISHED ;
75
+ this . emit ( 'response' , content ) ;
76
+ } , ( content ) => { // on string response
77
+ this . emit ( 'string-response' , content ) ;
78
+ } ) . then ( ( content ) => {
79
+ this . emit ( 'inference-done' , content ) ;
80
+ } ) ;
81
+ }
82
+ return await getChatCompletions ( this . tube , messages , this . config , this . options ,
68
83
( content ) => { // on complete
69
84
this . chatState = ChatState . FINISHED ;
70
85
this . emit ( 'response' , content ) ;
@@ -73,16 +88,11 @@ export class Bot extends EventEmitter {
73
88
} ) . then ( ( content ) => {
74
89
this . emit ( 'inference-done' , content ) ;
75
90
} ) ;
91
+ } catch ( ex : any ) {
92
+ // this.emit('error', ex.message);
93
+ this . tube . enqueue ( { event : 'error' , data : ex . message } ) ;
94
+ this . tube . cancel ( ) ;
76
95
}
77
- return getChatCompletions ( this . tube , messages , this . config , this . options ,
78
- ( content ) => { // on complete
79
- this . chatState = ChatState . FINISHED ;
80
- this . emit ( 'response' , content ) ;
81
- } , ( content ) => { // on string response
82
- this . emit ( 'string-response' , content ) ;
83
- } ) . then ( ( content ) => {
84
- this . emit ( 'inference-done' , content ) ;
85
- } ) ;
86
96
}
87
97
88
98
get state ( ) {
0 commit comments