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

# 快速开始

> 使用 Curl、JavaScript Fetch、Python 或 OpenAI SDK 完成第一个 Yelu AI API 请求。

本指南将发送一个非流式聊天请求、读取助手消息，并说明如何切换到官方 OpenAI SDK。

## 1. 创建 API Key

登录 [Yelu Dashboard](https://yelu.ai) 创建 API Key，并立即保存到密码管理器或密钥管理服务。不要提交到源代码仓库。

## 2. 配置环境变量

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
export YELU_API_KEY="your_api_key"
```

生产环境应使用部署平台的 Secret Store 注入密钥，而不是写入镜像或配置文件。

## 3. 发送请求

<CodeGroup>
  ```bash Curl theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl https://api.yelu.ai/v1/chat/completions \
    -H "Authorization: Bearer $YELU_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "gpt-4o-mini",
      "messages": [
        {"role": "system", "content": "回答清晰且简洁。"},
        {"role": "user", "content": "用两句话解释语义搜索。"}
      ]
    }'
  ```

  ```javascript JavaScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
  const response = await fetch('https://api.yelu.ai/v1/chat/completions', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.YELU_API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      model: 'gpt-4o-mini',
      messages: [
        { role: 'system', content: '回答清晰且简洁。' },
        { role: 'user', content: '用两句话解释语义搜索。' },
      ],
    }),
  });
  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.error?.message ?? `HTTP ${response.status}`);
  }
  console.log((await response.json()).choices[0].message.content);
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
  import os
  import requests

  response = requests.post(
      "https://api.yelu.ai/v1/chat/completions",
      headers={"Authorization": f"Bearer {os.environ['YELU_API_KEY']}"},
      json={
          "model": "gpt-4o-mini",
          "messages": [
              {"role": "system", "content": "回答清晰且简洁。"},
              {"role": "user", "content": "用两句话解释语义搜索。"},
          ],
      },
      timeout=60,
  )
  response.raise_for_status()
  print(response.json()["choices"][0]["message"]["content"])
  ```
</CodeGroup>

<Tip>
  模型权限与账号相关。如果示例模型不可用，请调用 [`GET /v1/models`](/zh/api-reference/models) 并使用返回的模型 ID。
</Tip>

## 4. 使用 OpenAI SDK

<CodeGroup>
  ```bash Curl theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl https://api.yelu.ai/v1/models \
    -H "Authorization: Bearer $YELU_API_KEY"
  ```

  ```javascript JavaScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
  import OpenAI from 'openai';

  const client = new OpenAI({
    apiKey: process.env.YELU_API_KEY,
    baseURL: 'https://api.yelu.ai/v1',
  });
  const completion = await client.chat.completions.create({
    model: 'gpt-4o-mini',
    messages: [{ role: 'user', content: '你好，Yelu。' }],
  });
  console.log(completion.choices[0].message.content);
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
  import os
  from openai import OpenAI

  client = OpenAI(
      api_key=os.environ["YELU_API_KEY"],
      base_url="https://api.yelu.ai/v1",
  )
  completion = client.chat.completions.create(
      model="gpt-4o-mini",
      messages=[{"role": "user", "content": "你好，Yelu。"}],
  )
  print(completion.choices[0].message.content)
  ```
</CodeGroup>

成功响应包含 `id`、`model`、`choices` 和 `usage`。生成文本位于 `choices[0].message.content`。

<CardGroup cols={2}>
  <Card title="流式输出" icon="activity" href="/zh/guides/streaming">边生成边展示内容。</Card>
  <Card title="工具调用" icon="wrench" href="/zh/guides/function-calling">把模型连接到应用函数。</Card>
</CardGroup>
