前提条件
示例代码
说明
示例代码中 <YOUR_BOT_ID>
需要替换为您在平台上创建的应用 ID。
单轮
package com.volcengine.ark.runtime;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionRequest;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionResult;
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 java.util.ArrayList;
import java.util.List;
public class BotChatCompletionsExample {
public static void main(String[] args) {
String apiKey = System.getenv("ARK_API_KEY");
ArkService service = ArkService.builder().apiKey(apiKey).build();
System.out.println("\n----- standard request -----");
final List<ChatMessage> messages = new ArrayList<>();
final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("常见的十字花科植物有哪些?").build();
messages.add(systemMessage);
messages.add(userMessage);
BotChatCompletionRequest chatCompletionRequest = BotChatCompletionRequest.builder()
// 将<YOUR_BOT_ID>替换为您的应用ID
.model("<YOUR_BOT_ID>")
.messages(messages)
.build();
BotChatCompletionResult chatCompletionResult = service.createBotChatCompletion(chatCompletionRequest);
chatCompletionResult.getChoices().forEach(
choice -> System.out.println(choice.getMessage().getContent())
);
// the references example
chatCompletionResult.getReferences().forEach(
ref -> System.out.println(ref.getUrl())
);
}
}
多轮
package com.volcengine.ark.runtime;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionRequest;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionResult;
import com.volcengine.ark.runtime.model.completion.chat.*;
import com.volcengine.ark.runtime.service.ArkService;
import java.util.*;
public class BotChatCompletionsExample {
public static void main(String[] args) {
String apiKey = System.getenv("ARK_API_KEY");
ArkService service = ArkService.builder().apiKey(apiKey).build();
System.out.println("\n----- multiple rounds request -----");
final List<ChatMessage> messages = Arrays.asList(
ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build(),
ChatMessage.builder().role(ChatMessageRole.USER).content("花椰菜是什么?").build(),
ChatMessage.builder().role(ChatMessageRole.ASSISTANT).content("花椰菜又称菜花、花菜,是一种常见的蔬菜。").build(),
ChatMessage.builder().role(ChatMessageRole.USER).content("再详细点").build()
);
BotChatCompletionRequest chatCompletionRequest = BotChatCompletionRequest.builder()
// 将<YOUR_BOT_ID>替换为您的应用ID
.model("<YOUR_BOT_ID>")
.messages(messages)
.build();
BotChatCompletionResult chatCompletionResult = service.createBotChatCompletion(chatCompletionRequest);
chatCompletionResult.getChoices().forEach(
choice -> System.out.println(choice.getMessage().getContent())
);
// the references example
chatCompletionResult.getReferences().forEach(
ref -> System.out.println(ref.getUrl())
);
}
}
流式
package com.volcengine.ark.runtime;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionRequest;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionResult;
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 java.util.ArrayList;
import java.util.List;
public class BotChatCompletionsExample {
public static void main(String[] args) {
String apiKey = System.getenv("ARK_API_KEY");
ArkService service = ArkService.builder().apiKey(apiKey).build();
System.out.println("\n----- streaming request -----");
final List<ChatMessage> streamMessages = new ArrayList<>();
final ChatMessage streamSystemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
final ChatMessage streamUserMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("常见的十字花科植物有哪些?").build();
streamMessages.add(streamSystemMessage);
streamMessages.add(streamUserMessage);
BotChatCompletionRequest streamChatCompletionRequest = BotChatCompletionRequest.builder()
// 将<YOUR_BOT_ID>替换为您的应用ID
.model("<YOUR_BOT_ID>")
.messages(streamMessages)
.build();
service.streamBotChatCompletion(streamChatCompletionRequest)
.doOnError(Throwable::printStackTrace)
.blockingForEach(
choice -> {
// references usage example
if (choice.getReferences() != null && !choice.getReferences().isEmpty()) {
choice.getReferences().forEach(ref -> System.out.println(ref.getUrl()));
}
if (!choice.getChoices().isEmpty()) {
System.out.print(choice.getChoices().get(0).getMessage().getContent());
}
}
);
// shutdown service
service.shutdownExecutor();
}
}
群聊
package com.volcengine.ark.runtime;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionRequest;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionResult;
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 java.util.ArrayList;
import java.util.List;
public class BotChatCompletionsExample {
public static void main(String[] args) {
String apiKey = System.getenv("ARK_API_KEY");
ArkService service = ArkService.builder().apiKey(apiKey).build();
System.out.println("\n----- standard request -----");
final List<ChatMessage> messages = new ArrayList<>();
final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("你是谁?").build();
messages.add(userMessage);
// build metadata
Map<String, Object> metadata = new HashMap<>();
Map<String, Object> groupChatConfig = new HashMap<>();
List<Map<String, Object>> characterConfigs = new ArrayList<>();
Map<String, Object> characterConfig1 = new HashMap<>();
Map<String, Object> modelDesc = new HashMap<>();
modelDesc.put("endpoint_id", "{YOUR_ENDPOINT_ID}");
characterConfig1.put("name", "群聊角色");
characterConfig1.put("system_prompt", "群聊角色设定");
characterConfig1.put("model_desc", modelDesc);
characterConfigs.add(characterConfig1);
groupChatConfig.put("characters", characterConfigs);
groupChatConfig.put("description", "场景描述");
groupChatConfig.put("user_name", "用户角色名");
metadata.put("group_chat_config", groupChatConfig);
metadata.put("target_character_name", "群聊角色");
BotChatCompletionRequest streamChatCompletionRequest = BotChatCompletionRequest.builder()
// 将<YOUR_BOT_ID>替换为您的应用ID
.model("<YOUR_BOT_ID>")
.messages(messages)
.metadata(metadata)
.build();
BotChatCompletionResult chatCompletionResult = service.createBotChatCompletion(chatCompletionRequest);
chatCompletionResult.getChoices().forEach(
choice -> System.out.println(choice.getMessage().getContent())
);
// the references example
chatCompletionResult.getReferences().forEach(
ref -> System.out.println(ref.getUrl())
);
}
}
package com.volcengine.ark.runtime;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionRequest;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionResult;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class BotChatCompletionsExample {
public static void main(String[] args) {
String apiKey = System.getenv("ARK_API_KEY");
ArkService service = ArkService.builder().apiKey(apiKey).build();
System.out.println("\n----- standard request -----");
final List<ChatMessage> messages = new ArrayList<>();
final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("常见的十字花科植物有哪些?").build();
messages.add(systemMessage);
messages.add(userMessage);
BotChatCompletionRequest chatCompletionRequest = BotChatCompletionRequest.builder()
// 将<YOUR_BOT_ID>替换为您的应用ID
.model("<YOUR_BOT_ID>")
.messages(messages)
.build();
BotChatCompletionResult chatCompletionResult = service.createBotChatCompletion(chatCompletionRequest, new HashMap<String, String>(){{put(Const.CLIENT_REQUEST_HEADER, "20240627115839147D61D8875537A133C1");}}).getChoices().forEach(choice -> System.out.println(choice.getMessage().getContent()));
chatCompletionResult.getChoices().forEach(
choice -> System.out.println(choice.getMessage().getContent())
);
// the references example
chatCompletionResult.getReferences().forEach(
ref -> System.out.println(ref.getUrl())
);
}
}
异常处理
package com.volcengine.ark.runtime;
import com.volcengine.ark.runtime.exception.ArkHttpException;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionRequest;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionResult;
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 java.util.ArrayList;
import java.util.List;
public class BotChatCompletionsExample {
public static void main(String[] args) {
String apiKey = System.getenv("ARK_API_KEY");
ArkService service = ArkService.builder().apiKey(apiKey).build();
System.out.println("\n----- standard request -----");
final List<ChatMessage> messages = new ArrayList<>();
final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("常见的十字花科植物有哪些?").build();
messages.add(systemMessage);
messages.add(userMessage);
BotChatCompletionRequest chatCompletionRequest = BotChatCompletionRequest.builder()
// 将<YOUR_BOT_ID>替换为您的应用ID
.model("<YOUR_BOT_ID>")
.messages(messages)
.build();
try {
BotChatCompletionResult chatCompletionResult = service.createBotChatCompletion(chatCompletionRequest);
chatCompletionResult.getChoices().forEach(
choice -> System.out.println(choice.getMessage().getContent())
);
// the references example
chatCompletionResult.getReferences().forEach(
ref -> System.out.println(ref.getUrl())
);
} catch (ArkHttpException e) {
System.out.print(e.toString());
}
}
}
图片理解
应用已经能够支持视觉理解模型。您可以在应用中配置视觉理解模型的推理接入点,使应用具备理解图片、视频能力,处理如图片文字内容识别,根据图片绘制表格、开发代码等任务。
- 配置应用请参见:
- 支持视觉理解模型请参见支持的模型。
- 更详细的视觉理解模型使用教程请参见视觉理解。
package com.volcengine.ark.demo;
import com.volcengine.ark.runtime.exception.ArkHttpException;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionRequest;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionResult;
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionContentPart;
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 java.util.ArrayList;
import java.util.List;
public class BotChatCompletionsExample {
public static void main(String[] args) {
String apiKey = System.getenv("ARK_API_KEY");
ArkService service = ArkService.builder()
.apiKey(apiKey)
.build();
final List<ChatMessage> messages = new ArrayList<>();
final List<ChatCompletionContentPart> multiParts = new ArrayList<>();
multiParts.add(ChatCompletionContentPart.builder().type("text").text(
"对美国人来说最安全的支付APP是什么?"
).build());
multiParts.add(ChatCompletionContentPart.builder().type("image_url").imageUrl(
new ChatCompletionContentPart.ChatCompletionContentPartImageURL(
"https://p9-arcosite.byteimg.com/tos-cn-i-goo7wpa0wc/7d2e7e810f054952aa8854fc99577260~tplv-goo7wpa0wc-image.image"))
.build());
final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER)
.multiContent(multiParts).build();
messages.add(userMessage);
BotChatCompletionRequest chatCompletionRequest = BotChatCompletionRequest.builder()
// 将<YOUR_BOT_ID>替换为您的应用ID
.model("<YOUR_BOT_ID>")
.messages(messages)
.build();
try {
BotChatCompletionResult chatCompletionResult = service.createBotChatCompletion(chatCompletionRequest);
chatCompletionResult.getChoices().forEach(
choice -> System.out.println(choice.getMessage().getContent()));
chatCompletionResult.getReferences().forEach(
ref -> System.out.println(ref.getUrl()));
} catch (ArkHttpException e) {
System.out.print(e.toString());
}
}
}
并发调用
当您的业务有较高的请求量,您可以通过单例模式结合线程池和连接池来调整并发调用,以应对不同的并发需求。
建议:
- 每个进程仅初始化ArkService一次,作为单例使用。多次初始化会构建多个线程池以及连接池。
- 进程退出前需要使用
service.shutdownExecutor()
关闭线程池。
package com.volcengine.ark.runtime;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionRequest;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionResult;
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 okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class BotChatCompletionsExample {
static String apiKey = System.getenv("ARK_API_KEY");
static ConnectionPool connectionPool = new ConnectionPool(5, 1, TimeUnit.SECONDS);
static Dispatcher dispatcher = new Dispatcher();
static ArkService service = ArkService.builder()
.dispatcher(dispatcher)
.connectionPool(connectionPool)
.apiKey(apiKey)
.build();
public static void main(String[] args) {
System.out.println("\n----- standard request -----");
final List<ChatMessage> messages = new ArrayList<>();
final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("常见的十字花科植物有哪些?").build();
messages.add(systemMessage);
messages.add(userMessage);
BotChatCompletionRequest chatCompletionRequest = BotChatCompletionRequest.builder()
// 将<YOUR_BOT_ID>替换为您的应用ID 零代码应用操作指南
.model("<YOUR_BOT_ID>")
.messages(messages)
.build();
BotChatCompletionResult chatCompletionResult = service.createBotChatCompletion(chatCompletionRequest);
chatCompletionResult.getChoices().forEach(
choice -> System.out.println(choice.getMessage().getContent())
);
// the references example
chatCompletionResult.getReferences().forEach(
ref -> System.out.println(ref.getUrl())
);
// shutdown service after all requests are finished
service.shutdownExecutor();
}
}
Bot SDK与Chat SDK差异对比
对比项 | Bot SDK | Chat SDK |
---|
单轮 | package com.volcengine.ark.runtime;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionRequest;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionResult;
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 java.util.ArrayList;
import java.util.List;
public class BotChatCompletionsExample {
public static void main(String[] args) {
String apiKey = System.getenv("ARK_API_KEY");
ArkService service = ArkService.builder().apiKey(apiKey).build();
System.out.println("\n----- standard request -----");
final List<ChatMessage> messages = new ArrayList<>();
final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("常见的十字花科植物有哪些?").build();
messages.add(systemMessage);
messages.add(userMessage);
BotChatCompletionRequest chatCompletionRequest = BotChatCompletionRequest.builder()// 注意此处存在差异
// 将<YOUR_BOT_ID>替换为您的应用ID
.model("<YOUR_BOT_ID>")
.messages(messages)
.build();
BotChatCompletionResult chatCompletionResult = service.createBotChatCompletion(chatCompletionRequest);// 注意此处存在差异
chatCompletionResult.getChoices().forEach(
choice -> System.out.println(choice.getMessage().getContent())
);
// the references example
chatCompletionResult.getReferences().forEach(
ref -> System.out.println(ref.getUrl())
);
}
}
| package com.volcengine.ark.runtime;
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 java.util.ArrayList;
import java.util.List;
public class ChatCompletionsExample {
public static void main(String[] args) {
String apiKey = System.getenv("ARK_API_KEY");
ArkService service = ArkService.builder().apiKey(apiKey).build();
System.out.println("\n----- standard request -----");
final List<ChatMessage> messages = new ArrayList<>();
final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("常见的十字花科植物有哪些?").build();
messages.add(systemMessage);
messages.add(userMessage);
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()// 注意此处存在差异
.model("<YOUR_ENDPOINT_ID>")// 注意此处为您的推理接入点ID
.messages(messages)
.build();
service.createChatCompletion(chatCompletionRequest).getChoices().forEach(choice -> System.out.println(choice.getMessage().getContent()));// 注意此处存在差异
// shutdown service
service.shutdownExecutor();
}
}
|
多轮 | package com.volcengine.ark.runtime;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionRequest;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionResult;
import com.volcengine.ark.runtime.model.completion.chat.*;
import com.volcengine.ark.runtime.service.ArkService;
import java.util.*;
public class BotChatCompletionsExample {
public static void main(String[] args) {
String apiKey = System.getenv("ARK_API_KEY");
ArkService service = ArkService.builder().apiKey(apiKey).build();
System.out.println("\n----- multiple rounds request -----");
final List<ChatMessage> messages = Arrays.asList(
ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build(),
ChatMessage.builder().role(ChatMessageRole.USER).content("花椰菜是什么?").build(),
ChatMessage.builder().role(ChatMessageRole.ASSISTANT).content("花椰菜又称菜花、花菜,是一种常见的蔬菜。").build(),
ChatMessage.builder().role(ChatMessageRole.USER).content("再详细点").build()
);
BotChatCompletionRequest chatCompletionRequest = BotChatCompletionRequest.builder()// 注意此处存在差异
// 将<YOUR_BOT_ID>替换为您的应用ID
.model("<YOUR_BOT_ID>")
.messages(messages)
.build();
BotChatCompletionResult chatCompletionResult = service.createBotChatCompletion(chatCompletionRequest);// 注意此处存在差异
chatCompletionResult.getChoices().forEach(
choice -> System.out.println(choice.getMessage().getContent())
);
// the references example
chatCompletionResult.getReferences().forEach(
ref -> System.out.println(ref.getUrl())
);
}
}
| package com.volcengine.ark.runtime;
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 java.util.Arrays;
import java.util.List;
public class ChatCompletionsExample {
public static void main(String[] args) {
String apiKey = System.getenv("ARK_API_KEY");
ArkService service = ArkService.builder().apiKey(apiKey).build();
System.out.println("\n----- multiple rounds request -----");
final List<ChatMessage> messages = Arrays.asList(
ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build(),
ChatMessage.builder().role(ChatMessageRole.USER).content("花椰菜是什么?").build(),
ChatMessage.builder().role(ChatMessageRole.ASSISTANT).content("花椰菜又称菜花、花菜,是一种常见的蔬菜。").build(),
ChatMessage.builder().role(ChatMessageRole.USER).content("再详细点").build()
);
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()// 注意此处存在差异
.model("<YOUR_ENDPOINT_ID>")// 注意此处为您的推理接入点ID
.messages(messages)
.build();
service.createChatCompletion(chatCompletionRequest).getChoices().forEach(choice -> System.out.println(choice.getMessage().getContent()));// 注意此处存在差异
// shutdown service
service.shutdownExecutor();
}
}
|
流式 | package com.volcengine.ark.runtime;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionRequest;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionResult;
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 java.util.ArrayList;
import java.util.List;
public class BotChatCompletionsExample {
public static void main(String[] args) {
String apiKey = System.getenv("ARK_API_KEY");
ArkService service = ArkService.builder().apiKey(apiKey).build();
System.out.println("\n----- streaming request -----");
final List<ChatMessage> streamMessages = new ArrayList<>();
final ChatMessage streamSystemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
final ChatMessage streamUserMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("常见的十字花科植物有哪些?").build();
streamMessages.add(streamSystemMessage);
streamMessages.add(streamUserMessage);
BotChatCompletionRequest streamChatCompletionRequest = BotChatCompletionRequest.builder()// 注意此处存在差异
// 将<YOUR_BOT_ID>替换为您的应用ID
.model("<YOUR_BOT_ID>")
.messages(streamMessages)
.build();
service.streamBotChatCompletion(streamChatCompletionRequest)// 注意此处存在差异
.doOnError(Throwable::printStackTrace)
.blockingForEach(
choice -> {
// references usage example
if (choice.getReferences() != null && !choice.getReferences().isEmpty()) {
choice.getReferences().forEach(ref -> System.out.println(ref.getUrl()));
}
if (!choice.getChoices().isEmpty()) {
System.out.print(choice.getChoices().get(0).getMessage().getContent());
}
}
);
// shutdown service
service.shutdownExecutor();
}
}
| package com.volcengine.ark.runtime;
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 java.util.ArrayList;
import java.util.List;
public class StreamChatCompletionsExample {
public static void main(String[] args) {
String apiKey = System.getenv("ARK_API_KEY");
ArkService service = ArkService.builder().apiKey(apiKey).build();
System.out.println("\n----- streaming request -----");
final List<ChatMessage> streamMessages = new ArrayList<>();
final ChatMessage streamSystemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
final ChatMessage streamUserMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("常见的十字花科植物有哪些?").build();
streamMessages.add(streamSystemMessage);
streamMessages.add(streamUserMessage);
ChatCompletionRequest streamChatCompletionRequest = ChatCompletionRequest.builder()// 注意此处存在差异
.model("<YOUR_ENDPOINT_ID>")// 注意此处为您的推理接入点ID
.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();
}
}
|
自定义header | package com.volcengine.ark.runtime;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionRequest;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionResult;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class BotChatCompletionsExample {
public static void main(String[] args) {
String apiKey = System.getenv("ARK_API_KEY");
ArkService service = ArkService.builder().apiKey(apiKey).build();
System.out.println("\n----- standard request -----");
final List<ChatMessage> messages = new ArrayList<>();
final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("常见的十字花科植物有哪些?").build();
messages.add(systemMessage);
messages.add(userMessage);
BotChatCompletionRequest chatCompletionRequest = BotChatCompletionRequest.builder()// 注意此处存在差异
// 将<YOUR_BOT_ID>替换为您的应用ID
.model("<YOUR_BOT_ID>")
.messages(messages)
.build();
BotChatCompletionResult chatCompletionResult = service.createBotChatCompletion(chatCompletionRequest, new HashMap<String, String>(){{put(Const.CLIENT_REQUEST_HEADER, "20240627115839147D61D8875537A133C1");}}).getChoices().forEach(choice -> System.out.println(choice.getMessage().getContent()));// 注意此处存在差异
chatCompletionResult.getChoices().forEach(
choice -> System.out.println(choice.getMessage().getContent())
);
// the references example
chatCompletionResult.getReferences().forEach(
ref -> System.out.println(ref.getUrl())
);
}
}
| package com.volcengine.ark.runtime;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ChatCompletionsExample {
public static void main(String[] args) {
String apiKey = System.getenv("ARK_API_KEY");
ArkService service = ArkService.builder().apiKey(apiKey).build();
System.out.println("\n----- standard request -----");
final List<ChatMessage> messages = new ArrayList<>();
final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("常见的十字花科植物有哪些?").build();
messages.add(systemMessage);
messages.add(userMessage);
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()// 注意此处存在差异
.model("<YOUR_ENDPOINT_ID>")// 注意此处为您的推理接入点ID
.messages(messages)
.build();
service.createChatCompletion(chatCompletionRequest, new HashMap<String, String>(){{put(Const.CLIENT_REQUEST_HEADER, "20240627115839147D61D8875537A133C1");}}).getChoices().forEach(choice -> System.out.println(choice.getMessage().getContent()));// 注意此处存在差异
// shutdown service
service.shutdownExecutor();
}
}
|
异常处理 | package com.volcengine.ark.runtime;
import com.volcengine.ark.runtime.exception.ArkHttpException;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionRequest;
import com.volcengine.ark.runtime.model.bot.completion.chat.BotChatCompletionResult;
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 java.util.ArrayList;
import java.util.List;
public class BotChatCompletionsExample {
public static void main(String[] args) {
String apiKey = System.getenv("ARK_API_KEY");
ArkService service = ArkService.builder().apiKey(apiKey).build();
System.out.println("\n----- standard request -----");
final List<ChatMessage> messages = new ArrayList<>();
final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("常见的十字花科植物有哪些?").build();
messages.add(systemMessage);
messages.add(userMessage);
BotChatCompletionRequest chatCompletionRequest = BotChatCompletionRequest.builder()// 注意此处存在差异
// 将<YOUR_BOT_ID>替换为您的应用ID
.model("<YOUR_BOT_ID>")
.messages(messages)
.build();
try {
BotChatCompletionResult chatCompletionResult = service.createBotChatCompletion(chatCompletionRequest);// 注意此处存在差异
chatCompletionResult.getChoices().forEach(
choice -> System.out.println(choice.getMessage().getContent())
);
// the references example
chatCompletionResult.getReferences().forEach(
ref -> System.out.println(ref.getUrl())
);
} catch (ArkHttpException e) {
System.out.print(e.toString());
}
}
}
| package com.volcengine.ark.runtime;
import com.volcengine.ark.runtime.exception.ArkHttpException;
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 java.util.ArrayList;
import java.util.List;
public class ChatCompletionsExample {
public static void main(String[] args) {
String apiKey = System.getenv("ARK_API_KEY");
ArkService service = ArkService.builder().apiKey(apiKey).build();
System.out.println("\n----- streaming request -----");
final List<ChatMessage> streamMessages = new ArrayList<>();
final ChatMessage streamSystemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
final ChatMessage streamUserMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("常见的十字花科植物有哪些?").build();
streamMessages.add(streamSystemMessage);
streamMessages.add(streamUserMessage);
ChatCompletionRequest streamChatCompletionRequest = ChatCompletionRequest.builder()// 注意此处存在差异
.model("<YOUR_ENDPOINT_ID>")// 注意此处为您的推理接入点ID
.messages(streamMessages)
.build();
try {
service.streamChatCompletion(streamChatCompletionRequest)// 注意此处存在差异
.doOnError(Throwable::printStackTrace)
.blockingForEach(
choice -> {
if (choice.getChoices().size() > 0) {
System.out.print(choice.getChoices().get(0).getMessage().getContent());
}
}
);
} catch (ArkHttpException e) {
System.out.print(e.toString());
}
// shutdown service
service.shutdownExecutor();
}
}
|