-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathconversation.py
50 lines (40 loc) · 1.36 KB
/
conversation.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
"""
This example is about how to create conversation and retrieve.
"""
import logging
import os
from cozepy import ( # noqa
COZE_COM_BASE_URL,
BotPromptInfo,
ChatEventType,
Coze,
Message,
MessageContentType,
MessageRole,
TokenAuth,
setup_logging,
)
# Get an access_token through personal access token or oauth.
coze_api_token = os.getenv("COZE_API_TOKEN")
# The default access is api.coze.com, but if you need to access api.coze.cn,
# please use base_url to configure the api endpoint to access
coze_api_base = os.getenv("COZE_API_BASE") or COZE_COM_BASE_URL
# Whether to print detailed logs
is_debug = os.getenv("DEBUG")
if is_debug:
setup_logging(logging.DEBUG)
# Init the Coze client through the access_token.
coze = Coze(auth=TokenAuth(token=coze_api_token), base_url=coze_api_base)
conversation = coze.conversations.create()
print("Create conversation: ", conversation)
conversation_retrieve = coze.conversations.retrieve(conversation_id=conversation.id)
print("Retrieve conversation:", conversation_retrieve)
message = coze.conversations.messages.create(
conversation_id=conversation.id,
role=MessageRole.USER,
content="hi",
content_type=MessageContentType.TEXT,
)
print("Append message to conversation:", message)
section = coze.conversations.clear(conversation_id=conversation.id)
print("Clear conversation:", section)