1
+ import websockets
2
+ import asyncio
3
+
4
+ from typing import Any
5
+ from websockets .server import serve
6
+
7
+ from llm_repl .repls import BaseREPL , REPLStyle
8
+
9
+
10
+ class WebsockerREPL (BaseREPL ):
11
+ """
12
+ Base class with all the methods that a REPL should implement
13
+ """
14
+
15
+ @property
16
+ def style (self ) -> REPLStyle :
17
+ """
18
+ Return the style of the REPL
19
+ """
20
+ raise NotImplementedError
21
+
22
+ def info (self ) -> str :
23
+ """
24
+ Print the information about the LLM currently loaded
25
+ """
26
+ return "This is a websocket REPL"
27
+
28
+ def exit (self ):
29
+ """
30
+ Exit the application
31
+ """
32
+ pass
33
+
34
+ def load_llm (self , llm_name : str ):
35
+ """
36
+ Load the LLM specified by the name and its custom commands if any
37
+
38
+ :param str llm_name: The name of the LLM to load
39
+ """
40
+ pass
41
+
42
+ def print (self , msg : Any , ** kwargs ):
43
+ """
44
+ Simply prints the message as a normal print statement.
45
+
46
+ :param Any msg: The message to be printed.
47
+ """
48
+
49
+ def print_client_msg (self , msg : str ):
50
+ """
51
+ Prints the client message with the appropriate style
52
+
53
+ :param str msg: The message to be printed.
54
+ """
55
+
56
+ def print_server_msg (self , msg : str ):
57
+ """
58
+ Prints the server message with the appropriate style
59
+
60
+ :param str msg: The message to be printed.
61
+ """
62
+
63
+ def print_error_msg (self , msg : str ):
64
+ """
65
+ Prints the error message with the appropriate style
66
+
67
+ :param str msg: The message to be printed.
68
+ """
69
+
70
+ def print_misc_msg (self , msg : str , ** kwargs ):
71
+ """
72
+ Print the miscellaneous message with the appropriate style
73
+
74
+ :param str msg: The message to be printed.
75
+ """
76
+
77
+ async def _handle_msg (self , websocket ):
78
+ async for message in websocket :
79
+ print (f"Received message: { message } " )
80
+ await websocket .send (message )
81
+
82
+ async def run (self , llm_name ):
83
+ """
84
+ Starts the REPL
85
+
86
+ :param str llm_name: The name of the LLM to load
87
+ """
88
+ print ("Starting websocket REPL" )
89
+ async with serve (self ._handle_msg , "localhost" , 8765 ):
90
+ await asyncio .Future ()
0 commit comments