Skip to content

Commit

Permalink
feat: Update documentation and export knowledge base classes
Browse files Browse the repository at this point in the history
  • Loading branch information
chyroc committed Sep 28, 2024
1 parent 268c5b3 commit db82a1f
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 6 deletions.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,53 @@ handle_workflow_iterator(coze.workflows.runs.stream(
'input_key': 'input value',
}
))
```
```

### Knowledge

```python
from cozepy import Coze, TokenAuth, DocumentBase, DocumentSourceInfo, DocumentChunkStrategy, DocumentUpdateRule

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

# create knowledge documents by local_file
documents = coze.knowledge.documents.create(
dataset_id='dataset id',
document_bases=[
DocumentBase(
name='document name',
source_info=DocumentSourceInfo.from_local_file('local file content')
)
],
chunk_strategy=DocumentChunkStrategy.auto()
)

# create knowledge documents by web_page
documents = coze.knowledge.documents.create(
dataset_id='dataset id',
document_bases=[
DocumentBase(
name='document name',
source_info=DocumentSourceInfo.from_web_page('https://example.com')
)
],
chunk_strategy=DocumentChunkStrategy.auto()
)

# update knowledge document
coze.knowledge.documents.update(
document_id='document id',
document_name='name',
update_rule=DocumentUpdateRule.no_auto_update()
)

# delete knowledge document
coze.knowledge.documents.delete(document_ids=['document id'])

# list knowledge documents
paged_documents = coze.knowledge.documents.list(
dataset_id='dataset id',
page_num=1,
page_size=10
)
```
21 changes: 21 additions & 0 deletions cozepy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@
from .coze import Coze
from .exception import CozeAPIError, CozeError, CozeEventError
from .files import File
from .knowledge.documents import (
Document,
DocumentBase,
DocumentChunkStrategy,
DocumentFormatType,
DocumentSourceInfo,
DocumentSourceType,
DocumentStatus,
DocumentUpdateRule,
DocumentUpdateType,
)
from .model import (
LastIDPaged,
NumberPaged,
Expand Down Expand Up @@ -83,6 +94,16 @@
"Conversation",
# files
"File",
# knowledge.documents
"DocumentChunkStrategy",
"DocumentFormatType",
"DocumentSourceType",
"DocumentStatus",
"DocumentUpdateType",
"Document",
"DocumentSourceInfo",
"DocumentUpdateRule",
"DocumentBase",
# workflows.runs
"WorkflowRunResult",
"WorkflowEventType",
Expand Down
29 changes: 26 additions & 3 deletions cozepy/knowledge/documents/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from enum import IntEnum
from typing import List

from cozepy import NumberPaged
from cozepy.auth import Auth
from cozepy.model import CozeModel
from cozepy.model import CozeModel, NumberPaged
from cozepy.request import Requester
from cozepy.util import base64_encode_string

Expand Down Expand Up @@ -49,6 +48,22 @@ class DocumentChunkStrategy(CozeModel):
# 在 chunk_type=1 时必选。
separator: str = None

@staticmethod
def auto() -> "DocumentChunkStrategy":
return DocumentChunkStrategy(chunk_type=0)

Check warning on line 53 in cozepy/knowledge/documents/__init__.py

View check run for this annotation

Codecov / codecov/patch

cozepy/knowledge/documents/__init__.py#L53

Added line #L53 was not covered by tests

@staticmethod
def custom(
max_tokens: int, separator: str, remove_extra_spaces: bool = False, remove_urls_emails: bool = False
) -> "DocumentChunkStrategy":
return DocumentChunkStrategy(

Check warning on line 59 in cozepy/knowledge/documents/__init__.py

View check run for this annotation

Codecov / codecov/patch

cozepy/knowledge/documents/__init__.py#L59

Added line #L59 was not covered by tests
chunk_type=1,
max_tokens=max_tokens,
remove_extra_spaces=remove_extra_spaces,
remove_urls_emails=remove_urls_emails,
separator=separator,
)


class DocumentFormatType(IntEnum):
# Document type, such as txt, pdf, online web pages, etc.
Expand Down Expand Up @@ -204,7 +219,7 @@ def from_local_file(content: str, file_type: str = "txt") -> "DocumentSourceInfo
return DocumentSourceInfo(file_base64=base64_encode_string(content), file_type=file_type)

@staticmethod
def from_web(url: str) -> "DocumentSourceInfo":
def from_web_page(url: str) -> "DocumentSourceInfo":
return DocumentSourceInfo(web_url=url, document_source=1)


Expand All @@ -217,6 +232,14 @@ class DocumentUpdateRule(CozeModel):
# 在线网页自动更新的频率。单位为小时,最小值为 24。
update_interval: int

@staticmethod
def no_auto_update() -> "DocumentUpdateRule":
return DocumentUpdateRule(update_type=DocumentUpdateType.NO_AUTO_UPDATE, update_interval=24)

Check warning on line 237 in cozepy/knowledge/documents/__init__.py

View check run for this annotation

Codecov / codecov/patch

cozepy/knowledge/documents/__init__.py#L237

Added line #L237 was not covered by tests

@staticmethod
def auto_update(interval: int) -> "DocumentUpdateRule":
return DocumentUpdateRule(update_type=DocumentUpdateType.AUTO_UPDATE, update_interval=interval)

Check warning on line 241 in cozepy/knowledge/documents/__init__.py

View check run for this annotation

Codecov / codecov/patch

cozepy/knowledge/documents/__init__.py#L241

Added line #L241 was not covered by tests


class DocumentBase(CozeModel):
# 文件名称。
Expand Down
3 changes: 1 addition & 2 deletions cozepy/workspaces/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from enum import Enum
from typing import List

from cozepy import NumberPaged
from cozepy.auth import Auth
from cozepy.model import CozeModel
from cozepy.model import CozeModel, NumberPaged
from cozepy.request import Requester


Expand Down

0 comments on commit db82a1f

Please sign in to comment.