在国内优雅的使用代理 Claude3 API

技术文章2周前更新 Flower
305 0

背景

Anthropic 的 Claude 3 据说能和 OpenAI 的 ChatGPT 4 有的一拼,那必须试试。

不过呢,最近 Claude 封禁非常严重,可能刚注册完,就被封了。

Your account has been disabled after an automatic review of your recent activities. Please review our Terms of Service and Acceptable Use Policy for more information.

在国内优雅的使用代理 Claude3 API

大概率是封禁了国外短信接码平台 sms-activate,而且并不是所有的地区接码正常,偶尔提示 Error sending code. Double check your phone number.

Anthropic 和 OpenAI 一样,不支持国内访问,直接访问会报错,需要准备代理,我用的是 Linode
App unavailable: Unfortunately, Claude is only available in certain regions right now. Please contact support if you believe you are receiving this message in error.
Supported countries and regions: Claude.ai

所以呢,不如直接某宝购买吧(10块1个,包含5美元),目前一直用着没出什么问题。

Anthropic 提供在线 Chat聊天工具 和 API,以下是 乐扣AI 使用 Chat 的截图。
在国内优雅的使用代理 Claude3 API

接下来,我们重点介绍 API 的使用,因为我们已经有了 Chat 客户端 Dify.

1. Claude3 API

Claude3 API 主要分为 MessagesText Completions 两种类型,不同模型使用不同的 API 类型。

以下是 Model comparison 中介绍目前所有 Model 的对比。

Claude 3 Opus Claude 3 Sonnet Claude 3 Haiku Claude 2.1 Claude 2 Claude Instant 1.2
Description Most powerful model for highly complex tasks Ideal balance of intelligence and speed for enterprise workloads Fastest and most compact model for
near-instant responsiveness Updated version of Claude 2 with improved accuracy Predecessor to Claude 3, offering strong all-round performance Our cheapest small and fast model, a predecessor of Claude Haiku.
Strengths Top-level performance, intelligence, fluency, and understanding Maximum utility at a lower price, dependable, balanced for scaled deployments Quick and accurate targeted performance Legacy model - performs less well than Claude 3 models Legacy model - performs less well than Claude 3 models Legacy model - performs less well than Claude 3 models
Multilingual Yes Yes Yes Yes, with less coverage, understanding, and skill than Claude 3 Yes, with less coverage, understanding, and skill than Claude 3 Yes, with less coverage, understanding, and skill than Claude 3
Vision Yes Yes Yes No No No
Latest API model name claude-3-opus-20240229 claude-3-sonnet-20240229 Coming soon claude-2.1 claude-2.0 claude-instant-1.2
API format Messages API Messages API Messages API Messages & Text Completions API Messages & Text Completions API Messages & Text Completions API
Comparative latency Moderately fast Fast Fastest Slower than Claude 3 model of similar intelligence Slower than Claude 3 model of similar intelligence Slower than Claude 3 model of similar intelligence
Context window 200K* 200K* 200K* 200K* 100K** 100K**
Max output 4096 tokens 4096 tokens 4096 tokens 4096 tokens 4096 tokens 4096 tokens
Cost (Input / Output per MTok^) $15.00 / $75.00 $3.00 / $15.00 $0.25 / $1.25 $8.00 / $24.0 $8.00 / $24.0 $0.80 / $2.40
Training data cut-off Aug 2023 Aug 2023 Aug 2023 Early 2023 Early 2023 Early 2023

*~150K words, ~680K unicode characters
**~75K words, ~350K unicode characters
^Millions of tokens

可以知道 claude-3-opus-20240229 的接口类型是 Messages,而 claude-2.0 Messages & Text Completions API 两者都支持。

为了真正在日常工作中使用 Claude API,我们需要做一个 代理转发,因为国内无法直接访问。而做代理之前,我们需要先在代理上体验原生是如何调用的,这样才方便做代理转发。

1.1 体验原生 Message API

  • 输入
curl https://api.anthropic.com/v1/messages \
     --header "x-api-key: <your_claude_secret_key>" \
     --header "anthropic-version: 2023-06-01" \
     --header "content-type: application/json" \
     --data \
'{
    "model": "claude-3-opus-20240229",
    "max_tokens": 1024,
    "messages": [
        {"role": "user", "content": "如何查看 Python 版本?"}
    ]
}'
  • 返回
{
    "id": "msg_018oD6xHFnSpin7WnA26vy9e",
    "type": "message",
    "role": "assistant",
    "content": [
        {
            "type": "text",
            "text": "有几种方法可以查看您的 Python 版本:\n\n1. 在命令行中运行 Python:\n\n打开命令行或终端,输入以下命令并按回车:\n\n```\npython --version\n```\n\n或者使用缩写形式:\n\n```\npython -V\n```\n\nPython 解释器将会输出它的版本号,例如 \"Python 3.9.5\"。\n\n2. 在 Python 交互模式下查看:\n\n打开命令行或终端,输入 `python` 并按回车进入 Python 交互模式,然后输入以下代码:\n\n```python\nimport sys\nprint(sys.version)\n```\n\n这将打印完整的 Python 版本信息,包括版本号、构建号和编译器等详细信息。\n\n3. 在 Python 脚本中查看:\n\n在 Python 脚本文件中,可以使用以下代码输出 Python 版本:\n\n```python\nimport sys\nprint(\"当前使用的 Python 版本 为: \", sys.version)\n```\n\n运行该脚本文件即可在控制台看到 Python 版本输出。\n\n4. 使用 IDE 或编辑器:\n\n大多数 Python IDE 或编辑器都会在界面的某个位置显示当前使用的 Python 解释器版本,例如状态栏、菜单栏等。具体可参考所用工具的帮助文档。\n\n 以上方法可以快速查看您正在使用的 Python 版本,以便进行开发和部署时的环境匹配与验证。"
        }
    ],
    "model": "claude-3-opus-20240229",
    "stop_reason": "end_turn",
    "stop_sequence": null,
    "usage": {
        "input_tokens": 18,
        "output_tokens": 441
    }
}

