Views
No views yet
1from transformers import pipeline
2
3pipe = pipeline("text-generation", model="0x404/ccs-code-llama-7b", device_map="auto")
4tokenizer = pipe.tokenizer
5
6
7def prepare_prompt(commit_message: str, git_diff: str, context_window: int = 1024):
8 prompt_head = "<s>[INST] <<SYS>>\nYou are a commit classifier based on commit message and code diff.Please classify the given commit into one of the ten categories: docs, perf, style, refactor, feat, fix, test, ci, build, and chore. The definitions of each category are as follows:\n**feat**: Code changes aim to introduce new features to the codebase, encompassing both internal and user-oriented features.\n**fix**: Code changes aim to fix bugs and faults within the codebase.\n**perf**: Code changes aim to improve performance, such as enhancing execution speed or reducing memory consumption.\n**style**: Code changes aim to improve readability without affecting the meaning of the code. This type encompasses aspects like variable naming, indentation, and addressing linting or code analysis warnings.\n**refactor**: Code changes aim to restructure the program without changing its behavior, aiming to improve maintainability. To avoid confusion and overlap, we propose the constraint that this category does not include changes classified as ``perf'' or ``style''. Examples include enhancing modularity, refining exception handling, improving scalability, conducting code cleanup, and removing deprecated code.\n**docs**: Code changes that modify documentation or text, such as correcting typos, modifying comments, or updating documentation.\n**test**: Code changes that modify test files, including the addition or updating of tests.\n**ci**: Code changes to CI (Continuous Integration) configuration files and scripts, such as configuring or updating CI/CD scripts, e.g., ``.travis.yml'' and ``.github/workflows''.\n**build**: Code changes affecting the build system (e.g., Maven, Gradle, Cargo). Change examples include updating dependencies, configuring build configurations, and adding scripts.\n**chore**: Code changes for other miscellaneous tasks that do not neatly fit into any of the above categories.\n<</SYS>>\n\n"
9 prompt_head_encoded = tokenizer.encode(prompt_head, add_special_tokens=False)
10
11 prompt_message = f"- given commit message:\n{commit_message}\n"
12 prompt_message_encoded = tokenizer.encode(prompt_message, max_length=64, truncation=True, add_special_tokens=False)
13
14 prompt_diff = f"- given commit diff: \n{git_diff}\n"
15 remaining_length = (context_window - len(prompt_head_encoded) - len(prompt_message_encoded) - 6)
16 prompt_diff_encoded = tokenizer.encode(prompt_diff, max_length=remaining_length, truncation=True, add_special_tokens=False)
17
18 prompt_end = tokenizer.encode(" [/INST]", add_special_tokens=False)
19 return tokenizer.decode(prompt_head_encoded + prompt_message_encoded + prompt_diff_encoded + prompt_end)
20
21
22def classify_commit(commit_message: str, git_diff: str, context_window: int = 1024):
23 prompt = prepare_prompt(commit_message, git_diff, context_window)
24 result = pipe(prompt, max_new_tokens=10, pad_token_id=pipe.tokenizer.eos_token_id)
25 label = result[0]["generated_text"].split()[-1]
26 return label
27classify_commit function to classify your commit by inputting the commit's message and git diff. The context_window controls the size of the entire prompt, set to 1024 by default but adjustable to a larger value like 2048 to include more git diff in one prompt. Here is an example of its usage:1import requests
2from github import Github
3
4def fetch_message_and_diff(repo_name, commit_sha):
5 g = Github()
6 try:
7 repo = g.get_repo(repo_name)
8 commit = repo.get_commit(commit_sha)
9 if commit.parents:
10 parent_sha = commit.parents[0].sha
11 diff_url = repo.compare(parent_sha, commit_sha).diff_url
12 return commit.commit.message, requests.get(diff_url).text
13 else:
14 raise ValueError("No parent found for this commit, unable to retrieve diff.")
15 except Exception as e:
16 raise RuntimeError(f"Error retrieving commit information: {e}")
17
18message, diff = fetch_message_and_diff("pytorch/pytorch", "9856bc50a251ac054debfdbbb5ed29fc4f6aeb39")
19print(classify_commit(message, diff))fetch_message_and_diff that fetches the commit message and diff for any specified SHA from a GitHub repository, enabling our model to classify the commit accordingly.