本节将说明如何基于多轮历史对话,使用大语言模型进行回答生成。
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-pro-32k | 250115, character-250715 |
Doubao-1-5-lite-32k | 250115 |
模型名称 | 可选版本 |
|---|---|
Doubao-seed-2-0-pro | 260215 |
Doubao-seed-2-0-lite | 260215 |
Doubao-seed-2-0-mini | 260215 |
Doubao-seed-1-8 | 251228 |
Doubao-seed-1-6-vision | 250815 |
Doubao-seed-1-6 | 251015, 250615 |
Doubao-seed-1-6-flash | 250828, 250615 |
Doubao-1-5-vision-pro-32k | 250115 |
方舟模型下线说明:https://www.volcengine.com/docs/82379/1350667?lang=zh
字段 | 类型 | 参数说明 |
|---|---|---|
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,可参考 使用说明
本示例使用 AK/SK 鉴权方式调用 chat_completions,当前该接口不支持 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; } }