-
Notifications
You must be signed in to change notification settings - Fork 0
/
explaincode.py
31 lines (26 loc) · 1.1 KB
/
explaincode.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
import os
import openai
# Get the OpenAI API key from a file
with open("./inputs/inputs.txt", "r") as f:
inputs = dict(line.strip().split(":") for line in f)
openai_api_key = inputs["openai_api_key"]
# Set up the OpenAI API client
openai.api_key = openai_api_key
# Define the code snippet to generate unit tests for
for filename in os.listdir('./sources'):
with open(os.path.join('./sources', filename), 'r') as file:
code_snippet = file.read()
# Call the OpenAI API to generate unit tests for the code snippet
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"Explain in natural language what this code is doing:\n\n{code_snippet}",
max_tokens=1024,
n=1,
stop=None,
temperature=0.7,
)
# Write the generated unit tests to a file
output_filename = filename + ".explained"
with open(os.path.join('./outputs', output_filename), 'w') as output_file:
output_file.write(response.choices[0].text)
print(f"Code explained here {output_filename}")