import os from openai import OpenAI from gradio import Server from fastapi.responses import HTMLResponse client = OpenAI( base_url="https://openrouter.ai/api/v1", api_key=os.environ.get("OPENROUTER_API_KEY", ""), ) MODEL = "poolside/laguna-xs.2:free" app = Server() @app.api(name="chat") def chat(message: str, history: list, enable_thinking: bool = True, max_tokens: int = 1024, temperature: float = 0.7) -> dict: """Chat with Laguna-XS.2 via OpenRouter. Supports reasoning/thinking mode.""" messages = [] for msg in history: entry = {"role": msg["role"], "content": msg["content"]} # Preserve reasoning_details from prior assistant turns for multi-turn reasoning if msg["role"] == "assistant" and msg.get("reasoning_details"): entry["reasoning_details"] = msg["reasoning_details"] messages.append(entry) messages.append({"role": "user", "content": message}) extra_body = {} if enable_thinking: extra_body["reasoning"] = {"enabled": True} response = client.chat.completions.create( model=MODEL, messages=messages, max_tokens=max_tokens, temperature=temperature, extra_body=extra_body if extra_body else None, ) choice = response.choices[0].message result = { "content": choice.content or "", "reasoning_details": None, } # Extract reasoning details if present if hasattr(choice, "reasoning_details") and choice.reasoning_details: result["reasoning_details"] = choice.reasoning_details return result @app.get("/") async def homepage(): html_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "index.html") with open(html_path, "r", encoding="utf-8") as f: return HTMLResponse(content=f.read()) app.launch(show_error=True)