Skip to content

Commit

Permalink
docs: Add example for multimodal data chat using workflow (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
chyroc authored Dec 25, 2024
1 parent 61445c6 commit cdeb532
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 23 deletions.
47 changes: 24 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,30 @@ pip install cozepy

### Examples

| Example | File |
|------------------------------|------------------------------------------------------------------------------|
| pat auth | [examples/auth_pat.py](examples/auth_pat.py) |
| oauth by web code | [examples/auth_oauth_web.py](examples/auth_oauth_web.py) |
| oauth by jwt flow | [examples/auth_oauth_jwt.py](examples/auth_oauth_jwt.py) |
| oauth by pkce flow | [examples/auth_oauth_pkce.py](examples/auth_oauth_pkce.py) |
| oauth by device flow | [examples/auth_oauth_device.py](examples/auth_oauth_device.py) |
| bot create, publish and chat | [examples/bot_publish.py](examples/bot_publish.py) |
| non-stream chat | [examples/chat_no_stream.py](examples/chat_no_stream.py) |
| steam chat | [examples/chat_stream.py](examples/chat_stream.py) |
| chat with conversation | [examples/chat_conversation_stream.py](examples/chat_conversation_stream.py) |
| chat with local plugin | [examples/chat_local_plugin.py](examples/chat_local_plugin.py) |
| chat with image | [examples/chat_multimodal_stream.py](examples/chat_multimode_stream.py) |
| chat with audio | [examples/chat_audio.py](examples/chat_audio.py) |
| non-stream workflow chat | [examples/workflow_no_stream.py](examples/workflow_no_stream.py) |
| stream workflow chat | [examples/workflow_stream.py](examples/workflow_stream.py) |
| async workflow run | [examples/workflow_async.py](examples/workflow_async.py) |
| workflow chat | [examples/workflow_chat_stream.py](examples/workflow_chat_stream.py) |
| conversation | [examples/conversation.py](examples/conversation.py) |
| workspace | [examples/workspace.py](examples/workspace.py) |
| timeout config | [examples/timeout.py](examples/timeout.py) |
| setup coze log config | [examples/log.py](examples/log.py) |
| how to handle exception | [examples/exception.py](examples/exception.py) |
| Example | File |
|------------------------------|------------------------------------------------------------------------------------------|
| pat auth | [examples/auth_pat.py](examples/auth_pat.py) |
| oauth by web code | [examples/auth_oauth_web.py](examples/auth_oauth_web.py) |
| oauth by jwt flow | [examples/auth_oauth_jwt.py](examples/auth_oauth_jwt.py) |
| oauth by pkce flow | [examples/auth_oauth_pkce.py](examples/auth_oauth_pkce.py) |
| oauth by device flow | [examples/auth_oauth_device.py](examples/auth_oauth_device.py) |
| bot create, publish and chat | [examples/bot_publish.py](examples/bot_publish.py) |
| non-stream chat | [examples/chat_no_stream.py](examples/chat_no_stream.py) |
| steam chat | [examples/chat_stream.py](examples/chat_stream.py) |
| chat with conversation | [examples/chat_conversation_stream.py](examples/chat_conversation_stream.py) |
| chat with local plugin | [examples/chat_local_plugin.py](examples/chat_local_plugin.py) |
| chat with image | [examples/chat_multimodal_stream.py](examples/chat_multimode_stream.py) |
| chat with audio | [examples/chat_audio.py](examples/chat_audio.py) |
| workflow non-stream run | [examples/workflow_no_stream.py](examples/workflow_no_stream.py) |
| workflow stream run | [examples/workflow_stream.py](examples/workflow_stream.py) |
| async workflow run | [examples/workflow_async.py](examples/workflow_async.py) |
| workflow chat | [examples/workflow_chat_stream.py](examples/workflow_chat_stream.py) |
| workflow chat with image | [examples/workflow_chat_multimode_stream.py](examples/workflow_chat_multimode_stream.py) |
| conversation | [examples/conversation.py](examples/conversation.py) |
| workspace | [examples/workspace.py](examples/workspace.py) |
| timeout config | [examples/timeout.py](examples/timeout.py) |
| setup coze log config | [examples/log.py](examples/log.py) |
| how to handle exception | [examples/exception.py](examples/exception.py) |

### Initialize the Coze client

Expand Down
89 changes: 89 additions & 0 deletions examples/workflow_chat_multimode_stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""
This example is about how to use the streaming interface to start a chat request
and handle chat events
"""

import logging
import os
import sys

from cozepy import (
COZE_COM_BASE_URL,
ChatEventType,
Coze,
JWTAuth,
JWTOAuthApp,
Message,
MessageObjectString,
setup_logging,
)

# Get the workflow id
workflow_id = os.getenv("COZE_WORKFLOW_ID") or "workflow id"
# Get the bot id
bot_id = os.getenv("COZE_BOT_ID")
# Whether to print detailed logs
is_debug = os.getenv("DEBUG")

if is_debug:
setup_logging(logging.DEBUG)


def init_coze_client() -> Coze:
# client ID
jwt_oauth_client_id = os.getenv("COZE_JWT_OAUTH_CLIENT_ID")
# path to the private key file (usually with .pem extension)
jwt_oauth_private_key_file_path = os.getenv("COZE_JWT_OAUTH_PRIVATE_KEY_FILE_PATH")
# public key id
jwt_oauth_public_key_id = os.getenv("COZE_JWT_OAUTH_PUBLIC_KEY_ID")
# private key
jwt_oauth_private_key = ""
with open(jwt_oauth_private_key_file_path, "r") as f:
jwt_oauth_private_key = f.read()

# 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

jwt_oauth_app = JWTOAuthApp(
client_id=jwt_oauth_client_id,
private_key=jwt_oauth_private_key,
public_key_id=jwt_oauth_public_key_id,
base_url=coze_api_base,
)

return Coze(auth=JWTAuth(base_url=coze_api_base, oauth_app=jwt_oauth_app), base_url=coze_api_base)


# Init the Coze client through the access_token.
coze = init_coze_client()

conversation = coze.conversations.create()

# Call the upload interface to upload a picture requiring text recognition, and
# obtain the file_id of the picture.
file_path = sys.argv[1] if len(sys.argv) > 1 else "/path/image.jpg"
file = coze.files.upload(file=file_path)

# Call the coze.chat.stream method to create a chat. The create method is a streaming
# chat and will return a Chat Iterator. Developers should iterate the iterator to get
# chat event and handle them.
for event in coze.workflows.chat.stream(
workflow_id=workflow_id,
bot_id=bot_id,
conversation_id=conversation.id,
additional_messages=[
Message.build_user_question_objects(
[
MessageObjectString.build_text("What text is on the picture?"),
MessageObjectString.build_image(file_id=file.id),
]
),
],
):
if event.event == ChatEventType.CONVERSATION_MESSAGE_DELTA:
print(event.message.content, end="", flush=True)

if event.event == ChatEventType.CONVERSATION_CHAT_COMPLETED:
print()
print("token usage:", event.chat.usage.token_count)

0 comments on commit cdeb532

Please sign in to comment.