> ## Documentation Index
> Fetch the complete documentation index at: https://docs.aiapi.xiaoliu.fun/llms.txt
> Use this file to discover all available pages before exploring further.

# API 参考

> 次API 提供强大的人工智能能力,兼容 OpenAI 接口标准,支持多种编程语言和开发框架

## Base URL

所有 API 请求的基础 URL:

```text theme={null}
https://aiapi.xiaoliu.fun/v1
```

## 认证方式

所有 API 请求都需要在请求头中包含 API Key:

```bash theme={null}
Authorization: Bearer sk-your-api-key-here
```

## 请求格式

所有 API 请求都使用 JSON 格式:

```bash theme={null}
curl -X POST "https://aiapi.xiaoliu.fun/v1/chat/completions" \
  -H "Authorization: <YOUR_API_KEY>" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4",
    "messages": [
      {
        "role": "system",
        "content": "string"
      }
    ]
  }'
```

## 响应格式

API 响应也使用 JSON 格式:

```json theme={null}
{
  "id": "string",
  "object": "chat.completion",
  "created": 0,
  "model": "string",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "system",
        "content": "string",
        "name": "string",
        "tool_calls": [
          {
            "id": "string",
            "type": "function",
            "function": {
              "name": "string",
              "arguments": "string"
            }
          }
        ],
        "tool_call_id": "string",
        "reasoning_content": "string"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 0,
    "completion_tokens": 0,
    "total_tokens": 0,
    "prompt_tokens_details": {
      "cached_tokens": 0,
      "text_tokens": 0,
      "audio_tokens": 0,
      "image_tokens": 0
    },
    "completion_tokens_details": {
      "text_tokens": 0,
      "audio_tokens": 0,
      "reasoning_tokens": 0
    }
  },
  "system_fingerprint": "string"
}
```

## 错误处理

API 使用标准的 HTTP 状态码:

| 状态码 | 说明               |
| --- | ---------------- |
| 200 | 请求成功             |
| 400 | 请求参数错误           |
| 401 | 认证失败(API Key 无效) |
| 429 | 请求频率超限           |
| 500 | 服务器错误            |

错误响应示例:

```json theme={null}
{
  "error": {
    "message": "string",
    "type": "string",
    "param": "string",
    "code": "string"
  }
}
```

## 流式响应

大多数文本生成接口支持流式响应,实时返回生成的内容:

```python theme={null}
client = openai.OpenAI(
    api_key="sk-your-api-key-here",
    base_url="https://aiapi.xiaoliu.fun/v1"
)

stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "讲个故事"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")
```

## SDK 和工具

### 官方 SDK

API 兼容 OpenAI SDK,可直接使用:

<CodeGroup>
  ```bash Python theme={null}
  pip install openai
  ```

  ```bash JavaScript theme={null}
  npm install openai
  ```
</CodeGroup>
