【最新|送API Key】零基础调用Anthropic Claude 3 API接口(Python)
warning:
这篇文章距离上次修改已过193天,其中的内容可能已经有所变动。
要调用Anthropic Claude 3 API接口,首先需要获取API Key。以下是使用Python调用Claude 3 API的基本步骤:
获取API Key:
访问Anthropic官网,注册账号并获取API Key。
安装必要的Python库:
pip install requests
- 使用Python代码调用API:
import requests
# 替换以下API_KEY为你的实际API Key
API_KEY = "your_api_key"
# 要调用的API端点
API_ENDPOINT = "https://api.anthropic.com/v1/chat/completions"
# 要发送的请求体
payload = {
"model": "claude3",
"input": "Hello, who are you?"
}
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
response = requests.post(API_ENDPOINT, json=payload, headers=headers)
if response.ok:
# 打印响应结果
print(response.json())
else:
print("Error:", response.status_code, response.text)
确保替换your_api_key
为你的实际API Key,并根据需要修改payload
中的model
和input
字段。
这段代码会向Claude 3 API发送一个消息,并打印出返回的响应。
评论已关闭