11import WebSocketAsPromised from 'websocket-as-promised'
2- import { ChatError , ErrorCode } from '~utils/errors'
3- import { AbstractBot , SendMessageParams } from '../abstract-bot'
4- import { generateSessionHash } from './utils'
5-
6- enum FnIndex {
7- Send = 7 ,
8- Receive = 8 ,
9- }
10-
11- interface ConversationContext {
12- sessionHash : string
13- }
14-
15- export class LMSYSBot extends AbstractBot {
16- public model : string
17- private conversationContext ?: ConversationContext
2+ import { GradioBot } from '../gradio'
183
4+ export class LMSYSBot extends GradioBot {
195 constructor ( model : string ) {
20- super ( )
21- this . model = model
22- }
23-
24- async doSendMessage ( params : SendMessageParams ) {
25- if ( ! this . conversationContext ) {
26- const sessionHash = await this . createSession ( params . signal )
27- this . conversationContext = { sessionHash }
28- }
29-
30- const sendWsp = await this . connectWebsocket (
31- FnIndex . Send ,
32- this . conversationContext . sessionHash ,
33- [ null , this . model , params . prompt ] ,
34- params . onEvent ,
35- )
36- const receiveWsp = await this . connectWebsocket (
37- FnIndex . Receive ,
38- this . conversationContext . sessionHash ,
39- [ null , 0.7 , 1 , 512 ] ,
40- params . onEvent ,
41- )
42-
43- params . signal ?. addEventListener ( 'abort' , ( ) => {
44- ; [ sendWsp , receiveWsp ] . forEach ( ( wsp ) => {
45- wsp . removeAllListeners ( )
46- wsp . close ( )
47- } )
48- } )
49- }
50-
51- async connectWebsocket ( fnIndex : number , sessionHash : string , data : unknown [ ] , onEvent : SendMessageParams [ 'onEvent' ] ) {
52- const wsp = new WebSocketAsPromised ( 'wss://chat.lmsys.org/queue/join' , {
53- packMessage : ( data ) => JSON . stringify ( data ) ,
54- unpackMessage : ( data ) => JSON . parse ( data as string ) ,
55- } )
56-
57- wsp . onUnpackedMessage . addListener ( async ( event ) => {
58- if ( event . msg === 'send_hash' ) {
59- wsp . sendPacked ( { fn_index : fnIndex , session_hash : sessionHash } )
60- } else if ( event . msg === 'send_data' ) {
61- wsp . sendPacked ( {
62- fn_index : fnIndex ,
63- data,
64- event_data : null ,
65- session_hash : sessionHash ,
66- } )
67- } else if ( event . msg === 'process_generating' ) {
68- if ( event . success && event . output . data ) {
69- if ( fnIndex === FnIndex . Receive ) {
70- const outputData = event . output . data
71- if ( outputData [ 1 ] . length > 0 ) {
72- const text = outputData [ 1 ] [ outputData [ 1 ] . length - 1 ] [ 1 ]
73- onEvent ( { type : 'UPDATE_ANSWER' , data : { text } } )
74- }
75- }
76- } else {
77- onEvent ( { type : 'ERROR' , error : new ChatError ( event . output . error , ErrorCode . UNKOWN_ERROR ) } )
78- }
79- } else if ( event . msg === 'queue_full' ) {
80- onEvent ( { type : 'ERROR' , error : new ChatError ( 'queue_full' , ErrorCode . UNKOWN_ERROR ) } )
81- } else if ( event . msg === 'process_completed' && fnIndex === FnIndex . Receive && ! event . output . data [ 1 ] . length ) {
82- onEvent ( {
83- type : 'ERROR' ,
84- error : new ChatError ( 'Session has been inactive for too long' , ErrorCode . LMSYS_SESSION_EXPIRED ) ,
85- } )
86- }
87- } )
88-
89- if ( fnIndex === FnIndex . Receive ) {
90- wsp . onClose . addListener ( ( ) => {
91- wsp . removeAllListeners ( )
92- onEvent ( { type : 'DONE' } )
93- } )
94- }
95-
96- try {
97- await wsp . open ( )
98- } catch ( err ) {
99- console . error ( 'lmsys ws open error' , err )
100- throw new ChatError ( 'Failed to establish websocket connection.' , ErrorCode . NETWORK_ERROR )
101- }
102-
103- return wsp
104- }
105-
106- resetConversation ( ) {
107- this . conversationContext = undefined
6+ super ( 'wss://chat.lmsys.org/queue/join' , model , [ 0.7 , 1 , 512 ] , 'text' )
1087 }
1098
110- async initializeSession ( fnIndex : number , sessionHash : string , data : unknown [ ] , signal ?: AbortSignal ) : Promise < void > {
111- const wsp = new WebSocketAsPromised ( 'wss://chat.lmsys.org/queue/join' , {
9+ private async initializeSession (
10+ fnIndex : number ,
11+ sessionHash : string ,
12+ data : unknown [ ] ,
13+ signal ?: AbortSignal ,
14+ ) : Promise < void > {
15+ const wsp = new WebSocketAsPromised ( this . wsUrl , {
11216 packMessage : ( data ) => JSON . stringify ( data ) ,
11317 unpackMessage : ( data ) => JSON . parse ( data as string ) ,
11418 } )
@@ -128,7 +32,7 @@ export class LMSYSBot extends AbstractBot {
12832 }
12933
13034 async createSession ( signal ?: AbortSignal ) {
131- const sessionHash = generateSessionHash ( )
35+ const sessionHash = await super . createSession ( signal )
13236 await Promise . all ( [
13337 this . initializeSession ( 36 , sessionHash , [ ] , signal ) ,
13438 this . initializeSession ( 43 , sessionHash , [ { } ] , signal ) ,
0 commit comments