Skip to content

Instantly share code, notes, and snippets.

@iTrooz
Last active November 7, 2023 09:01
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("Syntax : ", sys.argv[0], "prompt")
exit(1)
print("Me:", myPrompt, file=sys.stderr)
client = OpenAI(api_key = "YOUR KEY HERE")
stream = client.chat.completions.create(
model="gpt-4-1106-preview",
messages=[{"role": "user", "content": myPrompt}],
stream=True,
)
print("ChatGPT: ", end="", flush=True, file=sys.stderr)
for part in stream:
print(part.choices[0].delta.content or "", end="", flush=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment