说明
示例代码中 <YOUR_ENDPOINT_ID>
需要替换为您创建的推理接入点 ID。
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>") .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.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>") .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.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>") .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(); } }
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 io.reactivex.schedulers.Schedulers; import java.util.ArrayList; import java.util.List; public class AsyncStreamChatCompletionsExample { public static void main(String[] args) throws InterruptedException { 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>") .messages(streamMessages) .build(); service.streamChatCompletion(streamChatCompletionRequest) .doOnError(Throwable::printStackTrace) .subscribeOn(Schedulers.io()) .observeOn(Schedulers.single()) .subscribe( choice -> { if (choice.getChoices().size() > 0) { System.out.print(choice.getChoices().get(0).getMessage().getContent()); } } ); // just wait for result Thread.sleep(60000); // shutdown service service.shutdownExecutor(); } }
package com.volcengine.ark.runtime; import com.volcengine.ark.runtime.model.completion.chat.*; import com.volcengine.ark.runtime.service.ArkService; import java.util.*; public class FunctionCallChatCompletionsExample { public static void main(String[] args) { String apiKey = System.getenv("ARK_API_KEY"); ArkService service = ArkService.builder().apiKey(apiKey).build(); System.out.println("\n----- function call request -----"); final List<ChatMessage> messages = new ArrayList<>(); final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("北京今天天气如何?").build(); messages.add(userMessage); final List<ChatTool> tools = Arrays.asList( new ChatTool( "function", new ChatFunction.Builder() .name("get_current_weather") .description("获取给定地点的天气") .parameters(new Weather( "object", new HashMap<String, Object>() {{ put("location", new HashMap<String, String>() {{ put("type", "string"); put("description", "T地点的位置信息,比如北京"); }}); put("unit", new HashMap<String, Object>() {{ put("type", "string"); put("enum", Arrays.asList("摄氏度", "华氏度")); }}); }}, Collections.singletonList("location") )) .build() ) ); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder() .model("<YOUR_ENDPOINT_ID>") .messages(messages) .tools(tools) .build(); service.createChatCompletion(chatCompletionRequest).getChoices().forEach(System.out::println); // shutdown service service.shutdownExecutor(); } public static class Weather { public String type; public Map<String, Object> properties; public List<String> required; public Weather(String type, Map<String, Object> properties, List<String> required) { this.type = type; this.properties = properties; this.required = required; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Map<String, Object> getProperties() { return properties; } public void setProperties(Map<String, Object> properties) { this.properties = properties; } public List<String> getRequired() { return required; } public void setRequired(List<String> required) { this.required = required; } } }
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>") .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.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>") .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(); } }
当您的业务有较高的请求量,您可以通过单例模式结合线程池和连接池来调整并发调用,以应对不同的并发需求。
建议:
- 每个进程仅初始化ArkService一次,作为单例使用。多次初始化会构建多个线程池以及连接池。
- 进程退出前需要使用
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 okhttp3.ConnectionPool; import okhttp3.Dispatcher; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; public class ChatCompletionsExampleV2 { static String apiKey = System.getenv("ARK_API_KEY"); //创建一个连接池,最多允许5个空闲连接,空闲连接的最大存活时间为1秒。 static ConnectionPool connectionPool = new ConnectionPool(5, 1, TimeUnit.SECONDS); //创建一个调度器,用于管理 HTTP 请求的执行。 static Dispatcher dispatcher = new Dispatcher(); //使用 ArkService 构建器创建一个 ArkService 实例,配置了调度器、连接池、API 密钥和基础 URL。 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); ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder() .model("<YOUR_ENDPOINT_ID>") .messages(messages) .build(); service.createChatCompletion(chatCompletionRequest).getChoices().forEach(choice -> System.out.println(choice.getMessage().getContent())); // 请求结束需要关闭服务 service.shutdownExecutor(); } }