Compare commits
4 commits
f6ec5cde2e
...
0455b12d90
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0455b12d90 | ||
|
|
dc772722d6 | ||
|
|
d074c97645 | ||
|
|
0fb11b0691 |
57
assistant.py
57
assistant.py
|
|
@ -8,7 +8,7 @@ import argparse
|
|||
import pygments
|
||||
from pygments.lexers import get_lexer_by_name
|
||||
from pygments.formatters import TerminalFormatter
|
||||
|
||||
import os
|
||||
|
||||
server = 'localhost:11434'
|
||||
model = 'llama3.1:8b-instruct-q8_0'
|
||||
|
|
@ -17,6 +17,42 @@ temp = 0.2
|
|||
pattern = r'```[a-z]*\n[\s\S]*?\n```'
|
||||
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):
|
||||
# Check if the language is specified in the first line
|
||||
lexer_name = language_name
|
||||
|
|
@ -91,9 +127,11 @@ def copy_string_to_clipboard(string):
|
|||
|
||||
code_history = []
|
||||
|
||||
history = [
|
||||
{"role": "system", "content": "You are a helpful, smart, kind, and efficient AI assistant. You always fulfill the user's requests accurately and concisely."},
|
||||
]
|
||||
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."}
|
||||
|
||||
history = [ system_prompt ]
|
||||
|
||||
conversation = ""
|
||||
|
||||
def chat(message, stream=True):
|
||||
history.append({"role": "user", "content": message})
|
||||
|
|
@ -137,10 +175,19 @@ def chat(message, stream=True):
|
|||
return result
|
||||
|
||||
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)
|
||||
else:
|
||||
print('assistant: ', end='')
|
||||
result = chat(user_input, stream)
|
||||
|
||||
if result != '':
|
||||
conversation += 'user: ' + user_input + '\n'
|
||||
conversation += 'assistant: ' + result + '\n'
|
||||
return result
|
||||
|
||||
def highlightify_text(full_text):
|
||||
|
|
|
|||
Loading…
Reference in a new issue