SpringBoot 接入 豆包 火山方舟大模型
火山方舟控制台 开通模型推理、知识库 应用入口;
文档中心 各类接口说明及SDK 获取;
向量数据库VikingDB 文档 下翻找到有java操作案例;
实现目标功能效果:
-
通过SDK调用 豆包大模型,在代码内实现问答的效果;
官方示例 -
通过使用知识库,自己上传文档。让豆包大模型可以回答 你文档中的内容数据(即自定义属于你自己(自身业务领域)的大模型);
前置步骤:
- 点击右上角头像处去创建 API 访问密钥;
- 控制台内 模型推理 - 在线推理 创建一个推理接入点(按提示操作即可);
- 控制台内 知识库 - 创建知识库;
代码
package com.example.demo.service.doubao;
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionRequest;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole;
import com.volcengine.ark.runtime.service.ArkService;
import com.volcengine.auth.ISignerV4;
import com.volcengine.auth.impl.SignerV4Impl;
import com.volcengine.model.Credentials;
import com.volcengine.service.SignableRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.util.ArrayList;
import java.util.List;
/**
* @Author:wwf
* @Package:com.example.demo.service
* @Project:demo
* @name:CreateArkClientExample
* @Date:2024/11/28 15:32
* @Filename:CreateArkClientExample
*/
public class CreateArkClientExample {
//模型推理 - 在线推理 - API 调用 内查看获取
private static final String API_KEY = "ea9fxxxxxxxxx49c";
//这里获取 https://console.volcengine.com/iam/keymanage
private static final String AK = "AKLTZDI2Yxxxxxxxxx2U2NGZmMzA";
private static final String SK = "Tnpjd0xxxxxxxxxOa1pXRQ";
public static void main(String[] args) throws Exception {
// 以华北 2 (北京) 为例,<ARK_DOMAIN> 处应改为 ark.cn-beijing.volces.com
ArkService service = ArkService.builder()
.apiKey(API_KEY)
.baseUrl("https://ark.cn-beijing.volces.com/api/v3")
.build();
System.out.println("
----- standard request -----");
String host = "api-knowledgebase.mlp.cn-beijing.volces.com";
String path = "/api/knowledge/collection/search_knowledge";
String method = "POST";
String body = "{"name":"wwfTest","query":"总结介绍下王清"}";
SignableRequest request = prepareRequest(host, path, method, null, body, AK, SK);
System.out.println(request.getURI());
HttpClient client = HttpClients.createDefault();
HttpResponse response = client.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
String content = EntityUtils.toString(response.getEntity());
System.out.println(statusCode);
System.out.println(content);
final List<ChatMessage> streamMessages = new ArrayList<>();
final ChatMessage streamSystemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手")
.content(content)
.build();
final ChatMessage streamUserMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("总结介绍下王清").build();
streamMessages.add(streamSystemMessage);
streamMessages.add(streamUserMessage);
ChatCompletionRequest streamChatCompletionRequest = ChatCompletionRequest.builder()
.model("ep-20241128155250-zlbd8")
//回调函数
// .tools()
.messages(streamMessages)
.build();
service.streamChatCompletion(streamChatCompletionRequest)
.doOnError(Throwable::printStackTrace)
.blockingForEach(
choice -> {
if (choice.getChoices().size() > 0) {
System.out.print(choice.getChoices().get(0).getMessage().getContent());
}
}
);
// shutdown service
service.shutdownExecutor();
}
/**
* 构建请求
*
* @param host
* @param path
* @param method
* @param params
* @param body
* @param ak
* @param sk
* @return
* @throws Exception
*/
public static SignableRequest prepareRequest(String host, String path, String method, List<NameValuePair> params, String body, String ak, String sk) throws Exception {
SignableRequest request = new SignableRequest();
request.setMethod(method);
request.setHeader("Accept", "application/json");
request.setHeader("Content-Type", "application/json");
request.setHeader("Host", "api-knowledgebase.mlp.cn-beijing.volces.com");
request.setEntity(new StringEntity(body, "utf-8"));
URIBuilder builder = request.getUriBuilder();
builder.setScheme("https");
builder.setHost(host);
builder.setPath(path);
if (params != null) {
builder.setParameters(params);
}
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(2000).build();
request.setConfig(requestConfig);
Credentials credentials = new Credentials("cn-north-1", "air");
credentials.setAccessKeyID(ak);
credentials.setSecretAccessKey(sk);
// 签名
ISignerV4 ISigner = new SignerV4Impl();
ISigner.sign(request, credentials);
return request;
}
}
示例代码说明:
本人随意使用gpt 编写了一篇短片小说,通过平台内知识库,以文档形式上传进去。小说内有个人物叫 王清,于是问 豆包模型 介绍下王清的信息,此时 模型可以准确快速的响应 关于王清的信息;
,,,,,,,,,
本文为个人操作接入记录,官方文档示例还是比较全的。仅记录大致接入流程;