Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.aioagi.tech/llms.txt

Use this file to discover all available pages before exploring further.

AIOAGI 的 OpenAI 兼容接口可以直接配合常见 OpenAI SDK 使用。你只需要设置 baseURLapiKey

TypeScript 封装

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.AIO_API_KEY,
  baseURL: process.env.AIO_BASE_URL ?? "https://api.aiearth.dev/v1",
});

export async function askAio(prompt: string) {
  const response = await client.chat.completions.create({
    model: process.env.AIO_DEFAULT_MODEL ?? "gpt-4o-mini",
    messages: [{ role: "user", content: prompt }],
  });

  return response.choices[0]?.message?.content ?? "";
}

Python 封装

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["AIO_API_KEY"],
    base_url=os.environ.get("AIO_BASE_URL", "https://api.aiearth.dev/v1"),
)


def ask_aio(prompt: str) -> str:
    response = client.chat.completions.create(
        model=os.environ.get("AIO_DEFAULT_MODEL", "gpt-4o-mini"),
        messages=[{"role": "user", "content": prompt}],
    )
    return response.choices[0].message.content or ""

流式输出

const stream = await client.chat.completions.create({
  model: "gpt-4o-mini",
  messages: [{ role: "user", content: "逐步解释什么是 RAG。" }],
  stream: true,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}

重试建议

  • 对网络错误和 429 做指数退避重试。
  • 对不可重试错误,例如 401 和参数错误,直接返回明确提示。
  • 为关键业务配置备用模型和备用端点。
  • 记录请求耗时和错误码,便于排查线路问题。