-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatDemo.kt
121 lines (110 loc) · 4.21 KB
/
ChatDemo.kt
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.coze.demo
import com.coze.api.chat.ChatService
import com.coze.api.chat.MessageService
import com.coze.api.helper.RequestOptions
import com.coze.api.model.EventType
import com.coze.api.model.ChatStatus
import com.coze.api.model.ChatV3Message
import com.coze.api.model.EnterMessage
import com.coze.api.model.MessageType
import com.coze.api.model.RoleType
import com.coze.api.model.StreamChatData
import com.coze.api.model.chat.*
import com.coze.api.model.conversation.ListMessageReq
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.withContext
/**
* Chat Demo | 聊天演示
* Demonstrates chat functionality | 演示聊天功能
*/
class ChatDemo {
private val chatService = ChatService()
private val messageService = MessageService()
private val defaultBotId = "7373880376026103809"
private val defaultUserId = "007"
/**
* Create chat without streaming | 不使用流式创建聊天
* @return Flow<String> Chat events | 聊天事件流
*/
fun noneStreamCreate(): Flow<String> = flow {
val greeting = "hi there"
emit(greeting)
emit("(Visiting the chat v3 API!)...")
// Send request to get data | 发送请求获取数据
val data = chatService.createChat(
ChatRequest(
botId = defaultBotId,
userId = defaultUserId,
additionalMessages = listOf(EnterMessage(role = RoleType.USER, content = greeting))
)
).data
if (data != null) {
emit("(Response arrived. chat_id=${data.id}, conversation_id=${data.conversationId})")
}
}
/**
* Test chat streaming | 测试聊天流式处理
* @param msg Input message | 输入消息
* @return Flow<String> Chat events | 聊天事件流
*/
fun streamTest(msg: String=""): Flow<String> = flow {
val messageContent = msg.ifBlank { "Tell me 3 famous jokes" }
val streamFlow = chatService.stream(
ChatRequest(
botId = defaultBotId,
userId = defaultUserId,
additionalMessages = listOf(
EnterMessage(role = RoleType.USER, content = messageContent)
)
)
)
// Collect events from the stream and emit them | 从流中收集事件并发送
streamFlow.collect { event ->
if (event is StreamChatData.ChatMessageEvent && event.event == EventType.CONVERSATION_MESSAGE_DELTA) {
emit(event.data.content ?: " ")
}
}
}
/**
* Create chat and poll without streaming | 不使用流式创建聊天并轮询
* @param msg Input message | 输入消息
* @return Flow<String> Chat events | 聊天事件流
*/
fun noneStreamCreateAndPoll(msg: String=""): Flow<String> = flow {
val messageContent = msg.ifBlank { "Tell me 3 famous jokes" }
emit("(Visiting the chat v3 API!)...")
// Send request to get data | 发送请求获取数据
val data = chatService.createAndPollChat(
ChatRequest(
botId = defaultBotId,
userId = defaultUserId,
additionalMessages = listOf(EnterMessage(role = RoleType.USER, content = messageContent))
)
)
emit("(Response arrived.)")
if (data.chat.status == ChatStatus.COMPLETED) {
data.messages?.forEach { item ->
val prefix = if (item.type != MessageType.ANSWER) "> " else ""
emit("$prefix[${item.role}]:[${item.type}]:${item.content}")
}
emit("usage: ${data.chat.usage}")
} else {
emit("No messages")
}
}
/**
* List chat messages | 列出聊天消息
* @param conversationId Conversation ID | 对话ID
* @param chatId Chat ID | 聊天ID
* @return List<ChatV3Message> List of messages | 消息列表
*/
suspend fun listChatMessages(
conversationId: String,
chatId: String,
): List<ChatV3Message> = withContext(Dispatchers.IO) {
messageService.list(conversationId, chatId).data?: emptyList()
}
}