Skip to content

Instantly share code, notes, and snippets.

@iTrooz
Last active July 2, 2024 14:56
Show Gist options
  • Save iTrooz/f5a2f9b5e79fb54d8439cedba49bdfa3 to your computer and use it in GitHub Desktop.
Save iTrooz/f5a2f9b5e79fb54d8439cedba49bdfa3 to your computer and use it in GitHub Desktop.
basic ChatGPT terminal client
#!/bin/python3
import sys
from openai import OpenAI
myPrompt = ' '.join(sys.argv[1:]).strip()
if not myPrompt:
print("Enter prompt. Enter newline when done")
myPrompt = ""
while True:
a = input("> ")
if a == "":
break
myPrompt +=a+"\n"
print("Me:", myPrompt, file=sys.stderr)
client = OpenAI(api_key = "YOUR KEY HERE")
stream = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "Output clear, short and concise answers and use the less words possible unless told otherwise"},
{"role": "system", "content": "If I ask for a script, assume I'm going to pipe it into a file, so don't output anything but the code"},
{"role": "user", "content": myPrompt}
],
stream=True,
)
try:
print("ChatGPT: ", end="", flush=True, file=sys.stderr)
for part in stream:
print(part.choices[0].delta.content or "", end="", flush=True)
print()
except KeyboardInterrupt:
print("\n\nCtrl+C")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment