You need to enable JavaScript to run this app.
文档中心
向量数据库VikingDB

向量数据库VikingDB

复制全文
下载 pdf
知识库(Collection)
chat_completions
复制全文
下载 pdf
chat_completions

本节将说明如何基于多轮历史对话,使用大语言模型进行回答生成。

概述

chat_completions 用于向大模型发起一次对话请求,与新升级的 search_knowledge 连通,可以完成标准的检索生成链路。

请求参数
参数子参数类型是否必选默认值参数说明

model

--

String

是

Doubao-1-5-pro-32k

想要用于在线生成的大模型

  • 当指定为 doubao 系列模型时默认使用系统的公共推理接入点,适合调试场景,有 TPM 限流
  • 或指定为在方舟上创建的推理接入点 ID,适合生产场景,TPM 配置可在创建推理接入点时自行调整
    公共推理接入点模型可选范围详见下表。
    私有推理接入点 ID 形如:ep-202406040*****-*****
    注意当使用私有接入点时,需要配合传入 API key 进行鉴权

注:默认值可能随版本更新而变化。为确保产品效果稳定,建议用户指定模型版本

modelVersion--String否--对话模型和图像理解模型的可选范围详见下表。

thinking

--

Object

否

enabled

控制模型是否开启深度思考模式
取值范围:

  • enabled:开启思考模式,模型一定先思考后回答。
  • disabled:关闭思考模式,模型直接回答问题,不会进行思考。
  • auto:自动思考模式,模型根据问题自主判断是否需要思考,简单题目直接回答。
    当前支持的模型详情见:thinking
    示例:
{
  "thinking": {
    "type": "disabled" // 不使用深度思考能力
    // "type": "enabled" // 使用深度思考能力
    // "type": "auto" // 模型自行判断是否使用深度思考能力
  }
}

apiKey

--

String

否

--

接入点 API Key
可在火山方舟控制台为目标推理接入点生成
当需要使用私有模型接入点时,需传入对应 API key 进行鉴权

messages

--

List

是

--

发出消息的对话参与者角色,可选值包括:

  • system:System Message 系统消息
  • user:User Message 用户消息
  • assistant:Assistant Message 对话助手消息
    多轮文本问答:串联脚本示例可参考 知识库多轮检索问答样例
[
  {
    "role": "system",
    "content": "你是一位在线客服,你的首要任务是通过巧妙的话术回复用户的问题,你需要根据「参考资料」来回答接下来的「用户问题」,这些信息在 <context></context> XML tags 之内,你需要根据参考资料给出准确,简洁的回答。"
  },
  {
    "role": "user",
    "content": "推荐一个适合 3 岁小孩的玩具"
  },
  {
    "role": "assistant",
    "content": "您好!3 岁宝宝正处于身体、智力、情感多方面快速发展的阶段,您可以看看以下几款类型的玩具:乐高积木、拼图、消防局与消防直升机套装、医疗玩具套装、儿童滑板车"
  },
  {
    "role": "user",
    "content": "详细介绍下乐高积木有哪些合适的系列"
  }
]

多轮图文理解:串联脚本示例可参考 知识库图文问答样例

[
  {
    "role": "system",
    "content": "你是一位在线客服,你的首要任务是通过巧妙的话术回复用户的问题,你需要根据「参考资料」来回答接下来的「用户问题」,这些信息在 <context></context> XML tags 之内,你需要根据参考资料给出准确,简洁的回答。参考资料中可能会包含图片信息,图片的引用说明在 <img></img> XML tags 之内,参考资料内的图片顺序与用户上传的图片顺序一致。"
  },
  {
    "role": "user",
    "content": [
      {
        "type": "text",
        "text": "推荐一个适合 3 岁小孩的玩具"
      },
      {
        "type": "image_url",
        "image_url": {
          "url": "https://ark-project.tos-cn-beijing.volces.XXX.jpeg"
        }
      },
      {
        "type": "image_url",
        "image_url": {
          "url": "https://ark-project.tos-cn-beijing.volces.XXX.jpeg"
        }
      }
    ]
  }
]

stream

--

Boolean

否

false

响应内容是否流式返回

  • false:模型生成完所有内容后一次性返回结果
  • true:按 SSE 协议逐块返回模型生成内容,并以一条 data: [DONE] 消息结束

maxTokens

--

Integer

否

4096

模型可以生成的最大 token 数量

注意

  • 模型回复最大长度(单位 token),取值范围各个模型不同,详细见 模型列表。
  • 输入 token 和输出 token 的总长度还受模型的上下文长度限制。

returnTokenUsage

--

Boolean

否

false

是否返回 token 用量统计

  • true:在响应中返回 usage 字段;false:不返回 usage 字段

temperature

--

Double

否

0.1

采样温度
控制了生成文本时对每个候选词的概率分布进行平滑的程度。取值范围为 [0, 1]。当取值为 0 时模型仅考虑对数概率最大的一个 token。
较高的值(如 0.8)会使输出更加随机,而较低的值(如 0.2)会使输出更加集中确定。通常建议仅调整 temperature 或 top_p 其中之一,不建议两者都修改。

对话模型

模型名称可选版本
Doubao-1-5-pro-32k250115, character-250715
Doubao-1-5-lite-32k250115

图像理解模型

模型名称可选版本
Doubao-seed-2-0-pro260215
Doubao-seed-2-0-lite260215
Doubao-seed-2-0-mini260215
Doubao-seed-1-8251228
Doubao-seed-1-6-vision250815
Doubao-seed-1-6251015, 250615
Doubao-seed-1-6-flash250828, 250615
Doubao-1-5-vision-pro-32k250115

方舟模型下线说明:https://www.volcengine.com/docs/82379/1350667?lang=zh

响应消息
字段类型参数说明
codeInteger状态码
messageString返回信息
requestIdString标识每个请求的唯一标识符
dataChatCompletionResultChatCompletionResult

ChatCompletionResult

字段类型参数说明
reasoningContentString推理模型生成的内容
generatedAnswerString模型生成的回答
usageStringtoken 用量统计
promptStringprompt 内容
modelString模型名称
finishReasonString结束原因
totalTokensObject总 token 数量
状态码说明
状态码http状态码返回信息状态码说明
0200success成功
1000001401unauthorized缺乏鉴权信息
1000002403no permission权限不足
1000003400invalid request:%s非法参数
请求示例

首次使用知识库 SDK,可参考 使用说明
本示例使用 AK/SK(IAM)完成知识库 SDK 鉴权;请求参数中的 APIKey 仅在调用私有模型接入点时使用,不用于知识库 SDK 本身的鉴权。

package com.volcengine.vikingdb.runtime.knowledge.examples.chat_completion;

import com.fasterxml.jackson.databind.ObjectWriter;
import com.volcengine.vikingdb.runtime.core.ApiClient;
import com.volcengine.vikingdb.runtime.core.RequestAddition;
import com.volcengine.vikingdb.runtime.core.auth.Auth;
import com.volcengine.vikingdb.runtime.core.auth.AuthWithAkSk;
import com.volcengine.vikingdb.runtime.enums.Scheme;
import com.volcengine.vikingdb.runtime.knowledge.model.request.ChatCompletionRequest;
import com.volcengine.vikingdb.runtime.knowledge.model.request.ChatMessage;
import com.volcengine.vikingdb.runtime.knowledge.model.response.ChatCompletionResponse;
import com.volcengine.vikingdb.runtime.knowledge.service.KnowledgeService;

import java.util.Collections;

public class Main {
    private static final ObjectWriter PRETTY_JSON = ApiClient.objectMapper.writerWithDefaultPrettyPrinter();
    private static final Scheme SCHEME = Scheme.HTTPS;
    private static final String HOST = "api-knowledgebase.mlp.cn-beijing.volces.com";
    private static final String REGION = "cn-beijing";

    public static void main(String[] args) throws Exception {
        String ak = getEnv("VIKING_AK");
        String sk = getEnv("VIKING_SK");
        if (ak.isEmpty() || sk.isEmpty()) {
            System.out.println("missing_auth: set VIKING_AK/VIKING_SK");
            return;
        }
        Auth auth = new AuthWithAkSk(ak, sk);
        KnowledgeService service = newKnowledgeService(auth);

        ChatCompletionRequest req = ChatCompletionRequest.builder()
                .model("Doubao-1-5-pro-32k")
                .messages(Collections.singletonList(ChatMessage.builder()
                        .role("user")
                        .content("<YOUR_QUERY>")
                        .build()))
                .maxTokens(1024)
                .temperature(0.1)
                .returnTokenUsage(true)
                .stream(false)
                .build();

        ChatCompletionResponse resp = service.chatCompletion(req, new RequestAddition());
        printJson("chat_completion", resp);
    }

    private static KnowledgeService newKnowledgeService(Auth auth) {
        return new KnowledgeService(SCHEME, HOST, REGION, auth);
    }

    private static String getEnv(String name) {
        String v = System.getenv(name);
        if (v == null) {
            return "";
        }
        v = v.trim();
        return v.isEmpty() ? "" : v;
    }

    private static void printJson(String name, Object obj) throws Exception {
        if (obj == null) {
            System.out.println(name + ": null");
            return;
        }
        System.out.println(name + ": " + PRETTY_JSON.writeValueAsString(obj));
    }
}
package com.volcengine.vikingdb.runtime.knowledge.examples.chat_completion_stream;

import com.volcengine.vikingdb.runtime.core.RequestAddition;
import com.volcengine.vikingdb.runtime.core.auth.Auth;
import com.volcengine.vikingdb.runtime.core.auth.AuthWithAkSk;
import com.volcengine.vikingdb.runtime.enums.Scheme;
import com.volcengine.vikingdb.runtime.knowledge.model.request.ChatCompletionRequest;
import com.volcengine.vikingdb.runtime.knowledge.model.request.ChatMessage;
import com.volcengine.vikingdb.runtime.knowledge.model.response.ChatCompletionResponse;
import com.volcengine.vikingdb.runtime.knowledge.model.response.ChatCompletionResult;
import com.volcengine.vikingdb.runtime.knowledge.service.KnowledgeService;
import com.volcengine.vikingdb.runtime.knowledge.service.KnowledgeStream;

import java.util.Collections;

public class Main {
    private static final Scheme SCHEME = Scheme.HTTPS;
    private static final String HOST = "api-knowledgebase.mlp.cn-beijing.volces.com";
    private static final String REGION = "cn-beijing";

    public static void main(String[] args) throws Exception {
        String ak = getEnv("VIKING_AK");
        String sk = getEnv("VIKING_SK");
        if (ak.isEmpty() || sk.isEmpty()) {
            System.out.println("missing_auth: set VIKING_AK/VIKING_SK");
            return;
        }
        Auth auth = new AuthWithAkSk(ak, sk);
        KnowledgeService service = newKnowledgeService(auth);

        ChatCompletionRequest req = ChatCompletionRequest.builder()
                .model("Doubao-1-5-pro-32k")
                .messages(Collections.singletonList(ChatMessage.builder()
                        .role("user")
                        .content("<YOUR_QUERY>")
                        .build()))
                .maxTokens(1024)
                .temperature(0.1)
                .returnTokenUsage(true)
                .stream(true)
                .build();

        try (KnowledgeStream<ChatCompletionResponse> stream = service.chatCompletionStream(req,
                new RequestAddition())) {
            System.out.println("chat_completion_stream:");
            for (ChatCompletionResponse item : stream) {
                ChatCompletionResult data = item != null ? item.getData() : null;
                if (data != null && data.getGeneratedAnswer() != null) {
                    System.out.print(data.getGeneratedAnswer());
                }
                if (data != null && data.getFinishReason() != null && !data.getFinishReason().trim().isEmpty()) {
                    break;
                }
            }
            System.out.print("\n");
        }
    }

    private static KnowledgeService newKnowledgeService(Auth auth) {
        return new KnowledgeService(SCHEME, HOST, REGION, auth);
    }

    private static String getEnv(String name) {
        String v = System.getenv(name);
        if (v == null) {
            return "";
        }
        v = v.trim();
        return v.isEmpty() ? "" : v;
    }
}
最近更新时间:2026.09.24 18:54:07
这个页面对您有帮助吗?
有用
有用
无用
无用