Python 調(diào)用Anthropic API 的兩種方式
本文介紹了Python調(diào)用Anthropic API的兩種主要方式,包括
- requests 調(diào)用
- 官方 SDK 調(diào)用
https://api-docs.deepseek.com/zh-cn/guides/anthropic_api
Python 調(diào)用 Anthropic API 的兩種方式
Anthropic API 本質(zhì)是標(biāo)準(zhǔn) HTTP 接口,Python 中通常有兩種主流調(diào)用方式:
- 使用
requests:輕量、靈活、適合工程封裝 - 使用官方 SDK:封裝完善、自動(dòng)處理部分配置
下文對(duì)兩種方式進(jìn)行對(duì)比與示例說(shuō)明。
一、使用 requests 調(diào)用(適合生產(chǎn)環(huán)境工程封裝)
requests 是 Python 最通用的 HTTP 客戶(hù)端,適合你在框架(Django / FastAPI / 微服務(wù))中封裝統(tǒng)一的 AI 調(diào)用模塊。
1. 基本非流式調(diào)用
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. 流式響應(yīng)(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. 使用毫秒超時(shí)(API_TIMEOUT_MS)
Anthropic 配置通常使用毫秒,需要轉(zhuǎn)成秒:
API_TIMEOUT_MS = 600000 requests.post(url, json=payload, headers=headers, timeout=API_TIMEOUT_MS / 1000)
二、使用官方 SDK 調(diào)用(簡(jiǎn)單、封裝完善)
Anthropic 提供官方 Python SDK,支持自動(dòng)處理 headers、base_url、超時(shí)管理等。
安裝:
pip install anthropic
1. 基本調(diào)用
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. 流式調(diào)用(逐 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": "寫(xiě)一段話(huà)"}],
) as stream:
for event in stream:
if event.type == "message_delta" and event.delta.text:
print(event.delta.text, end="", flush=True)
三、兩種方式對(duì)比
| 對(duì)比項(xiàng) | requests | 官方 SDK |
|---|---|---|
| 輕量性 | 高 | 中 |
| 靈活度 | 高(可自由封裝) | 中 |
| 上手難度 | 需要寫(xiě) headers、處理 SSE | 簡(jiǎn)單直接 |
| 流式支持 | 需要手動(dòng)解析 SSE | 官方封裝 |
| 配置管理(base_url、timeout) | 手動(dòng)控制 | 構(gòu)造參數(shù)即可 |
| 適合場(chǎng)景 | 生產(chǎn)級(jí) API 服務(wù)、統(tǒng)一調(diào)用層 | 技術(shù)驗(yàn)證、快速開(kāi)發(fā) |
兩種方式都穩(wěn)定可靠,你可以針對(duì)團(tuán)隊(duì)習(xí)慣選擇合適的方式。
到此這篇關(guān)于Python 調(diào)用Anthropic API 的兩種方式的文章就介紹到這了,更多相關(guān)Python調(diào)用 Anthropic API 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)鬧鐘定時(shí)播放音樂(lè)功能
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)鬧鐘定時(shí)播放音樂(lè)功能,播放wav格式的音頻,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
對(duì)Python新手編程過(guò)程中如何規(guī)避一些常見(jiàn)問(wèn)題的建議
這篇文章中作者對(duì)Python新手編程過(guò)程中如何規(guī)避一些常見(jiàn)問(wèn)題給出了建議,主要著眼于初學(xué)者對(duì)于一些常用函數(shù)方法在平時(shí)的使用習(xí)慣中的問(wèn)題給出建議,需要的朋友可以參考下2015-04-04
pytorch 把MNIST數(shù)據(jù)集轉(zhuǎn)換成圖片和txt的方法
這篇文章主要介紹了pytorch 把MNIST數(shù)據(jù)集轉(zhuǎn)換成圖片和txt的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
詳解Python中pyautogui庫(kù)的最全使用方法
這篇文章主要介紹了詳解Python中pyautogui庫(kù)的最全使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
keras模型保存為tensorflow的二進(jìn)制模型方式
這篇文章主要介紹了keras模型保存為tensorflow的二進(jìn)制模型方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
python的launcher用法知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于python的launcher用法知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-08-08

