Back to Nexa Sdk

Local server

docs/en/run/cli/local-server.mdx

0.3.178.0 KB
Original Source

import Feedback from "/snippets/page-feedback.mdx";

GenieX includes a built-in inference server that exposes an OpenAI-compatible API. Run models on-device and connect them to any application or framework that speaks the OpenAI protocol — agentic frameworks like LangChain, AI-native apps like OpenClaw, or your own code. No cloud dependency.

Prerequisites

  • The CLI installed — see Install.
  • Interactive shell from container (Docker only) — see Run interactively.
  • A model pulled. geniex serve does not auto-download models.

Start the server

Pull a model:

bash
geniex pull ai-hub-models/Qwen3-4B-Instruct-2507

Start the server:

bash
geniex serve

The server runs on http://127.0.0.1:18181 by default. Keep this terminal open and make requests from another one. Run geniex serve -h for all configurable options.

POST /v1/chat/completions

Creates a model response for a conversation. Supports LLM (text-only) and VLM (image + text).

LLM request

json
{
  "model": "ai-hub-models/Qwen3-4B-Instruct-2507",
  "messages": [
    {"role": "user", "content": "Hello! Briefly introduce yourself."}
  ],
  "max_tokens": 256,
  "temperature": 0.7,
  "stream": false
}

Try it from Swagger UI

Open http://127.0.0.1:18181 in your browser to access the built-in Swagger UI.

Step 1. Expand the POST /v1/chat/completions endpoint to view the example request body and schema.

Step 2. Click Try it out, edit the request body as needed, then click Execute.

Step 3. View the response — a 200 status with the model's generated reply.

VLM request

image_url.url accepts three formats:

FormatExample
Local file path (the file:// prefix is optional)C:/Users/Username/Pictures/photo.jpg, file:///tmp/photo.jpg
HTTP / HTTPS URL — fetched by the serverhttps://example.com/image.jpg
Base64 data URL — inline image bytesdata:image/png;base64,iVBORw0KGgo...
<Note> **Running in Docker?** Local paths are resolved **inside the container**, not on your host. The install command already mounts `$PWD/data` to `/data` — drop your images there and pass `/data/cat.jpg`. Alternatively, use an HTTP URL or base64 data URL to skip the filesystem entirely. </Note>
json
{
  "model": "ai-hub-models/Qwen2.5-VL-7B-Instruct",
  "messages": [
    {
      "role": "user",
      "content": [
        {"type": "text", "text": "Describe this image succinctly."},
        {"type": "image_url", "image_url": {"url": "</path/to/image>"}}
      ]
    }
  ]
}

In Swagger UI, replace the request body with this VLM payload, point image_url.url to a local image, then click Execute.

Python client (OpenAI SDK)

Because the server speaks the OpenAI protocol, you can point the official openai Python client at the local endpoint and reuse any existing OpenAI code. Install with pip install openai, then create a client:

python
from openai import OpenAI

client = OpenAI(
    base_url="http://127.0.0.1:18181/v1",
    api_key="geniex",  # any non-empty string; the server does not check it
)

The examples below reuse this client. Replace the model value with a model you have already pulled. The optional :<precision> suffix (e.g. Q4_0, Q4_K_M, Q8_0) selects a quantization variant — Q4_0 is recommended for llama.cpp on Hexagon NPU. See Precisions (Quantizations) Supported.

Streaming

Print each delta as it arrives:

python
stream = client.chat.completions.create(
    model="unsloth/Qwen3-4B-GGUF:Q4_0",
    messages=[
        {"role": "user", "content": "Hello! Briefly introduce yourself."},
    ],
    max_tokens=256,
    temperature=0.7,
    stream=True,
)

for chunk in stream:
    delta = chunk.choices[0].delta.content
    if delta:
        print(delta, end="", flush=True)
print()

Chat completion (non-streaming)

Single request, single response, no streaming — the standard OpenAI chat.completions.create shape. The enable_think=False extra parameter turns off Qwen3's default <think>…</think> reasoning prefix so the reply content stays clean.

python
resp = client.chat.completions.create(
    model="unsloth/Qwen3-4B-GGUF:Q4_0",
    messages=[
        {"role": "user", "content": "Hello! Briefly introduce yourself."},
    ],
    max_tokens=128,
    temperature=0.7,
    extra_body={"enable_think": False},
)

print(resp.choices[0].message.content)
print("finish_reason:", resp.choices[0].finish_reason)
print("usage:", resp.usage)

Output:

text
Hello! I'm Qwen, a large language model developed by Alibaba Cloud. I can help with a wide range of tasks, including answering questions, writing articles, creating stories, and more. I'm here to assist you in any way I can! How can I help you today?
finish_reason: stop
usage: CompletionUsage(completion_tokens=58, prompt_tokens=19, total_tokens=77, ...)

Tool calling

Function/tool calling uses the standard OpenAI tools schema. The server extracts the tool call from the model's generated text (<tool_call>…</tool_call> tags or a fenced ```json block, produced by chat templates such as Qwen3's) and re-emits it as OpenAI tool_calls.

<Note> Only one tool call per assistant turn is parsed — parallel tool calls in a single response are not supported. </Note>

Two-step round trip: (1) the model returns a tool_calls message, (2) you execute the tool locally and feed the result back as a role="tool" message so the model can produce the final answer.

python
import json

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get the current weather in a given city.",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {"type": "string", "description": "City name, e.g. Beijing"},
                },
                "required": ["city"],
            },
        },
    }
]

def get_weather(city: str) -> dict:
    # replace this with your real implementation
    return {"city": city, "temperature_c": 24, "condition": "Sunny"}

messages = [
    {"role": "user", "content": "What is the weather in Beijing? Use the tool."},
]

# Step 1 — model requests a tool call.
first = client.chat.completions.create(
    model="unsloth/Qwen3-4B-GGUF:Q4_0",
    messages=messages,
    tools=tools,
    tool_choice="auto",
    max_tokens=256,
    extra_body={"enable_think": False},
)
call = first.choices[0].message.tool_calls[0]
print("finish_reason:", first.choices[0].finish_reason)  # -> "tool_calls"
print("call:", call.function.name, call.function.arguments)

# Step 2 — run the tool, feed the result back.
result = get_weather(**json.loads(call.function.arguments))

messages.append(first.choices[0].message)
messages.append(
    {
        "role": "tool",
        "tool_call_id": call.id,
        "content": json.dumps(result),
    }
)

final = client.chat.completions.create(
    model="unsloth/Qwen3-4B-GGUF:Q4_0",
    messages=messages,
    tools=tools,
    max_tokens=128,
    extra_body={"enable_think": False},
)
print(final.choices[0].message.content)

Output:

text
finish_reason: tool_calls
call: get_weather {"city": "Beijing"}
The weather in Beijing is 24°C and sunny.

Other endpoints

  • GET /v1/models — list available models.
  • GET /v1/models/{model} — get info about a specific model.
<Feedback/>