Created
June 2, 2023 08:24
-
-
Save tcapelle/8293fece9f64bdbd4c3ea90ba2cc8410 to your computer and use it in GitHub Desktop.
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
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