File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ # prompt llm interface
2+
3+ from ..chat import Chat
4+ from ..utils import sprint
5+
6+ from rich import print as rprint
7+ from prompt_toolkit import prompt
8+
9+ def print_header (name , color = 'green' ):
10+ rprint (f'[bold { color } ]{ name } >[/bold { color } ] ' , end = '' )
11+
12+ def loop (provider = 'local' , name = None , ** kwargs ):
13+ # get name
14+ if name is None :
15+ name = provider
16+
17+ # initialize chat
18+ chat = Chat (provider = provider , ** kwargs )
19+
20+ # main prompt loop
21+ while True :
22+ # get query
23+ print_header ('user' , 'red' )
24+ query = prompt ()
25+
26+ # skip if empty
27+ if query .strip () == '' :
28+ continue
29+
30+ # stream reply
31+ print ()
32+ print_header (name , 'blue' )
33+ for s in chat .stream (query ):
34+ sprint (s )
35+ print ('\n ' )
36+
37+ def main (** kwargs ):
38+ # exit gracefully
39+ try :
40+ loop (** kwargs )
41+ except (KeyboardInterrupt , EOFError ):
42+ print ()
43+ pass
You can’t perform that action at this time.
0 commit comments