Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
#1: Implement Receiving Input Prompt via FileInput
Signed-off-by: Deepanshu Arora <[email protected]>
  • Loading branch information
Deepansharora27 committed Jul 31, 2023
commit e6a9a1e60ada08d12acb7d669f6c657e1c3fe411
25 changes: 7 additions & 18 deletions fastchat/serve/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,13 @@ def stream_output(self, output_stream):
return text


class ProgrammaticChatIO(ChatIO):
class FileInputChatIO(ChatIO):
def __init__(self, input_file):
self._input_file = input_file

def prompt_for_input(self, role) -> str:
contents = ""
# `end_sequence` signals the end of a message. It is unlikely to occur in
# message content.
end_sequence = " __END_OF_A_MESSAGE_47582648__\n"
len_end = len(end_sequence)
while True:
if len(contents) >= len_end:
last_chars = contents[-len_end:]
if last_chars == end_sequence:
break
try:
char = sys.stdin.read(1)
contents = contents + char
except EOFError:
continue
contents = contents[:-len_end]
with open(self._input_file, 'r') as f:
contents = f.read()
print(f"[!OP:{role}]: {contents}", flush=True)
return contents

Expand Down Expand Up @@ -184,7 +173,7 @@ def main(args):
elif args.style == "rich":
chatio = RichChatIO(args.multiline, args.mouse)
elif args.style == "programmatic":
chatio = ProgrammaticChatIO()
chatio = FileInputChatIO(args.input_file)
else:
raise ValueError(f"Invalid style for console: {args.style}")
try:
Expand Down