-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathChatExample.java
More file actions
96 lines (85 loc) · 3.5 KB
/
ChatExample.java
File metadata and controls
96 lines (85 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package example.chat;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import com.coze.openapi.client.chat.*;
import com.coze.openapi.client.chat.model.Chat;
import com.coze.openapi.client.chat.model.ChatPoll;
import com.coze.openapi.client.chat.model.ChatStatus;
import com.coze.openapi.client.connversations.message.model.Message;
import com.coze.openapi.service.auth.TokenAuth;
import com.coze.openapi.service.service.CozeAPI;
/*
This example describes how to use the chat interface to initiate conversations,
poll the status of the conversation, and obtain the messages after the conversation is completed.
* */
public class ChatExample {
// For non-streaming chat API, it is necessary to create a chat first and then poll the chat
// results.
public static void main(String[] args) throws Exception {
// Get an access_token through personal access token or oauth.
String token = System.getenv("COZE_API_TOKEN");
String botID = System.getenv("PUBLISHED_BOT_ID");
String uid = System.getenv("USER_ID");
TokenAuth authCli = new TokenAuth(token);
// Init the Coze client through the access_token.
CozeAPI coze =
new CozeAPI.Builder()
.baseURL(System.getenv("COZE_API_BASE"))
.auth(authCli)
.readTimeout(10000)
.build();
/*
* Step one, create chat
* Call the coze.chat().create() method to create a chat. The create method is a non-streaming
* chat and will return a Chat class. Developers should periodically check the status of the
* chat and handle them separately according to different states.
* */
CreateChatReq req =
CreateChatReq.builder()
.botID(botID)
.userID(uid)
.messages(Collections.singletonList(Message.buildUserQuestionText("What can you do?")))
.build();
CreateChatResp chatResp = coze.chat().create(req);
System.out.println(chatResp);
Chat chat = chatResp.getChat();
// get chat id and conversationID
String chatID = chat.getID();
String conversationID = chat.getConversationID();
/*
* Step two, poll the result of chat
* Assume the development allows at most one chat to run for 10 seconds. If it exceeds 10 seconds,
* the chat will be cancelled.
* And when the chat status is not completed, poll the status of the chat once every second.
* After the chat is completed, retrieve all messages in the chat.
* */
long timeout = 10L;
long start = System.currentTimeMillis() / 1000;
while (ChatStatus.IN_PROGRESS.equals(chat.getStatus())) {
try {
// The API has a rate limit with 1 QPS.
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
if ((System.currentTimeMillis() / 1000) - start > timeout) {
// The chat can be cancelled before its completed.
System.out.println(coze.chat().cancel(CancelChatReq.of(conversationID, chatID)));
break;
}
RetrieveChatResp resp = coze.chat().retrieve(RetrieveChatReq.of(conversationID, chatID));
System.out.println(resp);
chat = resp.getChat();
if (ChatStatus.COMPLETED.equals(chat.getStatus())) {
break;
}
}
// The sdk provide an automatic polling method.
ChatPoll chat2 = coze.chat().createAndPoll(req);
System.out.println(chat2);
// the developer can also set the timeout.
ChatPoll chat3 = coze.chat().createAndPoll(req, timeout);
System.out.println(chat3);
}
}