当前位置: 代码网 > it编程>前端脚本>Python > Python 调用Anthropic API 的两种方式

Python 调用Anthropic API 的两种方式

2025年12月24日 Python 我要评论
本文介绍了python调用anthropic api的两种主要方式,包括requests 调用官方 sdk 调用https://api-docs.deepseek.com/zh-cn/guides/a

 本文介绍了python调用anthropic api的两种主要方式,包括

  • requests 调用
  • 官方 sdk 调用
https://api-docs.deepseek.com/zh-cn/guides/anthropic_api

python 调用 anthropic api 的两种方式

anthropic api 本质是标准 http 接口,python 中通常有两种主流调用方式:

  1. 使用 requests:轻量、灵活、适合工程封装
  2. 使用官方 sdk:封装完善、自动处理部分配置

下文对两种方式进行对比与示例说明。

一、使用 requests 调用(适合生产环境工程封装)

requests 是 python 最通用的 http 客户端,适合你在框架(django / fastapi / 微服务)中封装统一的 ai 调用模块。

1. 基本非流式调用

import requests

api_key = "your_api_key"
base_url = "https://api.deepseek.com/anthropic/v1/messages"

headers = {
    "content-type": "application/json",
    "x-api-key": api_key,
    "anthropic-version": "2023-06-01",
}

payload = {
    "model": "deepseek-chat",
    "max_tokens": 2048,
    "messages": [{"role": "user", "content": "你好"}],
}

resp = requests.post(base_url, json=payload, headers=headers, timeout=600)
print(resp.json())

2. 流式响应(sse stream)

import requests
import json

api_key = "your_api_key"
base_url = "https://api.deepseek.com/anthropic/v1/messages"

headers = {
    "content-type": "application/json",
    "x-api-key": api_key,
    "anthropic-version": "2023-06-01",
}

payload = {
    "model": "deepseek-chat",
    "max_tokens": 2000,
    "messages": [{"role": "user", "content": "介绍一下你自己"}],
    "stream": true,
}

with requests.post(base_url, json=payload, headers=headers, stream=true, timeout=600) as r:
    for line in r.iter_lines():
        if not line:
            continue
        data = line.decode("utf-8")
        if data.startswith("data: "):
            content = data[6:]
            if content == "[done]":
                break
            event = json.loads(content)
            delta = event.get("delta", {}).get("text")
            if delta:
                print(delta, end="", flush=true)

3. 使用毫秒超时(api_timeout_ms)

anthropic 配置通常使用毫秒,需要转成秒:

api_timeout_ms = 600000
requests.post(url, json=payload, headers=headers, timeout=api_timeout_ms / 1000)

二、使用官方 sdk 调用(简单、封装完善)

anthropic 提供官方 python sdk,支持自动处理 headers、base_url、超时管理等。

安装:

pip install anthropic

1. 基本调用

from anthropic import anthropic

client = anthropic(
    api_key="your_api_key",
    base_url="https://api.deepseek.com/anthropic",
    timeout=600,
)

resp = client.messages.create(
    model="deepseek-chat",
    max_tokens=2048,
    messages=[{"role": "user", "content": "你好"}],
)

print(resp)

2. 流式调用(逐 token 输出)

from anthropic import anthropic

client = anthropic(
    api_key="your_api_key",
    base_url="https://api.deepseek.com/anthropic",
)

with client.messages.stream(
    model="deepseek-chat",
    max_tokens=2048,
    messages=[{"role": "user", "content": "写一段话"}],
) as stream:
    for event in stream:
        if event.type == "message_delta" and event.delta.text:
            print(event.delta.text, end="", flush=true)

三、两种方式对比

对比项requests官方 sdk
轻量性
灵活度高(可自由封装)
上手难度需要写 headers、处理 sse简单直接
流式支持需要手动解析 sse官方封装
配置管理(base_url、timeout)手动控制构造参数即可
适合场景生产级 api 服务、统一调用层技术验证、快速开发

两种方式都稳定可靠,你可以针对团队习惯选择合适的方式。

到此这篇关于python 调用anthropic api 的两种方式的文章就介绍到这了,更多相关python调用 anthropic api 内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com