Skip to content

文本系列模型

1. 对话生成

AI Ping 的 API 请求和 Openai 的对话 API 非常类似,仅为了功能拓展在输入、输出格式上新增了一些字段。

1.1 Header 参数

Authorization

Bearer Token
在 Header 添加参数 Authorization , 其值为在 Bearer 之后拼接 Token
示例:
Authorization: Bearer ********************

1.2 Body 参数

采样参数会影响模型生成 token 的过程。你可以向 AI Ping 发送下列参数中的任意参数,也可以发送其他参数。 如果请求中缺少某些参数,AI Ping 使用下面列出的默认值(例如 stream 默认为 false)。

Body

modelstring必需
查询 v1/models 接口获取模型列表
messagesarray必需
rolestring必需
可选值:
systemuserassistant
contentUnion[str, List[ContentItem]]必需
[property: string]ContentItem必需
typestring必需
判别字段,决定使用哪个内容结构。
可选值:
textvideo_urlimage_url
textstring当 type="text"
文本内容。
video_urlobj当 type="video_url"
视频资源 URL。
urlstring当 type="video_url"
image_urlobj当 type="image_url"
图片资源 URL。
urlstring当 type="image_url"
max_tokensinteger可选
最大推理长度
temperaturefloat可选
可选值:
0.0-2.0
top_pfloat可选
可选值:
0.0-1.0
top_kinteger可选
OpenAI Python SDK不支持该参数
可选值:
>0
presence_penaltyfloat可选
可选值:
-2.0-2.0
streamboolean可选
默认值:
false
stream_optionsStreamOptions可选
include_usageboolean可选
默认值:
true
modalitiesstring可选
希望模型生成的输出类型。大多数模型都可以生成文本,文本是默认类型 text
可选值:
text
response_formatResponseFormat可选
typestring可选
可选值:
textjson_object
extra_bodyExtraBody可选
路由与策略控制等扩展字段
enable_thinkingboolean可选
思考功能开关,仅部分模型服务支持开启、关闭思考功能,请以具体模型行为为准
providerProvider必需
供应商选择/过滤/排序策略
onlyarray可选
白名单;仅在该集合内挑选供应商
可选值:
供应商
orderarray可选
供应商排序
可选值:
供应商
sortstringarray可选
排序策略,详见[路由策略] 支持多关键字排序,优先级按顺序降低。
可选值:
input_priceoutput_pricethroughputlatencyinput_length
input_price_rangearray可选
限制输入价格范围,单位: ¥ / 1M Token,如:[1,2],表示筛选输入价格 price:1<=price<=2 的供应商
output_price_rangearray可选
限制输出价格范围,单位: ¥ / 1M Token
throughput_rangearray可选
限制吞吐范围,单位:tokens/s, 基于实时的吞吐数据调度
latency_rangearray可选
限制延迟范围,单位:s, 基于实时的延迟数据调度
input_length_rangearray可选
限制最大输入长度范围
allow_filter_prompt_lengthboolean可选
是否允许打开自动按照输入提示词长度筛选供应商,默认为 True
ignorearray可选
黑名单;从候选中排除
allow_fallbacksboolean可选
值为 True 时,系统按照用户指定筛选策略(延迟、吞吐、最大输入长度)无法找到合适的供应商时,为了保证接口可用,将按照指定排序策略调度给其他的供应商。默认为 True

1.3 供应商获取

供应商列表见:https://aiping.cn/supplierList,使用时大小写敏感

1.4 请求格式

以下是接口 /api/v1/chat/completions 请求格式,完整的参数列表见Body 参数

python
class VideoUrl(BaseModel):
    url: str

class ImageUrl(BaseModel):
    url: str

class TextItem(BaseModel):
    type: Literal["text"]
    text: str

class VideoItem(BaseModel):
    type: Literal["video_url"]
    video_url: VideoUrl

class ImgItem(BaseModel):
    type: Literal["image_url"]
    image_url: ImageUrl

# 使用“判别联合类型”来组合
ContentItem = Union[TextItem, VideoItem, ImgItem]

class Message(BaseModel):
    role: Literal["system", "user", "assistant"] = "user"
    content: Union[str, List[ContentItem]] = "say hello"

class StreamOptions(MyBaseModel):
    # 你可以根据平台实际需要拓展字段
    include_usage: bool = False

class ResponseFormat(BaseModel):
    type: Literal["text", "json_object"] = "text"  # 支持 "text" 或 "json" 之类

SortEnum = Literal["input_price", "output_price", "throughput", "latency", "input_length"]

class Provider(MyBaseModel):
    only: Optional[List[str]] = []
    order: Optional[List[str]] = []
    sort: Optional[List[SortEnum]] = None
    input_price_range: List[float] = []
    output_price_range: List[float] = []
    throughput_range: List[float] = []
    latency_range: List[float] = []
    input_length_range: List[int] = []
    allow_fallbacks: bool = True
    sort: Optional[List[SortEnum]] = None
    ignore: Optional[List[str]] = []
    allow_filter_prompt_length: bool = True

class ExtraBody(MyBaseModel):
    provider: Optional[Provider] = None
    enable_thinking: bool = None

