Skip to content

使用 AidGenSE 部署 LLM HTTP Server

介绍

端侧部署大语言模型 (Large Language Model, LLM) 指将原本在云端运行的大模型压缩、量化并部署到本地设备上,实现离线、低时延的自然语言理解与生成。本章节以 AidGenSE 推理引擎为基础,演示如何在边缘设备上完成大语言模型 HTTP 服务的部署 (适配 OpenAI API)。

在本案例中,大语言模型推理运行在设备端,通过 HTTP API 调用相关接口接收用户输入并实时返回对话结果。

  • 设备:Rhino Pi-X1
  • 系统:Ubuntu 22.04
  • 模型:Qwen2.5-0.5B-Instruct

支持平台

平台运行方式
Rhino Pi-X1Ubuntu 22.04, AidLux

准备工作

  1. Rhino Pi-X1 硬件

  2. Ubuntu 22.04 系统或 AidLux 系统

案例部署

步骤一:安装 AidGenSE

bash
sudo aid-pkg update
sudo aid-pkg -i aidgense

步骤二:模型查询 & 获取

  • 已支持模型查看
bash
# 查看已支持的模型
aidllm remote-list api

#------------------------示例输出如下------------------------

Current Soc : 8550

Name                                 Url                                          CreateTime
-----                                ---------                                    ---------
qwen2.5-0.5B-Instruct-8550           aplux/qwen2.5-0.5B-Instruct-8550             2025-03-05 14:52:23
qwen2.5-3B-Instruct-8550             aplux/qwen2.5-3B-Instruct-8550               2025-03-05 14:52:37
...
  • 下载 Qwen2.5-0.5B-Instruct
bash
# 下载模型
aidllm pull api aplux/qwen2.5-0.5B-Instruct-8550

# 查看已下载模型
aidllm list api

步骤三:启动 HTTP 服务

bash
# 启动对应模型的 openai api 服务
aidllm start api -m qwen2.5-0.5B-Instruct-8550

# 查看状态
aidllm status api

# 停止服务: aidllm stop api

# 重启服务: aidllm restart api

💡注意

默认端口号是 8888

步骤四:对话测试

使用 Web UI 对话测试

bash
# 安装 UI 前端服务
sudo aidllm install ui

# 启动 UI 服务
aidllm start ui

# 查看 UI 服务状态: aidllm status ui

# 停止 UI 服务: aidllm stop ui

UI 服务启动后访问 http://ip:51104

使用 Python 对话测试

python
import os
import requests
import json

def stream_chat_completion(messages, model="qwen2.5-0.5B-Instruct-8550"):

    url = "http://127.0.0.1:8888/v1/chat/completions"
    headers = {
        "Content-Type": "application/json"
    }
    payload = {
        "model": model,
        "messages": messages,
        "stream": True    # 打开流式
    }

    # 发起带 stream=True 的请求
    response = requests.post(url, headers=headers, json=payload, stream=True)
    response.raise_for_status()

    # 逐行读取并解析 SSE 格式
    for line in response.iter_lines():
        if not line:
            continue
        # print(line)
        line_data = line.decode('utf-8')
        # SSE 每一行以 "data: " 前缀开头
        if line_data.startswith("data: "):
            data = line_data[len("data: "):]
            # 结束标志
            if data.strip() == "[DONE]":
                break
            try:
                chunk = json.loads(data)
            except json.JSONDecodeError:
                # 解析出错时打印并跳过
                print("无法解析JSON:", data)
                continue

            # 取出模型输出的 token
            content = chunk["choices"][0]["delta"].get("content")
            if content:
                print(content, end="", flush=True)

if __name__ == "__main__":
    # 示例对话
    messages = [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "你好。"}
    ]
    print("Assistant:", end=" ")
    stream_chat_completion(messages)
    print()  # 换行