33
44import com .volcengine .ark .runtime .model .bot .completion .chat .BotChatCompletionRequest ;
55import com .volcengine .ark .runtime .model .bot .completion .chat .BotChatCompletionResult ;
6- import com .volcengine .ark .runtime .model .completion .chat .ChatCompletionRequest ;
76import com .volcengine .ark .runtime .model .completion .chat .ChatMessage ;
87import com .volcengine .ark .runtime .model .completion .chat .ChatMessageRole ;
98import com .volcengine .ark .runtime .service .ArkService ;
9+ import okhttp3 .ConnectionPool ;
10+ import okhttp3 .Dispatcher ;
1011
1112import java .util .ArrayList ;
1213import java .util .List ;
14+ import java .util .concurrent .TimeUnit ;
1315
1416/*
1517# pom.xml
@@ -37,11 +39,13 @@ public class BotChatCompletionsExample {
3739 * To get your ak&sk, please refer to this document(https://www.volcengine.com/docs/6291/65568)
3840 * For more information,please check this document(https://www.volcengine.com/docs/82379/1263279)
3941 */
40- public static void main (String [] args ) {
4142
42- String apiKey = System .getenv ("ARK_API_KEY" );
43- ArkService service = new ArkService (apiKey );
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 ();
4447
48+ public static void main (String [] args ) {
4549 System .out .println ("\n ----- standard request -----" );
4650 final List <ChatMessage > messages = new ArrayList <>();
4751 final ChatMessage systemMessage = ChatMessage .builder ().role (ChatMessageRole .SYSTEM ).content ("你是豆包,是由字节跳动开发的 AI 人工智能助手" ).build ();
@@ -57,7 +61,9 @@ public static void main(String[] args) {
5761 BotChatCompletionResult chatCompletionResult = service .createBotChatCompletion (chatCompletionRequest );
5862 chatCompletionResult .getChoices ().forEach (choice -> System .out .println (choice .getMessage ().getContent ()));
5963 // the references example
60- chatCompletionResult .getReferences ().forEach (ref -> System .out .println (ref .getUrl ()));
64+ if (chatCompletionResult .getReferences () != null ) {
65+ chatCompletionResult .getReferences ().forEach (ref -> System .out .println (ref .getUrl ()));
66+ }
6167
6268 System .out .println ("\n ----- streaming request -----" );
6369 final List <ChatMessage > streamMessages = new ArrayList <>();
@@ -84,7 +90,7 @@ public static void main(String[] args) {
8490 }
8591 );
8692
87- // shutdown service
93+ // shutdown service after all requests is finished
8894 service .shutdownExecutor ();
8995 }
9096
0 commit comments