class ChatRequest(MyBaseModel):
    model: str = "DeepSeek-R1-0528"
    messages: List[Message]
    max_completion_tokens: int = Field(None, description="新的最大的推理长度")
    max_tokens: int = Field(None, description="旧的最大的推理长度")
    temperature: float = Field(None, description="默认温度")
    top_p: float = Field(None, description="默认top_p")
    top_k: int = Field(None, description="默认top_k")
    presence_penalty: Optional[float] = None
    stream: Optional[bool] = False
    stream_options: Optional[StreamOptions] = None
    modalities: Optional[Literal["text", "audio"]] = None
    response_format: Optional[ResponseFormat] = None
    extra_body: Optional[ExtraBody] = None
    provider: Optional[Provider] = Field(default=None, description="这个字段不填,如果填了的话会覆盖extra_body中的provider字段")
    enable_thinking: bool = None

1.5 返回格式

AI Ping 对不同模型和供应商的 schema 进行规范化,以符合 OpenAI Chat API 的要求,除此之外,每个返回的 chunk 新增 provider 字段,表示提供服务的供应商。

python
class ChatCompletionChunkResponse(BaseModel):
    id: str
    object: str
    created: int
    model: str
    choices: List[Choice]
    usage: Usage
    provider: str


class Usage(BaseModel):
    prompt_tokens: int
    completion_tokens: int
    total_tokens: int
    prompt_tokens_details: 可选[TokensDetails] = None
    completion_tokens_details: 可选[TokensDetails] = None


class Delta(BaseModel):
    role: 可选[str]
    content: 可选[str]


class Choice(BaseModel):
    index: int
    delta: Delta


class TokensDetails(BaseModel):
    cached_tokens: 可选[int] = 0
    reasoning_tokens: 可选[int] = 0

以下是一个实例

json
{
  "id": "fe444exxxxxxx3",
  "object": "chat.completion.chunk",
  "created": 1758002830,
  "model": "DeepSeek-V3.1",
  "choices": [
    {
      "index": 0,
      "delta": {
        "role": "assistant",
        "content": " of"
      }
    }
  ],
  "usage": {
    "prompt_tokens": 108,
    "completion_tokens": 1500,
    "total_tokens": 1608,
    "prompt_tokens_details": {
      "cached_tokens": 0
    },
    "completion_tokens_details": {
      "reasoning_tokens": 0
    }
  },
  "provider": "XX云"
}

1.6 请求示例

流式请求

python
from openai import OpenAI

openai_client = OpenAI(
  base_url="https://aiping.cn/api/v1",
  api_key="<API_KEY>",
)
stream = openai_client.chat.completions.create(
    model="DeepSeek-R1-0528",
    stream=True,
    messages=[
        {
            "role": "user",
            "content": "Hello"
        }
    ]
)

for chunk in stream:
    if not getattr(chunk, "choices", None):
        continue

    content = getattr(chunk.choices[0].delta, "content", None)
    if content:
        print(content, end="", flush=True)
shell
curl -N -X POST https://aiping.cn/api/v1/chat/completions \
-H "Authorization: Bearer <API_KEY>" \
-H "Content-Type: application/json" \
-d '{
    "model": "DeepSeek-R1-0528",
    "stream": true,
    "messages": [
        {
            "role": "user",
            "content": "Hello"
        }
    ]
}'

非流式请求

python
from openai import OpenAI
openai_client = OpenAI(
  base_url="https://aiping.cn/api/v1",
  api_key="<API_KEY>",
)
completion = openai_client.chat.completions.create(
    model="DeepSeek-R1-0528",

    messages=[
        {
            "role": "user",
            "content": "Hello"
        }
    ]
)
print(completion.choices[0].message.content)
shell
curl -N -X POST https://aiping.cn/api/v1/chat/completions \
-H "Authorization: Bearer <API_KEY>" \
-H "Content-Type: application/json" \
-d '{
    "model": "DeepSeek-R1-0528",
    "stream": false,
    "messages": [
        {
            "role": "user",
            "content": "Hello"
        }
    ]
}'

VL 模型

python
from openai import OpenAI

openai_client = OpenAI(
    base_url="https://aiping.cn/api/v1",
    api_key="<API_KEY>",
)

response = openai_client.chat.completions.create(
    model="Qwen2.5-VL-32B-Instruct",
    stream=True,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image_url",
                    "image_url": {
                        "url": (
                            "https://img.alicdn.com/imgextra/i1/"
                            "O1CN01gDEY8M1W114Hi3XcN_!!6000000002727-0-tps-1024-406.jpg"
                        ),
                    },
                },
                {
                    "type": "text",
                    "text": "这道题怎么解答?"
                },
            ],
        }
    ]
)

for chunk in response:
    if not getattr(chunk, "choices", None):
        continue

    reasoning_content = getattr(chunk.choices[0].delta, "reasoning_content", None)
    if reasoning_content:
        print(reasoning_content, end="", flush=True)

    content = getattr(chunk.choices[0].delta, "content", None)
    if content:
        print(content, end="", flush=True)