From 16d4998d704c5578a4fe5d939a1c812472893bf2 Mon Sep 17 00:00:00 2001 From: 0x01FE Date: Thu, 2 Oct 2025 13:12:46 -0500 Subject: [PATCH] trying to get tooling working --- main.py | 17 +++++++++++++++-- textgen/llm.py | 34 +++++++++++++++++++++++++--------- textgen/weather.py | 10 +++++----- tts.py | 2 +- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/main.py b/main.py index 805d9f0..628e444 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,24 @@ import tts -from textgen import tool_funcs +from textgen import llm + +MODEL_PATH = "./textgen/models/mistral-7b-instruct-v0.3.Q4_K_M.gguf" + t = tts.TTS() save_path = 'audio/weather.wav' -t.create_audio(tool_funcs.get_high_low(), save_path) +l = llm.TextGen( + MODEL_PATH, + 2048, + 0 +) + +r = l.chat_completion("What is the weather like today?") + +print(r) + +t.create_audio(r, save_path) diff --git a/textgen/llm.py b/textgen/llm.py index 43dbd48..941835e 100644 --- a/textgen/llm.py +++ b/textgen/llm.py @@ -1,10 +1,11 @@ # Import the Llama class from the llama_cpp library import llama_cpp import json +import random from . import tool_funcs -tools: list[dict] = json.loads(open('tools.json', 'r').read())['tools'] +tools: list[dict] = json.loads(open('./textgen/tools.json', 'r').read())['tools'] class TextGen: @@ -19,12 +20,12 @@ class TextGen: # Provide the path to your downloaded .gguf file # n_ctx is the maximum context size (number of tokens) the model can handle. # n_gpu_layers specifies how many layers to offload to the GPU. -1 means offload all possible layers. - llm = llama_cpp.Llama( - model_path="./models/mistral-7b-instruct-v0.2.Q4_K_M.gguf", # Path to your GGUF model - n_ctx=n_ctx, # Context window size - n_gpu_layers=n_gpu_layers, # Offload all layers to GPU. Set to 0 if no GPU. - verbose=False, # Suppress verbose output - chat_format='chatml' + self.llm = llama_cpp.Llama( + model_path=model_path, # Path to your GGUF model + n_ctx=n_ctx, # Context window size + n_gpu_layers=n_gpu_layers, # Offload all layers to GPU. Set to 0 if no GPU. + verbose=False, # Suppress verbose output + chat_format='chatml' ) def generate(self, prompt: str) -> str: @@ -52,14 +53,18 @@ class TextGen: "content": user_message }) + print(tools) + response = self.llm.create_chat_completion( messages=self.messages, tools=tools, - tool_choice='auto' + tool_choice='auto', + seed=random.randint(0, 20000000000) ) tool_call = response['choices'][0]['message'].get('tool_calls') if not tool_call: + # print(response['choices'][0]['message']['content']) return response['choices'][0]['message']['content'] call_info = tool_call[0]['function'] @@ -68,5 +73,16 @@ class TextGen: print(f'Assistant decided to call {function_name}') tool_output = tool_funcs.get_high_low() - + + self.messages.append(response['choices'][0]['message']) + self.messages.append( + { + "role" : "tool", + "content": tool_output + } + ) + + final_response = self.llm.create_chat_completion(messages=self.messages) + return final_response['choices'][0]['message']['content'] + diff --git a/textgen/weather.py b/textgen/weather.py index 8b2022d..57af929 100644 --- a/textgen/weather.py +++ b/textgen/weather.py @@ -36,10 +36,10 @@ class OpenMeteo: # Process first location. Add a for-loop for multiple locations or weather models response = responses[0] - print(f"Coordinates: {response.Latitude()}°N {response.Longitude()}°E") - print(f"Elevation: {response.Elevation()} m asl") - print(f"Timezone: {response.Timezone()}{response.TimezoneAbbreviation()}") - print(f"Timezone difference to GMT+0: {response.UtcOffsetSeconds()}s") + # print(f"Coordinates: {response.Latitude()}°N {response.Longitude()}°E") + # print(f"Elevation: {response.Elevation()} m asl") + # print(f"Timezone: {response.Timezone()}{response.TimezoneAbbreviation()}") + # print(f"Timezone difference to GMT+0: {response.UtcOffsetSeconds()}s") # Process hourly data. The order of variables needs to be the same as requested. hourly = response.Hourly() @@ -57,7 +57,7 @@ class OpenMeteo: hourly_data['rain'] = hourly_rain hourly_dataframe = pd.DataFrame(data = hourly_data) - print("\nHourly data\n", hourly_dataframe) + # print("\nHourly data\n", hourly_dataframe) return hourly_dataframe diff --git a/tts.py b/tts.py index 12c5009..c5fe6cc 100644 --- a/tts.py +++ b/tts.py @@ -14,7 +14,7 @@ class TTS: def create_audio(self, text: str, save_path: str) -> None: generator = self.pipeline(text, voice='af_heart') for i, (gs, ps, audio) in enumerate(generator): - print(i, gs, ps) + # print(i, gs, ps) display(Audio(data=audio, rate=24000, autoplay=i==0)) sf.write(save_path, audio, 24000)