Created
March 8, 2024 07:52
-
-
Save saalaus/db2ebbd7980ebf2793af4283a419aa18 to your computer and use it in GitHub Desktop.
ChatGPT in terminal with multiple line, markdown highlight and keybinds.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ChatGPT in terminal with multiple line, markdown highlight and keybind. | |
# requirements - pip install openai==1.3.5 rich==13.7.0 prompt-toolkit==3.0.39 | |
# keybind: ctrl+c - clear input or exit, f1 - new line, enter - send message | |
# commands: clear - clear context | |
from openai import OpenAI | |
from prompt_toolkit import prompt | |
from prompt_toolkit.key_binding import KeyBindings | |
from rich.console import Console | |
from rich.live import Live | |
from rich.markdown import Markdown | |
client = OpenAI( | |
api_key= "openai api key" | |
) | |
console = Console() | |
bindings = KeyBindings() | |
@bindings.add("f1") | |
def _(event): | |
event.current_buffer.insert_text("\n") | |
@bindings.add("enter") | |
def _(event): | |
event.current_buffer.validate_and_handle() | |
@bindings.add("c-c") | |
def _(event): | |
if event.current_buffer.text: | |
event.current_buffer.text = "" | |
else: | |
raise KeyboardInterrupt() | |
def print_text_from_response(response): | |
markdown_text = "▼ " | |
try: | |
with Live(console=console, refresh_per_second=50) as live: | |
for data in response: | |
text = data.choices[0].delta.content | |
markdown_text += text if text else "" | |
md = Markdown(markdown_text) | |
live.update(md) | |
except KeyboardInterrupt: | |
response.close() | |
console.print("skip completion...") | |
except Exception as error: | |
console.print(f"rate limit... :(\n{error}") | |
return markdown_text[2:] | |
def get_user_input(text: str): | |
return prompt(text, key_bindings=bindings, multiline=True, | |
complete_while_typing=False) | |
def main(): | |
context = [] | |
while True: | |
message = get_user_input("▶ ") | |
if message == "clear": | |
context = [] | |
console.clear() | |
print("Context clear") | |
continue | |
context.append({"role": "user", "content": message}) | |
try: | |
completion = client.chat.completions.create(model="gpt-3.5-turbo-0125", messages=context, stream=True, timeout=10) | |
complete_text = print_text_from_response(completion) | |
context.append({"role": "assistant", "content": complete_text}) | |
console.print() | |
except Exception as error: | |
console.print(f"API error... :(\n{error}") | |
continue | |
if __name__ == "__main__": | |
try: | |
main() | |
except KeyboardInterrupt: | |
print("Exit...") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment