forked from litch/ai-school-tech-writer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
69 lines (54 loc) · 2.53 KB
/
utility.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
import os
import base64
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
def format_data_for_openai(diffs, readme_content, commit_messages):
# Combine the changes into a string with clear delineation.
changes = '\n'.join([f"File: {file['filename']}\nDiff: {file['patch']}" for file in diffs])
# Combine all commit messages
commit_messages = '\n'.join(commit_messages) + '\n\n'
# Decode the README content
readme_content = base64.b64decode(readme_content.content).decode('utf-8')
# Construct the prompt with clear instructions for the LLM.
prompt = (
"Please review the following code changes and commit messages from a GitHub pull request:\n"
"Code changes from Pull Request:\n"
f"{changes}\n"
"Commit messages:\n"
f"{commit_messages}"
"Here is the current README file content:\n"
f"{readme_content}\n"
"Consider the code changes and commit messages, determine if the README needs to be updated. If so, edit the README, ensuring to maintain its existing style and clarity.\n"
"Updated README:\n"
)
return prompt
def call_openai(prompt):
client = ChatOpenAI(api_key=os.getenv('OPENAI_API_KEY'), model="gpt-3.5-turbo-0125")
try:
messages = [
{
"role": "system",
"content": "You are an AI trained with updating README files based on commit messages and code files"
},
{
"role": "user",
"content": prompt
}
]
response = client.invoke(input=messages)
parser = StrOutputParser()
content = parser.invoke(input=response)
return content
except Exception as e:
print(f"Error making LLM call: {e}")
def update_readme_and_create_pr(repo, updated_readme, readme_sha):
commit_message = "AI COMMIT: Proposed README update based on recent code changes."
commit_sha = os.getenv('COMMIT_SHA')
main_branch = repo.get_branch("main")
new_branch_name = f'update-readmy-{commit_sha[:7]}'
new_branch = repo.create_git_ref(ref=f"refs/heads/{new_branch_name}", sha=main_branch.commit.sha)
repo.update_file("README.md", commit_message, updated_readme, readme_sha, branch=new_branch_name)
pr_title = "AI PR: Update README based on recent changes"
pr_body = "This is an AI PR. Please review the README."
pull_request = repo.create_pull(title=pr_title, body=pr_body, head=new_branch_name, base="main")
return pull_request