-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoh_git_commit.py
More file actions
96 lines (81 loc) · 3.07 KB
/
Copy pathoh_git_commit.py
File metadata and controls
96 lines (81 loc) · 3.07 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
87
88
89
90
91
92
93
94
95
96
import subprocess
import litellm
from litellm import completion
from litellm import Router
from pathlib import Path
import yaml
from jinja2 import Template
def render_local_prompt(file_name, **vars):
with open(file_name) as f:
# Simple logic to split role and content
raw = Template(f.read()).render(**vars)
# Convert to LiteLLM format
return [{"role": "user", "content": raw}]
# 1. Get the directory where the script is located
# .parent gives you the directory of the current file
SCRIPT_DIR = Path(__file__).resolve().parent
# 2. Define your paths (Clean and readable)
CONFIG_PATH = SCRIPT_DIR / "config.yaml"
PROMPTS_DIR = SCRIPT_DIR / "prompts"
with CONFIG_PATH.open("r") as f:
config = yaml.safe_load(f)
# 2. Pass the 'model_list' key from your YAML to the Router
# config['model_list'] is now a true Python list of dictionaries
router = Router(model_list=config["model_list"])
# 2. Register the 'prompts' and 'litellm_settings' globally
# This is what makes prompt_id="gitmoji-commit" work in .completion()
if "prompts" in config:
litellm.prompts = config["prompts"]
if "litellm_settings" in config:
# Set the global search path for .prompt files
litellm.global_prompt_directory = config["litellm_settings"].get("global_prompt_directory", "./prompt")
def get_git_diff():
try:
# Get staged changes first, then unstaged if staged is empty
diff = subprocess.check_output(["git", "diff", "--cached"], text=True)
if not diff:
diff = subprocess.check_output(["git", "diff"], text=True)
return diff
except Exception:
return None
def generate_with_prompt_id(diff_content):
"""
Uses the local .prompt file logic.
LiteLLM will look for 'hello.prompt' in the global_prompt_directory.
"""
logs = subprocess.check_output(
["git", "log", "-n", "1", "--format=%s"],
text=True,
stderr=subprocess.DEVNULL
)
response = router.completion(
model="oh-git-commit-model", # Use the name from your config
messages=render_local_prompt(
PROMPTS_DIR/"gitmoji-commit.prompt",
user_message=f"{diff_content}",
logs=logs)
## not allowed params of Router
# messages=[{"role": "user", "content": "<IGNORED>"}], # <--- Add this line to satisfy the requirement
# prompt_id="gitmoji-commit",
# prompt_variables={
# "user_message": f"{diff_content}"
# }
)
return response.choices[0].message.content.strip()
def main():
diff = get_git_diff()
if not diff:
print("No changes found.")
return
print("🤖 Generating commit message...")
msg = generate_with_prompt_id(diff)
print(f"\nProposed Commit:\n{msg}\n")
confirm = input("Apply? (y/n/-y): ").lower()
if confirm in ['y', '-y']:
if confirm == '-y':
subprocess.run(["git", "add", "."])
# Wrap msg in quotes for the shell
subprocess.run(["git", "commit", "-m", msg])
print("🚀 Committed successfully!")
if __name__ == "__main__":
main()