Skip to content

AidGenSE 开发者文档

介绍

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

💡注意

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

支持情况

模型格式及后端支持情况

模型格式CPUGPUNPU
.gguf
.bin
.aidem

✅:已支持 ❌:不支持

操作系统支持情况

LinuxAndroid
🚧

✅:已支持 🚧:计划支持

AidGenSE 服务安装与运行

安装

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
Qwen2.5-VL-3B-392x392-8550     aplux/Qwen2.5-VL-3B-392x392-8550 2025-12-02 16:48:32
Qwen2.5-VL-3B-672x672-8550     aplux/Qwen2.5-VL-3B-672x672-8550 2025-12-02 16:48:05
Qwen2.5-VL-3B-Instruct-q4_k_m  aplux/Qwen2.5-VL-3B-Instruct-q4_k_m 2026-03-10 11:00:27
...
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

语言大模型对话测试

通过 HTTP POST 请求调用 /v1/chat/completions 接口,传入 messages 对话消息列表即可与大语言模型进行对话。设置 "stream": true 可启用流式输出,逐 token 返回生成内容。

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()  # 换行

视觉语言模型对话测试

AidGenSE 支持视觉语言模型(VLM),可对图像进行理解和描述。在 messages 中通过 content 数组同时传入文本和图像:文本以 {"type": "text", "text": "..."} 表示,图像以 {"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,..."}} 传入 base64 编码数据,即可进行多模态视觉对话。

Python 调用示例:

python
import os
import requests
import json
import base64

def encode_image_to_base64(image_path):
    """Encode image file to base64 string."""
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode("utf-8")

def stream_chat_completion(messages, model="Qwen2.5-VL-3B-392x392-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
        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__":
    # 将图片编码为 base64
    image_path = "/path/to/your/image.jpg"
    image_base64 = encode_image_to_base64(image_path)

    # 携带图片的示例对话
    messages = [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "请描述这张图片中的内容。"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "data:image/jpeg;base64," + image_base64
                    }
                }
            ]
        }
    ]
    print("Assistant:", end=" ")
    stream_chat_completion(messages)
    print()  # 换行

图像格式限制

  • MIME 类型:仅支持 image/jpegimage/png 两种格式。如为 PNG 格式,请将 URL 中的 MIME 从 image/jpeg 改为 image/png
  • 编码方式:仅支持 base64 编码,格式为 data:image/jpeg;base64,<base64_string>
  • 图像尺寸:单边最大 7680 像素,总像素数不超过 33177600(约 8K UHD 分辨率),最小支持 1×1 像素
  • 不支持:传入图片 URL 自动下载分析

示例