Skip to content

AidGenSE

介绍

AidGenSE 是基于 AidGen SDK 封装的适配了 OpenAI HTTP 协议的生成式 AI HTTP 服务。开发者可以通过 HTTP 方式调用生成式 AI 并快速集成到自己的应用中。

💡注意

Model Farm 支持的大语言模型均通过 AidGen 实现在高通芯片NPU上的推理加速。

支持情况

模型格式及后端支持情况

模型格式CPUGPUNPU
.gguf
.bin
.aidem

✅:已支持 ❌:不支持

操作系统支持情况

LinuxAndroid
🚧

✅:已支持 🚧:计划支持

大语言模型

安装

bash
# 安装 aidgen sdk
sudo aid-pkg update
sudo aid-pkg -i aidgense

模型查询 & 获取

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

示例输出:

yaml
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
...
bash
# 下载模型
aidllm pull api [Url] # aplux/qwen2.5-3B-Instruct-8550

# 查看已下载模型
aidllm list api

# 删除已下载模型
sudo aidllm rm api [Name] # qwen2.5-3B-Instruct-8550

启动服务

bash
# 启动对应模型的 openai api 服务
aidllm start api -m <model_name> 

# 查看状态
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-3B-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()  # 换行

示例:在高通 8550 上与 Qwen2.5-3B-Instruct 对话

  1. 安装 AidGenSE
bash
sudo aid-pkg -i aidgense
  1. 下载 Qwen2.5-3B-Instruct 模型
bash
aidllm pull api aplux/qwen2.5-3B-Instruct-8550
  1. 启动服务
bash
aidllm start api -m qwen2.5-3B-Instruct-8550
  1. 使用 Web UI 对话测试
bash
# 安装 UI 前端服务
sudo aidllm install ui

# 启动 UI 服务
aidllm start ui

访问 http://ip:51104

  1. 使用 Python 对话测试
python
import os
import requests
import json

def stream_chat_completion(messages, model="qwen2.5-3B-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()  # 换行