This repository was archived by the owner on Jan 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgpt.py
More file actions
executable file
·66 lines (56 loc) · 1.71 KB
/
gpt.py
File metadata and controls
executable file
·66 lines (56 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
import argparse
import json
import os
import requests
from pygments import highlight
from pygments.lexers import JsonLexer
from pygments.formatters import TerminalFormatter
# Set default values for command-line arguments
MODEL = "text-davinci-003" # model to use
TOKEN_COUNT = 300 # number of tokens to generate
TEMPERATURE = 0.4 # temperature
TOP_P = 1 # top_p value
FREQUENCY = 0.5 # frequency penalty
PRESENCE = 0.5 # presence penalty
def main():
"""Main entry point."""
# Parse command-line arguments
parser = argparse.ArgumentParser()
parser.add_argument("prompt", nargs="?", default="",
help="prompt to use as input for the GPT-3 model")
parser.add_argument("-k", "--api-key",
help="OpenAI API key")
args = parser.parse_args()
# Get the OpenAI API key
api_key = args.api_key or os.environ.get("OPENAI_API_KEY")
if not api_key:
print("Error: OPENAI_API_KEY is not set.")
return
# Set up the API request
url = "https://api.openai.com/v1/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
data = {
"model": MODEL,
"prompt": args.prompt,
"temperature": TEMPERATURE,
"top_p": TOP_P,
"frequency_penalty": FREQUENCY,
"presence_penalty": PRESENCE,
"max_tokens": TOKEN_COUNT,
}
# Send the API request
response = requests.post(url, headers=headers, json=data)
# Check for errors in the API response
if response.status_code != 200:
print("Error:", response.json()["error"])
return
# Extract the text from the response
text = response.json()["choices"][0]["text"]
# Print the output
print(text)
if __name__ == "__main__":
main()