Compare commits

Author SHA1 Message Date
asepharyanaandClaude Opus 5 7cec411cba fix(stream): robust reasoning/content split + flush un-tagged output
The incremental </think> search missed tags split across tokens, inverting
reasoning/content classification. Detect the first </think> on the full
buffer and track the content boundary as a byte offset. If the model never
closes </think>, flush the buffered text as content so clients always
receive the response. Chat UI now renders reasoning_content too.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-03 11:03:26 +07:00
asepharyanaandClaude Opus 5 b636496497 refactor(chat): unify generation flow and fix tool/streaming bugs
- Unified synchronous generate() core with callback; both streaming and
  non-streaming paths run it via spawn_blocking (context: std Mutex).
- build_prompt now passes tool definitions to the template (was dead) and
  embeds assistant tool-call history as XML matching the parser format;
  fixes double <tool_response> wrap and template set-scoping bug.
- Tokenize with AddBos::Never (template owns <s>) to remove double BOS.
- Streaming: preserve inter-word spaces (per-chunk trim removed), add
  [DONE] + usage chunk, emit error events, single-shot tool_calls delta.
- Strict model validation (400 on unknown model); health/UI/README aligned
  to minicpm5-1b-fable5-v2-thinking; auth returns JSON errors; n_ctx/
  n_batch/n_threads env-configurable.
- Added 18 unit tests; cargo check/clippy/fmt clean.
- scripts/smoke-test.sh for post-deploy verification on the VPS.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-03 08:58:13 +07:00
asepharyana 344bc195fa docs: README Nix+Caddy deploy 2026-08-02 16:44:43 +07:00
asepharyana 81c5177249 chore: port 8080 to 4010 2026-08-02 16:18:17 +07:00
Asep Haryana 67861f384b fix(stream): add anti-buffer headers for real SSE streaming
Notify Parent Repo / dispatch (push) Canceled after 0s
- Add X-Accel-Buffering: no, Cache-Control: no-cache, Connection: keep-alive
- Prevents Traefik/proxy from buffering SSE events
- Each token is flushed immediately as generated
2026-07-26 19:13:00 +07:00
Asep Haryana 5cba76d280 fix(stream): separate reasoning_content from content in SSE streaming
- Before </think>: tokens sent as delta.reasoning_content
- After </think>: tokens sent as delta.content
- Handles </think> split across token boundaries
- Clean <|im_start|>/<|im_end|>/<think>/</think> from streamed text
- Normal models (no thinking) send all as content as before
2026-07-26 19:04:29 +07:00
Asep Haryana 6ff31b5f62 feat: render GGUF Jinja template via minijinja crate
- Replaced manual prompt building with GGUF's chat template rendered through minijinja (Rust Jinja2 engine)
- Simplified template: removed multi-step tool detection, reasoning extraction (not needed at template level)
- Added reasoning_content separation: clean_text() returns (reasoning, answer) tuple
- Added reasoning_content field to ResponseMessage and SseDelta for OpenAI-compatible output
- Embedded template at build time via include_str! from templates/chat_template.jinja
- Built-in support for enable_thinking, tool_definitions, tool_calls, tool_response
2026-07-26 18:21:35 +07:00
Asep Haryana 9a63ff1601 feat: use GGUF chat template via apply_chat_template
- Replaced manual prompt building with LlamaModel::apply_chat_template
- Uses model's baked-in Jinja template (system/user/assistant/tool format)
- Added <think> trigger after template for thinking mode
- Clean_text strips only <|im_end|>, <|im_start|>, <think>, </think>
2026-07-26 17:10:42 +07:00
Asep Haryana 59c77108a5 fix: skip leading EOS tokens in streaming + non-streaming
- Skip <|im_end|> generated as first token (prevents empty responses)
- Clean <think> tags as plain text in generated output
2026-07-26 16:09:04 +07:00
Asep Haryana 495b9ed126 fix: remove /think trigger, add back <think> in clean_text
- Removed /think trigger from prompt (model generates better without it)
- Added <think> and </think> back to clean_text (model uses plain text
  thinking tags, not special tokens)
