Skip to content

Instantly share code, notes, and snippets.

@nonnontrivial
Last active April 19, 2023 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nonnontrivial/7eb3e5f746c06989042f0723b8735fb7 to your computer and use it in GitHub Desktop.
Save nonnontrivial/7eb3e5f746c06989042f0723b8735fb7 to your computer and use it in GitHub Desktop.
prompt script
#!/usr/bin/env python3
"""
script for gpt-assisted commandline workflows.
simply add this to your $PATH, rename to remo-
ve the .py extension, and chmod +x to make it
executable.
>>> gpt where does the time go?
"""
import openai
import os
import sys
import typing as t
model = os.environ.get("OPENAI_API_MODEL", "gpt-3.5-turbo")
api_key = os.environ.get("OPENAI_API_KEY", "")
default_system_prompt = "you are technical GPT, who can assist with all things related to linux,python,golang,java,typescript, and beyond"
def main(query: t.List[str]):
query = " ".join(query)
response = openai.ChatCompletion.create(
model=model,
messages=[
{"role": "system", "content": default_system_prompt},
{"role": "user", "content": query},
],
temperature=0.8,
stream=True,
)
for chunk in response:
[choice] = chunk["choices"]
try:
content = choice["delta"]["content"]
except KeyError as e:
pass
else:
print(content, end="")
print("\n")
if __name__ == "__main__":
# treat all arguments as separate words
_, *query = sys.argv
main(query)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment