Spaces:
Running
Running
File size: 17,692 Bytes
6011cd2 800c574 6011cd2 f1eba13 6011cd2 40e0574 6011cd2 800c574 6011cd2 800c574 6011cd2 800c574 6011cd2 800c574 6011cd2 800c574 6011cd2 800c574 6011cd2 800c574 6011cd2 800c574 6011cd2 800c574 6011cd2 800c574 6011cd2 800c574 6011cd2 800c574 6011cd2 800c574 6011cd2 800c574 6011cd2 800c574 6011cd2 800c574 6011cd2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | import os
import uuid
import time
import asyncio
import threading
import json
import re
from typing import Optional
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
API_SECRET_KEY = os.getenv("API_SECRET_KEY", "2026-2026")
ZAI_MODELS = ["GLM-5.1", "GLM-5-Turbo", "GLM-5V-Turbo","GLM-5", "GLM-4.7", "GLM-4.6V", "GLM-4.5-Air" ]
class AsyncBrowserThread(threading.Thread):
def __init__(self):
super().__init__(daemon=True)
self.loop = asyncio.new_event_loop()
self.ready_event = threading.Event()
self.browser = None
self.playwright = None
def run(self):
asyncio.set_event_loop(self.loop)
self.loop.run_until_complete(self._start_browser())
self.ready_event.set()
print("[ZAI-SERVER] Browser is ready!")
self.loop.run_forever()
async def _start_browser(self):
from playwright.async_api import async_playwright
print("[ZAI-SERVER] Starting Chrome...")
self.playwright = await async_playwright().start()
self.browser = await self.playwright.chromium.launch(
headless=True,
channel="chrome",
args=[
'--disable-blink-features=AutomationControlled',
'--no-sandbox',
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-setuid-sandbox',
'--single-process',
'--no-zygote',
]
)
print("[ZAI-SERVER] Chrome launched!")
async def _talk_to_zai(self, prompt: str):
context = await self.browser.new_context(
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
viewport={'width': 1920, 'height': 1080}
)
await context.add_init_script(
"Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"
)
page = await context.new_page()
try:
page.set_default_timeout(120000)
await page.goto("https://chat.z.ai/", wait_until="domcontentloaded")
await asyncio.sleep(3)
# ββ Input: confirmed from DOM βββββββββββββββββββββββββββββ
await page.wait_for_selector("textarea#chat-input", timeout=60000)
await page.fill("textarea#chat-input", prompt)
await asyncio.sleep(0.5)
await page.press("textarea#chat-input", "Enter")
print(f"[ZAI-SERVER] Sent ({len(prompt)} chars)")
# ββ Wait for response container βββββββββββββββββββββββββββ
await asyncio.sleep(2)
await page.wait_for_selector("#response-content-container", timeout=120000)
# ββ Wait until response stabilizes ββββββββββββββββββββββββ
last_text = ""
unchanged_count = 0
while unchanged_count < 5:
current_text = await page.evaluate("""
() => {
const prose = document.querySelector(
'#response-content-container .markdown-prose'
);
if (!prose) return '';
const clone = prose.cloneNode(true);
// β
Ψ΄ΩΩ ΩΩ direct div children (thinking blocks)
// Ψ§ΩΨ±Ψ― Ψ§ΩΨΩΩΩΩ ΩΩΩ ΩΩ <p> Ω
Ψ΄ <div>
clone.querySelectorAll(':scope > div').forEach(el => el.remove());
return clone.innerText.trim();
}
""")
if current_text == last_text and current_text.strip():
unchanged_count += 1
else:
last_text = current_text
unchanged_count = 0
await asyncio.sleep(1.0)
print(f"[ZAI-SERVER] Response: {len(last_text)} chars")
return last_text.strip()
except Exception as e:
print(f"[ZAI-SERVER] Error: {e}")
raise e
finally:
await page.close()
await context.close()
def process_request(self, prompt: str):
if not self.ready_event.wait(timeout=60):
raise Exception("Error From Browser")
future = asyncio.run_coroutine_threadsafe(self._talk_to_zai(prompt), self.loop)
return future.result(timeout=120)
browser_engine = AsyncBrowserThread()
browser_engine.start()
# ====================================================================
# Smart Prompt Builder
# ====================================================================
def format_prompt(messages, tools=None):
parts = []
system_parts = []
has_tool_results = False
user_question = ""
for msg in messages:
role = msg.get("role", "")
msg_type = msg.get("type", "")
content = msg.get("content", "")
if isinstance(content, list):
text_parts = []
for item in content:
if isinstance(item, dict):
text_parts.append(item.get("text", item.get("content", str(item))))
else:
text_parts.append(str(item))
content = "\n".join(text_parts)
if role == "system":
system_parts.append(content)
elif role == "tool":
has_tool_results = True
tool_name = msg.get("name", "tool")
parts.append(f"[TOOL RESULT from '{tool_name}']:\n{content}")
elif msg_type == "function_call_output":
has_tool_results = True
call_id = msg.get("call_id", "")
output_content = msg.get("output", content)
parts.append(f"[TOOL RESULT (call_id: {call_id})]:\n{output_content}")
elif msg_type == "function_call":
func_name = msg.get("name", "?")
func_args = msg.get("arguments", "{}")
parts.append(f"[PREVIOUS TOOL CALL: Called '{func_name}' with arguments: {func_args}]")
elif role == "assistant":
assistant_content = content if content else ""
tool_calls_in_msg = msg.get("tool_calls", [])
if tool_calls_in_msg:
tc_descriptions = []
for tc in tool_calls_in_msg:
func = tc.get("function", {})
tc_descriptions.append(
f"Called '{func.get('name', '?')}' with: {func.get('arguments', '{}')}"
)
assistant_content += "\n[Previous tool calls: " + "; ".join(tc_descriptions) + "]"
if assistant_content.strip():
parts.append(f"[Assistant]: {assistant_content}")
elif role == "user" or (msg_type == "message" and role != "system"):
user_question = content
parts.append(content)
has_tool_results = False
elif content:
parts.append(content)
final = ""
if system_parts:
if tools and not has_tool_results:
final += "=== YOUR ROLE ===\n"
final += "\n\n".join(system_parts)
final += "\n=== END OF ROLE ===\n\n"
else:
final += "=== SYSTEM INSTRUCTIONS (FOLLOW STRICTLY) ===\n"
final += "\n\n".join(system_parts)
final += "\n=== END OF INSTRUCTIONS ===\n\n"
if tools and not has_tool_results:
final += format_tools_instruction(tools, user_question)
if has_tool_results:
final += "=== CONTEXT FROM TOOLS ===\n"
final += "The following information was retrieved by the tools you requested.\n"
final += "Use ONLY this information to answer the user's question.\n\n"
if parts:
final += "\n".join(parts)
if has_tool_results:
final += "\n\n=== INSTRUCTION ===\n"
final += "Now answer the user's question based ONLY on the tool results above.\n"
return final
def format_tools_instruction(tools, user_question=""):
instruction = "\n=== MANDATORY TOOL USAGE ===\n"
instruction += "You MUST use one of the tools below to answer this question.\n"
instruction += "Do NOT answer directly. Do NOT say you don't have information.\n"
instruction += "You MUST respond with ONLY a JSON object to call the tool.\n\n"
instruction += "RESPONSE FORMAT - respond with ONLY this JSON, nothing else:\n"
instruction += '{"tool_calls": [{"name": "TOOL_NAME", "arguments": {"param": "value"}}]}\n\n'
instruction += "RULES:\n"
instruction += "- Your ENTIRE response must be valid JSON only\n"
instruction += "- No markdown, no code blocks, no explanation\n"
instruction += "- No text before or after the JSON\n\n"
instruction += "Available tools:\n\n"
for tool in tools:
func = tool.get("function", tool)
name = func.get("name", "unknown")
desc = func.get("description", "No description")
params = func.get("parameters", {})
instruction += f"Tool: {name}\nDescription: {desc}\n"
if params.get("properties"):
instruction += "Parameters:\n"
required_params = params.get("required", [])
for param_name, param_info in params["properties"].items():
param_type = param_info.get("type", "string")
param_desc = param_info.get("description", "")
is_required = "required" if param_name in required_params else "optional"
instruction += f" - {param_name} ({param_type}, {is_required}): {param_desc}\n"
instruction += "\n"
instruction += "=== END OF TOOLS ===\n\n"
first_tool = tools[0] if tools else {}
first_func = first_tool.get("function", first_tool)
first_name = first_func.get("name", "tool")
instruction += f'EXAMPLE: If the user asks a question, respond with:\n'
instruction += '{"tool_calls": [{"name": "' + first_name + '", "arguments": {"input": "the user question here"}}]}\n\n'
instruction += "Now respond with the JSON to call the appropriate tool:\n\n"
return instruction
def parse_tool_calls(response_text):
cleaned = response_text.strip()
if "```" in cleaned:
code_block_match = re.search(r'```(?:json)?\s*\n?(.*?)\n?\s*```', cleaned, re.DOTALL)
if code_block_match:
cleaned = code_block_match.group(1).strip()
json_candidates = [cleaned]
json_match = re.search(r'\{[\s\S]*"tool_calls"[\s\S]*\}', cleaned)
if json_match:
json_candidates.append(json_match.group(0))
for candidate in json_candidates:
try:
parsed = json.loads(candidate)
if isinstance(parsed, dict) and "tool_calls" in parsed:
raw_calls = parsed["tool_calls"]
if isinstance(raw_calls, list) and len(raw_calls) > 0:
formatted_calls = []
for call in raw_calls:
tool_name = call.get("name", "")
arguments = call.get("arguments", {})
if isinstance(arguments, dict):
arguments_str = json.dumps(arguments, ensure_ascii=False)
else:
arguments_str = str(arguments)
formatted_calls.append({
"id": f"call_{uuid.uuid4().hex[:24]}",
"type": "function",
"function": {
"name": tool_name,
"arguments": arguments_str
}
})
return formatted_calls
except (json.JSONDecodeError, TypeError, KeyError):
continue
return None
# ====================================================================
# FastAPI App
# ====================================================================
app = FastAPI(title="zai_api for n8n")
def _auth(request: Request) -> bool:
auth = request.headers.get("authorization", "")
return auth.replace("Bearer ", "").strip() == API_SECRET_KEY
@app.post("/v1/chat/completions")
async def chat_completions(request: Request):
try:
data = await request.json()
except Exception:
return JSONResponse(status_code=400, content={"error": {"message": "Invalid JSON payload"}})
if not _auth(request):
return JSONResponse(status_code=401, content={"error": {"message": "Invalid API Key"}})
messages = data.get("messages", [])
if not messages:
return JSONResponse(status_code=400, content={"error": {"message": "messages field is required"}})
try:
tools = data.get("tools", None)
prompt = format_prompt(messages, tools=tools)
start_time = time.time()
print(f"[ZAI-SERVER] Processing request ({len(prompt)} chars)")
response_text = browser_engine.process_request(prompt)
p_tokens = len(prompt.split())
c_tokens = len(response_text.split())
tool_calls = parse_tool_calls(response_text) if tools else None
if tool_calls:
return {
"id": f"chatcmpl-{uuid.uuid4().hex[:29]}",
"object": "chat.completion",
"created": int(start_time),
"model": data.get("model", "GLM-5.1"),
"choices": [{"index": 0, "message": {"role": "assistant", "content": None,
"tool_calls": tool_calls}, "finish_reason": "tool_calls"}],
"usage": {"prompt_tokens": p_tokens, "completion_tokens": c_tokens,
"total_tokens": p_tokens + c_tokens}
}
return {
"id": f"chatcmpl-{uuid.uuid4().hex[:29]}",
"object": "chat.completion",
"created": int(start_time),
"model": data.get("model", "GLM-5.1"),
"choices": [{"index": 0, "message": {"role": "assistant", "content": response_text},
"finish_reason": "stop"}],
"usage": {"prompt_tokens": p_tokens, "completion_tokens": c_tokens,
"total_tokens": p_tokens + c_tokens}
}
except Exception as e:
return JSONResponse(status_code=500, content={"error": str(e)})
@app.post("/v1/responses")
async def responses(request: Request):
try:
data = await request.json()
except Exception:
return JSONResponse(status_code=400, content={"error": {"message": "Invalid JSON payload"}})
if not _auth(request):
return JSONResponse(status_code=401, content={"error": {"message": "Invalid API Key"}})
input_data = data.get("input", "")
if isinstance(input_data, str):
messages = [{"role": "user", "content": input_data}]
elif isinstance(input_data, list):
messages = input_data
else:
messages = data.get("messages", [])
if not messages:
return JSONResponse(status_code=400, content={"error": {"message": "input field is required"}})
try:
tools = data.get("tools", None)
instructions = data.get("instructions", "")
if instructions:
messages.insert(0, {"role": "system", "content": instructions})
prompt = format_prompt(messages, tools=tools)
start_time = time.time()
response_text = browser_engine.process_request(prompt)
p_tokens = len(prompt.split())
c_tokens = len(response_text.split())
tool_calls = parse_tool_calls(response_text) if tools else None
if tool_calls:
output_items = []
for tc in tool_calls:
output_items.append({
"type": "function_call",
"id": tc["id"],
"call_id": tc["id"],
"name": tc["function"]["name"],
"arguments": tc["function"]["arguments"],
"status": "completed"
})
return {
"id": f"resp-{uuid.uuid4().hex[:29]}",
"object": "response",
"created_at": int(start_time),
"model": data.get("model", "GLM-5.1"),
"status": "completed",
"output": output_items,
"usage": {"input_tokens": p_tokens, "output_tokens": c_tokens,
"total_tokens": p_tokens + c_tokens}
}
return {
"id": f"resp-{uuid.uuid4().hex[:29]}",
"object": "response",
"created_at": int(start_time),
"model": data.get("model", "GLM-5.1"),
"status": "completed",
"output": [{"type": "message", "role": "assistant",
"content": [{"type": "output_text", "text": response_text}]}],
"usage": {"input_tokens": p_tokens, "output_tokens": c_tokens,
"total_tokens": p_tokens + c_tokens}
}
except Exception as e:
return JSONResponse(status_code=500, content={"error": str(e)})
@app.get("/v1/models")
async def list_models():
return {
"object": "list",
"data": [
{"id": m, "object": "model", "owned_by": "zai"}
for m in ZAI_MODELS
]
}
@app.get("/health")
@app.get("/")
async def health_check():
return {
"status": "running",
"message": "zai_api Server is active!",
"models": ZAI_MODELS,
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860)
|