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

# 兼容 Anthropic SDK

export const AnthropicCompatibilityModels = () => {
  if (typeof document === "undefined") {
    return null;
  } else {
    let attempts = 0;
    const maxAttempts = 50;
    const INIT_DISPLAY_COUNT = 3;
    const interval = setInterval(() => {
      const clientComponent = document.getElementById("anthropic-compatibility-models");
      if (clientComponent && window.jiekouRemoteData.llmModels.status === 'loaded') {
        const modelList = window.jiekouRemoteData.llmModels.data.filter(model => {
          return (model.endpoints || []).includes('anthropic');
        });
        let displayModels = modelList.slice(0, INIT_DISPLAY_COUNT).map(model => {
          return `<li><span class="model-id-item">${model.id}</span></li>`;
        }).join('');
        let showMoreButton = '';
        if (modelList.length > INIT_DISPLAY_COUNT) {
          showMoreButton = `<button id="show-more-anthropic-compatibility-model-btn" style="margin-left: 32px; color: rgb(40 116 255)">查看更多</button>`;
        }
        clientComponent.innerHTML = `
          <ul>${displayModels}</ul>
          ${showMoreButton}
        `;
        document.getElementById('show-more-anthropic-compatibility-model-btn')?.addEventListener('click', () => {
          clientComponent.innerHTML = `
            <ul>${modelList.map(model => {
            return `<li><span class="model-id-item">${model.id}</span></li>`;
          }).join('')}</ul>
          `;
        });
        clearInterval(interval);
      }
      attempts++;
      if (attempts >= maxAttempts) {
        clearInterval(interval);
      }
    }, 200);
    return <div id="anthropic-compatibility-models"></div>;
  }
};

JieKou AI 提供了与 Anthropic SDK 兼容的 API 服务，方便您集成到现有应用程序中。如果您已经使用 Anthropic SDK 开发了应用程序，只需要将 base URL 和 API Key 替换为 JieKou AI 的 API 地址和 API Key 即可。请参考下面的接入指南。

## 支持的模型

目前只有以下模型提供了 Anthropic SDK 兼容性支持：

<AnthropicCompatibilityModels />

## 快速开始

### 1. 安装 Anthropic SDK

<CodeGroup>
  ```bash Python icon="python" theme={null}
  pip install anthropic
  ```

  ```bash TypeScript icon="js" theme={null}
  npm install @anthropic-ai/sdk
  ```
</CodeGroup>

### 2. 初始化客户端

Anthropic SDK 会尝试从环境变量 `ANTHROPIC_API_KEY` 和 `ANTHROPIC_BASE_URL` 中分别获取 API Key 和 base URL。您也可以在初始化客户端的时候通过参数来指定。

* 基于环境变量设置

<CodeGroup>
  ```bash Bash icon="terminal" theme={null}
  export ANTHROPIC_BASE_URL="https://api.highwayapi.ai/anthropic"
  export ANTHROPIC_API_KEY="<JieKou AI  API Key>"
  ```
</CodeGroup>

* 通过在初始化 Anthropic 客户端时设置参数

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

  client = anthropic.Anthropic(
      base_url="https://api.highwayapi.ai/anthropic",
      api_key="<JieKou AI  API Key>",
      # 重写 header
      default_headers={
          "Content-Type": "application/json",
          "Authorization": "Bearer <JieKou AI  API Key>",
      }
  )
  ```

  ```typescript TypeScript icon="js" theme={null}
  import Anthropic from "@anthropic-ai/sdk";

  const anthropic = new Anthropic({
      baseURL: "https://api.highwayapi.ai/anthropic",
      apiKey: "<JieKou AI  API Key>",
      // 重写 header
      defaultHeaders: {
        "Content-Type": "application/json",
        Authorization: `Bearer <JieKou AI  API Key>`,
      }
  });
  ```
</CodeGroup>

### 3. 调用 API

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

  # 初始化客户端，如果您已经通过环境变量 `ANTHROPIC_BASE_URL` 和 `ANTHROPIC_API_KEY` 
  # 设置了 API Key 和 base URL，可以省略 `api_key` 和 `base_url` 参数。
  client = anthropic.Anthropic(
      base_url="https://api.highwayapi.ai/anthropic",
      api_key="<JieKou AI  API Key>",
      # 重写 header
      default_headers={
          "Content-Type": "application/json",
          "Authorization": "Bearer <JieKou AI  API Key>",
      }
  )

  message = client.messages.create(
      model="moonshotai/kimi-k2-instruct",
      max_tokens=1000,
      temperature=1,
      system=[
          {
              "type": "text",
              "text": "你是 JieKou AI AI 助手，你会以诚实专业的态度帮助用户，用中文回答问题。"
          }
      ],
      messages=[
          {
              "role": "user",
              "content": [
                  {
                      "type": "text",
                      "text": "你是谁?"
                  }
              ]
          }
      ]
  )

  print(message.content)
  ```

  ```typescript TypeScript icon="js" theme={null}
  import Anthropic from "@anthropic-ai/sdk";

  // 初始化客户端，如果您已经通过环境变量 `ANTHROPIC_BASE_URL` 和 `ANTHROPIC_API_KEY` 
  // 设置了 API Key 和 base URL，可以省略 `baseURL` 和 `apiKey` 参数。
  const anthropic = new Anthropic({
      baseURL: "https://api.highwayapi.ai/anthropic",
      apiKey: "<JieKou AI  API Key>",
      // 重写 header
      defaultHeaders: {
        "Content-Type": "application/json",
        Authorization: `Bearer <JieKou AI  API Key>`,
      },
  });

  const msg = await anthropic.messages.create({
    model: "moonshotai/kimi-k2-instruct",
    max_tokens: 1000,
    temperature: 1,
    system: "你是 JieKou AI AI 助手，你会以诚实专业的态度帮助用户，用中文回答问题。",
    messages: [
      {
        role: "user",
        content: [
          {
            type: "text",
            text: "你是谁?"
          }
        ]
      }
    ]
  });

  console.log(msg);
  ```
</CodeGroup>
