| 150 | |
| 151 | |
| 152 | class ProgrammaticChatIO(ChatIO): |
| 153 | def prompt_for_input(self, role) -> str: |
| 154 | contents = "" |
| 155 | # `end_sequence` signals the end of a message. It is unlikely to occur in |
| 156 | # message content. |
| 157 | end_sequence = " __END_OF_A_MESSAGE_47582648__\n" |
| 158 | len_end = len(end_sequence) |
| 159 | while True: |
| 160 | if len(contents) >= len_end: |
| 161 | last_chars = contents[-len_end:] |
| 162 | if last_chars == end_sequence: |
| 163 | break |
| 164 | try: |
| 165 | char = sys.stdin.read(1) |
| 166 | contents = contents + char |
| 167 | except EOFError: |
| 168 | continue |
| 169 | contents = contents[:-len_end] |
| 170 | print(f"[!OP:{role}]: {contents}", flush=True) |
| 171 | return contents |
| 172 | |
| 173 | def prompt_for_output(self, role: str): |
| 174 | print(f"[!OP:{role}]: ", end="", flush=True) |
| 175 | |
| 176 | def stream_output(self, output_stream): |
| 177 | pre = 0 |
| 178 | for outputs in output_stream: |
| 179 | output_text = outputs["text"] |
| 180 | output_text = output_text.strip().split(" ") |
| 181 | now = len(output_text) - 1 |
| 182 | if now > pre: |
| 183 | print(" ".join(output_text[pre:now]), end=" ", flush=True) |
| 184 | pre = now |
| 185 | print(" ".join(output_text[pre:]), flush=True) |
| 186 | return " ".join(output_text) |
| 187 | |
| 188 | def print_output(self, text: str): |
| 189 | print(text) |
| 190 | |
| 191 | |
| 192 | def main(args): |
no outgoing calls
no test coverage detected
searching dependent graphs…