valkyrie/main.py

57 lines
1.6 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import ollama
import subprocess
def shell_command(command: str) -> str:
"""
Run a shell command.
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
saw_content = 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
if msg.thinking:
print(f"\033[90m{msg.thinking}", end="", flush=True)
if msg.content:
if not saw_content:
print("")
saw_content = True
print(f"\033[97m{msg.content}", end="", flush=True)
# loop again if we just satisfied a tool call
if not ran_tool:
break