Skip to content

Commit aa7fab3

Browse files
authored
⚡️add help-ai command for AI generated help responses (#66)
* ⚡️add help-ai command for AI generated help responses * ⚡️update help-ai command for improved user interaction * ⚡️add environment variable support for openai api key retrieval * ✨add anthropic api key env support, refactor anthropic service
1 parent 1c20cd1 commit aa7fab3

File tree

5 files changed

+98
-6
lines changed

5 files changed

+98
-6
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ git-ai-commit --version # verify installation
4141
git-ai-commit config --setup
4242
```
4343

44+
3. Your done, happy committing! Check out our fun range of command, the LLM can even help you, just run...
45+
46+
```bash
47+
git-ai-commit help-ai [question?]
48+
49+
# or get help the ol fashion way
50+
51+
git-ai-commit --help
52+
```
53+
4454
## ⚡️ Quick Start: Setup Git Hook
4555

4656
To quickly setup your [`prepare-commit-msg`](<https://git-scm.com/docs/githooks#_prepare_commit_msg>) git hook, execute the command below.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from ai_commit_msg.services.openai_service import OpenAiService
2+
3+
4+
def help_ai_handler(args, help_menu):
5+
user_message = " ".join(args.message)
6+
7+
if not user_message:
8+
print(
9+
"Let me know what you need help with \n\n git-ai-commit help-ai [message]"
10+
)
11+
return
12+
13+
prompt = [
14+
{
15+
"role": "system",
16+
"content": f"""
17+
Hey GPT, based on the follow documentation on the CLI's arguments
18+
19+
{help_menu}
20+
21+
what are set of arguments and options should i use if someone requests for help?
22+
23+
Please only respond with the exact command that should be used and nothing else.
24+
""",
25+
},
26+
{
27+
"role": "user",
28+
"content": user_message,
29+
},
30+
]
31+
32+
ai_gen_commit_msg = OpenAiService().chat_with_openai(prompt)
33+
34+
print(ai_gen_commit_msg)
35+
36+
return

ai_commit_msg/main.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
from typing import Sequence
66

7+
from ai_commit_msg.cli.help_ai_handler import help_ai_handler
78
from ai_commit_msg.cli.summary_handler import summary_handler
89
from ai_commit_msg.cli.config_handler import config_handler, handle_config_setup
910
from ai_commit_msg.cli.gen_ai_commit_message_handler import (
@@ -88,6 +89,13 @@ def main(argv: Sequence[str] = sys.argv[1:]) -> int:
8889
# Help command
8990
subparsers.add_parser("help", help="Display this help message")
9091

92+
help_ai_parser = subparsers.add_parser(
93+
"help-ai", help="🤖 Get help from AI to find the right command for you"
94+
)
95+
help_ai_parser.add_argument(
96+
"message", nargs=argparse.REMAINDER, help="Additional message for help"
97+
)
98+
9199
# Hook command
92100
hook_parser = subparsers.add_parser(
93101
"hook", help="🪝 Run the prepare-commit-msg hook to generate commit messages"
@@ -132,10 +140,20 @@ def main(argv: Sequence[str] = sys.argv[1:]) -> int:
132140

133141
args = parser.parse_args(argv)
134142

143+
def get_full_help_menu():
144+
full_help_menu = "\nAvailable commands:\n"
145+
for name, subparser in subparsers.choices.items():
146+
full_help_menu += f"\n{name}:\n"
147+
full_help_menu += subparser.format_help()
148+
149+
return full_help_menu
150+
135151
if args.command == "config":
136152
config_handler(args)
137153
elif args.command == "help":
138-
parser.print_help()
154+
print(get_full_help_menu())
155+
elif args.command == "help-ai":
156+
help_ai_handler(args, help_menu=get_full_help_menu())
139157
elif args.command == "hook":
140158
hook_handler(args)
141159
elif args.command == "summarize" or args.command == "summary":

ai_commit_msg/services/anthropic_service.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
from ai_commit_msg.utils.models import ANTHROPIC_MODEL_LIST
1+
import os
22
import anthropic
3+
4+
from ai_commit_msg.utils.models import ANTHROPIC_MODEL_LIST
35
from ai_commit_msg.services.config_service import ConfigService
46
from ai_commit_msg.utils.error import map_error
57

@@ -8,7 +10,7 @@ class AnthropicService:
810
api_key = ""
911

1012
def __init__(self):
11-
self.api_key = ConfigService().get_anthropic_api_key()
13+
self.api_key = AnthropicService.get_anthropic_api_key()
1214

1315
if self.api_key is None or self.api_key == "":
1416
raise Exception(
@@ -19,6 +21,20 @@ def __init__(self):
1921
api_key=self.api_key,
2022
)
2123

24+
@staticmethod
25+
def get_anthropic_api_key():
26+
27+
local_api_key = ConfigService().get_anthropic_api_key()
28+
29+
if local_api_key != "":
30+
return local_api_key
31+
32+
env_api_key = os.environ.get("ANTHROPIC_API_KEY")
33+
if env_api_key != "":
34+
return env_api_key
35+
36+
return ""
37+
2238
def chat_completion(self, messages):
2339
select_model = ConfigService.get_model()
2440

ai_commit_msg/services/openai_service.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from logging import Logger
2-
from ai_commit_msg.utils.error import map_error
1+
import os
32
from openai import OpenAI
3+
4+
from ai_commit_msg.utils.error import map_error
45
from ai_commit_msg.services.config_service import ConfigService
56
from ai_commit_msg.services.local_db_service import (
67
LocalDbService,
@@ -43,7 +44,18 @@ def chat_with_openai(self, messages):
4344
@staticmethod
4445
def get_openai_api_key():
4546
raw_json_db = LocalDbService().get_db()[CONFIG_COLLECTION_KEY]
46-
return raw_json_db["openai_api_key"]
47+
48+
# if the key is set in the local db, use that...
49+
local_api_key = raw_json_db["openai_api_key"]
50+
if local_api_key != "":
51+
return local_api_key
52+
53+
# ...otherwise, check the environment variable
54+
env_api_key = os.environ.get("OPENAI_API_KEY")
55+
if env_api_key != "":
56+
return env_api_key
57+
58+
return ""
4759

4860
@staticmethod
4961
def set_openai_api_key(api_key):

0 commit comments

Comments
 (0)