From 4a8026ec723a31acec1cecad46e136450646ea08 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Mon, 20 May 2024 13:24:41 +0200 Subject: [PATCH] added better documentation --- scripts/BaseOllama.py | 6 +++--- scripts/GUIChat.py | 6 +++--- scripts/Message.py | 47 ++++++++++++++++++++++++----------------- scripts/Rag.py | 15 ++++++------- scripts/TerminalChat.py | 4 ++-- 5 files changed, 43 insertions(+), 35 deletions(-) diff --git a/scripts/BaseOllama.py b/scripts/BaseOllama.py index 10d3610..42e3ebd 100644 --- a/scripts/BaseOllama.py +++ b/scripts/BaseOllama.py @@ -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: diff --git a/scripts/GUIChat.py b/scripts/GUIChat.py index 566022c..05ab1ae 100644 --- a/scripts/GUIChat.py +++ b/scripts/GUIChat.py @@ -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): diff --git a/scripts/Message.py b/scripts/Message.py index 97051d8..8c21d0a 100644 --- a/scripts/Message.py +++ b/scripts/Message.py @@ -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 \ No newline at end of file diff --git a/scripts/Rag.py b/scripts/Rag.py index 4b34789..6b567ab 100644 --- a/scripts/Rag.py +++ b/scripts/Rag.py @@ -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}" \ No newline at end of file diff --git a/scripts/TerminalChat.py b/scripts/TerminalChat.py index cd60e57..4fd12e5 100644 --- a/scripts/TerminalChat.py +++ b/scripts/TerminalChat.py @@ -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