-
Notifications
You must be signed in to change notification settings - Fork 0
/
gut.py
59 lines (49 loc) · 1.6 KB
/
gut.py
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
import click
import requests
import os
import subprocess
import json
GROQ_API_KEY = os.environ.get('GROQ_API_KEY')
CONFIG_FILE = os.path.expanduser('~/.gut_config')
def load_api_key():
global GROQ_API_KEY
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, 'r') as f:
GROQ_API_KEY = f.read().strip()
return GROQ_API_KEY
def save_api_key(api_key):
with open(CONFIG_FILE, 'w') as f:
f.write(api_key)
def request_api_key():
api_key = click.prompt('Please enter your GROQ API key', type=str)
save_api_key(api_key)
return api_key
def send_to_groq(git_output):
# Construct the path to 'prompt.txt' using os.path.join and os.path.dirname
prompt_path = os.path.join(os.path.dirname(__file__), 'prompt.txt')
with open(prompt_path, 'r') as f:
prompt = f.read().strip()
url = "https://api.groq.com/openai/v1/chat/completions"
headers = {
"Authorization": f"Bearer {GROQ_API_KEY}",
"Content-Type": "application/json"
}
data = {
"messages": [{"role": "user", "content": prompt + git_output}],
"model": "llama3-8b-8192"
}
response = requests.post(url, headers=headers, json=data)
return response.json()['choices'][0]['message']['content']
@click.group()
def cli():
global GROQ_API_KEY
GROQ_API_KEY = load_api_key()
if not GROQ_API_KEY:
GROQ_API_KEY = request_api_key()
@cli.command()
def status():
git_output = subprocess.check_output(['git', 'status']).decode('utf-8')
result = send_to_groq(git_output)
click.echo(result)
if __name__ == '__main__':
cli()