Anthropic Claude
1. 概述
Claude是Anthropic开发的大语言模型,具有强大的对话和写作能力。它能理解上下文、生成连贯文本、编写代码,并擅长逻辑推理和分析。注重安全性和道德准则,会明确表明自己是AI助手的身份。它支持多语言交流,能够处理复杂任务和长对话。
本 API 与 OpenAI 接口格式兼容。
模型列表:
claude-3-opus-20240229claude-3-haiku-20240307claude-3-5-haiku-20241022claude-3-5-sonnet-20240620claude-3-5-sonnet-20241022claude-3-7-sonnet-20250219(支持reasoning_effort参数)claude-opus-4-20250514(支持reasoning_effort参数)claude-sonnet-4-20250514(支持reasoning_effort参数)claude-opus-4-1-20250805claude-sonnet-4-5-20250929claude-haiku-4-5-20251001
2. 请求说明
请求方法:
POST请求地址:
https://gateway.theturbo.ai/v1/chat/completions
3. 请求参数
3.1 Header 参数
Content-Type
string
是
设置请求头类型,必须为 application/json
application/json
Accept
string
是
设置响应类型,建议统一为 application/json
application/json
Authorization
string
是
身份验证所需的 API_KEY,格式 Bearer $YOUR_API_KEY
Bearer $YOUR_API_KEY
3.2 Body 参数 (application/json)
messages
array
是
聊天消息列表,格式与 OpenAI 兼容。数组中的每个对象包含 role(角色) 与 content(内容)。
[{"role": "user","content": "你好"}]
role
string
否
消息角色,可选值:system、user、assistant。
user
content
string
否
消息的具体内容。
你好,请给我讲个笑话。
temperature
number
否
采样温度,取值 0~2。数值越大,输出越随机;数值越小,输出越集中和确定。
0.7
top_p
number
否
另一种调节采样分布的方式,取值 0~1。和 temperature 通常二选一设置。
0.9
stream
boolean
否
是否开启流式输出。设置为 true 时,返回类似 ChatGPT 的流式数据。
false
max_tokens
number
否
单次回复可生成的最大 token 数量,受模型上下文长度限制。
8192
reasoning_effort
string
否
用来控制模型在推理任务中投入多少“计算精力”。支持low medium high none。默认为none。
none
4. 请求示例
4.1 聊天对话
POST /v1/chat/completions
Content-Type: application/json
Accept: application/json
Authorization: Bearer $YOUR_API_KEY
{
"model": "claude-3-5-haiku-20241022",
"messages": [
{
"role": "user",
"content": "你好,给我科普一下量子力学吧"
}
],
"temperature": 0.7,
"max_tokens": 1024
}curl https://gateway.theturbo.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer $YOUR_API_KEY" \
-d "{
\"model\": \"claude-3-5-haiku-20241022\",
\"messages\": [{
\"role\": \"user\",
\"content\": \"你好,给我科普一下量子力学吧\"
}]
}"package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
const (
YOUR_API_KEY = "sk-123456789012345678901234567890123456789012345678"
REQUEST_PAYLOAD = `{
"model": "claude-3-5-haiku-20241022",
"messages": [{
"role": "user",
"content": "你好,给我科普一下量子力学吧"
}],
"temperature": 0.7,
"max_tokens": 1024
}`
)
func main() {
requestURL := "https://gateway.theturbo.ai/v1/chat/completions"
requestMethod := "POST"
requestPayload := strings.NewReader(REQUEST_PAYLOAD)
req, err := http.NewRequest(requestMethod, requestURL, requestPayload)
if err != nil {
fmt.Println("Create request failed, err: ", err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", "Bearer "+YOUR_API_KEY)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Do request failed, err: ", err)
return
}
defer resp.Body.Close()
respBodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Read response body failed, err: ", err)
return
}
fmt.Println(string(respBodyBytes))
}4.2 图片理解
多模态图片理解功能,通过上传图片或图片URL提交图片给大模型。
4.2.1 通过base64上传图片(推荐方式)
POST /v1/chat/completions
Content-Type: application/json
Accept: application/json
Authorization: Bearer $YOUR_API_KEY
{
"model": "claude-sonnet-4-20250514",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "这张图片里有什么?"
},
{
"type": "image_url",
"image_url": {
"url": "data:image/jpeg;base64,${base64_image}"
}
}
]
}
],
"temperature": 0.7,
"max_tokens": 1024
}base64_image=$(base64 -i "Path/to/agi/image.jpeg");
curl https://gateway.theturbo.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer $YOUR_API_KEY" \
-d "{
\"model\": \"claude-sonnet-4-20250514\",
\"messages\": [{
\"role\": \"user\",
\"content\": [{
\"type\": \"text\",
\"text\": \"这张图片里有什么?\"
},
{
\"type\": \"image_url\",
\"image_url\": {
\"url\": \"data:image/jpeg;base64,${base64_image}\"
}
}
]
}]
}"package main
import (
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)
const (
YOUR_API_KEY = "sk-123456789012345678901234567890123456789012345678"
FILE_MIME_TYPE = "image/jpeg"
FILE_PATH = "/path/to/your/image.jpeg"
REQUEST_PAYLOAD = `{
"model": "claude-sonnet-4-20250514",
"messages": [{
"role": "user",
"content": [{
"type": "text",
"text": "这张图片里有什么?"
},
{
"type": "image_url",
"image_url": {
"url": "data:%s;base64,%s"
}
}
]
}],
"temperature": 0.7,
"max_tokens": 1024
}`
)
func GetFileDataBase64(filePath string) (string, error) {
file, err := os.Open(FILE_PATH)
if err != nil {
return "", err
}
defer file.Close()
fileData, err := ioutil.ReadAll(file)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(fileData), nil
}
func main() {
requestURL := "https://gateway.theturbo.ai/v1/chat/completions"
requestMethod := "POST"
fileBase64String, err := GetFileDataBase64(FILE_PATH)
if err != nil {
fmt.Println("Read file failed, err: ", err)
return
}
requestPayload := strings.NewReader(fmt.Sprintf(REQUEST_PAYLOAD,
FILE_MIME_TYPE,
fileBase64String,
))
req, err := http.NewRequest(requestMethod, requestURL, requestPayload)
if err != nil {
fmt.Println("Create request failed, err: ", err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", "Bearer "+YOUR_API_KEY)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Do request failed, err: ", err)
return
}
defer resp.Body.Close()
respBodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Read response body failed, err: ", err)
return
}
fmt.Println(string(respBodyBytes))
}4.2.2 提交图片URL
POST /v1/chat/completions
Content-Type: application/json
Accept: application/json
Authorization: Bearer $YOUR_API_KEY
{
"model": "claude-sonnet-4-20250514",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "这张图片里有什么?"
},
{
"type": "image_url",
"image_url": {
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
}
}
]
}
],
"temperature": 0.7,
"max_tokens": 1024
}curl https://gateway.theturbo.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer $YOUR_API_KEY" \
-d "{
\"model\": \"claude-sonnet-4-20250514\",
\"messages\": [{
\"role\": \"user\",
\"content\": [{
\"type\": \"text\",
\"text\": \"这张图片里有什么?\"
},
{
\"type\": \"image_url\",
\"image_url\": {
\"url\": \"https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg\"
}
}
]
}]
}"package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
const (
YOUR_API_KEY = "sk-123456789012345678901234567890123456789012345678"
REQUEST_PAYLOAD = `{
"model": "claude-sonnet-4-20250514",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "这张图片里有什么?"
},
{
"type": "image_url",
"image_url": {
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
}
}
]
}
],
"temperature": 0.7,
"max_tokens": 1024
}`
)
func main() {
requestURL := "https://gateway.theturbo.ai/v1/chat/completions"
requestMethod := "POST"
requestPayload := strings.NewReader(REQUEST_PAYLOAD)
req, err := http.NewRequest(requestMethod, requestURL, requestPayload)
if err != nil {
fmt.Println("Create request failed, err: ", err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", "Bearer "+YOUR_API_KEY)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Do request failed, err: ", err)
return
}
defer resp.Body.Close()
respBodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Read response body failed, err: ", err)
return
}
fmt.Println(string(respBodyBytes))
}4.3 函数调用
POST /v1/chat/completions
Content-Type: application/json
Accept: application/json
Authorization: Bearer $YOUR_API_KEY
{
"model": "claude-3-5-haiku-20241022",
"messages": [{
"role": "user",
"content": "What's the weather like in Boston today?"
}],
"tools": [{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
}],
"tool_choice": "auto"
}curl https://gateway.theturbo.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer $YOUR_API_KEY" \
-d "{
\"model\": \"claude-3-5-haiku-20241022\",
\"messages\": [{
\"role\": \"user\",
\"content\": \"What's the weather like in Boston today?\"
}],
\"tools\": [{
\"type\": \"function\",
\"function\": {
\"name\": \"get_current_weather\",
\"description\": \"Get the current weather in a given location\",
\"parameters\": {
\"type\": \"object\",
\"properties\": {
\"location\": {
\"type\": \"string\",
\"description\": \"The city and state, e.g. San Francisco, CA\"
},
\"unit\": {
\"type\": \"string\",
\"enum\": [\"celsius\", \"fahrenheit\"]
}
},
\"required\": [\"location\"]
}
}
}],
\"tool_choice\": \"auto\"
}"package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
const (
YOUR_API_KEY = "sk-123456789012345678901234567890123456789012345678"
REQUEST_PAYLOAD = `{
"model": "claude-3-5-haiku-20241022",
"messages": [{
"role": "user",
"content": "What's the weather like in Boston today?"
}],
"tools": [{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
}],
"tool_choice": "auto"
}`
)
func main() {
requestURL := "https://gateway.theturbo.ai/v1/chat/completions"
requestMethod := "POST"
requestPayload := strings.NewReader(REQUEST_PAYLOAD)
req, err := http.NewRequest(requestMethod, requestURL, requestPayload)
if err != nil {
fmt.Println("Create request failed, err: ", err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", "Bearer "+YOUR_API_KEY)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Do request failed, err: ", err)
return
}
defer resp.Body.Close()
respBodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Read response body failed, err: ", err)
return
}
fmt.Println(string(respBodyBytes))
}5. 响应示例
{
"id": "chatcmpl-1234567890",
"object": "chat.completion",
"created": 1699999999,
"model": "claude-3-5-haiku-20241022",
"choices": [
{
"message": {
"role": "assistant",
"content": "量子力学是研究微观世界的物理学分支……"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 30,
"total_tokens": 40
}
}最后更新于