|
| 1 | +package com.volcengine.ark.runtime; |
| 2 | + |
| 3 | + |
| 4 | +import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionRequest; |
| 5 | +import com.volcengine.ark.runtime.model.completion.chat.ChatMessage; |
| 6 | +import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole; |
| 7 | +import com.volcengine.ark.runtime.service.ArkService; |
| 8 | +import okhttp3.ConnectionPool; |
| 9 | +import okhttp3.Dispatcher; |
| 10 | +import org.apache.commons.lang.StringUtils; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import java.util.concurrent.TimeUnit; |
| 15 | + |
| 16 | +/* |
| 17 | +# pom.xml |
| 18 | +<dependency> |
| 19 | + <groupId>com.volcengine</groupId> |
| 20 | + <artifactId>volcengine-java-sdk-ark-runtime</artifactId> |
| 21 | + <version>LATEST</version> |
| 22 | +</dependency> |
| 23 | +*/ |
| 24 | + |
| 25 | +public class ChatCompletionsReasoningExample { |
| 26 | + |
| 27 | + /** |
| 28 | + * Authentication |
| 29 | + * 1.If you authorize your endpoint using an API key, you can set your api key to environment variable "ARK_API_KEY" |
| 30 | + * String apiKey = System.getenv("ARK_API_KEY"); |
| 31 | + * ArkService service = new ArkService(apiKey); |
| 32 | + * Note: If you use an API key, this API key will not be refreshed. |
| 33 | + * To prevent the API from expiring and failing after some time, choose an API key with no expiration date. |
| 34 | + * |
| 35 | + * 2.If you authorize your endpoint with Volcengine Identity and Access Management(IAM), set your api key to environment variable "VOLC_ACCESSKEY", "VOLC_SECRETKEY" |
| 36 | + * String ak = System.getenv("VOLC_ACCESSKEY"); |
| 37 | + * String sk = System.getenv("VOLC_SECRETKEY"); |
| 38 | + * ArkService service = new ArkService(ak, sk); |
| 39 | + * To get your ak&sk, please refer to this document(https://www.volcengine.com/docs/6291/65568) |
| 40 | + * For more information,please check this document(https://www.volcengine.com/docs/82379/1263279) |
| 41 | + */ |
| 42 | + |
| 43 | + static String apiKey = System.getenv("ARK_API_KEY"); |
| 44 | + static ConnectionPool connectionPool = new ConnectionPool(5, 1, TimeUnit.SECONDS); |
| 45 | + static Dispatcher dispatcher = new Dispatcher(); |
| 46 | + static ArkService service = ArkService.builder().dispatcher(dispatcher).connectionPool(connectionPool).apiKey(apiKey).build(); |
| 47 | + |
| 48 | + public static void main(String[] args) { |
| 49 | + System.out.println("\n----- standard request -----"); |
| 50 | + final List<ChatMessage> messages = new ArrayList<>(); |
| 51 | + final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("How many Rs are there in the word 'strawberry'?").build(); |
| 52 | + messages.add(userMessage); |
| 53 | + |
| 54 | + ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder() |
| 55 | + .model("${YOUR_ENDPOINT_ID}") |
| 56 | + .messages(messages) |
| 57 | + .build(); |
| 58 | + |
| 59 | + service.createChatCompletion(chatCompletionRequest).getChoices().forEach( |
| 60 | + choice-> { |
| 61 | + if(StringUtils.isNotEmpty(choice.getMessage().getReasoningContent())){ |
| 62 | + System.out.println(choice.getMessage().getReasoningContent()); |
| 63 | + }else{ |
| 64 | + System.out.println(choice.getMessage().getContent()); |
| 65 | + } |
| 66 | + } |
| 67 | + ); |
| 68 | + |
| 69 | + System.out.println("\n----- streaming request -----"); |
| 70 | + final List<ChatMessage> streamMessages = new ArrayList<>(); |
| 71 | + final ChatMessage streamUserMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("How many Rs are there in the word 'strawberry'?").build(); |
| 72 | + streamMessages.add(streamUserMessage); |
| 73 | + |
| 74 | + ChatCompletionRequest streamChatCompletionRequest = ChatCompletionRequest.builder() |
| 75 | + .model("${YOUR_ENDPOINT_ID}") |
| 76 | + .messages(streamMessages) |
| 77 | + .build(); |
| 78 | + |
| 79 | + service.streamChatCompletion(streamChatCompletionRequest) |
| 80 | + .doOnError(Throwable::printStackTrace) |
| 81 | + .blockingForEach( |
| 82 | + delta -> { |
| 83 | + if (!delta.getChoices().isEmpty()) { |
| 84 | + if(StringUtils.isNotEmpty(delta.getChoices().get(0).getMessage().getReasoningContent())){ |
| 85 | + System.out.print(delta.getChoices().get(0).getMessage().getReasoningContent()); |
| 86 | + }else{ |
| 87 | + System.out.print(delta.getChoices().get(0).getMessage().getContent()); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + ); |
| 92 | + |
| 93 | + // shutdown service after all requests is finished |
| 94 | + service.shutdownExecutor(); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments