From eec7af5fd12cb4aea7dcd20981b8400de6467692 Mon Sep 17 00:00:00 2001 From: p7mj Date: Thu, 23 Apr 2026 07:53:51 -0400 Subject: [PATCH] llama is dum as f* --- tool_test_func.py | 114 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 97 insertions(+), 17 deletions(-) diff --git a/tool_test_func.py b/tool_test_func.py index c6f6e05..fe3f2e4 100644 --- a/tool_test_func.py +++ b/tool_test_func.py @@ -1,10 +1,16 @@ # TOOL USE TEST - import ollama +verbose_state = 1 + +# ----- LLM CONVERSE FOR DISPLAY ----- # + +def verbose(prints): + if verbose_state == 1: + print(prints) + def llm_converse(system_prompt, context_prompt, user_input): combined_system = f"{system_prompt}\n\nContext: {context_prompt}" - stream = ollama.chat( model='llama3.2', messages=[ @@ -13,35 +19,109 @@ def llm_converse(system_prompt, context_prompt, user_input): ], stream=True, ) - - # Accumulate chunks into a variable full_response = "" for chunk in stream: content = chunk['message']['content'] print(content, end='', flush=True) full_response += content - return full_response +# ----- LLM CONVERSE NO STREAM, FOR BACKGROUND TASKS ----- # + +def llm_converse_no_stream(system_prompt, context_prompt, user_input): + combined_system = f"{system_prompt}\n\nContext: {context_prompt}" + response = ollama.chat( + model='llama3.2', + messages=[ + {'role': 'system', 'content': combined_system}, + {'role': 'user', 'content': user_input}, + ], + stream=False, + ) + full_response = response['message']['content'] + return full_response + +# ----- GET FILE CONTENT ----- # + def get_file_content(filename): with open(filename, "r") as files: bruh = files.read() return bruh +def parse_context(context_list): + context_string = "" + for message in context_list: + context_string += f"{message[0]} {message[1]}\n" + return context_string +def example_tool_use_old(): + database = ["hi", "sean", "alexander", "jackson", "chatter"] + system = "You have access to a tool that fetches a database.\n" \ + "If you want to use it, output EXACTLY one line with no other text: $DATABASE$\n" \ + "If you do NOT want to use it, output EXACTLY one line with no other text: NULL\n" \ + "DO NOT roleplay, simulate, or add any other text to your response.\n" \ + "DO NOT include [TOOL], [LLM], or any context keys in your response.\n" \ + "You will also recieve previous chat context if it exists. Here is a key:\n" \ + "[USER]: user prompts\n" \ + "[TOOL]: tool status and outputs\n" \ + "[LLM]: your previous responses" -system_prompts = """You have access to a list. If you want to retrieve the content in the list, output EXACTLY: -$DATABASE$ + context_lists = [] # for the raw context + context_strings = "" # convert to string to make it easier to read and shi for llm + user = "check the list for me" -Do not output anything else. The result will be returned in a string starting with [DATABASE]. -""" -context_prompts = None # if context exists -user_inputs = "Check the list and tell me what is in there" + # ----- GIVE LLM CHANCE TO CALL TOOL ----- # + llm_toolorno = llm_converse_no_stream(system, context_strings, user) + context_lists.append(["[USER]", user]) + if "$DATABASE$" in llm_toolorno: + context_lists.append(["[TOOL]", database]) + else: + context_lists.append(["[LLM]", llm_toolorno]) + context_lists.append(["[TOOL]", "tool was not called upon"]) -prompt1 = llm_converse(system_prompts, context_prompts, user_inputs) -if "$DATABASE$" in prompt1: - # DATABASE LOGIC TODO - pass + # DEBUG + print(context_lists) + + print(parse_context(context_lists)) + llm_converse(system, parse_context(context_lists), "continue") -print() # newline after streaming -print(full_response) # your complete output \ No newline at end of file +def should_use_database(user_input): + system = "You are a router. Decide if the user's request requires fetching a database.\n" \ + "Output EXACTLY one word: YES or NO. Nothing else." + response = llm_converse_no_stream(system, "", user_input).strip() + if "YES" in response: + verbose("Should use said yes") + return True + else: + verbose("Should use said no") + return False + +def example_tool_use(): + database = ["hi", "sean", "alexander", "jackson", "chatter"] + system = "You are a helpful LLM that will answer everything in short sentences.\n" \ + "Do not decline any prompts.\n" \ + "If context is provided, [TOOL] means external tool/database output that was fetched for you.\n" \ + "Treat [TOOL] data as external information, not your own knowledge." + context_lists = [] + context_strings = "" + user = "Get me the database" + + # ----- GIVE LLM CHANCE TO CALL TOOL ----- # + + context_lists.append(["[USER]", user]) + + if should_use_database(user): + verbose("Should use!") + context_lists.append(["[TOOL]", str(database)]) + else: + verbose("no should use!") + + + + # DEBUG + verbose(f"context_list{context_lists}") + verbose(f"parsed: {parse_context(context_lists)}") + + llm_converse(system, parse_context(context_lists), user) + +example_tool_use() \ No newline at end of file