import httpx from config import ( MC2_BASE_URL, CHAT_MODEL, VISION_MODEL, MAX_RESPONSE_TOKENS, REQUEST_TIMEOUT, SYSTEM_PROMPT, SCREENSHOT_ON_QUERY, ) def ask(text: str, screenshot_b64: str | None = None) -> str: """Schickt Text (+ optionalen Screenshot) an MC2 und gibt die Antwort zurück.""" use_vision = screenshot_b64 is not None and SCREENSHOT_ON_QUERY model = VISION_MODEL if use_vision else CHAT_MODEL if use_vision: user_content = [ {"type": "text", "text": text}, {"type": "image_url", "image_url": { "url": f"data:image/jpeg;base64,{screenshot_b64}" }}, ] else: user_content = text payload = { "model": model, "messages": [ {"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": user_content}, ], "max_tokens": MAX_RESPONSE_TOKENS, "stream": False, } try: with httpx.Client(timeout=REQUEST_TIMEOUT) as client: response = client.post( f"{MC2_BASE_URL}/chat/completions", json=payload, ) response.raise_for_status() return response.json()["choices"][0]["message"]["content"].strip() except httpx.TimeoutException: return "Entschuldigung, die Antwort hat zu lange gedauert." except Exception as e: return f"Verbindungsfehler: {e}"