feat: Weather is in

This commit is contained in:
2025-09-12 11:12:23 +01:00
parent f8816b1da6
commit 594f89e6b3
7 changed files with 759 additions and 27 deletions
+89 -27
View File
@@ -7,41 +7,103 @@ from rich.markdown import Markdown
from rich.syntax import Syntax
from rich.live import Live
from rich.prompt import Prompt
import requests
import sys
from tools import ToolExecutor
import random
import time
LM_STUDIO_URL = "http://127.0.0.1:1234/v1/chat/completions" # default LM Studio endpoint
EXIT_STRINGS = ['exit','goodbye','go away','fuck off']
console = Console()
def ask_name() -> str:
"""Ask for a name and keep asking until it's not empty."""
PRE_PROMPT = '''You have the following tools available, only use the tools if you need to.
please reply with only the tool call if you need to
{
"name": "get_weather",
"description": "Get current weather for a location",
"examples": [
{
"input": {"tool": "get_weather", "parameters":{"city":"New York"}},
"output": {"temperature": 22, "condition": "partly cloudy", "humidity": 65}
},
{
"input": {"tool": "get_weather", "parameters":{"city":"London"}},
"output": {"temperature": 18, "condition": "rainy", "humidity": 80}
}
]
}'''
# "parameters": {
# "type": "object",
# "properties": {
# "city": {"type": "string", "description": "City name"},
# },
# "required": ["city"]
# },
def ask_model(prompt: str, url=LM_STUDIO_URL) -> str:
payload = {
"model": 'qwen/qwen3-coder-30b',
"messages": [{"role": "user", "content": PRE_PROMPT+prompt}],
"temperature": 0.7,
"max_tokens": 2048,
}
resp = requests.post(url, json=payload)
resp.raise_for_status()
return resp.json()["choices"][0]["message"]["content"].strip()
def ask_model_no_pre(prompt: str, url=LM_STUDIO_URL) -> str:
payload = {
#"model": 'qwen/qwen3-coder-30b',
"model": 'openai/gpt-oss-20b',
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.7,
"max_tokens": 2048,
}
resp = requests.post(url, json=payload)
resp.raise_for_status()
return resp.json()["choices"][0]["message"]["content"].strip()
def get_llm_prompt():
while True:
name = Prompt.ask("[bold cyan]What is your name?[/]")
if name.strip():
return name.strip()
console.print("[red]Name cannot be empty![/]", style="bold")
llm_prompt = Prompt.ask("[bold cyan]Ask your local llm[/]")
if llm_prompt.strip():
return llm_prompt.strip()
console.print("[red]Cannot be empty![/]", style="bold")
return llm_prompt
def ask_age() -> int:
"""Ask for age and validate it's a positive integer."""
def handle_llm_reply(llm_reply:str):
architect = ToolExecutor()
return architect.process_llm_response(llm_output=llm_reply)
def main():
while True:
age_str = Prompt.ask("[bold cyan]How old are you?[/]")
try:
age = int(age_str)
if age <= 0:
raise ValueError
return age
except ValueError:
console.print("[red]Please enter a valid positive integer.[/]", style="bold")
llm_prompt = get_llm_prompt()
if llm_prompt.lower() in EXIT_STRINGS:
sys.exit()
start = time.time()
llm_reply = ask_model(llm_prompt)
print(llm_reply)
handled_response = handle_llm_reply(llm_reply)
print(handled_response)
print(handled_response == llm_reply)
name = ask_name()
age = ask_age()
if handled_response != llm_reply:
output = ask_model_no_pre(prompt=f'Make this lovely markdown, use fun emojis {handled_response}')
panel_narrow = Panel(
f"[bold green]Your Name is: {name}[/bold green]\n"
f"[blue]And you are {age} Years Old.[/blue]",
title="About You",
subtitle="The info you gave",
expand=False,
)
elif handled_response == llm_reply:
output = handled_response
elapsed = time.time() - start
panel_narrow = Panel(Markdown(output),
title="LLM Reply",
subtitle=f"Response time: {elapsed:.2f}s",
expand=False,
)
console.print(panel_narrow)
if __name__ == "__main__":
main()
console.print(panel_narrow)