Skip to content

Commit

Permalink
enable gac help-ai to work with all supported models, patch summari…
Browse files Browse the repository at this point in the history
…ze crash (#67)

* ✨refactor cli help_ai_handler,gen_commit_msg,add llm_chat_completion module

* lint

* fix summarize command
  • Loading branch information
ming1in authored Sep 12, 2024
1 parent 746b9e0 commit fe0b9b8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 27 deletions.
6 changes: 3 additions & 3 deletions ai_commit_msg/cli/help_ai_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ai_commit_msg.services.openai_service import OpenAiService
from ai_commit_msg.core.gen_commit_msg import llm_chat_completion


def help_ai_handler(args, help_menu):
Expand All @@ -20,7 +20,7 @@ def help_ai_handler(args, help_menu):
what are set of arguments and options should i use if someone requests for help?
Please only respond with the exact command that should be used and nothing else.
Please only respond with the exact commands that should be used and nothing else.
""",
},
{
Expand All @@ -29,7 +29,7 @@ def help_ai_handler(args, help_menu):
},
]

ai_gen_commit_msg = OpenAiService().chat_with_openai(prompt)
ai_gen_commit_msg = llm_chat_completion(prompt)

print(ai_gen_commit_msg)

Expand Down
2 changes: 1 addition & 1 deletion ai_commit_msg/cli/summary_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def summaryFromDiffFile(diff_file_path):

def summary_handler(args):

if args.diff:
if hasattr(args, 'diff') and args.diff is not None:
summaryFromDiffFile(args.diff)
return

Expand Down
24 changes: 2 additions & 22 deletions ai_commit_msg/core/gen_commit_msg.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
from ai_commit_msg.core.llm_chat_completion import llm_chat_completion
from ai_commit_msg.core.prompt import get_prompt
from ai_commit_msg.services.anthropic_service import AnthropicService
from ai_commit_msg.services.config_service import ConfigService
from ai_commit_msg.services.git_service import GitService
from ai_commit_msg.services.ollama_service import OLlamaService
from ai_commit_msg.services.openai_service import OpenAiService
from ai_commit_msg.utils.logger import Logger
from ai_commit_msg.utils.models import ANTHROPIC_MODEL_LIST, OPEN_AI_MODEL_LIST


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

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

select_model = ConfigService.get_model()

prompt = get_prompt(diff)

# TODO - create a factory with a shared interface for calling the LLM models, this will make it easier to add new models
ai_gen_commit_msg = None
if str(select_model) in OPEN_AI_MODEL_LIST:
ai_gen_commit_msg = OpenAiService().chat_with_openai(prompt)
elif select_model.startswith("ollama"):
ai_gen_commit_msg = OLlamaService().chat_completion(prompt)
elif select_model in ANTHROPIC_MODEL_LIST:
ai_gen_commit_msg = AnthropicService().chat_completion(prompt)

if ai_gen_commit_msg is None:
Logger().log("Unsupported model: " + select_model)
return ""
ai_gen_commit_msg = llm_chat_completion(prompt)

prefix = ConfigService().prefix

return prefix + ai_gen_commit_msg
25 changes: 25 additions & 0 deletions ai_commit_msg/core/llm_chat_completion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from ai_commit_msg.services.anthropic_service import AnthropicService
from ai_commit_msg.services.config_service import ConfigService
from ai_commit_msg.services.ollama_service import OLlamaService
from ai_commit_msg.services.openai_service import OpenAiService
from ai_commit_msg.utils.logger import Logger
from ai_commit_msg.utils.models import ANTHROPIC_MODEL_LIST, OPEN_AI_MODEL_LIST


def llm_chat_completion(prompt):
select_model = ConfigService.get_model()

# TODO - create a factory with a shared interface for calling the LLM models, this will make it easier to add new models
ai_gen_commit_msg = None
if str(select_model) in OPEN_AI_MODEL_LIST:
ai_gen_commit_msg = OpenAiService().chat_with_openai(prompt)
elif select_model.startswith("ollama"):
ai_gen_commit_msg = OLlamaService().chat_completion(prompt)
elif select_model in ANTHROPIC_MODEL_LIST:
ai_gen_commit_msg = AnthropicService().chat_completion(prompt)

if ai_gen_commit_msg is None:
Logger().log("Unsupported model: " + select_model)
return ""

return ai_gen_commit_msg
4 changes: 3 additions & 1 deletion ai_commit_msg/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ def main(argv: Sequence[str] = sys.argv[1:]) -> int:
help="Setup the prepare-commit-msg hook",
)
summary_cmd_parser.add_argument(
"-d", "--diff", help="🔍 Provide a diff to generate a commit message"
"-d", "--diff",
default=None,
help="🔍 Provide a diff to generate a commit message"
)

args = parser.parse_args(argv)
Expand Down

0 comments on commit fe0b9b8

Please sign in to comment.