* Studio: add API key authentication for programmatic access External users want to hit the Studio API (chat completions with tool calling, training, export, etc.) without going through the browser login flow. This adds sk-unsloth- prefixed API keys that work as a drop-in replacement for JWTs in the Authorization: Bearer header. Backend: - New api_keys table in SQLite (storage.py) - create/list/revoke/validate functions with SHA-256 hashed storage - API key detection in _get_current_subject before the JWT path - POST/GET/DELETE /api/auth/api-keys endpoints on the auth router Frontend: - /api-keys page with create form, one-time key reveal, keys table - API Keys link in desktop and mobile navbar - Route registered with requireAuth guard Zero changes to any existing route handler -- every endpoint that uses Depends(get_current_subject) automatically works with API keys. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Use actual origin in API key usage examples The examples on /api-keys were hardcoded to localhost:8888 which is wrong for remote users. Use window.location.origin so the examples show the correct URL regardless of where the user is connecting from. * Add `unsloth studio run` CLI command for one-liner model serving Adds a `run` subcommand that starts Studio, loads a model, creates an API key, and prints a ready-to-use curl command -- similar to `ollama run` or `vllm serve`. Usage: unsloth studio run -m unsloth/Qwen3-1.7B-GGUF --gguf-variant UD-Q4_K_XL * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add end-to-end tests for `unsloth studio run` and API key usage Tests the 4 usage examples from the API Keys page: 1. curl basic (non-streaming) chat completions 2. curl streaming (SSE) chat completions 3. OpenAI Python SDK streaming completions 4. curl with tools (web_search + python) Also tests --help output, invalid key rejection, and no-key rejection. All 7 tests pass against Qwen3-1.7B-GGUF. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add /v1/completions, /v1/embeddings, /v1/responses endpoints and --parallel support - llama_cpp.py: accept n_parallel param, pass to llama-server --parallel - run.py: plumb llama_parallel_slots through to app.state - inference.py: add /completions and /embeddings as transparent proxies to llama-server, add /responses as application-level endpoint that converts to ChatCompletionRequest; thread n_parallel through load_model - studio.py: set llama_parallel_slots=4 for `unsloth studio run` path * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Make /v1/responses endpoint match OpenAI Responses API format The existing /v1/responses shim returned Chat Completions format, which broke OpenAI SDK clients using openai.responses.create(). This commit replaces the endpoint with a proper implementation that: - Returns `output` array with `output_text` content parts instead of `choices` with `message` - Uses `input_tokens`/`output_tokens` instead of `prompt_tokens`/ `completion_tokens` in usage - Sets `object: "response"` and `id: "resp_..."` - Emits named SSE events for streaming (response.created, response.output_text.delta, response.completed, etc.) - Accepts all OpenAI Responses API fields (tools, store, metadata, previous_response_id) without erroring -- silently ignored - Maps `developer` role to `system` and `input_text`/`input_image` content parts to the internal Chat format Adds Pydantic schemas for request/response models and 23 unit tests covering schema validation, input normalisation, and response format. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Studio: add Anthropic-compatible /v1/messages endpoint (#4981) * Add Anthropic-compatible /v1/messages endpoint with tool support Translate Anthropic Messages API format to/from internal OpenAI format and reuse the existing server-side agentic tool loop. Supports streaming SSE (message_start, content_block_delta, etc.) and non-streaming JSON. Includes offline unit tests and e2e tests in test_studio_run.py. * Add enable_tools, enabled_tools, session_id to /v1/messages endpoint Support the same shorthand as /v1/chat/completions: enable_tools=true with an optional enabled_tools list uses built-in server tools without requiring full Anthropic tool definitions. session_id is passed through for sandbox isolation. max_tokens is now optional. * Strip leaked tool-call XML from Anthropic endpoint content Apply _TOOL_XML_RE to content events in both streaming and non-streaming tool paths, matching the OpenAI endpoint behavior. * Emit custom tool_result SSE event in Anthropic stream Adds a non-standard tool_result event between the tool_use block close and the next text block, so clients can see server-side tool execution results. Anthropic SDKs ignore unknown event types. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Split /v1/messages into server-side and client-side tool paths enable_tools=true runs the existing server-side agentic loop with built-in tools (web_search/python/terminal). A bare tools=[...] field now triggers a client-side pass-through: client-provided tools are forwarded to llama-server and any tool_use output is returned to the caller with stop_reason=tool_use for client execution. This fixes Claude Code (and any Anthropic SDK client) which sends tools=[...] expecting client-side execution but was previously routed through execute_tool() and failing with 'Unknown tool'. Adds AnthropicPassthroughEmitter to convert llama-server OpenAI SSE chunks into Anthropic SSE events, plus unit tests covering text blocks, tool_use blocks, mixed, stop reasons, and usage. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix httpcore GeneratorExit in /v1/messages passthrough stream Explicitly aclose aiter_lines() before the surrounding async with blocks unwind, mirroring the prior fix in external_provider.py (a41160d3) and cc757b78's RuntimeError suppression. * Wire stop_sequences through /v1/messages; warn on tool_choice Plumb payload.stop_sequences to all three code paths (server-side tool loop, no-tool plain, client-side passthrough) so Anthropic SDK clients setting stop_sequences get the behavior they expect. The llama_cpp backend already accepted `stop` on both generate_chat_ completion and generate_chat_completion_with_tools; the Anthropic handler simply wasn't passing it. tool_choice remains declared on the request model for Anthropic SDK compatibility (the SDK often sets it by default) but is not yet honored. Log a structured warning on each request carrying a non- null tool_choice so the silent drop is visible to operators. * Wire min_p / repetition_penalty / presence_penalty through /v1/messages Align the Anthropic endpoint's sampling surface with /v1/chat/completions. Adds the three fields as x-unsloth extensions on AnthropicMessagesRequest and threads them through all three code paths: server-side tool loop, no-tool plain, and client-side passthrough. The passthrough builder emits "repeat_penalty" (not "repetition_penalty") because that is llama-server's field name; the backend methods already apply the same rename internally. * Fix block ordering and prev_text reset in non-streaming tool path _anthropic_tool_non_streaming was building the response by appending all tool_use blocks first, then a single concatenated text block at the end — losing generation order and merging pre-tool and post-tool text into one block. It also never reset prev_text between synthesis turns, so the first N characters of each post-tool turn were dropped (where N = length of the prior turn's final cumulative text). Rewrite to build content_blocks incrementally in generation order, matching the streaming emitter's behavior: deltas within a turn are merged into the trailing text block, tool_use blocks interrupt the text sequence, and prev_text is reset on tool_end so turn N+1 diffs against an empty baseline. Caught by gemini-code-assist[bot] review on #4981. * Make test_studio_run.py e2e tests pytest-compatible Add a hybrid session-scoped studio_server fixture in conftest.py that feeds base_url / api_key into the existing e2e test functions. Three invocation modes are now supported: 1. Script mode (unchanged) — python tests/test_studio_run.py 2. Pytest + external server — point at a running instance via UNSLOTH_E2E_BASE_URL / UNSLOTH_E2E_API_KEY env vars, no per-run GGUF load cost 3. Pytest + fixture-managed server — pytest drives _start_server / _kill_server itself via --unsloth-model / --unsloth-gguf-variant, CI-friendly The existing _start_server / _kill_server helpers and main() stay untouched so the script entry point keeps working exactly as before. Test function signatures are unchanged — the (base_url, api_key) parameters now resolve via the new fixtures when running under pytest. * Rename test_studio_run.py -> test_studio_api.py The file is entirely about HTTP API endpoint testing (OpenAI-compatible /v1/chat/completions, Anthropic-compatible /v1/messages, API key auth, plus a CLI --help sanity check on the command that runs the API). None of its tests cover training, export, chat-UI, or internal-Python-API concerns. The old name misleadingly suggested "tests for the unsloth studio run CLI subcommand" — the new name reflects the actual scope. Updates: - git mv the file (rename tracked, history preserved) - Rewrite opening docstring to state the API surface focus and call out what is explicitly out of scope - Update all 4 Usage-block path references to the new filename - LOG_FILE renamed to test_studio_api.log - conftest.py fixture import rewritten from test_studio_run to test_studio_api, plus 7 docstring/comment references updated No functional changes to test logic, signatures, or main(). --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * Fix httpcore asyncgen cleanup in /v1/messages and /v1/completions The earlier fix in985e92a9was incomplete: it closed aiter_lines() explicitly but still used `async with httpx.AsyncClient()` / `async with client.stream()` inside the generator. When the generator is orphaned (e.g. client disconnects mid-stream and Starlette drops the StreamingResponse iterator without explicitly calling aclose()), Python's asyncgen finalizer runs the cleanup in a DIFFERENT task than the one that originally entered the httpx context managers. The `async with` exits then trigger httpcore's HTTP11ConnectionByteStream .aclose(), which enters anyio.CancelScope.__exit__ with a mismatched task and raises RuntimeError("Attempted to exit cancel scope in a different task"). That error escapes any user-owned try/except because it happens during GC finalization. Replace `async with` with manual client/response lifecycle in both /v1/messages passthrough and /v1/completions proxy. Close the response and client in a finally block wrapped in `try: ... except Exception: pass`. This suppresses RuntimeError (and other Exception subclasses) from the anyio cleanup noise while letting GeneratorExit (a BaseException, not Exception) propagate cleanly so the generator terminates as Python expects. Traceback observed in user report: File ".../httpcore/_async/connection_pool.py", line 404, in __aiter__ yield part RuntimeError: async generator ignored GeneratorExit ... File ".../anyio/_backends/_asyncio.py", line 455, in __exit__ raise RuntimeError( RuntimeError: Attempted to exit cancel scope in a different task * Expand unsloth studio run banner with SDK base URL and more curl examples Add an explicit "OpenAI / Anthropic SDK base URL" line inside the info box so SDK users don't accidentally copy the bare server URL (without /v1) into their OpenAI/Anthropic SDK constructors and hit 404s. Replace the single /v1/chat/completions curl example with three labeled blocks: chat/completions, Anthropic /messages, and OpenAI Responses. The Anthropic example includes max_tokens (Anthropic SDKs require it even though Studio accepts None). All examples derived from a computed sdk_base_url so the /v1 prefix stays in sync if the public path ever changes. * Hash API keys with HMAC-SHA256 + persistent server secret Stores the HMAC secret in a new app_secrets singleton table. Fixes CodeQL py/weak-sensitive-data-hashing alert on storage.py:74-76, 394-395. Refresh tokens stay on plain SHA-256 (unchanged _hash_token) so existing user sessions survive upgrade — API keys are new on this branch so there is no migration. * Use PBKDF2 for API key hashing per CodeQL recommendation HMAC-SHA256 was still flagged by py/weak-sensitive-data-hashing. Switch to hashlib.pbkdf2_hmac, which is in CodeQL's recommended allowlist (Argon2/scrypt/bcrypt/PBKDF2). Persistent server-side salt stays in app_secrets for defense-in-depth. 100k iterations to match auth/hashing.py's password hasher. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Roland Tannous <115670425+rolandtannous@users.noreply.github.com> Co-authored-by: Roland Tannous <rolandtannous@gravityq.ai>
643 lines
22 KiB
Python
643 lines
22 KiB
Python
# SPDX-License-Identifier: AGPL-3.0-only
|
|
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
|
|
|
|
"""
|
|
End-to-end tests for Unsloth Studio's HTTP API surface.
|
|
|
|
Covers the OpenAI-compatible and Anthropic-compatible endpoints exposed
|
|
by the server that ``unsloth studio run`` boots, plus API key
|
|
authentication and the CLI's ``--help`` output:
|
|
|
|
1. curl -- basic chat completions (non-streaming)
|
|
2. curl -- streaming chat completions
|
|
3. Python OpenAI SDK -- streaming completions
|
|
4. curl -- with tools (web_search + python)
|
|
5. Anthropic Messages API -- basic non-streaming
|
|
6. Anthropic Messages API -- streaming SSE
|
|
7. Anthropic Python SDK -- non-streaming
|
|
8. Anthropic Messages API -- streaming with tools
|
|
|
|
Training, export, fine-tuning, and chat-UI concerns are out of scope —
|
|
see the unit suites elsewhere under ``studio/backend/tests/`` for those.
|
|
|
|
Usage:
|
|
|
|
# Script mode — launches its own server via ``unsloth studio run``.
|
|
python tests/test_studio_api.py
|
|
python tests/test_studio_api.py --model unsloth/... --gguf-variant ...
|
|
|
|
# Pytest mode, external server — start a Studio server yourself,
|
|
# then point pytest at it. Fastest iteration loop.
|
|
unsloth studio run --model unsloth/Qwen3-1.7B-GGUF --gguf-variant UD-Q4_K_XL &
|
|
export UNSLOTH_E2E_BASE_URL=http://127.0.0.1:8080
|
|
export UNSLOTH_E2E_API_KEY=sk-unsloth-... # from the server banner
|
|
pytest tests/test_studio_api.py -v
|
|
|
|
# Pytest mode, fixture-managed server — pytest launches and tears
|
|
# down the server itself. One-shot verification, CI-friendly.
|
|
pytest tests/test_studio_api.py -v \\
|
|
--unsloth-model unsloth/Qwen3-1.7B-GGUF \\
|
|
--unsloth-gguf-variant UD-Q4_K_XL
|
|
|
|
The ``base_url`` / ``api_key`` parameters on the test functions resolve
|
|
via the ``studio_server`` session fixture in ``conftest.py``.
|
|
|
|
Requires a GPU and ~2 GB of disk for the GGUF download.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
import re
|
|
import signal
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
from pathlib import Path
|
|
|
|
|
|
# ── Configuration ────────────────────────────────────────────────────
|
|
|
|
DEFAULT_MODEL = "unsloth/Qwen3-1.7B-GGUF"
|
|
DEFAULT_VARIANT = "UD-Q4_K_XL"
|
|
PORT = 18222 # high port unlikely to collide
|
|
HOST = "127.0.0.1"
|
|
STARTUP_TIMEOUT = 120 # seconds to wait for banner
|
|
LOG_FILE = (
|
|
Path(__file__).resolve().parent.parent.parent.parent
|
|
/ "temp"
|
|
/ "test_studio_api.log"
|
|
)
|
|
|
|
|
|
# ── Helpers ──────────────────────────────────────────────────────────
|
|
|
|
|
|
def _http(
|
|
method: str,
|
|
url: str,
|
|
*,
|
|
body: dict | None = None,
|
|
headers: dict | None = None,
|
|
timeout: int = 60,
|
|
) -> tuple[int, str]:
|
|
"""Minimal stdlib HTTP helper. Returns (status_code, body_text)."""
|
|
data = json.dumps(body).encode() if body else None
|
|
req = urllib.request.Request(url, data = data, headers = headers or {}, method = method)
|
|
if body:
|
|
req.add_header("Content-Type", "application/json")
|
|
try:
|
|
with urllib.request.urlopen(req, timeout = timeout) as resp:
|
|
return resp.status, resp.read().decode()
|
|
except urllib.error.HTTPError as exc:
|
|
return exc.code, exc.read().decode(errors = "replace")
|
|
|
|
|
|
def _stream_http(
|
|
url: str,
|
|
*,
|
|
body: dict,
|
|
headers: dict,
|
|
timeout: int = 60,
|
|
) -> tuple[int, list[dict]]:
|
|
"""POST a streaming request and collect SSE chunks."""
|
|
data = json.dumps(body).encode()
|
|
req = urllib.request.Request(url, data = data, headers = headers, method = "POST")
|
|
req.add_header("Content-Type", "application/json")
|
|
chunks: list[dict] = []
|
|
try:
|
|
with urllib.request.urlopen(req, timeout = timeout) as resp:
|
|
status = resp.status
|
|
for raw_line in resp:
|
|
line = raw_line.decode().strip()
|
|
if line.startswith("data: ") and line != "data: [DONE]":
|
|
try:
|
|
chunks.append(json.loads(line[6:]))
|
|
except json.JSONDecodeError:
|
|
pass
|
|
return status, chunks
|
|
except urllib.error.HTTPError as exc:
|
|
return exc.code, []
|
|
|
|
|
|
# ── Test functions ───────────────────────────────────────────────────
|
|
|
|
|
|
def test_help_output():
|
|
"""``unsloth studio run --help`` should show all documented options."""
|
|
result = subprocess.run(
|
|
["unsloth", "studio", "run", "--help"],
|
|
capture_output = True,
|
|
text = True,
|
|
timeout = 15,
|
|
)
|
|
out = result.stdout
|
|
assert result.returncode == 0, f"--help exited with {result.returncode}"
|
|
|
|
for flag in [
|
|
"--model",
|
|
"--gguf-variant",
|
|
"--max-seq-length",
|
|
"--load-in-4bit",
|
|
"--api-key-name",
|
|
"--port",
|
|
"--host",
|
|
"--frontend",
|
|
"--silent",
|
|
]:
|
|
assert flag in out, f"Missing flag {flag!r} in --help output"
|
|
print(" PASS --help shows all flags")
|
|
|
|
|
|
def test_curl_basic(base_url: str, api_key: str):
|
|
"""Example 1: basic non-streaming chat completion via HTTP."""
|
|
status, text = _http(
|
|
"POST",
|
|
f"{base_url}/v1/chat/completions",
|
|
body = {
|
|
"messages": [{"role": "user", "content": "Say just the word hello"}],
|
|
"stream": False,
|
|
},
|
|
headers = {"Authorization": f"Bearer {api_key}"},
|
|
)
|
|
assert status == 200, f"Expected 200, got {status}: {text[:300]}"
|
|
data = json.loads(text)
|
|
assert "choices" in data, f"Missing 'choices' in response: {text[:300]}"
|
|
content = data["choices"][0]["message"]["content"]
|
|
assert len(content) > 0, "Empty assistant content"
|
|
print(f" PASS curl basic: {content[:80]!r}")
|
|
|
|
|
|
def _collect_streamed_content(chunks: list[dict]) -> str:
|
|
"""Extract text from SSE chunks, skipping role-only and usage chunks."""
|
|
parts = []
|
|
for c in chunks:
|
|
choices = c.get("choices", [])
|
|
if not choices:
|
|
continue
|
|
delta = choices[0].get("delta", {})
|
|
part = delta.get("content")
|
|
if part:
|
|
parts.append(part)
|
|
return "".join(parts)
|
|
|
|
|
|
def test_curl_streaming(base_url: str, api_key: str):
|
|
"""Example 2: streaming chat completion via HTTP SSE."""
|
|
status, chunks = _stream_http(
|
|
f"{base_url}/v1/chat/completions",
|
|
body = {
|
|
"messages": [{"role": "user", "content": "Count from 1 to 3"}],
|
|
"stream": True,
|
|
},
|
|
headers = {"Authorization": f"Bearer {api_key}"},
|
|
)
|
|
assert status == 200, f"Expected 200, got {status}"
|
|
assert len(chunks) > 0, "No SSE chunks received"
|
|
full = _collect_streamed_content(chunks)
|
|
assert len(full) > 0, "Streamed content is empty"
|
|
print(f" PASS curl streaming: got {len(chunks)} chunks, {len(full)} chars")
|
|
|
|
|
|
def test_openai_sdk(base_url: str, api_key: str):
|
|
"""Example 3: OpenAI Python SDK streaming completion."""
|
|
try:
|
|
from openai import OpenAI
|
|
except ImportError:
|
|
print(" SKIP openai SDK not installed")
|
|
return
|
|
|
|
client = OpenAI(base_url = f"{base_url}/v1", api_key = api_key)
|
|
response = client.chat.completions.create(
|
|
model = "current",
|
|
messages = [
|
|
{"role": "user", "content": "What is 2+2? Answer with just the number."}
|
|
],
|
|
stream = True,
|
|
)
|
|
content_parts = []
|
|
for chunk in response:
|
|
if not chunk.choices:
|
|
continue
|
|
delta_content = chunk.choices[0].delta.content
|
|
if delta_content:
|
|
content_parts.append(delta_content)
|
|
full = "".join(content_parts)
|
|
assert len(full) > 0, "OpenAI SDK returned empty content"
|
|
print(f" PASS OpenAI SDK streaming: {full.strip()[:80]!r}")
|
|
|
|
|
|
def test_curl_with_tools(base_url: str, api_key: str):
|
|
"""Example 4: chat completion with tool calling enabled.
|
|
|
|
Note: when ``enable_tools`` is set the server always returns SSE
|
|
streaming regardless of the ``stream`` flag, so we parse SSE chunks.
|
|
The model may or may not produce visible content -- tool orchestration
|
|
can intercept the response -- so we only assert the endpoint succeeds.
|
|
"""
|
|
status, chunks = _stream_http(
|
|
f"{base_url}/v1/chat/completions",
|
|
body = {
|
|
"messages": [
|
|
{
|
|
"role": "user",
|
|
"content": "What is 123 * 456? Use code to compute it.",
|
|
}
|
|
],
|
|
"stream": True,
|
|
"enable_tools": True,
|
|
"enabled_tools": ["python"],
|
|
"session_id": "test-session",
|
|
},
|
|
headers = {"Authorization": f"Bearer {api_key}"},
|
|
timeout = 120,
|
|
)
|
|
assert status == 200, f"Expected 200, got {status}"
|
|
assert len(chunks) > 0, "No SSE chunks received for tools request"
|
|
|
|
# Check that at least one chunk has the expected shape
|
|
has_valid_chunk = any("choices" in c or "type" in c for c in chunks)
|
|
assert has_valid_chunk, "No valid chunks in tools response"
|
|
full = _collect_streamed_content(chunks)
|
|
print(f" PASS curl with tools: {len(chunks)} chunks, {len(full)} chars content")
|
|
|
|
|
|
def test_invalid_key_rejected(base_url: str):
|
|
"""Requests with a bad API key should be rejected."""
|
|
status, _text = _http(
|
|
"POST",
|
|
f"{base_url}/v1/chat/completions",
|
|
body = {
|
|
"messages": [{"role": "user", "content": "Hello"}],
|
|
"stream": False,
|
|
},
|
|
headers = {"Authorization": "Bearer sk-unsloth-boguskey123"},
|
|
)
|
|
assert status == 401, f"Expected 401 for invalid key, got {status}"
|
|
print(" PASS invalid API key rejected (401)")
|
|
|
|
|
|
def test_no_key_rejected(base_url: str):
|
|
"""Requests without any auth header should be rejected."""
|
|
status, _text = _http(
|
|
"POST",
|
|
f"{base_url}/v1/chat/completions",
|
|
body = {
|
|
"messages": [{"role": "user", "content": "Hello"}],
|
|
"stream": False,
|
|
},
|
|
)
|
|
assert status == 401 or status == 403, f"Expected 401/403 for no key, got {status}"
|
|
print(f" PASS no API key rejected ({status})")
|
|
|
|
|
|
# ── Anthropic SSE helper ─────────────────────────────────────────────
|
|
|
|
|
|
def _stream_anthropic_http(
|
|
url: str,
|
|
*,
|
|
body: dict,
|
|
headers: dict,
|
|
timeout: int = 60,
|
|
) -> tuple[int, list[tuple[str, dict]]]:
|
|
"""POST a streaming request and collect Anthropic SSE events.
|
|
|
|
Returns (status, [(event_type, data_dict), ...]).
|
|
"""
|
|
data = json.dumps(body).encode()
|
|
req = urllib.request.Request(url, data = data, headers = headers, method = "POST")
|
|
req.add_header("Content-Type", "application/json")
|
|
events: list[tuple[str, dict]] = []
|
|
try:
|
|
with urllib.request.urlopen(req, timeout = timeout) as resp:
|
|
status = resp.status
|
|
current_event = None
|
|
for raw_line in resp:
|
|
line = raw_line.decode().strip()
|
|
if line.startswith("event: "):
|
|
current_event = line[7:]
|
|
elif line.startswith("data: ") and current_event:
|
|
try:
|
|
events.append((current_event, json.loads(line[6:])))
|
|
except json.JSONDecodeError:
|
|
pass
|
|
current_event = None
|
|
return status, events
|
|
except urllib.error.HTTPError as exc:
|
|
return exc.code, []
|
|
|
|
|
|
def _collect_anthropic_text(events: list[tuple[str, dict]]) -> str:
|
|
"""Extract text content from Anthropic SSE events."""
|
|
parts = []
|
|
for etype, data in events:
|
|
if etype == "content_block_delta":
|
|
delta = data.get("delta", {})
|
|
if delta.get("type") == "text_delta":
|
|
parts.append(delta.get("text", ""))
|
|
return "".join(parts)
|
|
|
|
|
|
# ── Anthropic /v1/messages test functions ────────────────────────────
|
|
|
|
|
|
def test_anthropic_basic(base_url: str, api_key: str):
|
|
"""Anthropic Messages API: non-streaming."""
|
|
status, text = _http(
|
|
"POST",
|
|
f"{base_url}/v1/messages",
|
|
body = {
|
|
"model": "default",
|
|
"max_tokens": 100,
|
|
"messages": [{"role": "user", "content": "Say just the word hello"}],
|
|
},
|
|
headers = {"Authorization": f"Bearer {api_key}"},
|
|
)
|
|
assert status == 200, f"Expected 200, got {status}: {text[:300]}"
|
|
data = json.loads(text)
|
|
assert data.get("type") == "message", f"Expected type 'message': {text[:300]}"
|
|
assert data.get("role") == "assistant"
|
|
content = data.get("content", [])
|
|
assert len(content) > 0, "Empty content array"
|
|
text_block = content[-1]
|
|
assert text_block.get("type") == "text", f"Expected text block: {text_block}"
|
|
assert len(text_block.get("text", "")) > 0, "Empty text in response"
|
|
print(f" PASS anthropic basic: {text_block['text'][:80]!r}")
|
|
|
|
|
|
def test_anthropic_streaming(base_url: str, api_key: str):
|
|
"""Anthropic Messages API: streaming SSE."""
|
|
status, events = _stream_anthropic_http(
|
|
f"{base_url}/v1/messages",
|
|
body = {
|
|
"model": "default",
|
|
"max_tokens": 100,
|
|
"messages": [{"role": "user", "content": "Count from 1 to 3"}],
|
|
"stream": True,
|
|
},
|
|
headers = {"Authorization": f"Bearer {api_key}"},
|
|
)
|
|
assert status == 200, f"Expected 200, got {status}"
|
|
assert len(events) > 0, "No SSE events received"
|
|
|
|
event_types = [e[0] for e in events]
|
|
assert "message_start" in event_types, "Missing message_start event"
|
|
assert "message_stop" in event_types, "Missing message_stop event"
|
|
|
|
full = _collect_anthropic_text(events)
|
|
assert len(full) > 0, "Streamed text content is empty"
|
|
print(f" PASS anthropic streaming: {len(events)} events, {len(full)} chars")
|
|
|
|
|
|
def test_anthropic_sdk(base_url: str, api_key: str):
|
|
"""Anthropic Python SDK: non-streaming."""
|
|
try:
|
|
from anthropic import Anthropic
|
|
except ImportError:
|
|
print(" SKIP anthropic SDK not installed")
|
|
return
|
|
|
|
client = Anthropic(base_url = f"{base_url}/v1", api_key = api_key)
|
|
message = client.messages.create(
|
|
model = "default",
|
|
max_tokens = 100,
|
|
messages = [
|
|
{"role": "user", "content": "What is 2+2? Answer with just the number."}
|
|
],
|
|
)
|
|
assert message.role == "assistant"
|
|
assert len(message.content) > 0, "Empty content"
|
|
text = message.content[0].text
|
|
assert len(text) > 0, "Empty text"
|
|
print(f" PASS Anthropic SDK: {text.strip()[:80]!r}")
|
|
|
|
|
|
def test_anthropic_with_tools(base_url: str, api_key: str):
|
|
"""Anthropic Messages API: streaming with tools."""
|
|
status, events = _stream_anthropic_http(
|
|
f"{base_url}/v1/messages",
|
|
body = {
|
|
"model": "default",
|
|
"max_tokens": 1024,
|
|
"messages": [
|
|
{
|
|
"role": "user",
|
|
"content": "What is 123 * 456? Use code to compute it.",
|
|
}
|
|
],
|
|
"tools": [
|
|
{
|
|
"name": "python",
|
|
"description": "Execute Python code in a sandbox and return stdout/stderr.",
|
|
"input_schema": {
|
|
"type": "object",
|
|
"properties": {
|
|
"code": {
|
|
"type": "string",
|
|
"description": "The Python code to run",
|
|
},
|
|
},
|
|
"required": ["code"],
|
|
},
|
|
}
|
|
],
|
|
"stream": True,
|
|
},
|
|
headers = {"Authorization": f"Bearer {api_key}"},
|
|
timeout = 120,
|
|
)
|
|
assert status == 200, f"Expected 200, got {status}"
|
|
assert len(events) > 0, "No SSE events received for tools request"
|
|
|
|
event_types = [e[0] for e in events]
|
|
assert "message_start" in event_types, "Missing message_start"
|
|
assert "message_stop" in event_types, "Missing message_stop"
|
|
|
|
full = _collect_anthropic_text(events)
|
|
print(
|
|
f" PASS anthropic with tools: {len(events)} events, {len(full)} chars content"
|
|
)
|
|
|
|
|
|
# ── Server lifecycle ─────────────────────────────────────────────────
|
|
|
|
|
|
def _start_server(model: str, variant: str | None) -> tuple[subprocess.Popen, str]:
|
|
"""Launch ``unsloth studio run`` and parse the API key from its banner.
|
|
|
|
Returns (process, api_key).
|
|
"""
|
|
cmd = [
|
|
"unsloth",
|
|
"studio",
|
|
"run",
|
|
"--model",
|
|
model,
|
|
"--port",
|
|
str(PORT),
|
|
"--host",
|
|
HOST,
|
|
"--api-key-name",
|
|
"test",
|
|
]
|
|
if variant:
|
|
cmd.extend(["--gguf-variant", variant])
|
|
|
|
LOG_FILE.parent.mkdir(parents = True, exist_ok = True)
|
|
log_fh = open(LOG_FILE, "w")
|
|
proc = subprocess.Popen(
|
|
cmd,
|
|
stdout = log_fh,
|
|
stderr = subprocess.STDOUT,
|
|
preexec_fn = os.setsid,
|
|
)
|
|
|
|
# Wait for the banner containing the API key
|
|
api_key = None
|
|
deadline = time.monotonic() + STARTUP_TIMEOUT
|
|
while time.monotonic() < deadline:
|
|
time.sleep(2)
|
|
if proc.poll() is not None:
|
|
log_fh.flush()
|
|
log_text = LOG_FILE.read_text()
|
|
raise RuntimeError(
|
|
f"Server exited early (code {proc.returncode}):\n{log_text[-2000:]}"
|
|
)
|
|
log_text = LOG_FILE.read_text()
|
|
m = re.search(r"API Key:\s+(sk-unsloth-[a-f0-9]+)", log_text)
|
|
if m:
|
|
api_key = m.group(1)
|
|
break
|
|
|
|
if not api_key:
|
|
log_text = LOG_FILE.read_text()
|
|
_kill_server(proc)
|
|
raise RuntimeError(
|
|
f"Timed out waiting for API key in server output:\n{log_text[-2000:]}"
|
|
)
|
|
|
|
# Wait a moment for the model to be fully loaded
|
|
time.sleep(2)
|
|
return proc, api_key
|
|
|
|
|
|
def _kill_server(proc: subprocess.Popen):
|
|
"""Send SIGTERM to the process group and wait for cleanup."""
|
|
try:
|
|
os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
|
|
except (ProcessLookupError, PermissionError):
|
|
pass
|
|
try:
|
|
proc.wait(timeout = 10)
|
|
except subprocess.TimeoutExpired:
|
|
try:
|
|
os.killpg(os.getpgid(proc.pid), signal.SIGKILL)
|
|
except (ProcessLookupError, PermissionError):
|
|
pass
|
|
proc.wait(timeout = 5)
|
|
|
|
|
|
# ── Main ─────────────────────────────────────────────────────────────
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(
|
|
description = "End-to-end tests for unsloth studio run"
|
|
)
|
|
parser.add_argument(
|
|
"--model",
|
|
default = DEFAULT_MODEL,
|
|
help = f"Model to test with (default: {DEFAULT_MODEL})",
|
|
)
|
|
parser.add_argument(
|
|
"--gguf-variant",
|
|
default = DEFAULT_VARIANT,
|
|
help = f"GGUF variant (default: {DEFAULT_VARIANT})",
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
passed = 0
|
|
failed = 0
|
|
skipped = 0
|
|
|
|
def run_test(fn, *a, **kw):
|
|
nonlocal passed, failed, skipped
|
|
try:
|
|
fn(*a, **kw)
|
|
passed += 1
|
|
except AssertionError as exc:
|
|
failed += 1
|
|
print(f" FAIL {fn.__name__}: {exc}")
|
|
except Exception as exc:
|
|
failed += 1
|
|
print(f" ERROR {fn.__name__}: {type(exc).__name__}: {exc}")
|
|
|
|
# ── 1. Test --help (no server needed) ────────────────────────────
|
|
print("\n[1/11] Testing --help output")
|
|
run_test(test_help_output)
|
|
|
|
# ── 2-11. Start server and run API tests ─────────────────────────
|
|
print(
|
|
f"\nStarting server: {args.model} (variant={args.gguf_variant}) on port {PORT}..."
|
|
)
|
|
proc = None
|
|
try:
|
|
proc, api_key = _start_server(args.model, args.gguf_variant)
|
|
base_url = f"http://{HOST}:{PORT}"
|
|
print(f"Server ready. API Key: {api_key[:20]}...\n")
|
|
|
|
print("[2/11] Testing curl basic (non-streaming)")
|
|
run_test(test_curl_basic, base_url, api_key)
|
|
|
|
print("[3/11] Testing curl streaming")
|
|
run_test(test_curl_streaming, base_url, api_key)
|
|
|
|
print("[4/11] Testing OpenAI Python SDK (streaming)")
|
|
run_test(test_openai_sdk, base_url, api_key)
|
|
|
|
print("[5/11] Testing curl with tools")
|
|
run_test(test_curl_with_tools, base_url, api_key)
|
|
|
|
print("[6/11] Testing invalid API key rejection")
|
|
run_test(test_invalid_key_rejected, base_url)
|
|
|
|
print("[7/11] Testing no API key rejection")
|
|
run_test(test_no_key_rejected, base_url)
|
|
|
|
print("[8/11] Testing Anthropic basic (non-streaming)")
|
|
run_test(test_anthropic_basic, base_url, api_key)
|
|
|
|
print("[9/11] Testing Anthropic streaming")
|
|
run_test(test_anthropic_streaming, base_url, api_key)
|
|
|
|
print("[10/11] Testing Anthropic Python SDK")
|
|
run_test(test_anthropic_sdk, base_url, api_key)
|
|
|
|
print("[11/11] Testing Anthropic with tools")
|
|
run_test(test_anthropic_with_tools, base_url, api_key)
|
|
|
|
except RuntimeError as exc:
|
|
print(f"\nFATAL: Server failed to start: {exc}")
|
|
failed += 11 # count remaining tests as failed
|
|
finally:
|
|
if proc:
|
|
print("\nStopping server...")
|
|
_kill_server(proc)
|
|
print("Server stopped.")
|
|
|
|
# ── Summary ──────────────────────────────────────────────────────
|
|
total = passed + failed
|
|
print(f"\n{'=' * 40}")
|
|
print(f"Results: {passed}/{total} passed, {failed} failed")
|
|
print(f"Log: {LOG_FILE}")
|
|
print(f"{'=' * 40}")
|
|
sys.exit(1 if failed else 0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|