1.2 体验官方原生的 Text Completions API

  • 输入
curl https://api.anthropic.com/v1/complete \
     --header "x-api-key: <your_claude_secret_key>" \
     --header "anthropic-version: 2023-06-01" \
     --header "content-type: application/json" \
     --data \
'{
    "model": "claude-2.1",
    "max_tokens_to_sample": 1024,
    "prompt": "\n\nHuman: 你最后更新的大概时间是多少?我需要明确知道你的知识库时间\n\nAssistant:"
}'
  • 输出
{
    "type": "completion",
    "id": "compl_015VAvZcc8EE83aVppahFVZG",
    "completion": " 对不起,我没有关于自己最后更新时间的确切信息。 我是 Claude,一名由 Anthropic 训练的 AI 助手。",
    "stop_reason": "stop_sequence",
    "model": "claude-2.1",
    "stop": "\n\nHuman:",
    "log_id": "compl_015VAvZcc8EE83aVppahFVZG"
}

2. 准备 API 代理

前置条件

  • Nginx 已安装

2.1 修改 Nginx 配置

        location /claude/ {
            proxy_pass https://api.anthropic.com/;
            proxy_ssl_server_name on;
            proxy_set_header Host api.anthropic.com;
            proxy_buffering off;
            proxy_send_timeout 600s;
            proxy_cache off;
            chunked_transfer_encoding on;
        }

用 Claude 3 解读一下这段 Nginx 配置

这段 Nginx 配置是用于设置一个反向代理,将 /claude/ 路径下的请求转发到 https://api.anthropic.com/。具体来说:

  1. location /claude/ { 定义了一个新的location块,用于匹配以 /claude/ 开头的 URI 请求。

  2. proxy_pass https://api.anthropic.com/; 指定了将请求代理到 https://api.anthropic.com/

  3. proxy_ssl_server_name on; 启用了 SSL 服务器名称指示,以便 Nginx 可以将正确的服务器名称传递给上游服务器。

  4. proxy_set_header Host api.anthropic.com; 设置了发送到上游服务器的 Host 头为 api.anthropic.com

  5. proxy_buffering off; 禁用了代理缓冲,这意味着 Nginx 将直接将响应传递给客户端,而不会先缓存。

  6. proxy_send_timeout 600s; 设置了代理发送超时时间为 600 秒(10 分钟)。

  7. proxy_cache off; 禁用了代理缓存。

  8. chunked_transfer_encoding on; 启用了分块传输编码,这对于处理大型或动态响应很有用。

总的来说,这个配置将 /claude/ 路径下的所有请求代理到 https://api.anthropic.com/。它还设置了一些选项,如禁用缓冲和缓存、启用分块传输编码等,以优化代理性能和行为。这种配置通常用于将前端服务器与后端 API 或服务分离,实现更好的可伸缩性和安全性。

2.2 请求代理 Claude API

在国内请求代理的 Claude API

  • 输入
curl https://<your_nginx_proxy>/claude/v1/messages \
     --header "x-api-key: <your_claude_secret_key>" \
     --header "anthropic-version: 2023-06-01" \
     --header "content-type: application/json" \
     --data \
'{
    "model": "claude-3-sonnet-20240229",
    "max_tokens": 1024,
    "messages": [
        {"role": "user", "content": "如何查看 Python 版本?"}
    ]
}'
  • 输出
{
    "id": "msg_01V8EYWu8FD9coiAujzNvNQm",
    "type": "message",
    "role": "assistant",
    "content": [
        {
            "type": "text",
            "text": "有几种方法可以查看您计算机上安装的 Python 版本:\n\n1. 在命令行/终端中输入:\n\n```\npython --version\n```\n\n它将显示您当前正在使用的 Python 版本。\n\n2. 在 Python 交互式shell中输入:\n\n```python\nimport sys\nprint(sys.version)\n```\n\n它将打印出 Python 版本信息,包括版本号和其他细节。\n\n3. 如果您使用的是 Python IDE 或代码编辑器,通常在状态栏或底部会显示正在使用的 Python 版本号。\n\n4. 在 Windows 上,您可以打开命令提示符窗口,输入 `python` 并按 Enter 键。它将显示你当前使用的 Python 版本。\n\n5. 在 macOS 或 Linux 上,您可以在终端中输入 `python` 或 `python3` 并按 Enter 键。它也会显示您当前使用的 Python 版本。\n\n这些方法都可以让您快速查看计算机上安装的 Python 版本号。对于编写和运行 Python 程序非常有用,因为有时不同版本之间存在语法或功能上的差异。"
        }
    ],
    "model": "claude-3-sonnet-20240229",
    "stop_reason": "end_turn",
    "stop_sequence": null,
    "usage": {
        "input_tokens": 18,
        "output_tokens": 354
    }
}

一切正常。

3. 在 LLM 工具中使用代理 Claude API

比如 Dify,在模型供应商中配置 Claude API
在国内优雅的使用代理 Claude3 API

创建应用,模型选择 Claude
在国内优雅的使用代理 Claude3 API

开始问答,效果不错~
在国内优雅的使用代理 Claude3 API

© 版权声明

相关文章

暂无评论

暂无评论...