Skip to content

Instantly share code, notes, and snippets.

@shouya
Last active March 16, 2023 05:17
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 shouya/a19978cdbb9bc9b9bd2889169ae58090 to your computer and use it in GitHub Desktop.
Save shouya/a19978cdbb9bc9b9bd2889169ae58090 to your computer and use it in GitHub Desktop.
Simple ChatGPT used in a Unix pipe

To use this script, you need to install the openai package using pip. You also need to set the OPENAI_API_KEY environment variable to your OpenAI API key.

For example, to ask a natural language question, you can pipe the output of ls -al to the script like this:

ls -al | ./gpt "What are the permissions of the files?"

The script will use the OpenAI GPT-3 model to generate a response to your question with a temperature of 0.2. The response will be printed to the console.


The above text is generated with the following prompt:

cat gpt | gpt "write a basic usage example for this script. the filename of the script is 'gpt'. explain the dep (pip install) and env variable is needed. explain how it can be used it with a meaningful example on asking it a natural language question by piping the output of ls -al to it. be concise"
#!/usr/bin/env python
import sys
import openai
x = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "Do not explain anything. Be concise."},
{"role": "system", "content": sys.argv[1]},
{"role": "user", "content": sys.stdin.read()},
],
temperature=0.2
)
print(x["choices"][0]["message"]["content"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment