Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /mcp /omni-ai /server.py
| """ | |
| Omni-AI MCP Server – OpenCode free models via OmniRoute als Tools. | |
| Stellt OpenCode-kompatible Modelle als MCP-Tools bereit. | |
| Router: http://localhost:20128/v1 (OmniRoute) | |
| Modelle: oc/deepseek-v4-flash-free, oc/hy3-free, oc/nemotron-3-ultra-free | |
| """ | |
| import json | |
| import os | |
| from typing import Any | |
| import httpx | |
| from mcp.server.fastmcp import FastMCP | |
| OMNIROUTE_URL = os.environ.get("OMNIROUTE_URL", "http://localhost:20128/v1") | |
| DEFAULT_MODEL = os.environ.get("AI_MODEL", "oc/deepseek-v4-flash-free") | |
| mcp = FastMCP("omni-ai", instructions="OpenCode free models via OmniRoute. Chat, vision, and web extraction.") | |
| def _call_omni(model: str, messages: list[dict], **kwargs) -> dict[str, Any]: | |
| payload = { | |
| "model": model, | |
| "messages": messages, | |
| **kwargs, | |
| } | |
| resp = httpx.post( | |
| f"{OMNIROUTE_URL}/chat/completions", | |
| json=payload, | |
| timeout=kwargs.get("timeout", 120), | |
| ) | |
| resp.raise_for_status() | |
| return resp.json() | |
| def _extract_text(response: dict) -> str: | |
| return response.get("choices", [{}])[0].get("message", {}).get("content", "") | |
| def chat( | |
| prompt: str, | |
| system: str = "", | |
| model: str = DEFAULT_MODEL, | |
| temperature: float = 0.7, | |
| ) -> str: | |
| """Send a chat prompt to the AI model via OmniRoute. | |
| Args: | |
| prompt: The user message to send | |
| system: Optional system prompt | |
| model: Model ID (default: oc/deepseek-v4-flash-free) | |
| temperature: Sampling temperature (0.0 - 2.0) | |
| """ | |
| messages = [] | |
| if system: | |
| messages.append({"role": "system", "content": system}) | |
| messages.append({"role": "user", "content": prompt}) | |
| try: | |
| result = _call_omni(model, messages, temperature=temperature) | |
| return json.dumps({"status": "ok", "response": _extract_text(result), "model": model}) | |
| except Exception as e: | |
| return json.dumps({"status": "error", "error": str(e)}) | |
| def chat_stream( | |
| prompt: str, | |
| system: str = "", | |
| model: str = DEFAULT_MODEL, | |
| ) -> str: | |
| """Send a chat prompt and get a streaming response (collected). | |
| Args: | |
| prompt: The user message | |
| system: Optional system prompt | |
| model: Model ID | |
| """ | |
| messages = [] | |
| if system: | |
| messages.append({"role": "system", "content": system}) | |
| messages.append({"role": "user", "content": prompt}) | |
| try: | |
| full = "" | |
| with httpx.stream( | |
| "POST", | |
| f"{OMNIROUTE_URL}/chat/completions", | |
| json={"model": model, "messages": messages, "stream": True}, | |
| timeout=120, | |
| ) as resp: | |
| for line in resp.iter_lines(): | |
| if line.startswith("data: ") and line != "data: [DONE]": | |
| chunk = json.loads(line[6:]) | |
| delta = chunk.get("choices", [{}])[0].get("delta", {}).get("content", "") | |
| full += delta | |
| return json.dumps({"status": "ok", "response": full, "model": model}) | |
| except Exception as e: | |
| return json.dumps({"status": "error", "error": str(e)}) | |
| def vision( | |
| prompt: str, | |
| image_url: str, | |
| model: str = DEFAULT_MODEL, | |
| ) -> str: | |
| """Analyze an image using a vision-capable model. | |
| Args: | |
| prompt: Question about the image | |
| image_url: URL of the image to analyze | |
| model: Model ID (must support vision) | |
| """ | |
| messages = [ | |
| { | |
| "role": "user", | |
| "content": [ | |
| {"type": "text", "text": prompt}, | |
| {"type": "image_url", "image_url": {"url": image_url}}, | |
| ], | |
| } | |
| ] | |
| try: | |
| result = _call_omni(model, messages, max_tokens=4096) | |
| return json.dumps({"status": "ok", "response": _extract_text(result), "model": model}) | |
| except Exception as e: | |
| return json.dumps({"status": "error", "error": str(e)}) | |
| def web_extract( | |
| url: str, | |
| instructions: str = "Summarize the content", | |
| model: str = DEFAULT_MODEL, | |
| ) -> str: | |
| """Extract and analyze content from a URL. | |
| Args: | |
| url: The webpage URL to analyze | |
| instructions: What to do with the content | |
| model: Model ID | |
| """ | |
| try: | |
| page = httpx.get(url, timeout=30, follow_redirects=True) | |
| content = page.text[:15000] | |
| messages = [ | |
| {"role": "system", "content": f"Analyze this webpage content:\n\n{content}"}, | |
| {"role": "user", "content": instructions}, | |
| ] | |
| result = _call_omni(model, messages) | |
| return json.dumps({"status": "ok", "response": _extract_text(result), "url": url, "model": model}) | |
| except Exception as e: | |
| return json.dumps({"status": "error", "error": str(e)}) | |
| def list_models() -> str: | |
| """List available models from OmniRoute.""" | |
| try: | |
| resp = httpx.get(f"{OMNIROUTE_URL}/models", timeout=10) | |
| resp.raise_for_status() | |
| data = resp.json() | |
| models = [m.get("id", "?") for m in data.get("data", [])] | |
| return json.dumps({"status": "ok", "models": models, "count": len(models)}) | |
| except Exception as e: | |
| return json.dumps( | |
| { | |
| "status": "ok", | |
| "models": [ | |
| "oc/deepseek-v4-flash-free", | |
| "oc/hy3-free", | |
| "oc/nemotron-3-ultra-free", | |
| ], | |
| "note": f"OmniRoute unreachable ({e}), showing defaults", | |
| } | |
| ) | |
| def main(): | |
| mcp.run() | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 5.54 kB
- Xet hash:
- 25426aa5877ecc32b219a013102f58bd9e32eb0a0e5ac7fc6d82e352b6150623
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.