CozeSDK_Guide
CozeSDK_Guide
Welcome to the **Coze Python API SDK**! This SDK provides a seamless way to
integrate with the Coze platform, enabling you to leverage various functionalities
such as chat, bot management, conversations, file handling, workflows, and
knowledge management within your Python applications.
## Table of Contents
- [Features](#features)
- [Requirements](#requirements)
- [Installation](#installation)
- [Getting Started](#getting-started)
- [Authentication](#authentication)
- [Personal Auth Token](#personal-auth-token)
- [JWT OAuth App](#jwt-oauth-app)
- [Initializing the Coze Client](#initializing-the-coze-client)
- [Usage](#usage)
- [Chat](#chat)
- [Bots](#bots)
- [Conversations](#conversations)
- [Files](#files)
- [Workflows](#workflows)
- [Knowledge](#knowledge)
- [OAuth Apps](#oauth-apps)
- [Web OAuth App](#web-oauth-app)
- [JWT OAuth App](#jwt-oauth-app-1)
- [PKCE OAuth App](#pkce-oauth-app)
- [Device OAuth App](#device-oauth-app)
- [Asynchronous Usage](#asynchronous-usage)
- [Streaming Usage](#streaming-usage)
- [Examples](#examples)
- [Support](#support)
- [Contributing](#contributing)
- [License](#license)
## Features
## Requirements
## Installation
```shell
pip install cozepy
```
## Getting Started
### Authentication
Before interacting with the Coze API, you need to authenticate your client. The SDK
supports multiple authentication methods:
```python
from cozepy import Coze, TokenAuth
```python
from cozepy import Coze, JWTAuth
# Replace with your actual client ID, private key, and key ID
coze = Coze(auth=JWTAuth("your_client_id", "your_private_key", "your_key_id"))
```
## Usage
### Chat
Interact with the chat functionality to create and manage chat sessions.
```python
import time
from cozepy import Coze, TokenAuth, ChatStatus, Message
time.sleep(1)
chat = coze.chat.retrieve(conversation_id=chat.conversation_id,
chat_id=chat.chat_id)
```python
from cozepy import Coze, TokenAuth, ChatEventType, Message
### Bots
```python
from cozepy import Coze, TokenAuth
# Initialize Coze client
coze = Coze(auth=TokenAuth("your_token"))
### Conversations
```python
from cozepy import Coze, TokenAuth, Message, MessageContentType
# Delete a message
coze.conversations.messages.delete(conversation_id=conversation.id,
message_id=message.id)
### Files
```python
from cozepy import Coze, TokenAuth
# Upload a file
uploaded_file = coze.files.upload(file='/path/to/your/file')
### Workflows
Execute and monitor workflows with both streaming and non-streaming options.
```python
from cozepy import Coze, TokenAuth
handle_workflow_stream(stream)
```
### Knowledge
```python
from cozepy import Coze, TokenAuth, DocumentBase, DocumentSourceInfo,
DocumentChunkStrategy, DocumentUpdateRule
Coze SDK supports various OAuth flows for authentication. Below are examples of
different OAuth App types.
```python
from cozepy import Coze, TokenAuth, WebOAuthApp
# After user authorization, capture the 'code' from the redirect URI
code = 'authorization_code_received'
```python
from cozepy import Coze, TokenAuth, JWTOAuthApp
# Note: JWT OAuth does not support token refresh. Obtain a new token when needed.
```
Implement OAuth 2.0 with Proof Key for Code Exchange (PKCE) for enhanced security.
```python
from cozepy import Coze, TokenAuth, PKCEOAuthApp
# After user authorization, capture the 'code' from the redirect URI
code = 'authorization_code_received'
Use OAuth 2.0 Device Authorization Grant for devices with limited input
capabilities.
```python
from cozepy import Coze, TokenAuth, DeviceOAuthApp
## Asynchronous Usage
```python
import asyncio
from cozepy import TokenAuth, Message, AsyncCoze
## Streaming Usage
Coze SDK supports real-time streaming for chat and workflow runs, allowing you to
handle events as they occur.
```python
from cozepy import Coze, TokenAuth, ChatEventType, Message
```python
from cozepy import Coze, TokenAuth, Stream, WorkflowEvent, WorkflowEventType
handle_workflow_stream(stream)
```
```python
import asyncio
from cozepy import TokenAuth, ChatEventType, Message, AsyncCoze
---