From bbb6581c74aeaced21bdbed6e7209d91156f5c19 Mon Sep 17 00:00:00 2001 From: Xory Date: Wed, 6 Aug 2025 17:58:24 +0300 Subject: [PATCH] init: nvm we ollama in this bitch --- .gitignore | 3 +-- main.py | 59 ++++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 6c4ffc4..21d0b89 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -.env -.venv/ \ No newline at end of file +.venv/ diff --git a/main.py b/main.py index e2bf89c..939bf97 100644 --- a/main.py +++ b/main.py @@ -1,14 +1,51 @@ -from google import genai -from dotenv import load_dotenv -import os +import ollama +import subprocess -load_dotenv() -prompt = input("Prompt: ") +def shell_command(command: str) -> str: + """ + Run a shell command. -client = genai.Client(api_key=os.getenv("GEM_API_KEY")) -response = client.models.generate_content( - model="gemini-2.5-flash", - contents=prompt -) -print(response.text) + Args: + command: the command to run + + Returns: + stdout: the stdout + stderr: the stderr + """ + exec = subprocess.run(command, encoding="utf-8", shell=True, capture_output=True) + return f"stdout:\n{exec.stdout}\n\nstderr:\n{exec.stderr}" + + +tools = [shell_command] # ollama-python turns this into JSON + +messages = [{"role": "user", "content": input("prompt > ")}] + +while True: + stream = ollama.chat(model="gpt-oss", messages=messages, + tools=tools, stream=True) + + ran_tool = False + for chunk in stream: + msg = chunk.message + + # 1️⃣ the model is asking us to run something + if msg.tool_calls: + for call in msg.tool_calls: + if call["function"]["name"] == "shell_command": + cmd = call["function"]["arguments"]["command"] + output = shell_command(cmd) + messages.append({ + "role": "tool", + "name": "shell_command", + "content": output + }) + ran_tool = True + + # 2️⃣ normal user-visible text + elif msg.content: + print(msg.content, end="", flush=True) + + # loop again if we just satisfied a tool call + if not ran_tool: + break