Skip to content

Commit

Permalink
docs: Update README to include usage instructions for stream and async (
Browse files Browse the repository at this point in the history
#62)

- Added instructions on how to use stream functionality
- Included guidelines for implementing async operations
  • Loading branch information
chyroc authored Oct 9, 2024
1 parent 0b16636 commit 4ec2d5f
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 6 deletions.
121 changes: 117 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ for message in message_list:
print('got message:', message.content)

# stream chat
chat_iterator = coze.chat.stream(
stream = coze.chat.stream(
bot_id='bot id',
user_id='user id',
additional_messages=[
Message.user_text_message('how are you?'),
Message.assistant_text_message('I am fine, thank you.')
],
)
for event in chat_iterator:
for event in stream:
if event.event == ChatEventType.CONVERSATION_MESSAGE_DELTA:
print('got message delta:', event.message.content)
```
Expand Down Expand Up @@ -198,8 +198,8 @@ result = coze.workflows.runs.create(


# stream workflow run
def handle_workflow_iterator(iterator: Stream[WorkflowEvent]):
for event in iterator:
def handle_workflow_iterator(stream: Stream[WorkflowEvent]):
for event in stream:
if event.event == WorkflowEventType.MESSAGE:
print('got message', event.message)
elif event.event == WorkflowEventType.ERROR:
Expand Down Expand Up @@ -315,6 +315,9 @@ jwt_oauth_app = JWTOAuthApp(
oauth_token = jwt_oauth_app.get_access_token(ttl=3600)

# And it does not support refresh. If you want to get a new token, you can call the get_access_token interface again.

# use the access token to init Coze client
coze = Coze(auth=TokenAuth(oauth_token.access_token))
```

#### PKCE OAuth App
Expand Down Expand Up @@ -368,3 +371,113 @@ coze = Coze(auth=TokenAuth(oauth_token.access_token))
# When the token expires, you can also refresh and re-obtain the token
oauth_token = device_oauth_app.refresh_access_token(oauth_token.refresh_token)
```

### Async usage

cozepy supports asynchronous calls through httpx.AsyncClient.

Just replace the `Coze` client with the `AsyncCoze` client to use all the asynchronous calls of the Coze OpenAPI.

```python
import asyncio

from cozepy import TokenAuth, Message, AsyncCoze

coze = AsyncCoze(auth=TokenAuth("your_token"))


async def main() -> None:
chat = await coze.chat.create(
bot_id='bot id',
user_id='user id',
additional_messages=[
Message.user_text_message('how are you?'),
Message.assistant_text_message('I am fine, thank you.')
],
)
print('chat', chat)


asyncio.run(main())
```

### Streaming usage

Bot chat and workflow run support running in streaming mode.

chat streaming example:

```python
from cozepy import Coze, TokenAuth, ChatEventType, Message

coze = Coze(auth=TokenAuth("your_token"))

stream = coze.chat.stream(
bot_id='bot id',
user_id='user id',
additional_messages=[
Message.user_text_message('how are you?'),
Message.assistant_text_message('I am fine, thank you.')
],
)
for event in stream:
if event.event == ChatEventType.CONVERSATION_MESSAGE_DELTA:
print('got message delta:', event.message.content)
```

workflow streaming example:

```python
from cozepy import Coze, TokenAuth, Stream, WorkflowEvent, WorkflowEventType

coze = Coze(auth=TokenAuth("your_token"))

def handle_workflow_iterator(stream: Stream[WorkflowEvent]):
for event in stream:
if event.event == WorkflowEventType.MESSAGE:
print('got message', event.message)
elif event.event == WorkflowEventType.ERROR:
print('got error', event.error)
elif event.event == WorkflowEventType.INTERRUPT:
handle_workflow_iterator(coze.workflows.runs.resume(
workflow_id='workflow id',
event_id=event.interrupt.interrupt_data.event_id,
resume_data='hey',
interrupt_type=event.interrupt.interrupt_data.type,
))


handle_workflow_iterator(coze.workflows.runs.stream(
workflow_id='workflow id',
parameters={
'input_key': 'input value',
}
))
```

Asynchronous calls also support streaming mode:

```python
import asyncio

from cozepy import TokenAuth, ChatEventType, Message, AsyncCoze

coze = AsyncCoze(auth=TokenAuth("your_token"))


async def main():
stream = await coze.chat.stream(
bot_id='bot id',
user_id='user id',
additional_messages=[
Message.user_text_message('how are you?'),
Message.assistant_text_message('I am fine, thank you.')
],
)
async for event in stream:
if event.event == ChatEventType.CONVERSATION_MESSAGE_DELTA:
print('got message delta:', event.message.content)


asyncio.run(main())
```
4 changes: 2 additions & 2 deletions cozepy/bots/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def update(
:param bot_id: The ID of the bot that the API interacts with.
:param name: The name of the bot. It should be 1 to 20 characters long.
:param description: The description of the Bot. It can be 0 to 500 characters long. The default is empty.
:param icon_file_id: The file ID for the Bot's avatar. If no file ID is specified, the Douzhu platform will
:param icon_file_id: The file ID for the Bot's avatar. If no file ID is specified, the Coze platform will
assign a default avatar for the bot. To use a custom avatar, first upload the local file through the Upload
file interface and obtain the file ID from the interface response.
:param prompt_info: The personality and reply logic of the bot.
Expand Down Expand Up @@ -300,7 +300,7 @@ async def update(
:param bot_id: The ID of the bot that the API interacts with.
:param name: The name of the bot. It should be 1 to 20 characters long.
:param description: The description of the Bot. It can be 0 to 500 characters long. The default is empty.
:param icon_file_id: The file ID for the Bot's avatar. If no file ID is specified, the Douzhu platform will
:param icon_file_id: The file ID for the Bot's avatar. If no file ID is specified, the Coze platform will
assign a default avatar for the bot. To use a custom avatar, first upload the local file through the Upload
file interface and obtain the file ID from the interface response.
:param prompt_info: The personality and reply logic of the bot.
Expand Down

0 comments on commit 4ec2d5f

Please sign in to comment.