豆包 Doubao-LLM 大模型 API 接口

豆包 Doubao-LLM 大模型 API 接口

豆包大模型是字节跳动推出的自研大模型。经过字节跳动内部 50+ 业务场景实践验证,提供高质量的语言模型与多模态模型矩阵,以优质模型效果与丰富的模型组合为企业打造丰富的业务体验。

接口地址

POST https://wcode.net/api/gpt/v1/chat/completions

✅️ 此 API 接口兼容 OpenAI 的接口规范,也就是可以直接使用 OpenAI 的 SDK 来调用大模型。仅需要将 BASE_URL 替换为 https://wcode.net/api/gpt/v1 即可使用 OpenAI SDK 进行调用。

请求方法

POST

快速开始(请求示例)

(注:以下请求示例中的 API_KEY 需要替换后再发起请求。 获取 API_KEY 入口:https://wcode.net/get-apikey

curl --request POST 'https://wcode.net/api/gpt/v1/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer API_KEY' \
--data '{
    "model": "doubao-lite-32k",
    "messages": [
        {
            "role": "user",
            "content": "你好,请介绍一下你自己"
        }
    ]
}'

请求头(Request Headers)

Header 备注 示例 相关链接
Authorization Bearer API_KEY 格式:Bearer + 空格 + API_KEY Bearer sk-..... 获取 API_KEY 入口:https://wcode.net/get-apikey
Content-Type application/json 固定为 application/json

请求参数

🚀 model | ✅️必填 | string | 模型ID,可选 model 模型列表(部分):

模型ID 模型名称 每 1000 tokens 费用 更新日期
doubao-pro-32k Doubao-pro 0.003 2024-12-15 ❇️最近更新
doubao-pro-256k Doubao-pro-256k 0.0115 2024-11-15
doubao-vision-pro-32k Doubao-vision-pro-32k 0.0115 2024-10-28
doubao-vision-lite-32k Doubao-vision-lite-32k 0.0058 2024-10-15
doubao-lite-128k Doubao-lite-128k 0.0013 2024-08-28
doubao-lite-32k Doubao-lite 0.0009 2024-08-28
doubao-pro-128k Doubao-pro-128k 0.0115 2024-06-28

🚀 messages | ✅️必填 | array | 由历史对话组成的消息列表。array 中的每个元素形式通常为 {"role": 角色, "content": 内容}。角色当前可选值为:systemuserassistant

messages[0]中支持 rolesystem

一般情况下,userassistant 需要交替出现,且 messages 中最后一个元素的 roleuser


🚀 stream | 选填 | boolean | 默认值 false | 是否流式输出。

false(默认值):模型生成完所有内容后一次性返回结果。

true:返回符合 SSE 协议的响应,边生成边输出,即每生成一部分内容就立即输出一个片段(chunk),最后以一条 data: [DONE] 消息结束。


🚀 max_tokens | 选填 | int | 允许模型生成的最大 tokens 数。

应用场景:max_tokens 参数适用于需要限制字数(如生成摘要、关键词)、控制成本或减少响应时间的场景。


🚀 temperature | 选填 | float | 默认值 1 | 采样温度,用于控制模型生成文本的多样性。取值范围: [0, 1]。当取值为 0 时模型仅考虑对数概率最大的一个 token。temperature 越高,生成的文本更多样,反之,生成的文本更确定。


🚀 top_p | 选填 | float | 默认值 0.7 | 核采样概率阈值,用于控制模型生成文本的多样性。取值范围:[0, 1]。当取值为 0 时模型仅考虑对数概率最大的一个 token。top_p 越高,生成的文本更多样。反之,生成的文本更确定。

由于 temperature 与 top_p 均可以控制生成文本的多样性,因此建议您只设置其中一个值。


🚀 frequency_penalty | 选填 | float | 默认值 0 | 频率惩罚系数。如果值为正,会根据新 token 在文本中的出现频率对其进行惩罚,从而降低模型逐字重复的可能性。取值范围为 [-2.0, 2.0]

适用场景:

较高的 frequency_penalty 适用于要求多样性、趣味性或创造性的场景,如创意写作或头脑风暴。

较低的 frequency_penalty 适用于要求一致性或专业术语的场景,如技术文档或其他正式文档。


🚀 presence_penalty | 选填 | float | 默认值 0 | 存在惩罚系数。如果值为正,会根据新 token 到目前为止是否出现在文本中对其进行惩罚,从而增加模型谈论新主题的可能性。取值范围为 [-2.0, 2.0]


🚀 stop | 选填 | string 或 array | 模型遇到 stop 字段所指定的字符串时将停止继续生成,这个词语本身不会输出。最多支持 4 个字符串。

参数示例:["季节", "天气"]


🚀 logprobs | 选填 | boolean | 默认值 false | 是否返回输出 tokens 的对数概率。


🚀 top_logprobs | 选填 | int | 默认值 0 | 指定每个输出 token 位置最有可能返回的 token 数量,每个 token 都有关联的对数概率。仅当 logprobs: true 时可以设置 top_logprobs 参数,取值范围为 [0, 20]


🚀 logit_bias | 选填 | object | 调整指定 token 在模型输出内容中出现的概率,使模型生成的内容更加符合特定的偏好。

返回参数

不同模型返回参数可能存在差异,请以实际返回为准。

返回参数 数据类型 说明
id string 系统生成的标识本次调用的id。
model string 本次调用的模型名。
choices array 模型生成内容的详情。
choices[i].finish_reason string 结束标志。
choices[i].message object 模型生成的消息。
choices[i].message.role string 生成消息的角色。
choices[i].message.content string 生成消息的内容。
choices[i].index int 生成的结果序列编号。
created int 请求被创建时的时间戳。
usage object 请求所消耗的 tokens 数据。
usage.prompt_tokens int 用户输入转换成 token 后的数量。
usage.completion_tokens int 模型生成回复转换为 token 后的数量。
usage.total_tokens int prompt_tokens 与 completion_tokens 的总和。

响应头(Response Headers)

Header 说明 示例 相关链接
X-Account-Balance API_KEY 余额(元) 123.0165 API_KEY 费用充值入口:https://wcode.net/apikey-recharge

Python 代码示例(Requests,doubao-lite-32k)

import requests
import json

url = "https://wcode.net/api/gpt/v1/chat/completions"

payload = json.dumps({
  "model": "doubao-lite-32k",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "你好"
    }
  ]
})
headers = {
  'Content-Type': 'application/json',
  'Authorization': 'Bearer API_KEY'     # TODO: 这里的 API_KEY 需要替换,获取 API_KEY 入口:https://wcode.net/get-apikey
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

Java 代码示例(OkHttp,doubao-lite-32k)

OkHttpClient client = new OkHttpClient().newBuilder().build();

MediaType mediaType = MediaType.parse("application/json");

RequestBody body = RequestBody.create(mediaType, "{\"model\":\"doubao-lite-32k\",\"messages\":[{\"role\":\"system\",\"content\":\"You are a helpful assistant.\"},{\"role\":\"user\",\"content\":\"你好\"}]}");

Request request = new Request.Builder()
  .url("https://wcode.net/api/gpt/v1/chat/completions")
  .method("POST", body)
  .addHeader("Content-Type", "application/json")
  .addHeader("Authorization", "Bearer API_KEY")     // TODO: 这里的 API_KEY 需要替换,获取 API_KEY 入口:https://wcode.net/get-apikey
  .build();

Response response = client.newCall(request).execute();

PHP 代码示例(Guzzle,doubao-lite-32k)

<?php
$client = new Client();
$headers = [
  'Content-Type' => 'application/json',
  'Authorization' => 'Bearer API_KEY'     // TODO: 这里的 API_KEY 需要替换,获取 API_KEY 入口:https://wcode.net/get-apikey
];
$body = '{
  "model": "doubao-lite-32k",
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": "你好"
    }
  ]
}';
$request = new Request('POST', 'https://wcode.net/api/gpt/v1/chat/completions', $headers, $body);
$res = $client->sendAsync($request)->wait();
echo $res->getBody();

...其他编程语言可参考下方 curl http 请求示例进行调用

请求示例 1(doubao-lite-32k)

curl --request POST 'https://wcode.net/api/gpt/v1/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer API_KEY' \
--data '{
    "model": "doubao-lite-32k",
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": "你好"
        }
    ]
}'

响应示例 1(doubao-lite-32k)

{
    "id": "021734706666426999645dcddb1336731c3539e403e8f78ab8ab1",
    "object": "chat.completion",
    "created": 1734706667,
    "model": "doubao-lite-32k-240828",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "你好呀!有什么事或者想聊点什么都可以跟我说哦~ "
            },
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 20,
        "completion_tokens": 19,
        "total_tokens": 39,
        "prompt_tokens_details": {
            "cached_tokens": 0
        }
    }
}

请求示例 1.1(多轮对话,doubao-lite-32k)

curl --request POST 'https://wcode.net/api/gpt/v1/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer API_KEY' \
--data '{
    "model": "doubao-lite-32k",
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": "你好"
        },
        {
            "role": "assistant",
            "content": "你好!有什么可以帮助你的吗?"
        },
        {
            "role": "user",
            "content": "什么是AGI?"
        }
    ]
}'

响应示例 1.1(多轮对话,doubao-lite-32k)

{
    "id": "02173470678682385e02f6cbac7ef063385913b42830dc033f24f",
    "object": "chat.completion",
    "created": 1734706791,
    "model": "doubao-lite-32k-240828",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "AGI 全称为 Artificial General Intelligence,即通用人工智能。\n\n它指的是一种能够理解、学习、推理、解决各种类型问题,并在各种领域和任务中表现出与人类相似或超越人类智能水平的人工智能系统。\n\n与特定领域的人工智能(如只能进行图像识别或语言翻译等)不同,AGI 具备广泛的知识和技能,能够在不同的情境和任务中灵活应用和迁移知识,具有更强的适应性和通用性。\n\n例如,AGI 可能能够像人类一样理解自然语言、进行逻辑推理、进行创造性思考、解决复杂的数学问题、理解和处理各种感官信息等,能够在多个领域和任务中进行高效的交互和操作。\n\n目前,AGI 仍处于研究和发展阶段,虽然已经取得了一些重要的进展,但离真正实现通用人工智能还有很长的路要走。 "
            },
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 44,
        "completion_tokens": 187,
        "total_tokens": 231,
        "prompt_tokens_details": {
            "cached_tokens": 0
        }
    }
}

请求示例 2(doubao-lite-32k,流式输出)

curl --request POST 'https://wcode.net/api/gpt/v1/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer API_KEY' \
--data '{
    "model": "doubao-lite-32k",
    "stream": true,
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": "你好"
        }
    ]
}'

响应示例 2(doubao-lite-32k,流式输出)

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":"你"},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":"好"},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":"呀"},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":"!"},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":"有"},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":"什么"},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":"事"},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":"或者"},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":"想"},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":"聊"},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":"点"},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":"什么"},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":"都"},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":"可以"},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":"跟"},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":"我说"},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":"哦"},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":"~"},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":" "},"finish_reason":null}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":"stop"}]}

data: {"id":"021734706830038a7848f737d78cb3307c42b9c714bbd22b0abbd","object":"chat.completion.chunk","created":1734706830,"model":"doubao-lite-32k-240828","choices":[],"usage":{"prompt_tokens":20,"completion_tokens":19,"total_tokens":39,"prompt_tokens_details":{"cached_tokens":0}}}

data: [DONE]

请求示例 3(图片理解,doubao-vision-lite-32k)

curl --request POST 'https://wcode.net/api/gpt/v1/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer API_KEY' \
--data '{
    "model": "doubao-vision-lite-32k",
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "这张图片展示的画面是什么?"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://zdkjdemo.oss-cn-shanghai.aliyuncs.com/dog_playing_with_sticks.jpg"
                    }
                }
            ]
        }
    ]
}'

响应示例 3

{
    "id": "0217370361153263548e077c37335f09b0de47e6fc4b7f76383ca",
    "object": "chat.completion",
    "created": 1737036120,
    "model": "doubao-vision-lite-32k-241015",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "这张图片展示了一只狗在草地上玩耍的场景。狗的毛色主要是棕色和白色相间,它正趴在草地上,嘴里叼着一根木棍,显得非常专注和悠闲。周围是郁郁葱葱的绿色草地,背景中可以看到树木和模糊的自然景观,给人一种宁静和自然的感觉。整个画面充满了生机和活力,展现了宠物狗在户外享受自由时光的温馨场景。"
            },
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 873,
        "completion_tokens": 88,
        "total_tokens": 961,
        "prompt_tokens_details": {
            "cached_tokens": 0
        }
    }
}

异常响应示例

{
    "error": {
        "message": "Invalid API-KEY",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}