Skip to content
Merged
Changes from all commits
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
29 changes: 11 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,11 @@ def main(args):
elif args.style == "rich":
chatio = RichChatIO(args.multiline, args.mouse)
elif args.style == "programmatic":
chatio = ProgrammaticChatIO()
input_file_name = "input_prompt.txt"
inputs_directory = "inputs"
root_input_file = os.path.join(inputs_directory, input_file_name)
root_input_file_path = os.path.abspath(root_input_file)
chatio = FileInputChatIO(root_input_file_path)
else:
raise ValueError(f"Invalid style for console: {args.style}")
try:
Expand Down