Compare commits
2 Commits
953342c280
...
4a8026ec72
Author | SHA1 | Date |
---|---|---|
Falko Victor Habel | 4a8026ec72 | |
Falko Victor Habel | 52310a9021 |
17
project.py
17
project.py
|
@ -19,18 +19,21 @@ def main():
|
|||
|
||||
args = parser.parse_args()
|
||||
config = read_config()
|
||||
|
||||
# If no config file exists and user does not go into "config mode"
|
||||
if config is None and args.config is False:
|
||||
sys.exit("No Config available. please run: 'python project.py --config' to set it up")
|
||||
|
||||
# add config
|
||||
elif args.config is True:
|
||||
config = configure()
|
||||
write_config(config)
|
||||
# change ui mode
|
||||
elif args.m:
|
||||
config = handle_change_mode(args)
|
||||
write_config(config)
|
||||
# start terminal ui
|
||||
elif config["mode"] == "terminal":
|
||||
handle_terminal(args)
|
||||
# start customtkinter ui
|
||||
elif config["mode"] == "gui":
|
||||
# start gui
|
||||
try:
|
||||
|
@ -70,9 +73,13 @@ def read_config():
|
|||
with open(CONFIG_FILE, "r") as f:
|
||||
return json.load(f)
|
||||
|
||||
|
||||
def write_config(config: dict):
|
||||
with open(CONFIG_FILE, "w") as config_file:
|
||||
json.dump(config, config_file, indent=4)
|
||||
try:
|
||||
with open(CONFIG_FILE, "w") as config_file:
|
||||
json.dump(config, config_file, indent=4)
|
||||
except FileNotFoundError:
|
||||
sys.exit(ERROR_CORRPUTED_CONFIG)
|
||||
|
||||
def handle_change_mode(args):
|
||||
config = read_config()
|
||||
|
@ -88,7 +95,7 @@ def handle_change_mode(args):
|
|||
|
||||
def handle_terminal(args):
|
||||
config = read_config()
|
||||
|
||||
# if a prompt exists
|
||||
if args.p:
|
||||
try:
|
||||
bot = TerminalBot(args.p, args.f, **config["ollamaConfig"])
|
||||
|
|
|
@ -4,6 +4,7 @@ class OllamaChatBot:
|
|||
def __init__(self, base_url, model, headers):
|
||||
self.base_url = base_url
|
||||
self.model = model
|
||||
# check if header exists
|
||||
if self.is_empty(headers):
|
||||
self.headers = ""
|
||||
else:
|
||||
|
@ -22,12 +23,11 @@ class OllamaChatBot:
|
|||
headers = self.headers
|
||||
)
|
||||
|
||||
def is_empty(self, dictionary):
|
||||
def is_empty(self, dictionary: dict) -> bool:
|
||||
return len(dictionary) == 1 and list(dictionary.keys())[0] == '' and list(dictionary.values())[0] == ''
|
||||
|
||||
|
||||
|
||||
def get_request(self, prompt):
|
||||
def get_request(self, prompt: str) -> str:
|
||||
messanges = []
|
||||
self.messanges.append(prompt)
|
||||
if len(self.messanges) > 5:
|
||||
|
|
|
@ -24,7 +24,7 @@ class ChatGUI(CTk.CTk):
|
|||
self.create_widgets()
|
||||
self.start_message_processing_thread()
|
||||
|
||||
def get_response_from_ollama(self, prompt, context):
|
||||
def get_response_from_ollama(self, prompt: str, context: str):
|
||||
try:
|
||||
if context != "":
|
||||
if self.context != context:
|
||||
|
@ -52,7 +52,7 @@ class ChatGUI(CTk.CTk):
|
|||
self.entry_bar.delete(0, CTk.END) # Clear input field after sending
|
||||
Thread(target=lambda q, arg1, arg2: q.put(self.get_response_from_ollama(arg1, arg2)), args=(self.message_queue, message, context)).start()
|
||||
|
||||
def show_reply(self, message):
|
||||
def show_reply(self, message: str):
|
||||
bot_reply = Reply(master=self.messages_frame,reply_type = "Fabelous-AI-Bot", message=message)
|
||||
self.history.append(bot_reply)
|
||||
bot_reply.pack(anchor="w", padx=5, pady=2)
|
||||
|
@ -115,7 +115,7 @@ class ChatGUI(CTk.CTk):
|
|||
self.history = []
|
||||
self.bot.messanges = []
|
||||
self.rag.init_ollama()
|
||||
|
||||
self.file_entry.delete(0, "end")
|
||||
|
||||
|
||||
def start_message_processing_thread(self):
|
||||
|
|
|
@ -7,7 +7,6 @@ class Reply(tk.Frame):
|
|||
def __init__(self, master, message,reply_type, font_size=12, background="#252525", text_color="white"):
|
||||
super().__init__(master)
|
||||
|
||||
# Define style configurations
|
||||
self.font = ("Helvetica", font_size)
|
||||
self.background = background
|
||||
self.text_color = text_color
|
||||
|
@ -15,8 +14,8 @@ class Reply(tk.Frame):
|
|||
|
||||
self.message = self.extract_code(message)
|
||||
self.reply_scope = []
|
||||
|
||||
# Adjust the overall background of the ttk.Notebook
|
||||
|
||||
# change background of message
|
||||
self.configure(background=self.background)
|
||||
|
||||
|
||||
|
@ -28,6 +27,7 @@ class Reply(tk.Frame):
|
|||
row_index = 1 # Track the current grid row for widgets
|
||||
|
||||
for part in self.message:
|
||||
# part is a code block
|
||||
if part.startswith(CODE_PREFIX):
|
||||
part = part[len(CODE_PREFIX):]
|
||||
code = tk.Text(master=self, width=50, font=self.font,
|
||||
|
@ -48,22 +48,31 @@ class Reply(tk.Frame):
|
|||
row_index += 1
|
||||
|
||||
|
||||
def extract_code(self, input_string, replacement=CODE_PREFIX):
|
||||
split_parts = re.split(r'(```)', input_string)
|
||||
output_array = []
|
||||
previously_delimiter = False
|
||||
|
||||
for part in split_parts:
|
||||
if part == "```":
|
||||
previously_delimiter = True
|
||||
continue
|
||||
def extract_code(self, input_string: str, replacement=CODE_PREFIX) -> list:
|
||||
# Split the input string on the ``` delimiter
|
||||
split_parts = re.split(r'(```)', input_string) # Include the delimiter in the results
|
||||
|
||||
if previously_delimiter:
|
||||
part = re.sub(r'^\b\w+\b', replacement, part, count=1)
|
||||
previously_delimiter = False
|
||||
# Initialize an empty list to store the output array
|
||||
output_array = []
|
||||
|
||||
if part.strip():
|
||||
output_array.append(part)
|
||||
|
||||
return output_array
|
||||
# Track whether the previous part was a ``` delimiter
|
||||
previously_delimiter = False
|
||||
|
||||
|
||||
for part in split_parts:
|
||||
# Check if the current part is a ``` delimiter
|
||||
if part == "```":
|
||||
previously_delimiter = True # Set flag if a delimiter is found
|
||||
continue # Skip adding the delimiter to the output
|
||||
|
||||
# If the previous part was a delimiter, replace the first word with the specified string
|
||||
if previously_delimiter:
|
||||
part = re.sub(r'^\b\w+\b', replacement, part, count=1) # Replace the programming Language
|
||||
previously_delimiter = False # Reset the flag
|
||||
|
||||
# Only add non-empty parts to the output array
|
||||
if part.strip():
|
||||
output_array.append(part)
|
||||
|
||||
return output_array
|
||||
|
|
@ -20,7 +20,7 @@ class Rag:
|
|||
|
||||
self.base_url_llm = base_url_llm
|
||||
self.base_url_embed = base_url_embed
|
||||
|
||||
# check if header exists
|
||||
if self.is_empty(base_header):
|
||||
self.base_header = ""
|
||||
else:
|
||||
|
@ -38,7 +38,7 @@ class Rag:
|
|||
headers = self.base_header
|
||||
)
|
||||
|
||||
def get_file(self, file_path):
|
||||
def get_file(self, file_path: str):
|
||||
# Check if the file path starts with 'https://'
|
||||
if file_path.startswith('https://'):
|
||||
try:
|
||||
|
@ -73,12 +73,10 @@ class Rag:
|
|||
return True
|
||||
|
||||
|
||||
def is_empty(self, dictionary):
|
||||
def is_empty(self, dictionary: dict) -> bool:
|
||||
return len(dictionary) == 1 and list(dictionary.keys())[0] == '' and list(dictionary.values())[0] == ''
|
||||
|
||||
|
||||
|
||||
def receive_data(self, file_path):
|
||||
def receive_data(self, file_path: str):
|
||||
try:
|
||||
if self.get_file(file_path):
|
||||
text_splitter = RecursiveCharacterTextSplitter(chunk_size=250, chunk_overlap=0)
|
||||
|
@ -89,10 +87,11 @@ class Rag:
|
|||
return (True, f"'{file_path}' unsupported, read documentation for more information")
|
||||
except (ValueError, AttributeError) as e:
|
||||
return (True, f"An unexpected Error occuried: {e}")
|
||||
def get_request(self, prompt):
|
||||
|
||||
def get_request(self, prompt: str) -> str:
|
||||
qachain=RetrievalQA.from_chain_type(self.chat_ollama, retriever=self.retriever)
|
||||
try:
|
||||
return qachain.invoke({"query": prompt})["result"]
|
||||
except ValueError as e:
|
||||
return (True, f"An unexpected Error occuried: {e}")
|
||||
return f"An unexpected Error occuried: {e}"
|
||||
|
|
@ -70,7 +70,7 @@ class TerminalBot:
|
|||
else:
|
||||
print(colored(message + "\n", "white"))
|
||||
|
||||
def extract_code(self, input_string, replacement=CODE_PREFIX):
|
||||
def extract_code(self, input_string, replacement=CODE_PREFIX) -> list:
|
||||
# Split the input string on the ``` delimiter
|
||||
split_parts = re.split(r'(```)', input_string) # Include the delimiter in the results
|
||||
|
||||
|
@ -89,7 +89,7 @@ class TerminalBot:
|
|||
|
||||
# If the previous part was a delimiter, replace the first word with the specified string
|
||||
if previously_delimiter:
|
||||
part = re.sub(r'^\b\w+\b', replacement, part, count=1) # Replace the first word
|
||||
part = re.sub(r'^\b\w+\b', replacement, part, count=1) # Replace the programming Language
|
||||
previously_delimiter = False # Reset the flag
|
||||
|
||||
# Only add non-empty parts to the output array
|
||||
|
|
Loading…
Reference in New Issue