forked from litch/ai-school-tech-writer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
60 lines (54 loc) · 2.71 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
import os
import base64
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers.string 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: {diff["filename"]}\nDiff: \n{diff["patch"]}\n' for diff 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") # AI-GEN - Cursor
# Construct the prompt with clear instructions for the LLM.
return ("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"
)
def call_openai(prompt):
client=ChatOpenAI(api_key=os.environ.get("OPENAI_API_KEY"), model="gpt-3.5-turbo-0125")
try:
messages = [
{
"role": "system",
"content": "You are an AI trained to help updating README files based on code changes and commit messages from a GitHub pull request."
},
{
"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 code changes and commit messages."
commit_sha = os.environ.get("COMMIT_SHA")
main_branch = repo.get_branch("main")
new_branch_name = f'update-readme-{commit_sha[:7]}'
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: Proposed README update based on code changes and commit messages."
pr_body = "This PR proposes an update to the README based on code changes and commit messages from a GitHub pull request. The update was generated by an AI model."
pull_request = repo.create_pull(title=pr_title, body=pr_body, head=new_branch_name, base="main")
return pull_request