-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
86 lines (74 loc) · 2.45 KB
/
Copy pathmain.py
File metadata and controls
86 lines (74 loc) · 2.45 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
import openai
import subprocess
import logging
from typing import Union
openai.api_key = os.getenv("INPUT_OPENAIAPIKEY")
openai.organization = os.getenv("INPUT_OPENAIORG")
MAX_CHARACTERS = os.getenv("INPUT_MAXCHARACTERS") or 1000
TEMPERATURE = os.getenv("INPUT_TEMPERATURE") or 0.7
TOKENS = os.getenv("INPUT_TOKENS") or 256
PRESENCE = os.getenv("INPUT_PRESENCEPENALTY") or 0
FREQUENCY = os.getenv("INPUT_FREQUENCYPENALTY") or 0
ADDITIONAL_INSTRUCTION = (
os.getenv("INPUT_PROMPTINSTRUCTIONS")
or "Ignore words between colons, i.e. :"
)
BASE_PROMPT = f"Summarize the following code commits into a single paragraph. Do not use bullet points. Do not use a list. {ADDITIONAL_INSTRUCTION}"
def list_commits():
output = subprocess.run(
'git log --no-merges --pretty=format:"%s"'.split(" "),
stdout=subprocess.PIPE,
universal_newlines=True,
).stdout[:MAX_CHARACTERS]
return output
def get_prompt(commits: str):
return f"{BASE_PROMPT}:\n\n{commits}"
def coerce_output(text: str):
splits = text.strip().split("\n\n")
if len(splits) > 1:
return splits[-1]
return splits[0]
def main(
prompt: str,
temperature: float = 0.7,
tokens: int = 256,
frequency: Union[float, int] = 0,
presence: Union[float, int] = 0,
):
response = openai.Completion.create(
model="text-davinci-002",
prompt=prompt,
temperature=temperature,
max_tokens=tokens,
top_p=1,
frequency_penalty=frequency,
presence_penalty=presence,
)
if "choices" in response:
if len(response["choices"]) > 0: # type: ignore
if response["choices"][0]["text"]: # type: ignore
print(
r"{}".format(coerce_output(response["choices"][0]["text"]))
)
else:
print("The text reponse contained nothing.")
else:
print(
"Opps sorry, something went wrong with the request to the AI this time."
)
# if __name__ == "__main__":
# logging.basicConfig(level=logging.DEBUG)
logging.debug(f"Starting the commit translation process. {os.getcwd()}")
commits: str = list_commits()
logging.debug(f"The commits: {commits}")
prompt: str = get_prompt(commits)
logging.info(f"The prompt: {prompt}")
main(
prompt,
float(TEMPERATURE),
int(TOKENS),
float(FREQUENCY),
float(PRESENCE),
)
logging.debug("main(prompt) has been successfully run. Goodbye.")