Skip to content

Commit fe0b9b8

Browse files
authored
enable gac help-ai to work with all supported models, patch summarize crash (#67)
* ✨refactor cli help_ai_handler,gen_commit_msg,add llm_chat_completion module * lint * fix summarize command
1 parent 746b9e0 commit fe0b9b8

File tree

5 files changed

+34
-27
lines changed

5 files changed

+34
-27
lines changed

ai_commit_msg/cli/help_ai_handler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ai_commit_msg.services.openai_service import OpenAiService
1+
from ai_commit_msg.core.gen_commit_msg import llm_chat_completion
22

33

44
def help_ai_handler(args, help_menu):
@@ -20,7 +20,7 @@ def help_ai_handler(args, help_menu):
2020
2121
what are set of arguments and options should i use if someone requests for help?
2222
23-
Please only respond with the exact command that should be used and nothing else.
23+
Please only respond with the exact commands that should be used and nothing else.
2424
""",
2525
},
2626
{
@@ -29,7 +29,7 @@ def help_ai_handler(args, help_menu):
2929
},
3030
]
3131

32-
ai_gen_commit_msg = OpenAiService().chat_with_openai(prompt)
32+
ai_gen_commit_msg = llm_chat_completion(prompt)
3333

3434
print(ai_gen_commit_msg)
3535

ai_commit_msg/cli/summary_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def summaryFromDiffFile(diff_file_path):
2424

2525
def summary_handler(args):
2626

27-
if args.diff:
27+
if hasattr(args, 'diff') and args.diff is not None:
2828
summaryFromDiffFile(args.diff)
2929
return
3030

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,15 @@
1+
from ai_commit_msg.core.llm_chat_completion import llm_chat_completion
12
from ai_commit_msg.core.prompt import get_prompt
2-
from ai_commit_msg.services.anthropic_service import AnthropicService
33
from ai_commit_msg.services.config_service import ConfigService
4-
from ai_commit_msg.services.git_service import GitService
5-
from ai_commit_msg.services.ollama_service import OLlamaService
6-
from ai_commit_msg.services.openai_service import OpenAiService
7-
from ai_commit_msg.utils.logger import Logger
8-
from ai_commit_msg.utils.models import ANTHROPIC_MODEL_LIST, OPEN_AI_MODEL_LIST
94

105

116
def generate_commit_message(diff: str = None) -> str:
127

138
if diff is None:
149
raise ValueError("Diff is required to generate a commit message")
1510

16-
select_model = ConfigService.get_model()
17-
1811
prompt = get_prompt(diff)
19-
20-
# TODO - create a factory with a shared interface for calling the LLM models, this will make it easier to add new models
21-
ai_gen_commit_msg = None
22-
if str(select_model) in OPEN_AI_MODEL_LIST:
23-
ai_gen_commit_msg = OpenAiService().chat_with_openai(prompt)
24-
elif select_model.startswith("ollama"):
25-
ai_gen_commit_msg = OLlamaService().chat_completion(prompt)
26-
elif select_model in ANTHROPIC_MODEL_LIST:
27-
ai_gen_commit_msg = AnthropicService().chat_completion(prompt)
28-
29-
if ai_gen_commit_msg is None:
30-
Logger().log("Unsupported model: " + select_model)
31-
return ""
12+
ai_gen_commit_msg = llm_chat_completion(prompt)
3213

3314
prefix = ConfigService().prefix
34-
3515
return prefix + ai_gen_commit_msg
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from ai_commit_msg.services.anthropic_service import AnthropicService
2+
from ai_commit_msg.services.config_service import ConfigService
3+
from ai_commit_msg.services.ollama_service import OLlamaService
4+
from ai_commit_msg.services.openai_service import OpenAiService
5+
from ai_commit_msg.utils.logger import Logger
6+
from ai_commit_msg.utils.models import ANTHROPIC_MODEL_LIST, OPEN_AI_MODEL_LIST
7+
8+
9+
def llm_chat_completion(prompt):
10+
select_model = ConfigService.get_model()
11+
12+
# TODO - create a factory with a shared interface for calling the LLM models, this will make it easier to add new models
13+
ai_gen_commit_msg = None
14+
if str(select_model) in OPEN_AI_MODEL_LIST:
15+
ai_gen_commit_msg = OpenAiService().chat_with_openai(prompt)
16+
elif select_model.startswith("ollama"):
17+
ai_gen_commit_msg = OLlamaService().chat_completion(prompt)
18+
elif select_model in ANTHROPIC_MODEL_LIST:
19+
ai_gen_commit_msg = AnthropicService().chat_completion(prompt)
20+
21+
if ai_gen_commit_msg is None:
22+
Logger().log("Unsupported model: " + select_model)
23+
return ""
24+
25+
return ai_gen_commit_msg

ai_commit_msg/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ def main(argv: Sequence[str] = sys.argv[1:]) -> int:
135135
help="Setup the prepare-commit-msg hook",
136136
)
137137
summary_cmd_parser.add_argument(
138-
"-d", "--diff", help="🔍 Provide a diff to generate a commit message"
138+
"-d", "--diff",
139+
default=None,
140+
help="🔍 Provide a diff to generate a commit message"
139141
)
140142

141143
args = parser.parse_args(argv)

0 commit comments

Comments
 (0)