|
@@ -0,0 +1,56 @@
|
|
|
|
+package com.fdkankan.contro.controller;
|
|
|
|
+
|
|
|
|
+import com.coze.openapi.client.chat.CreateChatReq;
|
|
|
|
+import com.coze.openapi.client.chat.model.ChatEvent;
|
|
|
|
+import com.coze.openapi.client.chat.model.ChatEventType;
|
|
|
|
+import com.coze.openapi.client.connversations.message.model.Message;
|
|
|
|
+import com.coze.openapi.service.auth.TokenAuth;
|
|
|
|
+import com.coze.openapi.service.config.Consts;
|
|
|
|
+import com.coze.openapi.service.service.CozeAPI;
|
|
|
|
+import io.reactivex.Flowable;
|
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
|
+
|
|
|
|
+import java.util.Collections;
|
|
|
|
+
|
|
|
|
+public class Test {
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+
|
|
|
|
+ //客户端id
|
|
|
|
+ String token = "pat_0Y2BAveK0QbYuBf288449wQtuVoq52SWJsfeCkYU6WJ3Fi1iJnKezCi8LWolfr2z";
|
|
|
|
+ String botID = "7494469232462676008";
|
|
|
|
+ String userID = "123123";
|
|
|
|
+
|
|
|
|
+ TokenAuth authCli = new TokenAuth(token);
|
|
|
|
+
|
|
|
|
+ CozeAPI coze =
|
|
|
|
+ new CozeAPI.Builder()
|
|
|
|
+ .baseURL(Consts.COZE_CN_BASE_URL)
|
|
|
|
+ .auth(authCli)
|
|
|
|
+ .client(new OkHttpClient.Builder().build())
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ CreateChatReq req =
|
|
|
|
+ CreateChatReq.builder()
|
|
|
|
+ .botID(botID)
|
|
|
|
+ .userID(userID)
|
|
|
|
+ .messages(Collections.singletonList(Message.buildUserQuestionText("我帅吗?")))
|
|
|
|
+ .build();
|
|
|
|
+
|
|
|
|
+ Flowable<ChatEvent> resp = coze.chat().stream(req);
|
|
|
|
+ resp.blockingForEach(
|
|
|
|
+ event -> {
|
|
|
|
+ if (ChatEventType.CONVERSATION_MESSAGE_DELTA.equals(event.getEvent())) {
|
|
|
|
+ System.out.print(event.getMessage().getContent());
|
|
|
|
+ }
|
|
|
|
+ if (ChatEventType.CONVERSATION_CHAT_COMPLETED.equals(event.getEvent())) {
|
|
|
|
+ System.out.println("Token usage:" + event.getChat().getUsage().getTokenCount());
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ System.out.println("done");
|
|
|
|
+ coze.shutdownExecutor();
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+}
|