-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcozewrapper.py
288 lines (260 loc) · 9.9 KB
/
cozewrapper.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import logging
from pprint import pprint
import random
import time
from typing import Dict, List, Tuple
import urllib.parse
import requests
logging.basicConfig(
format='%(asctime)s [%(levelname)s] %(filename)s:%(funcName)s: %(message)s',
level=logging.DEBUG,
handlers=[
logging.StreamHandler() # Output logs to stdout
]
)
class CozeBotException(Exception):
"""
Custom exception class for CozeBot errors.
"""
def __init__(self, message):
"""
Initialize the CozeBotException.
Args:
message (str): The error message.
"""
super().__init__(message)
self.message = message
def __str__(self):
"""
Return a string representation of the exception.
Returns:
str: The error message.
"""
return self.message
class CozeBotWrapper:
"""
A wrapper class for interacting with the Coze Bot API.
"""
def __init__(
self,
api_key: str,
bot_id: str,
user_id: str,
base_url: str = "https://api.coze.com/v3",
):
self.api_endpoint = f"{base_url}/chat"
self.api_key = api_key
self.bot_id = bot_id
self.user_id = user_id
self.conversation_id = None
def _send_request(self, query = None, data = None, method = 'POST'):
"""
Sends a request to the Coze API with authentication and JSON data.
Args:
query (str): Optional query added to the URL (default: None).
data (dict): Optional JSON payload for the request (default: None).
method (str): Optional request method (POST or GET) to use (default: 'POST').
Returns:
dict: The parsed JSON response from the API.
Raises:
CozeBotException: If an error occurs during the request process.
"""
url = self.api_endpoint + query if query else self.api_endpoint
headers = {
'Authorization': f'Bearer {self.api_key}',
'Content-Type': 'application/json'
}
try:
if method == 'POST' or data:
response = requests.post(url, json=data, headers=headers, timeout=10)
else:
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
raise CozeBotException(f"An error occurred: {e}") from e
def start_chat(self, messages: List[str], conversation_id: str = None) -> Tuple[str, str]:
"""
Create a chat conversation with the Coze Bot.
Args:
messages (list): A list of message objects ("role", "content", "content_type").
Returns:
dict: The response from the API.
Example (https://www.coze.com/open):
curl --location --request POST 'https://api.coze.com/v3/chat'
--header 'Authorization: Bearer <API KEY>'
--header 'Content-Type: application/json'
--data-raw '{
"bot_id": "<BOT ID>",
"user_id": "<USER ID>",
"stream": false,
"auto_save_history":true,
"additional_messages":[
{
"role": "user",
"content": "<User message>",
"content_type": "text"
}
]
}'
Return:
{ 'code': 0,
'data': {'bot_id': '<BOT ID>',
'conversation_id': '<CONVERSATION ID>',
'created_at': <timestamp>,
'id': '<CHAT ID>',
'last_error': {'code': 0, 'msg': ''},
'status': 'in_progress'},
'msg': ''
}
"""
data = {
"bot_id": self.bot_id,
"user_id": self.user_id,
'stream': False,
"auto_save_history": True,
'additional_messages': messages,
}
query = urllib.parse.urlencode({'conversation_id': conversation_id}) if conversation_id else None
resp = self._send_request(data = data,
query=f"?{query}" if query else None)
if not 'data' in resp:
logging.error(f"No data in response:\n{resp}")
return None, None
if not 'id' in resp['data']:
logging.error(f"No chat_id in response:\n{resp}")
return None, None
# assert(resp['data']['bot_id'] == self.bot_id)
# assert(resp['data']['status'] == 'in_progress')
# Returns: <CONVERSATION_ID>, <CHAT_ID>
return resp['data']['conversation_id'], resp['data']['id']
def chat_status(self, conversation_id, chat_id) -> bool:
"""
Example (https://www.coze.com/open):
curl --location --request POST 'https://api.coze.com/v3/chat/retrieve?chat_id=<CHAT_ID>&conversation_id=<CONVERSATION_ID>'
--header 'Authorization: Bearer <API KEY>'
--header 'Content-Type: application/json'
"""
query = urllib.parse.urlencode({
'chat_id': chat_id,
'conversation_id': conversation_id
})
resp = self._send_request(query = f"/retrieve?{query}")
if not 'data' in resp:
logging.error(f"No data in response:\n{resp}")
return False
if not 'status' in resp['data']:
logging.error(f"No status in response:\n{resp}")
return False
# assert(resp['data']['bot_id'].strip() == self.bot_id)
# assert(resp['data']['id'].strip() == chat_id)
# assert(resp['data']['conversation_id'].strip() == conversation_id)
return resp['data']['status'].strip() == 'completed'
def get_messages(self, conversation_id, chat_id) -> List[Dict]:
"""
Example (https://www.coze.com/open):
curl --location --request POST 'https://api.coze.com/v3/chat/message/list?chat_id=<CHAT_ID>&conversation_id=<CONVERSATION_ID>'
--header 'Authorization: Bearer <API KEY>' \
--header 'Content-Type: application/json' \
Returns a list of message objects like this:
[
{
"bot_id": "<BOT_ID>",
"chat_id": "<CHAT_ID>",
"content": "........",
"content_type": "text",
"conversation_id": "<CONVERSATION_ID>",
"id": "<MESSAGE_ID>",
"role": "assistant",
"type": "answer"
},
{
"bot_id": "<BOT_ID>",
"chat_id": "<CHAT_ID>",
"content": "{\"msg_type\":\"generate_answer_finish\",\"data\":\"{\\\"finish_reason\\\":0}\",\"from_module\":null,\"from_unit\":null}",
"content_type": "text",
"conversation_id": "<CONVERSATION_ID>",
"id": "<MESSAGE_ID>",
"role": "assistant",
"type": "verbose"
},
{
"bot_id": "<BOT_ID>",
"chat_id": "<CHAT_ID>",
"content": ".....",
"content_type": "text",
"conversation_id": "<CONVERSATION_ID>",
"id": "<MESSAGE_ID>",
"role": "assistant",
"type": "follow_up"
},
...
]
"""
query = urllib.parse.urlencode({
'chat_id': chat_id,
'conversation_id': conversation_id
})
resp = self._send_request(query = f"/message/list?{query}")
if 'data' in resp:
return resp['data']
else:
logging.error(f"No data in response:\n{resp}")
return None
def chat(self, message: str) -> str:
"""
Start or continue a chat with the bot by sending a message.
Args:
message (str): The message to send to the chat.
Returns:
str: The response from the bot.
"""
self.conversation_id, self.chat_id = self.start_chat(
messages=[{"role": "user", "content": message, "content_type": "text"}],
conversation_id=self.conversation_id,
)
if not self.chat_id:
logging.error("Failed to start a chat, please try again")
return None
# Poll for the request to be completed.
time.sleep(random.uniform(0.8, 1.5))
for _ in range(20):
if self.chat_status(conversation_id=self.conversation_id, chat_id=self.chat_id):
break
else:
time.sleep(random.uniform(1,3))
# Get response messages.
resp_msgs = self.get_messages(
conversation_id=self.conversation_id, chat_id=self.chat_id
)
if not resp_msgs:
return None
for msg in resp_msgs:
if msg['type'] == 'answer':
# pprint(msg)
# assert(msg['role'] == 'assistant')
# assert(msg['conversation_id'] == self.conversation_id)
return msg['content']
# print(f"Assistant: {msg['content']}")
return None
def check_connection(self):
"""
Check the connection to the Coze Bot API.
Raises:
CozeBotException: If the connection check fails.
"""
headers = {
'Authorization': f'Bearer {self.api_key}',
'Content-Type': 'application/json'
}
try:
response = requests.get(self.api_endpoint, headers=headers, timeout=10)
response.raise_for_status()
return True
except requests.exceptions.RequestException as e:
raise CozeBotException(f"Connection check failed: {e}") from e
# Example usage:
# api_key = "your_api_key_here"
# bot = CozeBotWrapper(api_key, bot_id="<your_bot_id>", user_id="<user_id>")
# response = bot.chat("Hello")
# print(response)