2026-07-26 16:03:18 +07:00
Asep Haryana 254532458b Revert "fix: remove /think trigger from prompt, use plain assistant prefix"
This reverts commit 7c8f747faf.
2026-07-26 15:49:04 +07:00
Asep Haryana 7c8f747faf fix: remove /think trigger from prompt, use plain assistant prefix 2026-07-26 15:48:44 +07:00
Asep Haryana d8425ea3b3 feat: switch to MiniCPM5-1B-Claude-Opus-Fable5-V2-Thinking-Q8_0 GGUF
- Updated model path + model ID for new 1B thinking model
- Updated prompt builder to use MiniCPM5 native /think trigger
- Updated clean_text to strip MiniCPM5 special tokens
- Bumped n_ctx from 2048 to 8192
2026-07-26 15:38:05 +07:00
asepharyana 14032db705 ci: add workflow_dispatch to notify-parent for manual triggering 2026-07-25 18:13:35 +07:00
asepharyanaandClaude Code 2a5ab8b6d5 ci(llm-api): add notify-parent workflow
Trigger repository_dispatch to parent hub repo on push to master.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-07-25 15:11:38 +07:00
asepharyana 7e717cc808 Merge pull request #1 from asepharyana/refactor/clean-architecture
refactor(llm-api): implement clean architecture following scraper pat…
2026-07-25 15:09:04 +07:00
17 changed files with 1146 additions and 370 deletions
+25
View File
@@ -0,0 +1,25 @@
name: Notify Parent Repo
on:
push:
branches:
- master
workflow_dispatch:
jobs:
dispatch:
runs-on: ubuntu-latest
steps:
- name: Trigger root monorepo build
uses: peter-evans/repository-dispatch@v3
with:
token: ${{ secrets.DISPATCH_TOKEN }}
repository: asepharyana/asepharyana-hub
event-type: submodule-updated
client-payload: |
{
"service": "llm-api",
"ref": "${{ github.ref }}",
"sha": "${{ github.sha }}",
"actor": "${{ github.actor }}"
}
Generated
+17
View File
@@ -568,6 +568,7 @@ dependencies = [
"chrono", "chrono",
"futures", "futures",
"llama-cpp-2", "llama-cpp-2",
"minijinja",
"serde", "serde",
"serde_json", "serde_json",
"thiserror 1.0.69", "thiserror 1.0.69",
@@ -615,12 +616,28 @@ version = "2.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
[[package]]
name = "memo-map"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38d1115007560874e373613744c6fba374c17688327a71c1476d1a5954cc857b"
[[package]] [[package]]
name = "mime" name = "mime"
version = "0.3.17" version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
[[package]]
name = "minijinja"
version = "2.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb3d648e68cea56d9858d535ee28f9538404e2dd8cb08ed0bd05dca379477f39"
dependencies = [
"memo-map",
"serde",
]
[[package]] [[package]]
name = "minimal-lexical" name = "minimal-lexical"
version = "0.2.1" version = "0.2.1"
+1
View File
@@ -6,6 +6,7 @@ edition = "2021"
[dependencies] [dependencies]
# LLM inference # LLM inference
llama-cpp-2 = "0.1" llama-cpp-2 = "0.1"
minijinja = "2"
# HTTP server # HTTP server
axum = { version = "0.8", features = ["json"] } axum = { version = "0.8", features = ["json"] }
+32 -8
View File
@@ -2,7 +2,7 @@
OpenAI-compatible LLM inference server using `llama-cpp-2` (Rust). OpenAI-compatible LLM inference server using `llama-cpp-2` (Rust).
**Model:** MiniCPM-V-4.6 Q4_K_M (505 MB) **Model:** MiniCPM5-1B-Claude-Opus-Fable5-V2-Thinking (Q8_0)
**Engine:** llama.cpp via `llama-cpp-2` crate **Engine:** llama.cpp via `llama-cpp-2` crate
**Domain:** [ai.asepharyana.my.id](https://ai.asepharyana.my.id) **Domain:** [ai.asepharyana.my.id](https://ai.asepharyana.my.id)
@@ -10,7 +10,7 @@ OpenAI-compatible LLM inference server using `llama-cpp-2` (Rust).
### `GET /health` ### `GET /health`
```json ```json
{"status": "ok", "model": "minicpm-v-4.6-q4_k_m"} {"status": "ok", "model": "minicpm5-1b-fable5-v2-thinking"}
``` ```
### `GET /v1/models` ### `GET /v1/models`
@@ -19,11 +19,13 @@ OpenAI-compatible model listing.
### `POST /v1/chat/completions` ### `POST /v1/chat/completions`
OpenAI-compatible chat completions. OpenAI-compatible chat completions.
The server serves a single model and rejects unknown model ids with `400`:
```bash ```bash
curl https://ai.asepharyana.my.id/v1/chat/completions \ curl https://ai.asepharyana.my.id/v1/chat/completions \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d '{ -d '{
"model": "minicpm-v-4.6", "model": "minicpm5-1b-fable5-v2-thinking",
"messages": [{"role": "user", "content": "Hello!"}], "messages": [{"role": "user", "content": "Hello!"}],
"max_tokens": 100 "max_tokens": 100
}' }'
@@ -42,14 +44,37 @@ MODEL_PATH=/path/to/model.gguf ./target/release/llm-api
./target/release/llm-api ./target/release/llm-api
``` ```
## Docker ### Environment variables
| Var | Default | Description |
|-----|---------|-------------|
| `MODEL_PATH` | `/models/MiniCPM5-1B-Claude-Opus-Fable5-V2-Thinking-Q8_0.gguf` | GGUF model file |
| `API_KEY` | *(empty = auth off)* | Bearer token required on `/v1/chat/completions` |
| `SERVER_PORT` | `4010` | Listen port |
| `RUST_LOG` | `info` | Log level |
| `N_CTX` / `N_BATCH` / `N_THREADS` | `8192` / `512` / `4` | llama.cpp context/batch/threads |
### Smoke test (setelah deploy)
```bash ```bash
docker compose -f docker-compose.yml up -d ./scripts/smoke-test.sh http://127.0.0.1:4010 # tanpa auth
./scripts/smoke-test.sh https://ai.asepharyana.my.id "$API_KEY"
``` ```
## Deploy (Nix + systemd)
```bash
nix build .#default --impure --option sandbox false
# GitHub Actions: nix copy ssh://vps → systemctl restart llm-api
```
> **Legacy (2026-08-02):** Docker compose dihapus dari produksi. Deploy sekarang Nix+systemd.
## Benchmark ## Benchmark
> *Historic* (MiniCPM-V-4.6). Kept for reference; numbers predate the current
> MiniCPM5-1B Thinking model.
| Framework | Model Size | tok/s | vs PyTorch | | Framework | Model Size | tok/s | vs PyTorch |
|-----------|-----------|-------|------------| |-----------|-----------|-------|------------|
| PyTorch BF16 | 2.48 GB | 0.97 | 1.0x | | PyTorch BF16 | 2.48 GB | 0.97 | 1.0x |
@@ -57,6 +82,5 @@ docker compose -f docker-compose.yml up -d
## Infrastructure ## Infrastructure
- Traefik router: `ai.asepharyana.my.id``llm-api:8080` - Caddy reverse proxy: `ai.asepharyana.my.id``127.0.0.1:4010`
- Network: `app-shared-net` - systemd unit `llm-api`, deploy Nix via GitHub Actions
- Docker Compose: see `llm-api.yml`
+104
View File
@@ -0,0 +1,104 @@
#!/usr/bin/env bash
# Smoke test untuk llm-api — jalankan di VPS setelah deploy.
#
# Memverifikasi alur inti:
# /health, /v1/models, chat non-streaming, chat streaming (SSE + [DONE]),
# penolakan model tidak dikenal (400), dan request dengan tools.
#
# Usage:
# ./scripts/smoke-test.sh [BASE_URL] [API_KEY]
# BASE_URL default: http://127.0.0.1:4010
set -euo pipefail
BASE_URL="${1:-http://127.0.0.1:4010}"
API_KEY="${2:-}"
MODEL_ID="minicpm5-1b-fable5-v2-thinking"
AUTH=()
if [[ -n "$API_KEY" ]]; then
AUTH=(-H "Authorization: Bearer $API_KEY")
fi
pass=0
fail=0
check() {
local name="$1" ok="$2"
if [[ "$ok" == "0" ]]; then
echo " PASS: $name"
pass=$((pass + 1))
else
echo " FAIL: $name"
fail=$((fail + 1))
fi
}
echo "== 1. Health =="
health=$(curl -sf "$BASE_URL/health") || { echo "FAIL: /health unreachable"; exit 1; }
echo "$health" | jq -e ".status == \"ok\" and .model == \"$MODEL_ID\"" >/dev/null
check "health melaporkan $MODEL_ID" $?
echo "== 2. Models =="
models=$(curl -sf "$BASE_URL/v1/models")
echo "$models" | jq -e ".data[0].id == \"$MODEL_ID\"" >/dev/null
check "models mencantumkan $MODEL_ID" $?
echo "== 3. Chat non-streaming =="
resp=$(curl -sf "${AUTH[@]}" -H "Content-Type: application/json" \
-d "{\"model\":\"$MODEL_ID\",\"messages\":[{\"role\":\"user\",\"content\":\"Say hi\"}],\"max_tokens\":64}" \
"$BASE_URL/v1/chat/completions")
echo "$resp" | jq -e '.choices[0].message.content | type == "string"' >/dev/null
check "non-streaming mengembalikan content" $?
echo "$resp" | jq -e '.usage.total_tokens > 0' >/dev/null
check "non-streaming berisi usage" $?
echo "== 4. Chat streaming =="
stream=$(curl -sfN "${AUTH[@]}" -H "Content-Type: application/json" \
-d "{\"model\":\"$MODEL_ID\",\"messages\":[{\"role\":\"user\",\"content\":\"Count from 1 to 5\"}],\"stream\":true,\"max_tokens\":64}" \
"$BASE_URL/v1/chat/completions")
echo "$stream" | grep -q '\[DONE\]'
check "stream diakhiri [DONE]" $?
echo "$stream" | grep -q '"finish_reason":"stop"\|"finish_reason":"length"'
check "stream punya finish_reason" $?
# Gabungkan semua delta content untuk memastikan output tidak kosong
# dan tidak bocor markup (mis. <tool_call> / <|im_end|>).
joined=$(echo "$stream" | grep '^data: ' | sed 's/^data: //' \
| grep -v '\[DONE\]' \
| jq -r 'select((.choices? // []) | length > 0) | .choices[0].delta.content // empty' 2>/dev/null \
| tr -d '\n')
if [[ -z "$joined" ]]; then
# Sebagian output mungkin semua ber-label reasoning_content; cek keduanya.
joined=$(echo "$stream" | grep '^data: ' | sed 's/^data: //' \
| grep -v '\[DONE\]' \
| jq -r 'select((.choices? // []) | length > 0) | (.choices[0].delta.content // .choices[0].delta.reasoning_content) // empty' 2>/dev/null \
| tr -d '\n')
fi
[[ -n "$joined" ]]
check "stream menghasilkan teks" $?
if [[ -n "$joined" ]]; then
if [[ "$joined" == *"<"* ]]; then
check "tidak ada markup bocor di stream" 1
else
check "tidak ada markup bocor di stream" 0
fi
fi
echo "== 5. Model tidak dikenal ditolak =="
status=$(curl -s -o /dev/null -w "%{http_code}" "${AUTH[@]}" -H "Content-Type: application/json" \
-d '{"model":"minicpm-v-4.6","messages":[{"role":"user","content":"hi"}]}' \
"$BASE_URL/v1/chat/completions")
[[ "$status" == "400" ]]
check "unknown model -> 400" $?
echo "== 6. Request dengan tools diterima =="
status2=$(curl -s -o /dev/null -w "%{http_code}" "${AUTH[@]}" -H "Content-Type: application/json" \
-d "{\"model\":\"$MODEL_ID\",\"messages\":[{\"role\":\"user\",\"content\":\"What's the weather in Jakarta?\"}],\"tools\":[{\"type\":\"function\",\"function\":{\"name\":\"get_weather\",\"description\":\"Get weather\",\"parameters\":{\"type\":\"object\",\"properties\":{\"city\":{\"type\":\"string\"}}}}}],\"max_tokens\":128}" \
"$BASE_URL/v1/chat/completions")
[[ "$status2" == "200" ]]
check "tools request sukses (200)" $?
echo
echo "Result: $pass passed, $fail failed"
[[ $fail -eq 0 ]]
+4 -1
View File
@@ -1,3 +1,6 @@
pub mod use_cases; pub mod use_cases;
pub use use_cases::{build_prompt, build_sampler, clean_text, parse_tool_calls, SamplerParams}; pub use use_cases::{
build_prompt, build_sampler, clean_text, parse_tool_calls, split_stream_chunk, strip_markup,
validate_model, SamplerParams,
};
@@ -0,0 +1,44 @@
{{- bos_token }}
{%- if tools %}
{%- set tool_definitions %}
{{- "# Tools\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
{%- for tool in tools %}
{{- "\n" }}
{{- tool | tojson }}
{%- endfor %}
{{- '\n</tools>\n\nTool usage guidelines:\n- You may call zero or more functions. If no function calls are needed, just answer normally.\n- When calling a function, wrap each call in <tool_call> tags:\n<tool_call>\n<function=name>\n<parameter=key>value</parameter>\n</function>\n</tool_call>' }}
{%- endset %}
{{- '<|im_start|>system\n' }}
{%- if messages[0].role == 'system' %}
{{- messages[0].content + '\n\n' + tool_definitions }}
{%- else %}
{{- tool_definitions }}
{%- endif %}
{{- '<|im_end|>\n' }}
{%- else %}
{%- if messages[0].role == 'system' %}
{{- '<|im_start|>system\n' + messages[0].content + '<|im_end|>\n' }}
{%- endif %}
{%- endif %}
{%- for message in messages %}
{%- set content = message.content %}
{%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
{%- elif message.role == "assistant" %}
{#- Tool-call XML from history is pre-embedded in content by build_prompt. #}
{{- '<|im_start|>assistant\n' + content + '<|im_end|>\n' }}
{%- elif message.role == "tool" %}
{{- '<|im_start|>user\n<tool_response>\n' + content + '\n</tool_response><|im_end|>\n' }}
{%- endif %}
{%- endfor %}
{%- if add_generation_prompt %}
{{- '<|im_start|>assistant\n' }}
{%- if enable_thinking is defined %}
{%- if enable_thinking is false %}
{{- '<think>\n\n</think>\n\n' }}
{%- elif enable_thinking is true %}
{{- '<think>\n' }}
{%- endif %}
{%- endif %}
{%- endif %}
+472 -94
View File
@@ -1,97 +1,119 @@
//! Chat completion use cases. //! Chat completion use cases.
//! //!
//! Orchestrates prompt building, sampler construction, and output parsing. //! Orchestrates prompt building using the model's baked-in Jinja template
//! These are pure functions with no framework dependencies. //! via the `minijinja` crate, sampler construction, and output parsing.
use std::collections::HashMap;
use llama_cpp_2::sampling::LlamaSampler; use llama_cpp_2::sampling::LlamaSampler;
use minijinja::{Environment, Value};
use crate::domain::entity::{ChatMessage, ChatRequest, ToolCall, ToolCallFunction, ToolDef}; use crate::domain::entity::{ChatMessage, ChatRequest, ToolCall, ToolCallFunction};
/// Build a prompt string from conversation messages and optional tool definitions. /// Build a prompt from messages using the project's Jinja chat template.
/// ///
/// Uses ChatML format with `<|im_start|>` / `<|im_end|>` delimiters. Tool /// Renders the template (see `templates/chat_template.jinja`) via minijinja,
/// definitions are injected into the first system message. /// passing the message history, optional tool definitions, and the
pub fn build_prompt(messages: &[ChatMessage], tools: &Option<Vec<ToolDef>>) -> String { /// generation-prompt switches. The template owns the `<s>` BOS token, so
let mut prompt = String::new(); /// tokenization must NOT add another one (`AddBos::Never`).
pub fn build_prompt(
messages: &[ChatMessage],
tools: &Option<Vec<crate::domain::entity::ToolDef>>,
) -> Result<String, String> {
let template_str = include_str!("templates/chat_template.jinja");
for (i, msg) in messages.iter().enumerate() { let mut env = Environment::new();
match msg.role.as_str() { env.add_template("chat", template_str)
"system" => { .map_err(|e| format!("Template add error: {e}"))?;
let mut content = msg.content.clone().unwrap_or_default();
// Inject tools into the system message (first occurrence) // Register tojson filter (safe: Rust serde_json defaults to ensure_ascii=false)
if i == 0 { env.add_filter("tojson", |value: &Value| -> String {
if let Some(tools_list) = tools { serde_json::to_string(value).unwrap_or_default()
if !tools_list.is_empty() { });
let mut tools_text = String::from(
"\n\n# Tools\n\nYou have access to the following functions:\n\n<tools>", let tmpl = env
); .get_template("chat")
for tool in tools_list { .map_err(|e| format!("Template get error: {e}"))?;
tools_text.push('\n');
tools_text.push_str( // Build messages as serde_json::Value for minijinja.
&serde_json::to_string(tool).unwrap_or_default(), // Assistant tool calls from history are embedded directly into the content
); // as XML (in the exact format the model is told to emit) — the template
} // cannot accumulate `set` variables across a loop, so this is built here.
tools_text.push_str( let mut msgs_val: Vec<Value> = Vec::new();
"\n</tools>\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>", for msg in messages {
); let mut m: HashMap<String, Value> = HashMap::new();
content.push_str(&tools_text); m.insert("role".into(), Value::from(msg.role.clone()));
}
} let mut content = msg.content.clone().unwrap_or_default();
}
prompt.push_str(&format!("<|im_start|>system\n{}<|im_end|>\n", content)); if msg.role == "assistant" {
} if let Some(tcs) = &msg.tool_calls {
"user" => { for tc in tcs {
let content = msg.content.as_deref().unwrap_or(""); if tc.call_type == "function" {
if msg.tool_call_id.is_some() || msg.name.is_some() {
prompt.push_str(&format!(
"<|im_start|>user\n<tool_response>\n{}\n</tool_response><|im_end|>\n",
content content
)); .push_str(&format!("\n<tool_call>\n<function={}>\n", tc.function.name));
} else {
prompt.push_str(&format!("<|im_start|>user\n{}<|im_end|>\n", content));
}
}
"assistant" => {
let content = msg.content.as_deref().unwrap_or("");
if let Some(tcs) = &msg.tool_calls {
let mut asst = format!("<|im_start|>assistant\n{}", content);
for tc in tcs {
let args: serde_json::Value = let args: serde_json::Value =
serde_json::from_str(&tc.function.arguments).unwrap_or_default(); serde_json::from_str(&tc.function.arguments).unwrap_or_default();
asst.push_str(&format!(
"<tool_call>\n<function={}>\n",
tc.function.name
));
if let Some(obj) = args.as_object() { if let Some(obj) = args.as_object() {
for (k, v) in obj { for (k, v) in obj {
let val = match v { // Strings stay raw (no JSON quotes) so they round-trip
// through parse_tool_calls unchanged.
let rendered = match v {
serde_json::Value::String(s) => s.clone(), serde_json::Value::String(s) => s.clone(),
other => serde_json::to_string(other).unwrap_or_default(), other => other.to_string(),
}; };
asst.push_str(&format!("<parameter={}>\n{}\n</parameter>\n", k, val)); content
.push_str(&format!("<parameter={k}>{rendered}</parameter>\n"));
} }
} }
asst.push_str("</function>\n</tool_call>"); content.push_str("</function>\n</tool_call>");
} }
asst.push_str("<|im_end|>\n");
prompt.push_str(&asst);
} else {
prompt.push_str(&format!(
"<|im_start|>assistant\n{}<|im_end|>\n",
content
));
} }
} }
_ => { }
let content = msg.content.as_deref().unwrap_or("");
prompt.push_str(&format!("<|im_start|>user\n{}<|im_end|>\n", content)); // NOTE: the template wraps tool-role content in <tool_response>; do not
} // wrap here or it would be double-wrapped.
m.insert("content".into(), Value::from(content));
msgs_val.push(Value::from(m));
}
// BOS token for sentencepiece / unigram models (template-owned).
let bos_token: &str = "<s>";
// Build context
let mut ctx: HashMap<String, Value> = HashMap::new();
ctx.insert("bos_token".into(), Value::from(bos_token));
ctx.insert("messages".into(), Value::from(msgs_val));
ctx.insert("add_generation_prompt".into(), Value::from(true));
ctx.insert("enable_thinking".into(), Value::from(true));
// Tool definitions (optional) — previously dead, now actually rendered.
if let Some(tools) = tools {
if !tools.is_empty() {
let tools_val: Vec<Value> = tools.iter().map(Value::from_serialize).collect();
ctx.insert("tools".into(), Value::from(tools_val));
} }
} }
// Generation prompt: non-thinking mode // Render
prompt.push_str("<|im_start|>assistant\n<think>\n\n</think>\n\n"); let result = tmpl
prompt .render(&ctx)
.map_err(|e| format!("Template render error: {e}"))?;
Ok(result)
}
/// Validate that the requested model matches the single served model.
pub fn validate_model(model: &str) -> Result<(), String> {
if model != crate::config::MODEL_ID {
return Err(format!(
"Unknown model '{model}'. Available: {}",
crate::config::MODEL_ID
));
}
Ok(())
} }
/// Parameters for building a [`LlamaSampler`] chain. /// Parameters for building a [`LlamaSampler`] chain.
@@ -135,7 +157,6 @@ pub fn build_sampler(params: &SamplerParams) -> LlamaSampler {
let seed = params.seed; let seed = params.seed;
let mut samplers: Vec<LlamaSampler> = Vec::new(); let mut samplers: Vec<LlamaSampler> = Vec::new();
// Repetition/frequency/presence penalties
let repeat = repeat_penalty.unwrap_or(1.0); let repeat = repeat_penalty.unwrap_or(1.0);
let freq = frequency_penalty.unwrap_or(0.0); let freq = frequency_penalty.unwrap_or(0.0);
let present = presence_penalty.unwrap_or(0.0); let present = presence_penalty.unwrap_or(0.0);
@@ -143,22 +164,18 @@ pub fn build_sampler(params: &SamplerParams) -> LlamaSampler {
samplers.push(LS::penalties(64, repeat, freq, present)); samplers.push(LS::penalties(64, repeat, freq, present));
} }
// top_k
if let Some(k) = top_k { if let Some(k) = top_k {
samplers.push(LS::top_k(k as i32)); samplers.push(LS::top_k(k as i32));
} }
// top_p
if let Some(p) = top_p { if let Some(p) = top_p {
samplers.push(LS::top_p(p, 1)); samplers.push(LS::top_p(p, 1));
} }
// min_p
if let Some(p) = min_p { if let Some(p) = min_p {
samplers.push(LS::min_p(p, 1)); samplers.push(LS::min_p(p, 1));
} }
// Temperature + final selector
let temp = temperature.unwrap_or(0.0); let temp = temperature.unwrap_or(0.0);
if temp <= 0.0 { if temp <= 0.0 {
samplers.push(LS::greedy()); samplers.push(LS::greedy());
@@ -173,19 +190,36 @@ pub fn build_sampler(params: &SamplerParams) -> LlamaSampler {
LlamaSampler::chain_simple(samplers) LlamaSampler::chain_simple(samplers)
} }
// ═══════════════════════════════════════════════════════════════ // ═══════════════════════════════════════════════════════════════
// TEXT PROCESSING // TEXT PROCESSING
// ═══════════════════════════════════════════════════════════════ // ═══════════════════════════════════════════════════════════════
/// Remove special tokens from generated text. /// Remove special tokens from generated text and separate reasoning.
pub fn clean_text(text: &str) -> String { ///
text.replace("<|im_end|>", "") /// For thinking models, returns (reasoning, cleaned_answer).
.replace("<|im_start|>", "") pub fn clean_text(text: &str) -> (String, String) {
.replace("<think>", "") let text = text.replace("<|im_end|>", "").replace("<|im_start|>", "");
.replace("</think>", "")
.trim() // Separate reasoning (between <think>/</think>) from answer
.to_string() let text = text.trim();
let (reasoning, answer) = if let Some(close_idx) = text.find("</think>") {
let reasoning = text[..close_idx]
.trim()
.trim_start_matches("<think>")
.trim()
.to_string();
let answer = text[close_idx + 8..].trim().to_string();
(reasoning, answer)
} else if text.contains("<think>") {
// Still thinking — everything is reasoning
let reasoning = text.trim_start_matches("<think>").trim().to_string();
(reasoning, String::new())
} else {
(String::new(), text.to_string())
};
let answer = answer.replace("<think>", "").replace("</think>", "");
(reasoning, answer.trim().to_string())
} }
/// Parse tool calls from generated text in the format: /// Parse tool calls from generated text in the format:
@@ -240,9 +274,9 @@ pub fn parse_tool_calls(text: &str) -> (String, Vec<ToolCall>) {
for line in lines { for line in lines {
let line = line.trim(); let line = line.trim();
if let Some(param) = if let Some(param) = line
line.strip_prefix("<parameter=") .strip_prefix("<parameter=")
.and_then(|s| s.strip_suffix('>')) .and_then(|s| s.strip_suffix('>'))
{ {
if let Some(p) = current_param.take() { if let Some(p) = current_param.take() {
args_map.insert( args_map.insert(
@@ -264,9 +298,11 @@ pub fn parse_tool_calls(text: &str) -> (String, Vec<ToolCall>) {
continue; continue;
} }
} }
// Save last param
if let Some(p) = current_param.take() { if let Some(p) = current_param.take() {
args_map.insert(p, serde_json::Value::String(current_value.trim().to_string())); args_map.insert(
p,
serde_json::Value::String(current_value.trim().to_string()),
);
} }
let args_json = serde_json::Value::Object(args_map).to_string(); let args_json = serde_json::Value::Object(args_map).to_string();
@@ -283,12 +319,354 @@ pub fn parse_tool_calls(text: &str) -> (String, Vec<ToolCall>) {
idx = end; idx = end;
} }
// Remove tool_call blocks from the text
clean = clean.replace("<tool_call>", "").replace("</tool_call>", ""); clean = clean.replace("<tool_call>", "").replace("</tool_call>", "");
// XML-like tags are already fully parsed; remaining text is the content
clean = clean.trim().to_string(); clean = clean.trim().to_string();
// Strip remaining XML tags that aren't part of clean let (_reasoning, cleaned) = clean_text(&clean);
let cleaned = clean_text(&clean);
(cleaned, tool_calls) (cleaned, tool_calls)
} }
// ═══════════════════════════════════════════════════════════════
// STREAMING CHUNK SPLITTING
// ═══════════════════════════════════════════════════════════════
/// Remove special tokens and tool-call XML markup from a text fragment.
///
/// Handles both fixed tags (`<|im_end|>`, `<think>`, `<tool_call>`, …) and the
/// attribute-bearing openers used by this model's tool format (`<function=…>`,
/// `<parameter=…>`), even when a tag straddles a token boundary.
pub fn strip_markup(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut rest = text;
while !rest.is_empty() {
let Some(idx) = rest.find('<') else {
out.push_str(rest);
break;
};
out.push_str(&rest[..idx]);
let tail = &rest[idx..];
// Fixed tags (no attribute content).
let fixed = [
"<|im_start|>",
"<|im_end|>",
"<think>",
"</think>",
"<tool_call>",
"</tool_call>",
"</function>",
"</parameter>",
];
if let Some(tag) = fixed.iter().find(|t| tail.starts_with(**t)) {
rest = &tail[tag.len()..];
continue;
}
// Attribute-bearing openers: <function=…> / <parameter=…>.
if let Some(attr) = tail
.strip_prefix("<function=")
.or_else(|| tail.strip_prefix("<parameter="))
{
if let Some(end) = attr.find('>') {
rest = &attr[end + 1..];
continue;
}
}
// Not a known tag — keep this char and advance one UTF-8 char.
let ch = tail.chars().next().expect("non-empty tail");
out.push(ch);
rest = &tail[ch.len_utf8()..];
}
out
}
/// Compute `(reasoning, content)` deltas for an incremental streamed fragment.
///
/// * `new_text` — the not-yet-emitted fragment (`text_buf[sent_len..]`).
/// * `sent_len` — byte offset in the full buffer where `new_text` begins.
/// * `content_start` — byte offset in the full buffer where the content phase
/// begins (immediately after the first `</think>`); `None` while still
/// thinking.
///
/// The boundary is a position in the *full* buffer, not a string search in the
/// fragment — this stays correct even when `</think>` is split across tokens.
/// Whitespace inside a fragment is preserved; only the edges around the
/// boundary are trimmed. A stray second `</think>` is stripped, not re-split.
pub fn split_stream_chunk(
new_text: &str,
sent_len: usize,
content_start: Option<usize>,
) -> (Option<String>, String) {
match content_start {
// Still thinking — everything is reasoning.
None => {
let cleaned = strip_markup(new_text);
let reasoning = if cleaned.is_empty() {
None
} else {
Some(cleaned)
};
(reasoning, String::new())
}
// Boundary already emitted — everything is content.
Some(cs) if cs <= sent_len => (None, strip_markup(new_text)),
// Boundary falls inside this fragment (or beyond it, defensively).
Some(cs) => {
let rel = (cs - sent_len).min(new_text.len());
let (before, after) = new_text.split_at(rel);
let mut before = strip_markup(before);
let mut after = strip_markup(after);
while before.ends_with(['\n', ' ', '\t']) {
before.pop();
}
while after.starts_with(['\n', ' ', '\t']) {
after.remove(0);
}
let reasoning = if before.is_empty() {
None
} else {
Some(before)
};
(reasoning, after)
}
}
}
// ═══════════════════════════════════════════════════════════════
// TESTS
// ═══════════════════════════════════════════════════════════════
#[cfg(test)]
mod tests {
use super::*;
use crate::domain::entity::{
ChatMessage, ToolCallFunction, ToolCallResponse, ToolDef, ToolFunction,
};
fn msg(role: &str, content: &str) -> ChatMessage {
ChatMessage {
role: role.into(),
content: Some(content.into()),
tool_calls: None,
tool_call_id: None,
name: None,
}
}
fn tool_def() -> ToolDef {
ToolDef {
tool_type: "function".into(),
function: ToolFunction {
name: "get_weather".into(),
description: "Get weather for a city".into(),
parameters: serde_json::json!({
"type": "object",
"properties": {
"city": { "type": "string" }
},
"required": ["city"]
}),
},
}
}
// ── split_stream_chunk ──
#[test]
fn split_chunk_before_think_is_reasoning() {
let (reasoning, content) = split_stream_chunk("Hello ", 0, None);
assert_eq!(reasoning.as_deref(), Some("Hello "));
assert_eq!(content, "");
}
#[test]
fn split_chunk_preserves_internal_spaces() {
// Regression: trimming every fragment used to eat inter-word spaces.
let (r1, _) = split_stream_chunk("Hello", 0, None);
let (r2, _) = split_stream_chunk(" world", 5, None);
assert_eq!(format!("{}{}", r1.unwrap(), r2.unwrap()), "Hello world");
}
#[test]
fn split_chunk_boundary_trims_only_edges() {
// text_buf = "question\n</think>\n\nAnswer "; boundary right after the
// tag at byte 17.
let (reasoning, content) = split_stream_chunk("question\n</think>\n\nAnswer ", 0, Some(17));
assert_eq!(reasoning.as_deref(), Some("question"));
assert_eq!(content, "Answer ");
}
#[test]
fn split_chunk_after_think_is_content() {
let (reasoning, content) = split_stream_chunk(" answer", 0, Some(0));
assert_eq!(reasoning, None);
assert_eq!(content, " answer");
}
#[test]
fn split_chunk_stray_think_tag_is_stripped_not_split() {
// A second </think> (already past the boundary) must not restart
// reasoning classification.
let (reasoning, content) = split_stream_chunk("...</think>more", 0, Some(0));
assert_eq!(reasoning, None);
assert_eq!(content, "...more");
}
#[test]
fn split_chunk_boundary_straddling_tokens() {
// `</think>` split as "</think" + ">" across two fragments: the boundary
// is detected on the full buffer, so the answer still becomes content.
let (r1, _) = split_stream_chunk("reasoning...</think", 0, None);
assert!(r1.is_some());
let (r2, c2) = split_stream_chunk("\n\n2 + 2 = 4.", 18, Some(18));
assert_eq!(r2, None);
assert_eq!(c2, "\n\n2 + 2 = 4.");
}
#[test]
fn split_chunk_strips_special_and_markup() {
// boundary at byte 30 (right after "</think>").
let (reasoning, content) = split_stream_chunk(
"<|im_end|><think>Hello</think>\n<tool_call><function=get_weather>",
0,
Some(30),
);
assert_eq!(reasoning.as_deref(), Some("Hello"));
assert_eq!(content, "");
}
// ── clean_text ──
#[test]
fn clean_text_splits_reasoning_and_answer() {
let (reasoning, answer) =
clean_text("<think>Let me think\nabout it</think>\nThe answer is 42.");
assert_eq!(reasoning, "Let me think\nabout it");
assert_eq!(answer, "The answer is 42.");
}
#[test]
fn clean_text_no_think_returns_answer() {
let (reasoning, answer) = clean_text("Just an answer");
assert_eq!(reasoning, "");
assert_eq!(answer, "Just an answer");
}
// ── parse_tool_calls ──
#[test]
fn parse_single_tool_call() {
let text = "I'll look that up.\n<tool_call>\n<function=get_weather>\n<parameter=city>Jakarta</parameter>\n</function>\n</tool_call>";
let (cleaned, calls) = parse_tool_calls(text);
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].function.name, "get_weather");
assert!(calls[0].function.arguments.contains("Jakarta"));
assert!(!cleaned.contains("<tool_call>"));
}
#[test]
fn parse_multiple_tool_calls() {
let text = "<tool_call>\n<function=a>\n<parameter=x>1</parameter>\n</function>\n</tool_call>\n<tool_call>\n<function=b>\n<parameter=y>2</parameter>\n</function>\n</tool_call>";
let (_cleaned, calls) = parse_tool_calls(text);
assert_eq!(calls.len(), 2);
assert_eq!(calls[0].function.name, "a");
assert_eq!(calls[1].function.name, "b");
}
#[test]
fn parse_malformed_tool_call_returns_empty() {
let (cleaned, calls) = parse_tool_calls("no calls here");
assert!(calls.is_empty());
assert_eq!(cleaned, "no calls here");
}
// ── validate_model ──
#[test]
fn validate_model_accepts_served_id() {
assert!(validate_model(crate::config::MODEL_ID).is_ok());
}
#[test]
fn validate_model_rejects_unknown_id() {
assert!(validate_model("minicpm-v-4.6").is_err());
}
// ── build_sampler (smoke — no model required) ──
#[test]
fn build_sampler_constructs_for_common_params() {
let params = SamplerParams {
temperature: Some(0.8),
top_p: Some(0.9),
top_k: Some(40),
min_p: Some(0.05),
repeat_penalty: Some(1.1),
frequency_penalty: Some(0.0),
presence_penalty: Some(0.0),
seed: Some(42),
};
let _ = build_sampler(&params);
let greedy = SamplerParams {
temperature: Some(0.0),
..params
};
let _ = build_sampler(&greedy);
}
// ── build_prompt ──
#[test]
fn build_prompt_renders_messages() {
let messages = vec![msg("system", "You are helpful."), msg("user", "Hi!")];
let prompt = build_prompt(&messages, &None).unwrap();
assert!(prompt.starts_with("<s>"));
assert!(prompt.contains("<|im_start|>system\nYou are helpful."));
assert!(prompt.contains("<|im_start|>user\nHi!"));
assert!(prompt.contains("<|im_start|>assistant\n<think>\n"));
}
#[test]
fn build_prompt_includes_tool_definitions() {
let messages = vec![msg("user", "What's the weather?")];
let prompt = build_prompt(&messages, &Some(vec![tool_def()])).unwrap();
assert!(prompt.contains("<tools>"));
assert!(prompt.contains("get_weather"));
assert!(prompt.contains("Tool usage guidelines"));
}
#[test]
fn build_prompt_wraps_tool_response_once() {
let messages = vec![msg("user", "Weather?"), msg("tool", "Sunny")];
let prompt = build_prompt(&messages, &None).unwrap();
assert_eq!(prompt.matches("<tool_response>").count(), 1);
assert_eq!(prompt.matches("</tool_response>").count(), 1);
}
#[test]
fn build_prompt_renders_assistant_tool_calls_history() {
let assistant = ChatMessage {
role: "assistant".into(),
content: Some("".into()),
tool_calls: Some(vec![ToolCallResponse {
id: "call_1".into(),
call_type: "function".into(),
function: ToolCallFunction {
name: "get_weather".into(),
arguments: r#"{"city":"Jakarta"}"#.into(),
},
}]),
tool_call_id: None,
name: None,
};
let prompt = build_prompt(&[assistant], &None).unwrap();
assert!(prompt.contains("<function=get_weather>"));
assert!(prompt.contains("<parameter=city>Jakarta</parameter>"));
}
}
+4 -10
View File
@@ -32,15 +32,12 @@ impl Application {
pub async fn build() -> anyhow::Result<Self> { pub async fn build() -> anyhow::Result<Self> {
// Initialize tracing // Initialize tracing
let env_filter = EnvFilter::new(&CONFIG.log_level); let env_filter = EnvFilter::new(&CONFIG.log_level);
tracing_subscriber::fmt() tracing_subscriber::fmt().with_env_filter(env_filter).init();
.with_env_filter(env_filter)
.init();
tracing::info!("🚀 LLM API starting up..."); tracing::info!("🚀 LLM API starting up...");
// Load model (fail-fast) // Load model (fail-fast)
let engine = LlamaEngine::load().map_err(|e| { let engine = LlamaEngine::load()
anyhow::anyhow!("Failed to initialize LLM engine: {e}") .map_err(|e| anyhow::anyhow!("Failed to initialize LLM engine: {e}"))?;
})?;
let engine = Arc::new(engine); let engine = Arc::new(engine);
let state = Arc::new(AppState { engine }); let state = Arc::new(AppState { engine });
@@ -51,10 +48,7 @@ impl Application {
// Bind listener // Bind listener
let addr = format!("0.0.0.0:{}", CONFIG.server_port); let addr = format!("0.0.0.0:{}", CONFIG.server_port);
let listener = TcpListener::bind(&addr).await?; let listener = TcpListener::bind(&addr).await?;
tracing::info!( tracing::info!("Server listening on {}", listener.local_addr()?);
"Server listening on {}",
listener.local_addr()?
);
Ok(Self { Ok(Self {
port: CONFIG.server_port, port: CONFIG.server_port,
+14 -6
View File
@@ -4,8 +4,16 @@
use std::sync::LazyLock; use std::sync::LazyLock;
const DEFAULT_MODEL_PATH: &str = "/models/MiniCPM-V-4.6-Q4_K_M.gguf"; const DEFAULT_MODEL_PATH: &str = "/models/MiniCPM5-1B-Claude-Opus-Fable5-V2-Thinking-Q8_0.gguf";
pub const MODEL_ID: &str = "minicpm-v-4.6"; pub const MODEL_ID: &str = "minicpm5-1b-fable5-v2-thinking";
/// Read an env var as `T`, falling back to `default` on absence or parse error.
fn env_or<T: std::str::FromStr>(key: &str, default: T) -> T {
std::env::var(key)
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(default)
}
/// Application configuration loaded at startup from environment variables. /// Application configuration loaded at startup from environment variables.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@@ -42,11 +50,11 @@ impl AppConfig {
server_port: std::env::var("SERVER_PORT") server_port: std::env::var("SERVER_PORT")
.ok() .ok()
.and_then(|v| v.parse().ok()) .and_then(|v| v.parse().ok())
.unwrap_or(8080), .unwrap_or(4010),
log_level: std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string()), log_level: std::env::var("RUST_LOG").unwrap_or_else(|_| "info".to_string()),
n_ctx: 2048, n_ctx: env_or("N_CTX", 8192),
n_batch: 512, n_batch: env_or("N_BATCH", 512),
n_threads: 4, n_threads: env_or("N_THREADS", 4),
} }
} }
} }
+89 -1
View File
@@ -96,12 +96,15 @@ pub struct Choice {
#[derive(Serialize)] #[derive(Serialize)]
pub struct ResponseMessage { pub struct ResponseMessage {
pub role: String, pub role: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>, pub content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub reasoning_content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Vec<ToolCall>>, pub tool_calls: Option<Vec<ToolCall>>,
} }
#[derive(Serialize)] #[derive(Serialize, Clone)]
pub struct Usage { pub struct Usage {
pub prompt_tokens: u32, pub prompt_tokens: u32,
pub completion_tokens: u32, pub completion_tokens: u32,
@@ -136,6 +139,36 @@ pub struct ToolCallResponse {
pub function: ToolCallFunction, pub function: ToolCallFunction,
} }
// ═══════════════════════════════════════════════════════════════
// GENERATION FINISH REASON
// ═══════════════════════════════════════════════════════════════
/// Why a generation run stopped. Produced by the engine and rendered as the
/// OpenAI `finish_reason` at the presentation layer.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FinishReason {
/// Natural end-of-generation (EOS token) or a stop sequence matched.
Stop,
/// `max_tokens` exhausted.
Length,
/// A complete `<tool_call>` block was emitted.
ToolCalls,
/// Generation aborted early (e.g. client disconnected).
Aborted,
}
impl FinishReason {
/// Map to the OpenAI-compatible `finish_reason` string.
pub fn as_str(&self) -> &'static str {
match self {
FinishReason::Stop => "stop",
FinishReason::Length => "length",
FinishReason::ToolCalls => "tool_calls",
FinishReason::Aborted => "stop",
}
}
}
// ═══════════════════════════════════════════════════════════════ // ═══════════════════════════════════════════════════════════════
// SSE (STREAMING) TYPES // SSE (STREAMING) TYPES
// ═══════════════════════════════════════════════════════════════ // ═══════════════════════════════════════════════════════════════
@@ -148,6 +181,59 @@ pub struct SseChunk {
pub created: i64, pub created: i64,
pub model: String, pub model: String,
pub choices: Vec<SseChoice>, pub choices: Vec<SseChoice>,
#[serde(skip_serializing_if = "Option::is_none")]
pub usage: Option<Usage>,
}
impl SseChunk {
/// A delta chunk carrying partial reasoning/content/tool-call output.
pub fn delta(id: String, created: i64, model: String, delta: SseDelta) -> Self {
Self {
id,
object: "chat.completion.chunk".into(),
created,
model,
choices: vec![SseChoice {
index: 0,
delta,
finish_reason: None,
}],
usage: None,
}
}
/// The final chunk carrying the `finish_reason` (no token deltas).
pub fn finish(id: String, created: i64, model: String, finish_reason: &str) -> Self {
Self {
id,
object: "chat.completion.chunk".into(),
created,
model,
choices: vec![SseChoice {
index: 0,
delta: SseDelta {
role: None,
content: None,
tool_calls: None,
reasoning_content: None,
},
finish_reason: Some(finish_reason.into()),
}],
usage: None,
}
}
/// A trailing chunk with token usage and empty choices (OpenAI convention).
pub fn usage(id: String, created: i64, model: String, usage: Usage) -> Self {
Self {
id,
object: "chat.completion.chunk".into(),
created,
model,
choices: vec![],
usage: Some(usage),
}
}
} }
#[derive(Serialize)] #[derive(Serialize)]
@@ -165,6 +251,8 @@ pub struct SseDelta {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>, pub content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub reasoning_content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Vec<ToolCall>>, pub tool_calls: Option<Vec<ToolCall>>,
} }
+80 -57
View File
@@ -4,6 +4,7 @@
//! lifetime transmute), tokenization, and generation. //! lifetime transmute), tokenization, and generation.
use std::num::NonZeroU32; use std::num::NonZeroU32;
use std::sync::Mutex;
use llama_cpp_2::context::params::LlamaContextParams; use llama_cpp_2::context::params::LlamaContextParams;
use llama_cpp_2::context::LlamaContext; use llama_cpp_2::context::LlamaContext;
@@ -14,10 +15,10 @@ use llama_cpp_2::model::{AddBos, LlamaModel};
use llama_cpp_2::sampling::LlamaSampler; use llama_cpp_2::sampling::LlamaSampler;
use llama_cpp_2::token::LlamaToken; use llama_cpp_2::token::LlamaToken;
use llama_cpp_2::TokenToStringError; use llama_cpp_2::TokenToStringError;
use tokio::sync::Mutex;
use tracing::info; use tracing::info;
use crate::config::CONFIG; use crate::config::CONFIG;
use crate::domain::entity::FinishReason;
use crate::domain::LlmError; use crate::domain::LlmError;
// ── Thread-safe wrapper for raw llama.cpp context ── // ── Thread-safe wrapper for raw llama.cpp context ──
@@ -95,17 +96,28 @@ impl CtxInner {
// ── LlamaEngine ── // ── LlamaEngine ──
/// Outcome of a generation run.
pub struct GenerationOutcome {
/// Generated tokens (stop-sequence and EOG tokens are excluded).
pub tokens: Vec<LlamaToken>,
/// Accumulated decoded text (raw, before markup/special-token cleaning).
pub text: String,
/// Why generation stopped.
pub finish: FinishReason,
}
/// Safe interface to a llama.cpp model and inference context. /// Safe interface to a llama.cpp model and inference context.
/// ///
/// All access to the underlying context is serialized through a `Mutex`, /// All access to the underlying context is serialized through a `Mutex`,
/// so only one generation can happen at a time. This is intentional — /// so only one generation can happen at a time. This is intentional —
/// the model is designed for sequential inference. /// the model is designed for sequential inference. Generation is synchronous
/// and must be invoked from the tokio blocking pool (`spawn_blocking`).
pub struct LlamaEngine { pub struct LlamaEngine {
/// The loaded model (read-only after load, safe to share). /// The loaded model (read-only after load, safe to share).
pub model: LlamaModel, pub model: LlamaModel,
/// The inference context (single-threaded access via Mutex). /// The inference context (single-threaded access via Mutex).
pub ctx: Mutex<CtxInner>, ctx: Mutex<CtxInner>,
} }
impl LlamaEngine { impl LlamaEngine {
@@ -117,17 +129,17 @@ impl LlamaEngine {
/// cannot be created. /// cannot be created.
pub fn load() -> Result<Self, LlmError> { pub fn load() -> Result<Self, LlmError> {
info!("Initializing llama backend..."); info!("Initializing llama backend...");
let backend = LlamaBackend::init().map_err(|e| { let backend = LlamaBackend::init()
LlmError::Model(format!("Backend init failed: {e}")) .map_err(|e| LlmError::Model(format!("Backend init failed: {e}")))?;
})?;
// Backend must outlive model and context. We leak it to achieve 'static // Backend must outlive model and context. We leak it to achieve 'static
// lifetime since the engine lives for the program lifetime. // lifetime since the engine lives for the program lifetime.
let backend: &'static LlamaBackend = Box::leak(Box::new(backend)); let backend: &'static LlamaBackend = Box::leak(Box::new(backend));
info!("Loading model: {}", CONFIG.model_path); info!("Loading model: {}", CONFIG.model_path);
let model = LlamaModel::load_from_file(backend, &CONFIG.model_path, &LlamaModelParams::default()) let model =
.map_err(|e| LlmError::Model(format!("Failed to load model: {e}")))?; LlamaModel::load_from_file(backend, &CONFIG.model_path, &LlamaModelParams::default())
.map_err(|e| LlmError::Model(format!("Failed to load model: {e}")))?;
info!(" Vocab: {}", model.n_vocab()); info!(" Vocab: {}", model.n_vocab());
info!(" Params: {}", model.n_params()); info!(" Params: {}", model.n_params());
info!(" Layers: {}", model.n_layer()); info!(" Layers: {}", model.n_layer());
@@ -156,9 +168,12 @@ impl LlamaEngine {
} }
/// Tokenize a prompt string into tokens. /// Tokenize a prompt string into tokens.
///
/// `AddBos::Never`: the chat template already prepends the `<s>` BOS token,
/// so adding another here would produce a double BOS.
pub fn tokenize(&self, prompt: &str) -> Result<Vec<LlamaToken>, LlmError> { pub fn tokenize(&self, prompt: &str) -> Result<Vec<LlamaToken>, LlmError> {
self.model self.model
.str_to_token(prompt, AddBos::Always) .str_to_token(prompt, AddBos::Never)
.map_err(|e| LlmError::Model(format!("Tokenization failed: {e}"))) .map_err(|e| LlmError::Model(format!("Tokenization failed: {e}")))
} }
@@ -177,37 +192,28 @@ impl LlamaEngine {
String::from_utf8(bytes).unwrap_or_default() String::from_utf8(bytes).unwrap_or_default()
} }
/// Decode multiple tokens to a single string. /// Generate tokens and invoke `on_token` for each one.
pub fn decode_tokens(&self, tokens: &[LlamaToken]) -> String {
let mut out = String::with_capacity(tokens.len() * 4);
for &token in tokens {
out.push_str(&self.decode_token(token));
}
out
}
/// Check if a token is an end-of-generation token.
pub fn is_eog(&self, token: LlamaToken) -> bool {
self.model.is_eog_token(token)
}
/// Return a reference to the context mutex for advanced operations.
pub fn ctx(&self) -> &Mutex<CtxInner> {
&self.ctx
}
/// Generate tokens (non-streaming) and return output tokens, cleaned text, and tool calls.
/// ///
/// Locks the context mutex, prefill the prompt, then iterates sampling + decoding /// Synchronous (CPU-bound) — call from `spawn_blocking`. Locks the context,
/// until EOG, max_tokens, stop sequence, or tool call completion. /// prefills the prompt, then iterates sampling + decoding until EOG,
pub async fn generate( /// `max_tokens`, a stop sequence, or a complete `<tool_call>` block.
///
/// `on_token` is invoked for every generated token (after leading-EOG
/// skipping, before it is decoded into the KV cache) and may return `false`
/// to abort early (e.g. the streaming client disconnected).
pub fn generate(
&self, &self,
input_tokens: &[LlamaToken], input_tokens: &[LlamaToken],
sampler: &mut SendSampler, sampler: &mut LlamaSampler,
max_tokens: u32, max_tokens: u32,
stop: &[String], stop: &[String],
) -> Result<(Vec<LlamaToken>, String), LlmError> { enable_tool_detection: bool,
let mut inner = self.ctx.lock().await; on_token: &mut dyn FnMut(LlamaToken, &str) -> bool,
) -> Result<GenerationOutcome, LlmError> {
let mut inner = self
.ctx
.lock()
.map_err(|_| LlmError::Internal("context mutex poisoned".into()))?;
inner.clear(); inner.clear();
inner inner
.prefill(input_tokens) .prefill(input_tokens)
@@ -215,49 +221,66 @@ impl LlamaEngine {
let mut output: Vec<LlamaToken> = Vec::new(); let mut output: Vec<LlamaToken> = Vec::new();
let mut text_buf = String::new(); let mut text_buf = String::new();
let mut stop_now = false; let mut finish = FinishReason::Length;
let mut current = inner.sample(sampler); let mut current = inner.sample(sampler);
// Skip leading EOG tokens (like a stray <|im_end|> right after the prompt)
while output.is_empty() && self.model.is_eog_token(current) {
let pos = input_tokens.len() as i32 + output.len() as i32;
inner
.decode(current, pos)
.map_err(|e| LlmError::Model(format!("Decode: {e}")))?;
current = inner.sample(sampler);
}
for _ in 0..max_tokens { for _ in 0..max_tokens {
if self.model.is_eog_token(current) { if self.model.is_eog_token(current) {
finish = FinishReason::Stop;
break;
}
let piece = self.decode_token(current);
// Check stop sequences *before* committing, so the stop tokens never
// leak into the output text or the stream.
if stop
.iter()
.any(|s| !s.is_empty() && format!("{text_buf}{piece}").contains(s))
{
finish = FinishReason::Stop;
break; break;
} }
let pos = input_tokens.len() as i32 + output.len() as i32; let pos = input_tokens.len() as i32 + output.len() as i32;
output.push(current); output.push(current);
let piece = self.decode_token(current);
text_buf.push_str(&piece); text_buf.push_str(&piece);
// Check stop sequences // Complete <tool_call> block emitted?
for s in stop { if enable_tool_detection && text_buf.contains("<tool_call>") {
if text_buf.contains(s) { let open = text_buf.matches("<tool_call>").count();
stop_now = true; let close = text_buf.matches("</tool_call>").count();
break; if close >= open {
} finish = FinishReason::ToolCalls;
}
if stop_now {
break;
}
// Check for tool_call block completion
if text_buf.contains("<tool_call>") {
let close_count = text_buf.matches("</tool_call>").count();
let open_count = text_buf.matches("<tool_call>").count();
if open_count > 0 && close_count >= open_count {
break; break;
} }
} }
if let Err(e) = inner.decode(current, pos) { if !on_token(current, &piece) {
tracing::info!(" Decode error: {e}"); finish = FinishReason::Aborted;
break; break;
} }
inner
.decode(current, pos)
.map_err(|e| LlmError::Model(format!("Decode: {e}")))?;
current = inner.sample(sampler); current = inner.sample(sampler);
} }
Ok((output, text_buf)) Ok(GenerationOutcome {
tokens: output,
text: text_buf,
finish,
})
} }
} }
+41 -7
View File
@@ -72,6 +72,13 @@
display: flex; align-items: center; gap: 6px; display: flex; align-items: center; gap: 6px;
} }
.msg .tool-call::before { content: '\1F527'; } .msg .tool-call::before { content: '\1F527'; }
.msg .reasoning {
font-size: 12px; font-style: italic;
color: var(--text2);
white-space: pre-wrap; word-break: break-word;
border-bottom: 1px solid var(--border);
padding-bottom: 8px; margin-bottom: 8px;
}
.msg.error { .msg.error {
background: #2a1818; border-color: #4a2828; color: #f08080; background: #2a1818; border-color: #4a2828; color: #f08080;
} }
@@ -168,7 +175,7 @@ async function send() {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ body: JSON.stringify({
model: '', model: 'minicpm5-1b-fable5-v2-thinking',
messages: messages.slice(-5), // keep context window manageable messages: messages.slice(-5), // keep context window manageable
stream: true, stream: true,
max_tokens: 1024, max_tokens: 1024,
@@ -186,8 +193,33 @@ async function send() {
const decoder = new TextDecoder(); const decoder = new TextDecoder();
let buffer = ''; let buffer = '';
let full = ''; let full = '';
let reasoning = '';
let toolCalls = null; let toolCalls = null;
// Re-render the assistant bubble: reasoning (muted) above the answer.
function render() {
el.innerHTML = '';
if (reasoning) {
const r = document.createElement('div');
r.className = 'reasoning';
r.textContent = reasoning;
el.appendChild(r);
}
if (full) {
const c = document.createElement('div');
c.textContent = full;
el.appendChild(c);
}
if (toolCalls?.length) {
for (const tc of toolCalls) {
const t = document.createElement('div');
t.className = 'tool-call';
t.textContent = 'Calling tool: ' + (tc.function?.name || 'tool');
el.appendChild(t);
}
}
}
while (true) { while (true) {
const { done, value } = await reader.read(); const { done, value } = await reader.read();
if (done) break; if (done) break;
@@ -206,18 +238,20 @@ async function send() {
const delta = chunk.choices?.[0]?.delta; const delta = chunk.choices?.[0]?.delta;
const finish = chunk.choices?.[0]?.finish_reason; const finish = chunk.choices?.[0]?.finish_reason;
if (delta?.reasoning_content) {
reasoning += delta.reasoning_content;
render();
}
if (delta?.content) { if (delta?.content) {
full += delta.content; full += delta.content;
el.textContent = full; render();
} }
if (delta?.tool_calls) { if (delta?.tool_calls) {
toolCalls = delta.tool_calls; toolCalls = delta.tool_calls;
render();
} }
if (finish === 'tool_calls' && toolCalls) { if (finish === 'tool_calls') {
const t = document.createElement('div'); render();
t.className = 'tool-call';
t.textContent = 'Calling tool: ' + toolCalls.map(tc => tc.function?.name).join(', ');
el.appendChild(t);
} }
} catch (e) { /* skip malformed chunk */ } } catch (e) { /* skip malformed chunk */ }
} }
+209 -178
View File
@@ -1,11 +1,18 @@
//! Chat completions endpoint — streaming and non-streaming. //! Chat completions endpoint — streaming and non-streaming.
//!
//! Both paths share the same synchronous generation core
//! ([`LlamaEngine::generate`]), which runs on the tokio blocking pool via
//! `spawn_blocking` so worker threads are not hogged by CPU-bound inference.
//! The streaming path forwards each generated token into an SSE channel.
use std::convert::Infallible; use std::convert::Infallible;
use std::sync::Arc; use std::sync::Arc;
use axum::extract::State; use axum::extract::State;
use axum::http::HeaderMap;
use axum::response::sse::{Event, KeepAlive, Sse}; use axum::response::sse::{Event, KeepAlive, Sse};
use axum::{Json, response::{IntoResponse, Response}}; use axum::response::{IntoResponse, Response};
use axum::Json;
use chrono::Utc; use chrono::Utc;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tokio_stream::wrappers::ReceiverStream; use tokio_stream::wrappers::ReceiverStream;
@@ -13,7 +20,7 @@ use tracing::info;
use crate::application::chat; use crate::application::chat;
use crate::domain::entity::{ use crate::domain::entity::{
ChatRequest, ChatResponse, Choice, ResponseMessage, SseChunk, SseChoice, SseDelta, Usage, ChatRequest, ChatResponse, Choice, FinishReason, ResponseMessage, SseChunk, SseDelta, Usage,
}; };
use crate::infrastructure::llama::SendSampler; use crate::infrastructure::llama::SendSampler;
use crate::presentation::error::AppError; use crate::presentation::error::AppError;
@@ -24,11 +31,14 @@ pub async fn chat_completions(
State(state): State<Arc<AppState>>, State(state): State<Arc<AppState>>,
Json(req): Json<ChatRequest>, Json(req): Json<ChatRequest>,
) -> Result<Response, AppError> { ) -> Result<Response, AppError> {
// Strict model validation — reject unknown model ids up front.
chat::validate_model(&req.model).map_err(AppError::BadRequest)?;
let max_tokens = req.max_tokens.unwrap_or(256).min(1024); let max_tokens = req.max_tokens.unwrap_or(256).min(1024);
let stop = req.stop.clone().unwrap_or_default(); let stop = req.stop.clone().unwrap_or_default();
let prompt = chat::build_prompt(&req.messages, &req.tools); let prompt = chat::build_prompt(&req.messages, &req.tools).map_err(AppError::LlmError)?;
// Tokenize // Tokenize (fast — keep on the async thread).
let input_tokens = state let input_tokens = state
.engine .engine
.tokenize(&prompt) .tokenize(&prompt)
@@ -36,7 +46,7 @@ pub async fn chat_completions(
let prompt_tokens = input_tokens.len() as u32; let prompt_tokens = input_tokens.len() as u32;
info!( info!(
" Chat: {} prompt tokens, max_tokens={}, tools={}", "Chat: {} prompt tokens, max_tokens={}, tools={}",
prompt_tokens, prompt_tokens,
max_tokens, max_tokens,
req.tools.as_ref().is_some_and(|t| !t.is_empty()) req.tools.as_ref().is_some_and(|t| !t.is_empty())
@@ -47,7 +57,7 @@ pub async fn chat_completions(
} else { } else {
handle_non_streaming(state.clone(), req, max_tokens, stop, input_tokens).await? handle_non_streaming(state.clone(), req, max_tokens, stop, input_tokens).await?
}; };
Ok(response.into_response()) Ok(response)
} }
// ── Non-streaming path ── // ── Non-streaming path ──
@@ -64,24 +74,40 @@ async fn handle_non_streaming(
let prompt_tokens = input_tokens.len() as u32; let prompt_tokens = input_tokens.len() as u32;
let has_tools = req.tools.as_ref().is_some_and(|t| !t.is_empty()); let has_tools = req.tools.as_ref().is_some_and(|t| !t.is_empty());
let params = chat::SamplerParams::from_request(&req); let params = chat::SamplerParams::from_request(&req);
let mut sampler = SendSampler(chat::build_sampler(&params));
let (output_tokens, raw_text) = state let engine = state.engine.clone();
.engine let outcome = tokio::task::spawn_blocking(move || {
.generate(&input_tokens, &mut sampler, max_tokens, &stop) let mut sampler = SendSampler(chat::build_sampler(&params));
.await?; engine.generate(
&input_tokens,
&mut sampler,
max_tokens,
&stop,
has_tools,
&mut |_token, _piece| true,
)
})
.await
.map_err(|e| AppError::Internal(format!("Generation task panicked: {e}")))?
.map_err(AppError::from)?;
let (output_text, tool_calls) = chat::parse_tool_calls(&chat::clean_text(&raw_text)); let completion_tokens = outcome.tokens.len() as u32;
let completion_tokens = output_tokens.len() as u32;
info!(" {} generated tokens", completion_tokens); info!(" {} generated tokens", completion_tokens);
let finish_reason = if has_tools && !tool_calls.is_empty() { let (reasoning, cleaned) = chat::clean_text(&outcome.text);
"tool_calls" let (output_text, tool_calls) = chat::parse_tool_calls(&cleaned);
} else if completion_tokens < max_tokens {
"stop" let finish_reason = match (outcome.finish, tool_calls.is_empty()) {
(FinishReason::ToolCalls, false) => "tool_calls",
// Model opened a <tool_call> but never completed it — don't claim a call.
(FinishReason::ToolCalls, true) => "stop",
(f, _) => f.as_str(),
};
let reasoning_opt = if reasoning.is_empty() {
None
} else { } else {
"length" Some(reasoning)
}; };
Ok(Json(ChatResponse { Ok(Json(ChatResponse {
@@ -94,6 +120,7 @@ async fn handle_non_streaming(
message: ResponseMessage { message: ResponseMessage {
role: "assistant".into(), role: "assistant".into(),
content: Some(output_text), content: Some(output_text),
reasoning_content: reasoning_opt,
tool_calls: if tool_calls.is_empty() { tool_calls: if tool_calls.is_empty() {
None None
} else { } else {
@@ -124,187 +151,191 @@ async fn handle_streaming(
let created = Utc::now().timestamp(); let created = Utc::now().timestamp();
let has_tools = req.tools.as_ref().is_some_and(|t| !t.is_empty()); let has_tools = req.tools.as_ref().is_some_and(|t| !t.is_empty());
let model_name = req.model.clone(); let model_name = req.model.clone();
let prompt_tokens = input_tokens.len() as u32;
let params = chat::SamplerParams::from_request(&req); let params = chat::SamplerParams::from_request(&req);
let (tx, rx) = mpsc::channel::<Result<Event, Infallible>>(64); let (tx, rx) = mpsc::channel::<Result<Event, Infallible>>(64);
tokio::spawn(async move { // First chunk: announce the assistant role.
// Role chunk let role_chunk = SseChunk::delta(
let role_chunk = serde_json::to_string(&SseChunk { chat_id.clone(),
id: chat_id.clone(), created,
object: "chat.completion.chunk".into(), model_name.clone(),
created, SseDelta {
model: model_name.clone(), role: Some("assistant".into()),
choices: vec![SseChoice { content: None,
index: 0, tool_calls: None,
delta: SseDelta { reasoning_content: None,
role: Some("assistant".into()), },
content: None, );
tool_calls: None, let role_event = serde_json::to_string(&role_chunk).unwrap();
}, let _ = tx.send(Ok(Event::default().data(role_event))).await;
finish_reason: None,
}],
})
.unwrap();
if tx.send(Ok(Event::default().data(role_chunk))).await.is_err() {
return;
}
// Build sampler let engine = state.engine.clone();
tokio::task::spawn_blocking(move || {
let mut sampler = SendSampler(chat::build_sampler(&params)); let mut sampler = SendSampler(chat::build_sampler(&params));
// Lock context
let mut inner = state.engine.ctx().lock().await;
inner.clear();
if let Err(e) = inner.prefill(&input_tokens) {
info!(" Prefill error: {e}");
return;
}
let mut count = 0u32;
let mut text_buf = String::new(); let mut text_buf = String::new();
let mut current = inner.sample(&mut sampler); let mut sent_len: usize = 0;
// Byte offset in text_buf where the content phase begins (right after
// the first `</think>`); None while still thinking.
let mut content_start: Option<usize> = None;
loop { let outcome = engine.generate(
if count >= max_tokens { &input_tokens,
let chunk = serde_json::to_string(&SseChunk { &mut sampler,
id: chat_id.clone(), max_tokens,
object: "chat.completion.chunk".into(), &stop,
created, has_tools,
model: model_name.clone(), &mut |_token, piece| {
choices: vec![SseChoice { text_buf.push_str(piece);
index: 0,
delta: SseDelta {
role: None,
content: None,
tool_calls: None,
},
finish_reason: Some("length".into()),
}],
})
.unwrap();
let _ = tx.send(Ok(Event::default().data(chunk))).await;
break;
}
if state.engine.is_eog(current) { // Robust boundary detection on the *full* buffer — a `</think>`
let reason = if has_tools && text_buf.contains("<tool_call>") { // tag may be split across tokens, which would defeat a search
"tool_calls" // over the incremental fragment only.
} else { if content_start.is_none() {
"stop" if let Some(pos) = text_buf.find("</think>") {
}; content_start = Some(pos + 8);
let chunk = serde_json::to_string(&SseChunk { }
id: chat_id.clone(),
object: "chat.completion.chunk".into(),
created,
model: model_name.clone(),
choices: vec![SseChoice {
index: 0,
delta: SseDelta {
role: None,
content: None,
tool_calls: None,
},
finish_reason: Some(reason.into()),
}],
})
.unwrap();
let _ = tx.send(Ok(Event::default().data(chunk))).await;
break;
}
let piece = state.engine.decode_token(current);
let content = chat::clean_text(&piece);
if !content.is_empty() {
let chunk = serde_json::to_string(&SseChunk {
id: chat_id.clone(),
object: "chat.completion.chunk".into(),
created,
model: model_name.clone(),
choices: vec![SseChoice {
index: 0,
delta: SseDelta {
role: None,
content: Some(content.clone()),
tool_calls: None,
},
finish_reason: None,
}],
})
.unwrap();
if tx.send(Ok(Event::default().data(chunk))).await.is_err() {
break;
} }
}
text_buf.push_str(&piece); let new_text = &text_buf[sent_len..];
if new_text.is_empty() {
// Check stop sequences return true;
let mut stop_now = false;
for s in &stop {
if text_buf.contains(s) {
stop_now = true;
break;
} }
}
if stop_now {
let chunk = serde_json::to_string(&SseChunk {
id: chat_id.clone(),
object: "chat.completion.chunk".into(),
created,
model: model_name.clone(),
choices: vec![SseChoice {
index: 0,
delta: SseDelta {
role: None,
content: None,
tool_calls: None,
},
finish_reason: Some("stop".into()),
}],
})
.unwrap();
let _ = tx.send(Ok(Event::default().data(chunk))).await;
break;
}
// Check tool call completeness let (reasoning, content) =
if has_tools && text_buf.contains("<tool_call>") { chat::split_stream_chunk(new_text, sent_len, content_start);
let open = text_buf.matches("<tool_call>").count();
let close = text_buf.matches("</tool_call>").count(); if let Some(reasoning) = reasoning {
if close >= open { let chunk = SseChunk::delta(
let chunk = serde_json::to_string(&SseChunk { chat_id.clone(),
id: chat_id.clone(),
object: "chat.completion.chunk".into(),
created, created,
model: model_name.clone(), model_name.clone(),
choices: vec![SseChoice { SseDelta {
index: 0, role: None,
delta: SseDelta { content: None,
tool_calls: None,
reasoning_content: Some(reasoning),
},
);
let event = serde_json::to_string(&chunk).unwrap();
if tx.blocking_send(Ok(Event::default().data(event))).is_err() {
return false;
}
}
if !content.is_empty() {
let chunk = SseChunk::delta(
chat_id.clone(),
created,
model_name.clone(),
SseDelta {
role: None,
content: Some(content),
tool_calls: None,
reasoning_content: None,
},
);
let event = serde_json::to_string(&chunk).unwrap();
if tx.blocking_send(Ok(Event::default().data(event))).is_err() {
return false;
}
}
sent_len = text_buf.len();
true
},
);
match outcome {
Ok(outcome) => {
let completion_tokens = outcome.tokens.len() as u32;
let usage = Usage {
prompt_tokens,
completion_tokens,
total_tokens: prompt_tokens + completion_tokens,
};
// If the model never emitted `</think>`, everything was streamed
// as reasoning_content. Flush it as content so the client always
// receives the response text.
if content_start.is_none() && !text_buf.is_empty() {
let cleaned = chat::strip_markup(&text_buf);
if !cleaned.is_empty() {
let chunk = SseChunk::delta(
chat_id.clone(),
created,
model_name.clone(),
SseDelta {
role: None,
content: Some(cleaned),
tool_calls: None,
reasoning_content: None,
},
);
let event = serde_json::to_string(&chunk).unwrap();
let _ = tx.blocking_send(Ok(Event::default().data(event)));
}
}
// Single-shot tool-calls delta (this model emits whole blocks).
let mut sent_tool_calls = false;
if outcome.finish == FinishReason::ToolCalls {
let (_cleaned, calls) = chat::parse_tool_calls(&outcome.text);
if !calls.is_empty() {
sent_tool_calls = true;
let chunk = SseChunk::delta(
chat_id.clone(),
created,
model_name.clone(),
SseDelta {
role: None, role: None,
content: None, content: None,
tool_calls: None, tool_calls: Some(calls),
reasoning_content: None,
}, },
finish_reason: Some("tool_calls".into()), );
}], let event = serde_json::to_string(&chunk).unwrap();
}) let _ = tx.blocking_send(Ok(Event::default().data(event)));
.unwrap(); }
let _ = tx.send(Ok(Event::default().data(chunk))).await;
break;
} }
}
let pos = input_tokens.len() as i32 + count as i32; let finish_reason = match (outcome.finish, sent_tool_calls) {
if let Err(e) = inner.decode(current, pos) { (FinishReason::ToolCalls, true) => "tool_calls",
info!(" Decode error: {e}"); (FinishReason::ToolCalls, false) => "stop",
break; (f, _) => f.as_str(),
};
let finish_chunk =
SseChunk::finish(chat_id.clone(), created, model_name.clone(), finish_reason);
let event = serde_json::to_string(&finish_chunk).unwrap();
let _ = tx.blocking_send(Ok(Event::default().data(event)));
let usage_chunk = SseChunk::usage(chat_id, created, model_name, usage);
let event = serde_json::to_string(&usage_chunk).unwrap();
let _ = tx.blocking_send(Ok(Event::default().data(event)));
}
Err(e) => {
// Surface the error instead of silently truncating the stream.
let error_body = serde_json::json!({
"error": {
"message": e.to_string(),
"type": "server_error",
}
});
let _ = tx.blocking_send(Ok(Event::default().data(error_body.to_string())));
} }
count += 1;
current = inner.sample(&mut sampler);
} }
// OpenAI-compatible terminator.
let _ = tx.blocking_send(Ok(Event::default().data("[DONE]")));
}); });
let stream = ReceiverStream::new(rx); let stream = ReceiverStream::new(rx);
let mut headers = HeaderMap::new();
headers.insert("X-Accel-Buffering", "no".parse().unwrap());
headers.insert("Cache-Control", "no-cache".parse().unwrap());
headers.insert("Connection", "keep-alive".parse().unwrap());
let sse = Sse::new(stream).keep_alive(KeepAlive::default()); let sse = Sse::new(stream).keep_alive(KeepAlive::default());
Ok(sse.into_response()) Ok((headers, sse).into_response())
} }
+4 -1
View File
@@ -9,7 +9,10 @@ const HTML: &str = include_str!("chat-ui/index.html");
pub async fn chat_ui() -> Response { pub async fn chat_ui() -> Response {
( (
StatusCode::OK, StatusCode::OK,
[(header::CONTENT_TYPE, HeaderValue::from_static("text/html; charset=utf-8"))], [(
header::CONTENT_TYPE,
HeaderValue::from_static("text/html; charset=utf-8"),
)],
HTML, HTML,
) )
.into_response() .into_response()
+2 -1
View File
@@ -8,6 +8,7 @@ use crate::domain::entity::HealthResponse;
pub async fn health_check() -> Json<HealthResponse> { pub async fn health_check() -> Json<HealthResponse> {
Json(HealthResponse { Json(HealthResponse {
status: "ok".into(), status: "ok".into(),
model: format!("{MODEL_ID}-q4_k_m"), // Report the exact model id served by /v1/models (no extra suffix).
model: MODEL_ID.into(),
}) })
} }
+4 -6
View File
@@ -4,18 +4,18 @@
//! Only applied to routes that require authentication. //! Only applied to routes that require authentication.
use axum::extract::Request; use axum::extract::Request;
use axum::http::StatusCode;
use axum::middleware::Next; use axum::middleware::Next;
use axum::response::Response; use axum::response::Response;
use crate::config::CONFIG; use crate::config::CONFIG;
use crate::presentation::error::AppError;
/// Middleware that validates the Bearer token in the Authorization header. /// Middleware that validates the Bearer token in the Authorization header.
/// ///
/// If `API_KEY` is not set (empty), authentication is disabled and /// If `API_KEY` is not set (empty), authentication is disabled and
/// all requests pass through. If set, the middleware rejects requests /// all requests pass through. If set, the middleware rejects requests
/// without a matching token. /// without a matching token.
pub async fn auth_middleware(request: Request, next: Next) -> Result<Response, (StatusCode, String)> { pub async fn auth_middleware(request: Request, next: Next) -> Result<Response, AppError> {
let api_key = &CONFIG.api_key; let api_key = &CONFIG.api_key;
if api_key.is_empty() { if api_key.is_empty() {
return Ok(next.run(request).await); return Ok(next.run(request).await);
@@ -32,8 +32,6 @@ pub async fn auth_middleware(request: Request, next: Next) -> Result<Response, (
return Ok(next.run(request).await); return Ok(next.run(request).await);
} }
Err(( // AppError renders a JSON body with the correct Content-Type.
StatusCode::UNAUTHORIZED, Err(AppError::Unauthorized)
"{\"error\":\"unauthorized\",\"message\":\"Invalid API key\"}".into(),
))
} }