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

# 文本对话模型

> 使用 FocusAPI 调用 GPT、Claude、Gemini、DeepSeek 等文本对话模型：支持 curl、OpenAI Python SDK、Claude SDK、Gemini SDK、LiteLLM、流式输出与多轮消息。

文本对话是最通用的能力，适合聊天、总结、翻译、代码问答、客服机器人和知识库问答。多数模型可以通过 OpenAI 兼容的 Chat Completions 调用。

## 最常用接口

```http theme={null}
POST /v1/chat/completions
```

Base URL：

```text theme={null}
https://www.focusapi.cn/v1
```

鉴权、Base URL 和首次请求见 [快速开始](/quickstart)，本页只说明文本对话模型的选择与差异。

## 支持的模型来源

| 来源           | 适合场景               | 调用说明                                                   |
| ------------ | ------------------ | ------------------------------------------------------ |
| OpenAI / GPT | 通用对话、代码、多模态生态      | 通常使用 OpenAI SDK 最方便                                    |
| Claude       | 长文本、写作、复杂上下文       | 可用 OpenAI 兼容方式；如平台支持，也可用 Anthropic 格式                  |
| Gemini       | 快速响应、多模态、Google 生态 | 可用 OpenAI 兼容方式；图片输入见 [图片分析](/models/image-analysis)    |
| DeepSeek     | 中文对话、代码、推理性价比      | `deepseek-chat` 适合通用对话，推理任务见 [推理模型](/models/reasoning) |
| 其他兼容模型       | 垂直场景、低价或高并发        | 以模型广场能力标签为准                                            |

<Warning>
  `model` 字段必须使用控制台模型广场里的完整模型 ID。模型名区分大小写。
</Warning>

## curl 调用

curl 适合快速验证 Key、Base URL 和模型名是否正确。

```bash theme={null}
curl https://www.focusapi.cn/v1/chat/completions \
  -H "Authorization: Bearer sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [
      {"role": "user", "content": "用一句话介绍 FocusAPI"}
    ]
  }'
```

## Python SDK 调用

不同 SDK 的 Base URL 形态不完全一样：

* OpenAI SDK / LiteLLM：使用 OpenAI 兼容地址 `https://www.focusapi.cn/v1`
* Claude SDK：使用 Anthropic 原生地址 `https://www.focusapi.cn`
* Gemini SDK：使用 Gemini 原生地址 `https://www.focusapi.cn`

```bash theme={null}
pip install openai anthropic google-genai litellm
```

<CodeGroup>
  ```python OpenAI SDK theme={null}
  from openai import OpenAI

  client = OpenAI(
      base_url="https://www.focusapi.cn/v1",
      api_key="sk-your-api-key",
  )

  response = client.chat.completions.create(
      model="gpt-4o-mini",
      messages=[{"role": "user", "content": "Hello"}],
  )

  print(response.choices[0].message.content)
  ```

  ```python Claude SDK theme={null}
  from anthropic import Anthropic

  client = Anthropic(
      api_key="sk-your-api-key",
      base_url="https://www.focusapi.cn",
  )

  message = client.messages.create(
      model="claude-sonnet-4-20250514",
      max_tokens=1024,
      messages=[
          {"role": "user", "content": "Hello from FocusAPI"},
      ],
  )

  print(message.content[0].text)
  ```

  ```python Gemini SDK theme={null}
  from google import genai
  from google.genai import types

  client = genai.Client(
      api_key="sk-your-api-key",
      http_options=types.HttpOptions(base_url="https://www.focusapi.cn"),
  )

  response = client.models.generate_content(
      model="gemini-2.0-flash",
      contents="Hello from FocusAPI",
  )

  print(response.text)
  ```

  ```python LiteLLM theme={null}
  import litellm

  response = litellm.completion(
      model="openai/gpt-4o-mini",
      api_key="sk-your-api-key",
      api_base="https://www.focusapi.cn/v1",
      messages=[
          {"role": "user", "content": "Hello from FocusAPI"},
      ],
  )

  print(response.choices[0].message.content)
  ```
</CodeGroup>

<Note>
  原生 Claude / Gemini SDK 适合你已经在使用对应厂商 SDK，或需要厂商特殊参数的场景。普通文本对话优先用 OpenAI SDK，迁移成本最低。
</Note>

## 多轮对话

多轮对话通过 `messages` 传入历史上下文：

```json theme={null}
{
  "model": "gpt-4o-mini",
  "messages": [
    { "role": "system", "content": "你是一个简洁的助手" },
    { "role": "user", "content": "帮我总结这段内容" },
    { "role": "assistant", "content": "请把内容发给我。" },
    { "role": "user", "content": "..." }
  ]
}
```

上下文越长，输入 token 越多。成本控制见 [计费与用量](/billing/pricing)。

## 流式输出

设置 `"stream": true` 后返回 SSE 流：

```python theme={null}
stream = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "写一首短诗"}],
    stream=True,
)

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

如果流式中途断开或长时间无响应，先看 [524 超时](/errors/524)。

## 什么时候不要用本文本接口

* 需要图片生成：看 [图像生成](/models/image-generation)
* 需要图片理解、截图分析：看 [图片分析](/models/image-analysis)
* 需要视频生成：看 [视频生成](/models/video-generation)
* 需要语音转文字或 TTS：看 [语音模型](/models/audio)
* 需要向量化或重排：看 [Embedding / Rerank](/models/embedding-rerank)
* 需要工具调用参数：看 [工具调用](/models/tool-calling)
