diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0efd7e8..e39bd53 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -43,6 +43,7 @@ jobs: COZE_JWT_AUTH_CLIENT_ID: ${{ secrets.COZE_JWT_AUTH_CLIENT_ID }} COZE_JWT_AUTH_PRIVATE_KEY: ${{ secrets.COZE_JWT_AUTH_PRIVATE_KEY }} COZE_JWT_AUTH_KEY_ID: ${{ secrets.COZE_JWT_AUTH_KEY_ID }} + COZE_BOT_ID_TRANSLATE: ${{ secrets.COZE_BOT_ID_TRANSLATE }} - name: Upload coverage to Codecov uses: codecov/codecov-action@v3 diff --git a/cozepy/chat.py b/cozepy/chat.py index 702df21..085f308 100644 --- a/cozepy/chat.py +++ b/cozepy/chat.py @@ -1,6 +1,6 @@ import json from enum import Enum -from typing import Dict, List, Iterator, Tuple +from typing import Dict, List, Iterator, Tuple, Union from .auth import Auth from .model import Message, Chat, MessageResponse, CozeModel @@ -118,7 +118,7 @@ def chat_v3( auto_save_history: bool = True, meta_data: Dict[str, str] = None, conversation_id: str = None, - ) -> Tuple[Chat, ChatIterator]: + ) -> Union[Chat, ChatIterator]: """ Create a conversation. Conversation is an interaction between a bot and a user, including one or more messages. diff --git a/cozepy/request.py b/cozepy/request.py index 87ceabe..eedad80 100644 --- a/cozepy/request.py +++ b/cozepy/request.py @@ -1,4 +1,4 @@ -from typing import TYPE_CHECKING, Tuple, Optional, Iterator +from typing import TYPE_CHECKING, Tuple, Optional, Iterator, Union import requests from requests import Response @@ -37,7 +37,7 @@ def request( headers: dict = None, body: dict = None, stream: bool = False, - ) -> Tuple[T, Iterator[bytes]]: + ) -> Union[T, Iterator[bytes]]: """ Send a request to the server. """ diff --git a/tests/test_chat.py b/tests/test_chat.py new file mode 100644 index 0000000..9227d25 --- /dev/null +++ b/tests/test_chat.py @@ -0,0 +1,41 @@ +import os + +from cozepy import TokenAuth, Coze, COZE_CN_BASE_URL, Message, ChatIterator, Event +from cozepy.auth import _random_hex + + +def test_chat_v3_not_stream(): + token = os.getenv("COZE_TOKEN").strip() + bot_id = os.getenv("COZE_BOT_ID_TRANSLATE").strip() + + auth = TokenAuth(token) + cli = Coze(auth=auth, base_url=COZE_CN_BASE_URL) + + chat = cli.chat.chat_v3( + bot_id=bot_id, + user_id=_random_hex(10), + additional_messages=[Message.user_text_message("Hi, how are you?")], + stream=False, + ) + assert chat is not None + assert chat.id != "" + + +def test_chat_v3_stream(): + token = os.getenv("COZE_TOKEN").strip() + bot_id = os.getenv("COZE_BOT_ID_TRANSLATE").strip() + + auth = TokenAuth(token) + cli = Coze(auth=auth, base_url=COZE_CN_BASE_URL) + + chat_iter: ChatIterator = cli.chat.chat_v3( + bot_id=bot_id, + user_id=_random_hex(10), + additional_messages=[Message.user_text_message("Hi, how are you?")], + stream=True, + ) + for item in chat_iter: + assert item is not None + assert item.event != "" + if item.event == Event.conversation_message_delta: + assert item.message.content != ""