在 API Store Hub 上使用函数调用与结构化 JSON 输出

通过严格 JSON Schema(strict: true)约束模型稳定输出可解析 JSON,直接写入数据库与业务接口。

准备好通过 API Store Hub 的统一 API 密钥、透明计费与多上游容灾把 AI 投入生产了吗?

在 API Store Hub 上使用函数调用与结构化 JSON 输出 visual architecture diagram — API Store Hub
在 API Store Hub 上使用函数调用与结构化 JSON 输出 · Base URL https://apistorehub.com · Key prefix sk-ash-

安装命令

LanguageTypeScript
RuntimeNode.js 20+ / Python 3.10+, OpenAI & Anthropic SDK
ProtocolOpenAI Compatible (/v1/chat/completions) & Anthropic (/v1/messages)
安装命令npm install openai zod

3 步快速接入流程

  1. 在 API Store Hub 创建 API 密钥: 登录 API Store Hub 控制台生成前缀为 sk-ash- 的密钥,并按需配置日/月消费上限。
  2. 安装 SDK 并配置 Base URL: 安装官方 SDK 包并将环境变量指向 https://apistorehub.com,以通过 /v1/messages 发起请求。
  3. 调用模型并启用流式与重试: 使用显式超时与带抖动重试调用 gpt-6.2-sol;每次调用均记录 token、TTFT 与精确费用。

生产级示例代码

TypeScript

import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://apistorehub.com/v1", apiKey: process.env.API_KEY });

const res = await client.chat.completions.create({
  model: "gpt-6.2-sol",
  messages: [{ role: "user", content: "Extract order: 2 keyboards at 450000 VND for Minh." }],
  response_format: {
    type: "json_schema",
    json_schema: {
      name: "order_extract",
      strict: true,
      schema: {
        type: "object",
        properties: {
          customer: { type: "string" },
          item: { type: "string" },
          qty: { type: "integer" },
          unit_price_vnd: { type: "integer" }
        },
        required: ["customer", "item", "qty", "unit_price_vnd"],
        additionalProperties: false
      }
    }
  }
});
console.log(JSON.parse(res.choices[0].message.content || "{}"));

cURL

curl https://apistorehub.com/v1/chat/completions \
  -H "Authorization: Bearer sk-ash-your_key" \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-6.2-sol","response_format":{"type":"json_object"},"messages":[{"role":"system","content":"Return JSON only."},{"role":"user","content":"Extract city from: Giao hang tai Da Nang"}]}'

推荐模型与实时价格

模型Token 单价上下文与端点
GPT 6.2 Sol6,700 ST / 1M tokens1.1M
Claude Sonnet 5.57,700 ST / 1M tokens1M
Gemini 4 Neon20,400 ST / 1M tokens2M

生产环境最佳实践清单

  • 启用 strict: true 时,请务必为所有嵌套对象设置 additionalProperties: false 并在 required 中列出全部字段。
  • 对长输出(> 500 token)始终开启 stream=true,以降低首字延迟(TTFT)并防止连接超时。
  • 切勿在前端代码中硬编码密钥;请通过服务端环境变量注入并在 API Store Hub 控制台定期轮换。
  • 为 429/502/529 配置带抖动的指数退避,并在同一密钥下准备备用模型实现无缝回退。

常见问题解答

将 Function Calling & Structured JSON 接入 API Store Hub 是否需要重写现有调用逻辑?
不需要。API Store Hub 原生兼容 OpenAI(https://apistorehub.com/v1)与 Anthropic Messages(https://apistorehub.com/v1/messages)协议,只需替换 Base URL 与 sk-ash- 密钥。
一个 API 密钥可以同时支持流式输出、函数调用和向量嵌入吗?
可以。API Store Hub 的单一密钥即可调用全部 187 个模型,完整支持 SSE 流式、严格 JSON Schema、工具调用与向量嵌入。
费用与速率限制如何控制?
每次请求均按 token 或按次透明计费并可在 API Store Hub 控制台查询,绝不透支余额,且支持为单个密钥设置消费上限。