Finish prototype

This commit is contained in:
Tanner Collin 2024-09-26 05:09:21 +00:00
parent 4728069da0
commit 7556607f89

86
main.py Normal file
View File

@ -0,0 +1,86 @@
import os, logging
DEBUG = os.environ.get('DEBUG')
logging.basicConfig(
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s',
level=logging.DEBUG if DEBUG else logging.INFO)
import requests
import subprocess
LLAMA_URL = 'http://10.55.0.105:11434/api/generate'
NOTES_DIR = '/home/tanner/notes-git/notes'
#NOTES_DIR = '/home/tanner/'
def llama(prompt):
data = dict(model='llama3.1', prompt=prompt, stream=False)
r = requests.post(LLAMA_URL, json=data, timeout=10)
r = r.json()
return r['response']
def git_diff():
result = subprocess.run(['git', 'diff', '--cached', '-U0'], stdout=subprocess.PIPE, cwd=NOTES_DIR)
diff = result.stdout.decode()
if diff:
logging.info('Analyzing diff:\n%s', diff)
return diff
def git_add():
result = subprocess.run(['git', 'add', '-A'], stdout=subprocess.PIPE, cwd=NOTES_DIR)
def git_commit(message):
result = subprocess.run(['git', 'commit', '-m', message], stdout=subprocess.PIPE, cwd=NOTES_DIR)
return result.stdout.decode()
def generate_message(diff):
prompt = '''
Write a ONE LINE git commit message for the following diff.
Respond with only the commit message.
Do not include quotes.
Do not include file extensions.
DO NOT just say "Update notes", "Add new notes", "Refactor notes".
Tersely summarize how each note changed.
Write in imperitive tense.
\n\n==============\n\n'''
prompt += diff
message = llama(prompt)
logging.info('Generated message: %s', message)
if '.md' in message.lower():
logging.info(' Message contains .md extension, aborting')
return False
elif 'notes/' in message.lower():
logging.info(' Message contains notes directory, aborting')
return False
return message
def main():
git_add() # add so we can diff new files
diff = git_diff()
if not diff:
logging.info('No diff, exiting.')
exit(0)
for _ in range(5):
message = generate_message(diff)
if message:
break
else: # for loop never broke
logging.info('Unable to generate acceptable message, exiting.')
exit(1)
git_commit(message)
if __name__ == '__main__':
main()