forked from allenporter/gpt3-python-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt3.py
71 lines (59 loc) · 2.03 KB
/
gpt3.py
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
67
68
69
70
71
import argparse
import openai
import yaml
class ChatGPT:
def __init__(self):
self.api_key = openai.api_key
def generate_response(self, prompt):
"""
Generates a response to the given prompt using the GPT-3 API.
"""
response = openai.Completion.create(
#engine="text-davinci-002",
engine="code-davinci-002",
prompt=prompt,
temperature=0.2,
max_tokens=1024,
)
print(response)
return response.choices[0].text
def main():
# Parse command-line arguments
parser = argparse.ArgumentParser()
parser.add_argument("--api-key", required=True, help="API key for the GPT-3 API")
parser.add_argument("--project-file", required=True, help="YAML project file")
parser.add_argument("--training-data", required=True, help="Markdown training data file")
args = parser.parse_args()
# Set the API key for the GPT-3 API
openai.api_key = args.api_key
# Create an instance of the ChatGPT class
chat_gpt = ChatGPT()
# Read the project file
with open(args.project_file, "r") as f:
project = yaml.safe_load(f)
# Read the training data file
with open(args.training_data, "r") as f:
training_data = f.read()
# Create the prompts
prompts = []
for file in project["files"]:
prompts.append(f"{training_data}\nFile: {file['name']}\nDescription: {file['description']}\nContents:\n")
# Call the GPT-3 API
response = openai.Completion.create(
engine="text-davinci-002",
prompt=prompts,
temperature=0.5,
max_tokens=1024,
)
# Parse the response
for i, file in enumerate(project["files"]):
filename = file["name"]
contents = response["choices"][i]["text"]
print(f"Writing {filename}")
print("---")
print(contents)
print("---")
#with open(os.path.join(args.output_dir, filename), "w") as f:
# f.write(contents)
if __name__ == "__main__":
main()