> ## Documentation Index
> Fetch the complete documentation index at: https://wiki.llm.sg/llms.txt
> Use this file to discover all available pages before exploring further.

# 快速开始

> 通过 cURL、Python 或 JavaScript 配置 llm.sg 接入并发送第一个对话请求。

本文帮助你完成三个步骤：配置 API Key、获取模型 ID、发送第一个对话请求。

> 示例按 OpenAI Chat Completions 兼容格式编写。模型列表端点已确认需要鉴权；对话接口、SDK 和流式行为仍需使用有效 llm.sg 密钥验证。

## 1. 准备 API Key

按照[账号与 API Key](https://wiki.llm.sg/zh-Hans/docs/account-and-api-keys.md)获取密钥，并配置环境变量 `LLM_SG_API_KEY`。

## 2. 获取模型 ID

在终端请求模型列表：

```bash theme={null}
curl --fail-with-body --silent --show-error \
  'https://api.llm.sg/v1/models' \
  -H "Authorization: Bearer ${LLM_SG_API_KEY}"
```

获取列表后，选择可用于文本对话的模型，将其 ID 原样配置为 `LLM_SG_MODEL`。若接口采用兼容列表格式，模型 ID 位于 `data` 数组中各项的 `id` 字段；实际字段需以返回结果为准。

macOS 或 Linux：

```bash theme={null}
export LLM_SG_MODEL='YOUR_MODEL_ID'
```

Windows PowerShell：

```powershell theme={null}
$env:LLM_SG_MODEL = 'YOUR_MODEL_ID'
```

`YOUR_MODEL_ID` 是占位符，需要替换为平台提供的实际模型 ID。不要直接使用其他平台的模型别名。

## 3. 发送对话请求

选择以下任意一种方式。所有示例均使用 `LLM_SG_API_KEY`，SDK 示例还会读取 `LLM_SG_MODEL`。

> [tab] cURL
>
> 以下命令用于 Bash 或 Zsh。将 JSON 中的 `YOUR_MODEL_ID` 替换为上一步获取的实际模型 ID：
>
> ```bash theme={null}
> curl --fail-with-body --silent --show-error \
>   'https://api.llm.sg/v1/chat/completions' \
>   -H "Authorization: Bearer ${LLM_SG_API_KEY}" \
>   -H 'Content-Type: application/json' \
>   --data-raw '{
>     "model": "YOUR_MODEL_ID",
>     "messages": [
>       {"role": "user", "content": "你好，请用一句话介绍你自己。"}
>     ]
>   }'
> ```

> [tab] Python
>
> 安装 SDK：
>
> ```bash theme={null}
> python -m pip install openai
> ```
>
> 将以下代码保存为 `quickstart.py`，然后执行 `python quickstart.py`：
>
> ```python theme={null}
> import os
> from openai import OpenAI
>
> client = OpenAI(
>     api_key=os.environ["LLM_SG_API_KEY"],
>     base_url="https://api.llm.sg/v1",
> )
>
> response = client.chat.completions.create(
>     model=os.environ["LLM_SG_MODEL"],
>     messages=[{"role": "user", "content": "你好，请用一句话介绍你自己。"}],
> )
>
> print(response.choices[0].message.content)
> ```

> [tab] JavaScript
>
> 安装 SDK：
>
> ```bash theme={null}
> npm install openai
> ```
>
> 将以下代码保存为 `quickstart.mjs`，然后执行 `node quickstart.mjs`：
>
> ```javascript theme={null}
> import OpenAI from "openai";
>
> const apiKey = process.env.LLM_SG_API_KEY;
> const model = process.env.LLM_SG_MODEL;
> if (!apiKey || !model) {
>   throw new Error("请先设置 LLM_SG_API_KEY 和 LLM_SG_MODEL");
> }
>
> const client = new OpenAI({
>   apiKey,
>   baseURL: "https://api.llm.sg/v1",
> });
>
> const response = await client.chat.completions.create({
>   model,
>   messages: [{ role: "user", content: "你好，请用一句话介绍你自己。" }],
> });
>
> console.log(response.choices[0].message.content);
> ```

## 如何判断调用成功

在兼容 Chat Completions 的响应中，通常可以从 `choices[0].message.content` 读取文本回答。若返回 `usage`，可以据此查看本次请求的 Token 用量；它不直接代表最终扣费金额。

如请求失败，保留 HTTP 状态码、错误消息以及响应中的请求 ID，再参照[错误排查](https://wiki.llm.sg/zh-Hans/docs/troubleshooting.md)处理。

## 在第三方应用中配置

如果应用支持自定义 OpenAI 兼容服务，可按下列约定填写，具体是否兼容需要实际验证：

| 配置项 | 填写内容 |
| - | - |
| 服务类型 | 自定义 OpenAI 兼容服务 |
| Base URL | `https://api.llm.sg/v1` |
| API Key | 你的 llm.sg API Key |
| 模型 | llm.sg 提供的完整模型 ID |

如果应用要求完整接口地址，则填写 `https://api.llm.sg/v1/chat/completions`。部分应用会自动追加 `/v1`，请根据其字段说明和实际请求地址检查是否重复。

接下来：[模型选择](https://wiki.llm.sg/zh-Hans/docs/models.md)或[流式输出与多轮对话](https://wiki.llm.sg/zh-Hans/docs/chat.md)。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.