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

# 语言模型

> 语言模型（LLM）使用说明手册

<Callout icon="lightbulb" iconType="regular" color="#4885FF">
  支持通过 Anthropic 和 OpenAI 兼容接口进行文本生成调用。
</Callout>

## OpenAI 兼容接口 (推荐)

如部分功能本文档未提及，请参考 [OpenAI官方文档](https://platform.openai.com/docs/api-reference/chat/create)

### 消息体结构说明

| **消息类型**  | **功能描述**                        | **示例内容**           |
| :-------- | :------------------------------ | :----------------- |
| system    | 模型指令，设定AI角色，描述模型应一般如何行为和响应      | 例如：“你是有10年经验的儿科医生” |
| user      | 用户输入，将最终用户的消息传递给模型              | 例如：“幼儿持续低烧应如何处理？“  |
| assistant | 模型生成的历史回复，为模型提供示例，说明它应该如何回应当前请求 | 例如：“建议先测量体温…”      |

若希望模型按照分层指令进行响应，可以利用消息角色来提升输出的质量。不过，消息角色并不总是有确定性的表现，因此建议尝试多种用法，比较不同方法带来的效果，找出最适合你的方案。

### 基础对话

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

  client = OpenAI(
      base_url="https://BASE_URL/v1",
      api_key="", # 替换为你在本站点的 API Key
  )

  response = client.chat.completions.create(
      model="deepseek-r1",
      messages=[
          {
              "role": "user",
              "content": "say 1"
          }
      ]
  )

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

  ```javascript Node.js theme={null}
  import OpenAI from "openai";

  const client = new OpenAI(
      base_url="https://BASE_URL/v1",
      api_key="", # 替换为你在本站点的 API Key
  );

  const response = await client.chat.completions.create({
    model: "deepseek-r1",
    messages: [
      {
        role: "user",
        content: "say 1",
      },
    ],
  });

  console.log(response.choices[0].message.content);
  ```
</CodeGroup>

### 流式响应

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

  client = OpenAI(
      base_url="https://BASE_URL/v1",
      api_key="", # 替换为你在本站点的 API Key
  )

  stream = client.chat.completions.create(
      model="deepseek-r1",
      messages=[
          {
              "role": "user",
              "content": "写一首关于春天的诗"
          }
      ],
      stream=True
  )

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

  ```javascript Node.js theme={null}
  import OpenAI from "openai";

  const client = new OpenAI(
      base_url="https://BASE_URL/v1",
      api_key="", # 替换为你在本站点的 API Key
  );

  const stream = await client.chat.completions.create({
    model: "deepseek-r1",
    messages: [
      {
        role: "user",
        content: "写一首关于春天的诗",
      },
    ],
    stream: true,
  });

  for await (const chunk of stream) {
    if (chunk.choices[0].delta.content) {
      process.stdout.write(chunk.choices[0].delta.content);
    }
  }
  ```
</CodeGroup>

### 工具调用（Function Calling）

更多详情参考 [OpenAI 函数调用指南](https://platform.openai.com/docs/guides/tools)

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

  client = OpenAI(
      base_url="https://BASE_URL/v1",
      api_key="", # 替换为你在本站点的 API Key
  )

  tools = [
      {
          "type": "function",
          "function": {
              "name": "get_weather",
              "description": "获取指定城市的天气信息",
              "parameters": {
                  "type": "object",
                  "properties": {
                      "city": {
                          "type": "string",
                          "description": "城市名称"
                      }
                  },
                  "required": ["city"]
              }
          }
      }
  ]

  response = client.chat.completions.create(
      model="deepseek-r1",
      messages=[
          {
              "role": "user",
              "content": "北京今天天气怎么样？"
          }
      ],
      tools=tools
  )

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

  ```javascript Node.js theme={null}
  import OpenAI from "openai";

  const client = new OpenAI(
      base_url="https://BASE_URL/v1",
      api_key="", # 替换为你在本站点的 API Key
  );

  const tools = [
    {
      type: "function",
      function: {
        name: "get_weather",
        description: "获取指定城市的天气信息",
        parameters: {
          type: "object",
          properties: {
            city: {
              type: "string",
              description: "城市名称",
            },
          },
          required: ["city"],
        },
      },
    },
  ];

  const response = await client.chat.completions.create({
    model: "deepseek-r1",
    messages: [
      {
        role: "user",
        content: "北京今天天气怎么样？",
      },
    ],
    tools: tools,
  });

  console.log(response.choices[0].message);
  ```
</CodeGroup>

### Response 接口

* 目前该接口仅支持OpenAI模型
* 具体使用方案请参考 [OpenAI官方文档](https://platform.openai.com/docs/api-reference/responses/create)

### 注意事项

如果在使用过程中遇到任何问题：

* 联系我们的技术支持团队
* 在我们的 [工单中心](#) 提交工单反馈

<Warning>
  1. 使用时需要将 `OPENAI_BASE_URL` 设置为 `https://BASE_URL/v1`
  2. `OPENAI_API_KEY` 应设置为您的 API Key
  3. `temperature` 参数取值范围为(0.0, 1.0]，推荐使用 1.0，超出范围会返回错误
  4. 部分 OpenAI 参数（如`presence_penalty`、`frequency_penalty`、`logit_bias` 等）会被忽略
  5. 旧版的`function_call` 已废弃，请使用 `tools` 参数
</Warning>

## Anthropic 兼容接口

如部分功能本文档未提及，请参考 [Anthropic官方文档](https://docs.claude.com/en/api/messages)

### 基础对话

<CodeGroup>
  ```python Python theme={null}
  import anthropic

  client = anthropic.Anthropic(
      base_url="https://BASE_URL/anthropic",
      api_key=""  # 替换为你在本站点的 API Key
  )

  message = client.messages.create(
      model="deepseek-r1",
      max_tokens=1024,
      messages=[
          {
              "role": "user",
              "content": "请用简单的语言解释什么是机器学习"
          }
      ]
  )

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

  ```javascript Node.js theme={null}
  import Anthropic from "@anthropic-ai/sdk";

  const client = new Anthropic(
      base_url="https://BASE_URL/anthropic",
      api_key=""  # 替换为你在本站点的 API Key
  );

  const message = await client.messages.create({
    model: "deepseek-r1",
    max_tokens: 1024,
    messages: [
      {
        role: "user",
        content: "请用简单的语言解释什么是机器学习",
      },
    ],
  });

  console.log(message.content[0].text);
  ```
</CodeGroup>

### 流式响应

<CodeGroup>
  ```python Python theme={null}
  import anthropic

  client = anthropic.Anthropic(
      base_url="https://BASE_URL/anthropic",
      api_key=""  # 替换为你在本站点的 API Key
  )

  with client.messages.stream(
      model="deepseek-r1",
      max_tokens=1024,
      messages=[
          {
              "role": "user",
              "content": "写一首关于春天的诗"
          }
      ]
  ) as stream:
      for text in stream.text_stream:
          print(text, end="", flush=True)
  ```

  ```javascript Node.js theme={null}
  import Anthropic from "@anthropic-ai/sdk";

  const client = new Anthropic(
      base_url="https://BASE_URL/anthropic",
      api_key=""  # 替换为你在本站点的 API Key
  );

  const stream = await client.messages.stream({
    model: "deepseek-r1",
    max_tokens: 1024,
    messages: [
      {
        role: "user",
        content: "写一首关于春天的诗",
      },
    ],
  });

  for await (const chunk of stream) {
    if (
      chunk.type === "content_block_delta" &&
      chunk.delta.type === "text_delta"
    ) {
      process.stdout.write(chunk.delta.text);
    }
  }
  ```
</CodeGroup>

### 工具调用（Function Calling）

更多详情参考 [Anthropic 函数调用指南](https://docs.claude.com/en/docs/agents-and-tools/tool-use/overview)

<CodeGroup>
  ```python Python theme={null}
  import anthropic

  client = anthropic.Anthropic(
      base_url="https://BASE_URL/anthropic",
      api_key=""  # 替换为你在本站点的 API Key
  )

  tools = [
      {
          "name": "get_weather",
          "description": "获取指定城市的天气信息",
          "input_schema": {
              "type": "object",
              "properties": {
                  "city": {
                      "type": "string",
                      "description": "城市名称"
                  }
              },
              "required": ["city"]
          }
      }
  ]

  message = client.messages.create(
      model="deepseek-r1",
      max_tokens=1024,
      tools=tools,
      messages=[
          {
              "role": "user",
              "content": "北京今天天气怎么样？"
          }
      ]
  )

  print(message.content)
  ```

  ```javascript Node.js theme={null}
  import Anthropic from "@anthropic-ai/sdk";

  const client = new Anthropic(
      base_url="https://BASE_URL/anthropic",
      api_key=""  # 替换为你在本站点的 API Key
  );

  const tools = [
    {
      name: "get_weather",
      description: "获取指定城市的天气信息",
      input_schema: {
        type: "object",
        properties: {
          city: {
            type: "string",
            description: "城市名称",
          },
        },
        required: ["city"],
      },
    },
  ];

  const message = await client.messages.create({
    model: "deepseek-r1",
    max_tokens: 1024,
    tools: tools,
    messages: [
      {
        role: "user",
        content: "北京今天天气怎么样？",
      },
    ],
  });

  console.log(message.content);
  ```
</CodeGroup>

## 注意事项

<Warning>
  1. 使用时需要将 `ANTHROPIC_BASE_URL` 设置为 `https://BASE_URL/v1`
  2. `ANTHROPIC_API_KEY` 应设置为您的 API Key
</Warning>
