Skip to content

Instantly share code, notes, and snippets.

@tcapelle
Created June 2, 2023 08:24
Show Gist options
  • Save tcapelle/8293fece9f64bdbd4c3ea90ba2cc8410 to your computer and use it in GitHub Desktop.
Save tcapelle/8293fece9f64bdbd4c3ea90ba2cc8410 to your computer and use it in GitHub Desktop.
import os
import openai
from rich.console import Console
console = Console()
openai.api_key = os.getenv("OPENAI_API_KEY")
history = [{"role": "system", "content": "You are a helpful assistant."},]
while q := console.input("[bold red]> [/]"):
if q in ["quit", "exit", "q"]:
break
history.append({"role": "user", "content": q})
response = ""
for chunk in openai.ChatCompletion.create(
model="gpt-4",
messages=history,
stream=True
):
content = chunk["choices"][0]["delta"].get("content", "")
if content is not None:
response += content
console.print(f"[bold green]{content}[/]", end="")
console.print("")
history.append({"role": "assistant", "content": response})
print(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment