本节将说明如何基于多轮历史对话,使用大语言模型进行回答生成
chat_completions 用于向大模型发起一次对话请求,与新升级的search_knowledge 联通,可以完成标准的检索生成链路。
参数 | 子参数 | 类型 | 是否必选 | 默认值 | 参数说明 |
|---|---|---|---|---|---|
model | -- | String | 是 | Doubao-1-5-pro-32k | 想要用于在线生成的大模型
公共推理接入点模型可选范围见对话模型和图像理解模型。
|
modelVersion | -- | String | 否 | -- | |
thinking | -- | Object | 否 | enabled | 控制模型是否开启深度思考模式
当前支持的模型详情见:thinking
|
apiKey | -- | String | 否 | -- | 接入点 API Key |
messages | -- | List | 是 | -- | 发出消息的对话参与者角色,可选值包括:
多轮文本问答: 串联脚本示例可参考知识库多轮检索问答样例
多轮图文理解:串联脚本示例可参考知识库图文问答样例
|
stream | -- | Boolean | 否 | false | 响应内容是否流式返回
|
maxTokens | -- | Integer | 否 | 4096 | 模型可以生成的最大 token 数量
|
returnTokenUsage | -- | Boolean | 否 | false | 是否返回token用量统计 |
temperature | -- | Double | 否 | 0.1 | 采样温度 |
模型名称 | 可选版本 |
|---|---|
Doubao-1-5-thinking-pro | 250415 |
Deepseek-V3-128k | 250324, 241226 |
Deepseek-R1-128k | 250528, 250120 |
Doubao-1-5-pro-256k | 250115 |
Doubao-1-5-pro-32k | 250115(默认), character-250715, character-250228 |
Doubao-1-5-lite-32k | 250115 |
Doubao-pro-256k | 241115, 240828 |
Doubao-pro-32k | 241215, 240828, 240615, character-241215, character-240828, character-240528 |
Doubao-pro-128k | 240628, 240515(即将下线) |
Doubao-lite-32k | 240828,240628, 240428(即将下线), character-250228, character-241015 |
Doubao-lite-128k | 240828, 240428 |
模型名称 | 可选版本 |
|---|---|
Doubao-seed-1-8 | 251228 |
Doubao-seed-1-6-vision | 240428, 240828 |
Doubao-seed-1-6-thinking | 250715, 250615 |
Doubao-seed-1-6 | 251015, 250615 |
Doubao-seed-1-6-flash | 250828, 250715, 250615 |
Doubao-1-5-thinking-pro | m-250428, m-250415 |
Doubao-1-5-vision-pro | 250328 |
Doubao-1-5-vision-lite | 250315 |
Doubao-1-5-vision-pro-32k | 250115 |
Doubao-vision-pro-32k | 241028 |
字段 | 类型 | 参数说明 |
|---|---|---|
code | Integer | 状态码 |
message | String | 返回信息 |
requestId | String | 标识每个请求的唯一标识符 |
data | ChatCompletionResult | ChatCompletionResult |
字段 | 类型 | 参数说明 |
|---|---|---|
reasoningContent | String | 推理模型生成的内容 |
generatedAnswer | String | 模型生成的回答 |
usage | String | token 用量统计 |
prompt | String | prompt 内容 |
model | String | 模型名称 |
finishReason | String | 结束原因 |
totalTokens | Object | 总 token 数量 |
状态码 | http状态码 | 返回信息 | 状态码说明 |
|---|---|---|---|
0 | 200 | success | 成功 |
1000001 | 401 | unauthorized | 缺乏鉴权信息 |
1000002 | 403 | no permission | 权限不足 |
1000003 | 400 | invalid request:%s | 非法参数 |
首次使用知识库 SDK ,可参考 使用说明
请配置鉴权参数(VOLC_AK/VOLC_SK 或 VIKING_API_KEY),并可选配置 VIKING_CHAT_API_KEY(用于大模型服务鉴权)。
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.core.auth.AuthWithApiKey; 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 { Auth auth = preferAuth(); if (auth == null) { System.out.println("missing_auth: set VOLC_AK/VOLC_SK or VIKING_API_KEY"); return; } KnowledgeService service = newKnowledgeService(auth); String apiKey = getEnv("VIKING_CHAT_API_KEY"); if (apiKey.isEmpty()) { apiKey = null; } 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) .apiKey(apiKey) .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 Auth preferAuth() { String ak = getEnv("VOLC_AK"); String sk = getEnv("VOLC_SK"); if (!ak.isEmpty() && !sk.isEmpty()) { return new AuthWithAkSk(ak, sk); } String apiKey = getEnv("VIKING_API_KEY"); if (!apiKey.isEmpty()) { return new AuthWithApiKey(apiKey); } return null; } 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.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.core.auth.AuthWithApiKey; 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 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 { Auth auth = preferAuth(); if (auth == null) { System.out.println("missing_auth: set VOLC_AK/VOLC_SK or VIKING_API_KEY"); return; } KnowledgeService service = newKnowledgeService(auth); String apiKey = getEnv("VIKING_CHAT_API_KEY"); if (apiKey.isEmpty()) { apiKey = null; } 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) .apiKey(apiKey) .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 Auth preferAuth() { String ak = getEnv("VOLC_AK"); String sk = getEnv("VOLC_SK"); if (!ak.isEmpty() && !sk.isEmpty()) { return new AuthWithAkSk(ak, sk); } String apiKey = getEnv("VIKING_API_KEY"); if (!apiKey.isEmpty()) { return new AuthWithApiKey(apiKey); } return null; } 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)); } }