252 条记录
┌─────────────┐
│ 前端聊天UI │
│ ChatGPT类界面│
└──────┬──────┘
│ HTTP请求(OpenAI格式)
↓
┌─────────────────┐
│ llama-server │
│ localhost:8080 │
│ OpenAI兼容API │
└──────┬──────────┘
│
↓
┌─────────────┐
│ GGUF模型 │
│ Llama/Qwen │
└─────────────┘
Chat接口(最常用)
POST /v1/chat/completions
http://localhost:9192/v1/chat/completions
请求格式:
{
"model": "local-model",
"messages": [
{
"role": "system",
"content": "你是一个AI助手"
},
{
"role": "user",
"content": "你好,请介绍一下自己"
}
],
"temperature":0.7
}
返回:
{
"choices":[
{
"message":{
"role":"assistant",
"content":"你好,我是一个基于..."
}
}
]
}
前端怎么调用?
<input id="msg">
<button onclick="send()">发送</button>
<div id="answer"></div>
<script>
async function send(){
let text=document.getElementById("msg").value;
let res=await fetch(
"http://服务器IP:8080/v1/chat/completions",
{
method:"POST",
headers:{
"Content-Type":"application/json",
"Authorization":"Bearer YOUR_API_KEY"
},
body:JSON.stringify({
model:"local-model",
messages:[
{
role:"user",
content:text
}
]
})
});
let data=await res.json();
document.getElementById("answer").innerHTML=
data.choices[0].message.content;
}
</script>
流式输出(打字效果)
请求增加:
{
"stream":true
}
返回:
data:
{"content":"你"}
data:
{"content":"好"}
data:
{"content":"!"}
前端用:ReadableStream 读取即可。
限制访问量(1人)
nginx配置
新建/etc/nginx/conf.d/llama.conf
limit_conn_zone $binary_remote_addr zone=llama_limit:10m;
server {
listen 8080;
location / {
# 单用户连接限制
limit_conn llama_limit 1;
# 超过限制返回503
limit_conn_status 503;
proxy_pass http://127.0.0.91:9192;
proxy_http_version 1.1;
# SSE流式输出必须
proxy_set_header Connection "";
proxy_buffering off;
proxy_read_timeout 3600s;
}
# 自定义忙碌提示
error_page 503 = @busy;
location @busy {
default_type application/json;
return 503 '
{
"error":
"AI服务器正在服务其他用户,请稍后再试"
}';
}
}
systemctl reload nginx
llama.cpp本身建议这样启动:
./llama-server \
-m model.gguf \
--host 127.0.0.91 \
--port 9192 \
--parallel 1 \
--api-key-file /service/ai/api.key
关键 --parallel 1 llama.cpp内部只允许一个推理上下文。
常用的完整 POST JSON 可以这样写:
{
"model": "Talk-Qwen3-1.7B-Q8",
"messages": [
{
"role": "system",
"content": "你是一个专业、简洁、可靠的中文 AI 助手。回答要直接,不要输出 thinking,不要输出 <think> 标签。"
},
{
"role": "user",
"content": "请用三句话介绍一下 llama.cpp。"
}
],
"temperature": 0.6,
"top_p": 0.85,
"top_k": 40,
"min_p": 0.05,
"repeat_penalty": 1.2,
"presence_penalty": 0.3,
"frequency_penalty": 0.3,
"max_tokens": 512,
"stream": false
}
curl 示例:
curl http://127.0.0.91:9192/v1/chat/completions \
-H "Content-Type: application/json; charset=utf-8" \
-H "Authorization: Bearer 你的key内容" \
-d '{
"model": "Talk-Qwen3-1.7B-Q8",
"messages": [
{
"role": "system",
"content": "你是一个专业、简洁、可靠的中文 AI 助手。回答要直接,不要输出 thinking,不要输出 <think> 标签。"
},
{
"role": "user",
"content": "请用三句话介绍一下 llama.cpp。"
}
],
"temperature": 0.6,
"top_p": 0.85,
"top_k": 40,
"min_p": 0.05,
"repeat_penalty": 1.2,
"presence_penalty": 0.3,
"frequency_penalty": 0.3,
"max_tokens": 512,
"stream": false
}'
流式输出用:
"stream": true
多轮对话时,messages 自己带历史:
"messages": [
{"role": "system", "content": "你是中文助手。"},
{"role": "user", "content": "我叫张三。"},
{"role": "assistant", "content": "好的,我记住了。"},
{"role": "user", "content": "我叫什么?"}
]
不想有历史,就每次只传:
"messages": [
{"role": "system", "content": "你是中文助手。"},
{"role": "user", "content": "新的问题"}
]