Skip to content

Commit 88aed47

Browse files
committed
add prompt interface
1 parent ae5e957 commit 88aed47

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

oneping/interface/prompt.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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

0 commit comments

Comments
 (0)