Compare commits

..

4 commits

Author SHA1 Message Date
Hayden Johnson 0455b12d90 Add indicator in chat log for assistant responses
Now it is clear when the assistant responds in the terminal interface.
2024-10-01 13:00:32 -07:00
Hayden Johnson dc772722d6 Add /save /clear and /exit commands
The save command takes an optional filename to save the conversation to.
the clear command clears the chat history.
2024-10-01 12:50:55 -07:00
Hayden Johnson d074c97645 Add tracking of entire conversation 2024-10-01 11:52:31 -07:00
Hayden Johnson 0fb11b0691 Add initial framework for commands 2024-10-01 11:50:35 -07:00

View file

@ -8,7 +8,7 @@ import argparse
import pygments import pygments
from pygments.lexers import get_lexer_by_name from pygments.lexers import get_lexer_by_name
from pygments.formatters import TerminalFormatter from pygments.formatters import TerminalFormatter
import os
server = 'localhost:11434' server = 'localhost:11434'
model = 'llama3.1:8b-instruct-q8_0' model = 'llama3.1:8b-instruct-q8_0'
@ -17,6 +17,42 @@ temp = 0.2
pattern = r'```[a-z]*\n[\s\S]*?\n```' pattern = r'```[a-z]*\n[\s\S]*?\n```'
line_pattern = r'`[a-z]*[\s\S]*?`' line_pattern = r'`[a-z]*[\s\S]*?`'
def save_conversation(filename='conversation.md'):
# check if filename already exists and increment filename if so
if not filename.endswith('.md'):
filename += '.md'
base, extension = os.path.splitext(filename)
i = 1
while os.path.exists(filename):
filename = f"{base}_{i}{extension}"
i += 1
# save conversation to filename
global conversation
with open(filename, 'w') as f:
f.write(conversation)
def parse_commands(text):
# See if user wrote any commands here
# returns bool: True if command was executed, False if not
# importantly, the command doesn't need to execute succesfully for it to return True
tokens = text.split(' ')
match tokens[0]:
case '/save':
if len(tokens) > 1:
save_conversation(tokens[1])
else:
save_conversation()
return True
case '/clear':
global history
history = [ system_prompt ]
return True
case '/exit':
exit()
return False
def highlight_code(language_name, code): def highlight_code(language_name, code):
# Check if the language is specified in the first line # Check if the language is specified in the first line
lexer_name = language_name lexer_name = language_name
@ -91,9 +127,11 @@ def copy_string_to_clipboard(string):
code_history = [] code_history = []
history = [ system_prompt = {"role": "system", "content": "You are a helpful, smart, kind, and efficient AI assistant. You always fulfill the user's requests accurately and concisely."}
{"role": "system", "content": "You are a helpful, smart, kind, and efficient AI assistant. You always fulfill the user's requests accurately and concisely."},
] history = [ system_prompt ]
conversation = ""
def chat(message, stream=True): def chat(message, stream=True):
history.append({"role": "user", "content": message}) history.append({"role": "user", "content": message})
@ -137,10 +175,19 @@ def chat(message, stream=True):
return result return result
def chat2(args, user_input, stream=True): def chat2(args, user_input, stream=True):
if args.reflect: global conversation
if parse_commands(user_input):
result = ''
elif args.reflect:
print('assistant: ', end='')
result = reflection_mode(user_input, stream) result = reflection_mode(user_input, stream)
else: else:
print('assistant: ', end='')
result = chat(user_input, stream) result = chat(user_input, stream)
if result != '':
conversation += 'user: ' + user_input + '\n'
conversation += 'assistant: ' + result + '\n'
return result return result
def highlightify_text(full_text): def highlightify_text(full_text):