vikingdb-python-sdk 0.1.8__tar.gz → 0.1.10__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {vikingdb_python_sdk-0.1.8/vikingdb_python_sdk.egg-info → vikingdb_python_sdk-0.1.10}/PKG-INFO +8 -7
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/README.md +4 -3
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/pyproject.toml +2 -1
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/__init__.py +4 -0
- vikingdb_python_sdk-0.1.10/vikingdb/knowledge/__init__.py +126 -0
- vikingdb_python_sdk-0.1.10/vikingdb/knowledge/client.py +174 -0
- vikingdb_python_sdk-0.1.10/vikingdb/knowledge/collection.py +263 -0
- vikingdb_python_sdk-0.1.10/vikingdb/knowledge/exceptions.py +212 -0
- vikingdb_python_sdk-0.1.10/vikingdb/knowledge/models/__init__.py +0 -0
- vikingdb_python_sdk-0.1.10/vikingdb/knowledge/models/base.py +53 -0
- vikingdb_python_sdk-0.1.10/vikingdb/knowledge/models/chat.py +49 -0
- vikingdb_python_sdk-0.1.10/vikingdb/knowledge/models/doc.py +131 -0
- vikingdb_python_sdk-0.1.10/vikingdb/knowledge/models/point.py +128 -0
- vikingdb_python_sdk-0.1.10/vikingdb/knowledge/models/rerank.py +31 -0
- vikingdb_python_sdk-0.1.10/vikingdb/knowledge/models/search.py +58 -0
- vikingdb_python_sdk-0.1.10/vikingdb/knowledge/models/service_chat.py +78 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/vector/__init__.py +4 -1
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/vector/base.py +2 -2
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/vector/client.py +57 -2
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/vector/collection.py +2 -2
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/vector/index.py +2 -2
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/vector/models/__init__.py +10 -0
- vikingdb_python_sdk-0.1.10/vikingdb/vector/models/rerank.py +35 -0
- vikingdb_python_sdk-0.1.10/vikingdb/vector/rerank.py +33 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/version.py +1 -1
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10/vikingdb_python_sdk.egg-info}/PKG-INFO +8 -7
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb_python_sdk.egg-info/SOURCES.txt +14 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb_python_sdk.egg-info/requires.txt +1 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/LICENSE.txt +0 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/setup.cfg +0 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/_client.py +0 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/auth.py +0 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/exceptions.py +0 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/memory/__init__.py +0 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/memory/client.py +0 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/memory/collection.py +0 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/memory/exceptions.py +0 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/memory/types.py +0 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/request_options.py +0 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/vector/embedding.py +0 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/vector/exceptions.py +0 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/vector/models/base.py +0 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/vector/models/collection.py +0 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/vector/models/embedding.py +0 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb/vector/models/index.py +0 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb_python_sdk.egg-info/dependency_links.txt +0 -0
- {vikingdb_python_sdk-0.1.8 → vikingdb_python_sdk-0.1.10}/vikingdb_python_sdk.egg-info/top_level.txt +0 -0
{vikingdb_python_sdk-0.1.8/vikingdb_python_sdk.egg-info → vikingdb_python_sdk-0.1.10}/PKG-INFO
RENAMED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
2
|
Name: vikingdb-python-sdk
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.10
|
|
4
4
|
Summary: vikingdb Python SDK
|
|
5
|
-
License
|
|
5
|
+
License: Apache-2.0
|
|
6
6
|
Project-URL: Documentation, https://github.com/volcengine/volc-vikingdb-python-sdk/README.md
|
|
7
7
|
Project-URL: Source, https://github.com/volcengine/volc-vikingdb-python-sdk
|
|
8
8
|
Keywords: vikingdb,vector,bytedance,volcengine,sdk
|
|
@@ -20,10 +20,10 @@ License-File: LICENSE.txt
|
|
|
20
20
|
Requires-Dist: httpx>=0.24.1
|
|
21
21
|
Requires-Dist: requests>=2.28.1
|
|
22
22
|
Requires-Dist: pydantic>=2.5.3
|
|
23
|
+
Requires-Dist: typing_extensions>=4.9.0
|
|
23
24
|
Requires-Dist: urllib3>=1.21.1
|
|
24
25
|
Requires-Dist: volcengine>=1.0.0
|
|
25
26
|
Requires-Dist: aiohttp>=3.10.0
|
|
26
|
-
Dynamic: license-file
|
|
27
27
|
|
|
28
28
|
## VikingDB Python SDK (v2)
|
|
29
29
|
|
|
@@ -53,10 +53,10 @@ uv add vikingdb-python-sdk
|
|
|
53
53
|
```python
|
|
54
54
|
import os
|
|
55
55
|
from vikingdb import IAM
|
|
56
|
-
from vikingdb.vector import SearchByRandomRequest,
|
|
56
|
+
from vikingdb.vector import SearchByRandomRequest, VikingDB
|
|
57
57
|
|
|
58
58
|
auth = IAM(ak=os.environ["VIKINGDB_AK"], sk=os.environ["VIKINGDB_SK"])
|
|
59
|
-
client =
|
|
59
|
+
client = VikingDB(
|
|
60
60
|
host=os.environ["VIKINGDB_HOST"],
|
|
61
61
|
region=os.environ["VIKINGDB_REGION"],
|
|
62
62
|
auth=auth,
|
|
@@ -181,7 +181,7 @@ python examples/memory/03_search_memory.py
|
|
|
181
181
|
### Architecture Overview
|
|
182
182
|
|
|
183
183
|
- `vikingdb._client`, `vikingdb.auth`, `vikingdb.request_options`, and `vikingdb.vector.exceptions` form the shared runtime used by all present and future SDK domains (vector, memory, knowledge).
|
|
184
|
-
- Domain-specific features live under dedicated namespaces such as `vikingdb.vector` and `vikingdb.memory`, where the high-level clients (`
|
|
184
|
+
- Domain-specific features live under dedicated namespaces such as `vikingdb.vector` and `vikingdb.memory`, where the high-level clients (`VikingDB`, `VikingMem`) compose the shared auth stack atop the shared client.
|
|
185
185
|
- Vector request/response models now surface directly from `vikingdb.vector` (backed internally by `vikingdb/vector/models`).
|
|
186
186
|
- Memory APIs return plain dictionaries without object encapsulation, providing a lightweight interface for conversational memory management (session, profile, event operations).
|
|
187
187
|
- Imports from the root package now focus on cross-cutting utilities (auth, config, request options), while application code should pull domain-specific functionality from `vikingdb.vector` or `vikingdb.memory` explicitly.
|
|
@@ -199,6 +199,7 @@ vikingdb/
|
|
|
199
199
|
│ ├── base.py # Shared helpers for vector clients
|
|
200
200
|
│ ├── collection.py # Collection operations
|
|
201
201
|
│ ├── embedding.py # Embedding operations
|
|
202
|
+
│ ├── rerank.py # rerank operations
|
|
202
203
|
│ ├── index.py # Index/search operations
|
|
203
204
|
│ ├── client.py # Vector service wrapper and high-level client
|
|
204
205
|
│ ├── exceptions.py # Vector-specific exceptions
|
|
@@ -26,10 +26,10 @@ uv add vikingdb-python-sdk
|
|
|
26
26
|
```python
|
|
27
27
|
import os
|
|
28
28
|
from vikingdb import IAM
|
|
29
|
-
from vikingdb.vector import SearchByRandomRequest,
|
|
29
|
+
from vikingdb.vector import SearchByRandomRequest, VikingDB
|
|
30
30
|
|
|
31
31
|
auth = IAM(ak=os.environ["VIKINGDB_AK"], sk=os.environ["VIKINGDB_SK"])
|
|
32
|
-
client =
|
|
32
|
+
client = VikingDB(
|
|
33
33
|
host=os.environ["VIKINGDB_HOST"],
|
|
34
34
|
region=os.environ["VIKINGDB_REGION"],
|
|
35
35
|
auth=auth,
|
|
@@ -154,7 +154,7 @@ python examples/memory/03_search_memory.py
|
|
|
154
154
|
### Architecture Overview
|
|
155
155
|
|
|
156
156
|
- `vikingdb._client`, `vikingdb.auth`, `vikingdb.request_options`, and `vikingdb.vector.exceptions` form the shared runtime used by all present and future SDK domains (vector, memory, knowledge).
|
|
157
|
-
- Domain-specific features live under dedicated namespaces such as `vikingdb.vector` and `vikingdb.memory`, where the high-level clients (`
|
|
157
|
+
- Domain-specific features live under dedicated namespaces such as `vikingdb.vector` and `vikingdb.memory`, where the high-level clients (`VikingDB`, `VikingMem`) compose the shared auth stack atop the shared client.
|
|
158
158
|
- Vector request/response models now surface directly from `vikingdb.vector` (backed internally by `vikingdb/vector/models`).
|
|
159
159
|
- Memory APIs return plain dictionaries without object encapsulation, providing a lightweight interface for conversational memory management (session, profile, event operations).
|
|
160
160
|
- Imports from the root package now focus on cross-cutting utilities (auth, config, request options), while application code should pull domain-specific functionality from `vikingdb.vector` or `vikingdb.memory` explicitly.
|
|
@@ -172,6 +172,7 @@ vikingdb/
|
|
|
172
172
|
│ ├── base.py # Shared helpers for vector clients
|
|
173
173
|
│ ├── collection.py # Collection operations
|
|
174
174
|
│ ├── embedding.py # Embedding operations
|
|
175
|
+
│ ├── rerank.py # rerank operations
|
|
175
176
|
│ ├── index.py # Index/search operations
|
|
176
177
|
│ ├── client.py # Vector service wrapper and high-level client
|
|
177
178
|
│ ├── exceptions.py # Vector-specific exceptions
|
|
@@ -8,7 +8,7 @@ dynamic = ["version"]
|
|
|
8
8
|
description = "vikingdb Python SDK"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.8"
|
|
11
|
-
license = "Apache-2.0"
|
|
11
|
+
license = {text = "Apache-2.0"}
|
|
12
12
|
keywords = ["vikingdb", "vector", "bytedance", "volcengine", "sdk"]
|
|
13
13
|
classifiers = [
|
|
14
14
|
"Development Status :: 4 - Beta",
|
|
@@ -24,6 +24,7 @@ dependencies = [
|
|
|
24
24
|
"httpx>=0.24.1",
|
|
25
25
|
"requests>=2.28.1",
|
|
26
26
|
"pydantic>=2.5.3",
|
|
27
|
+
"typing_extensions>=4.9.0",
|
|
27
28
|
"urllib3>=1.21.1",
|
|
28
29
|
"volcengine>=1.0.0",
|
|
29
30
|
"aiohttp>=3.10.0",
|
|
@@ -11,6 +11,8 @@ from .vector import (
|
|
|
11
11
|
CollectionClient,
|
|
12
12
|
EmbeddingClient,
|
|
13
13
|
IndexClient,
|
|
14
|
+
RerankClient,
|
|
15
|
+
VikingDB,
|
|
14
16
|
VikingVector,
|
|
15
17
|
)
|
|
16
18
|
from .memory import (
|
|
@@ -23,8 +25,10 @@ __all__ = [
|
|
|
23
25
|
"APIKey",
|
|
24
26
|
"CollectionClient",
|
|
25
27
|
"EmbeddingClient",
|
|
28
|
+
"RerankClient",
|
|
26
29
|
"IndexClient",
|
|
27
30
|
"RequestOptions",
|
|
31
|
+
"VikingDB",
|
|
28
32
|
"VikingVector",
|
|
29
33
|
"vector",
|
|
30
34
|
"memory",
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# coding:utf-8
|
|
2
|
+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd.
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
from .client import VikingKnowledge
|
|
6
|
+
from .collection import KnowledgeCollection
|
|
7
|
+
from .exceptions import (
|
|
8
|
+
VikingKnowledgeException,
|
|
9
|
+
UnauthorizedException,
|
|
10
|
+
NoPermissionException,
|
|
11
|
+
InvalidRequestException,
|
|
12
|
+
CollectionExistException,
|
|
13
|
+
CollectionNotExistException,
|
|
14
|
+
OperationNotAllowedException,
|
|
15
|
+
IndexExistException,
|
|
16
|
+
IndexNotExistException,
|
|
17
|
+
QueryOpFailedException,
|
|
18
|
+
DataNotFoundException,
|
|
19
|
+
DelOpFailedException,
|
|
20
|
+
UpsertOpFailedException,
|
|
21
|
+
TokenMismatchException,
|
|
22
|
+
InvalidQueryVecException,
|
|
23
|
+
InvalidPrimaryKeyException,
|
|
24
|
+
InvalidPartitionException,
|
|
25
|
+
InvalidScalarCondException,
|
|
26
|
+
InvalidProxyServiceException,
|
|
27
|
+
IndexRecallException,
|
|
28
|
+
IndexFetchDataException,
|
|
29
|
+
IndexNotReadyException,
|
|
30
|
+
APINotImplementedException,
|
|
31
|
+
CalcEmbeddingFailedException,
|
|
32
|
+
ListEmbeddingModelsException,
|
|
33
|
+
InternalServerException,
|
|
34
|
+
QuotaLimiterException,
|
|
35
|
+
RerankException,
|
|
36
|
+
DocNotExistException,
|
|
37
|
+
DocIsFullException,
|
|
38
|
+
PointNotExistException,
|
|
39
|
+
)
|
|
40
|
+
from .models.base import CollectionMeta
|
|
41
|
+
from .models.doc import DocInfo, DocInfo, ListDocsResponse, AddDocRequest, AddDocV2Request, ListDocsRequest, DedupOptions, MetaItem, AddDocResponse, AddDocResponseData
|
|
42
|
+
from .models.point import (
|
|
43
|
+
PointInfo,
|
|
44
|
+
ListPointsResponse,
|
|
45
|
+
AddPointRequest,
|
|
46
|
+
UpdatePointRequest,
|
|
47
|
+
ListPointsRequest,
|
|
48
|
+
DeletePointRequest,
|
|
49
|
+
)
|
|
50
|
+
from .models.search import (
|
|
51
|
+
SearchCollectionRequest,
|
|
52
|
+
SearchResponse,
|
|
53
|
+
SearchKnowledgeResponse,
|
|
54
|
+
SearchKnowledgeRequest,
|
|
55
|
+
)
|
|
56
|
+
from .models.rerank import RerankDataItem, RerankResponse
|
|
57
|
+
from .models.service_chat import ServiceChatRequest, ServiceChatResponse
|
|
58
|
+
from .models.chat import (
|
|
59
|
+
ChatMessage,
|
|
60
|
+
ChatCompletionRequest,
|
|
61
|
+
ChatCompletionResponse,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
__all__ = [
|
|
65
|
+
"VikingKnowledge",
|
|
66
|
+
"KnowledgeCollection",
|
|
67
|
+
"VikingKnowledgeException",
|
|
68
|
+
"UnauthorizedException",
|
|
69
|
+
"NoPermissionException",
|
|
70
|
+
"InvalidRequestException",
|
|
71
|
+
"CollectionExistException",
|
|
72
|
+
"CollectionNotExistException",
|
|
73
|
+
"OperationNotAllowedException",
|
|
74
|
+
"IndexExistException",
|
|
75
|
+
"IndexNotExistException",
|
|
76
|
+
"QueryOpFailedException",
|
|
77
|
+
"DataNotFoundException",
|
|
78
|
+
"DelOpFailedException",
|
|
79
|
+
"UpsertOpFailedException",
|
|
80
|
+
"TokenMismatchException",
|
|
81
|
+
"InvalidQueryVecException",
|
|
82
|
+
"InvalidPrimaryKeyException",
|
|
83
|
+
"InvalidPartitionException",
|
|
84
|
+
"InvalidScalarCondException",
|
|
85
|
+
"InvalidProxyServiceException",
|
|
86
|
+
"IndexRecallException",
|
|
87
|
+
"IndexFetchDataException",
|
|
88
|
+
"IndexNotReadyException",
|
|
89
|
+
"APINotImplementedException",
|
|
90
|
+
"CalcEmbeddingFailedException",
|
|
91
|
+
"ListEmbeddingModelsException",
|
|
92
|
+
"InternalServerException",
|
|
93
|
+
"QuotaLimiterException",
|
|
94
|
+
"RerankException",
|
|
95
|
+
"DocNotExistException",
|
|
96
|
+
"DocIsFullException",
|
|
97
|
+
"PointNotExistException",
|
|
98
|
+
"CollectionMeta",
|
|
99
|
+
"DocInfo",
|
|
100
|
+
"DocInfo",
|
|
101
|
+
"ListDocsResponse",
|
|
102
|
+
"AddDocRequest",
|
|
103
|
+
"AddDocV2Request",
|
|
104
|
+
"AddDocResponse",
|
|
105
|
+
"AddDocResponseData",
|
|
106
|
+
"ListDocsRequest",
|
|
107
|
+
"PointInfo",
|
|
108
|
+
"DedupOptions",
|
|
109
|
+
"MetaItem",
|
|
110
|
+
"ListPointsResponse",
|
|
111
|
+
"AddPointRequest",
|
|
112
|
+
"UpdatePointRequest",
|
|
113
|
+
"ListPointsRequest",
|
|
114
|
+
"DeletePointRequest",
|
|
115
|
+
"SearchCollectionRequest",
|
|
116
|
+
"SearchResponse",
|
|
117
|
+
"SearchKnowledgeResponse",
|
|
118
|
+
"SearchKnowledgeRequest",
|
|
119
|
+
"RerankDataItem",
|
|
120
|
+
"RerankResponse",
|
|
121
|
+
"ServiceChatRequest",
|
|
122
|
+
"ServiceChatResponse",
|
|
123
|
+
"ChatMessage",
|
|
124
|
+
"ChatCompletionRequest",
|
|
125
|
+
"ChatCompletionResponse",
|
|
126
|
+
]
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# coding:utf-8
|
|
2
|
+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd.
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import json
|
|
8
|
+
from typing import Mapping, Optional, Union, Sequence, cast
|
|
9
|
+
|
|
10
|
+
from volcengine.ApiInfo import ApiInfo
|
|
11
|
+
|
|
12
|
+
from .. import APIKey
|
|
13
|
+
from .._client import Client, _REQUEST_ID_HEADER
|
|
14
|
+
from ..auth import Auth
|
|
15
|
+
from ..exceptions import VikingException, promote_exception
|
|
16
|
+
from .exceptions import EXCEPTION_MAP, VikingKnowledgeException
|
|
17
|
+
from ..version import __version__
|
|
18
|
+
from .models.base import CollectionMeta, Model
|
|
19
|
+
from .models.rerank import RerankDataItem, RerankResponse
|
|
20
|
+
from .models.chat import ChatCompletionRequest, ChatCompletionResponse
|
|
21
|
+
from .models.service_chat import ServiceChatRequest, ServiceChatResponse
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
_DEFAULT_USER_AGENT = f"vikingdb-python-sdk/{__version__}"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _get_common_viking_request_header():
|
|
28
|
+
header = {
|
|
29
|
+
"Accept": "application/json",
|
|
30
|
+
"Content-Type": "application/json",
|
|
31
|
+
"User-Agent": _DEFAULT_USER_AGENT,
|
|
32
|
+
}
|
|
33
|
+
return header
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class VikingKnowledge(Client):
|
|
37
|
+
def __init__(
|
|
38
|
+
self,
|
|
39
|
+
*,
|
|
40
|
+
host: str = "api-knowledgebase.mlp.cn-beijing.volces.com",
|
|
41
|
+
region: str = "cn-beijing",
|
|
42
|
+
auth: Auth,
|
|
43
|
+
sts_token: str = "",
|
|
44
|
+
scheme: str = "http",
|
|
45
|
+
timeout: int = 30,
|
|
46
|
+
):
|
|
47
|
+
super().__init__(
|
|
48
|
+
host=host,
|
|
49
|
+
region=region,
|
|
50
|
+
service="air",
|
|
51
|
+
auth=auth,
|
|
52
|
+
sts_token=sts_token,
|
|
53
|
+
scheme=scheme,
|
|
54
|
+
timeout=timeout,
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
def _build_api_info(self):
|
|
58
|
+
header = _get_common_viking_request_header()
|
|
59
|
+
return {
|
|
60
|
+
"SearchCollection": ApiInfo("POST", "/api/knowledge/collection/search", {}, {}, header),
|
|
61
|
+
"SearchKnowledge": ApiInfo("POST", "/api/knowledge/collection/search_knowledge", {}, {}, header),
|
|
62
|
+
"ServiceChat": ApiInfo("POST", "/api/knowledge/service/chat", {}, {}, header),
|
|
63
|
+
# Doc
|
|
64
|
+
"AddDoc": ApiInfo("POST", "/api/knowledge/doc/add", {}, {}, header),
|
|
65
|
+
"AddDocV2": ApiInfo("POST", "/api/knowledge/doc/v2/add", {}, {}, header),
|
|
66
|
+
"DeleteDoc": ApiInfo("POST", "/api/knowledge/doc/delete", {}, {}, header),
|
|
67
|
+
"GetDocInfo": ApiInfo("POST", "/api/knowledge/doc/info", {}, {}, header),
|
|
68
|
+
"ListDocs": ApiInfo("POST", "/api/knowledge/doc/list", {}, {}, header),
|
|
69
|
+
"UpdateDocMeta": ApiInfo("POST", "/api/knowledge/doc/update_meta", {}, {}, header),
|
|
70
|
+
"UpdateDoc": ApiInfo("POST", "/api/knowledge/doc/update", {}, {}, header),
|
|
71
|
+
# Point
|
|
72
|
+
"GetPointInfo": ApiInfo("POST", "/api/knowledge/point/info", {}, {}, header),
|
|
73
|
+
"ListPoints": ApiInfo("POST", "/api/knowledge/point/list", {}, {}, header),
|
|
74
|
+
"AddPoint": ApiInfo("POST", "/api/knowledge/point/add", {}, {}, header),
|
|
75
|
+
"UpdatePoint": ApiInfo("POST", "/api/knowledge/point/update", {}, {}, header),
|
|
76
|
+
"DeletePoint": ApiInfo("POST", "/api/knowledge/point/delete", {}, {}, header),
|
|
77
|
+
# Service
|
|
78
|
+
"Ping": ApiInfo("GET", "/ping", {}, {}, header),
|
|
79
|
+
"Rerank": ApiInfo("POST", "/api/knowledge/service/rerank", {}, {}, header),
|
|
80
|
+
# Chat
|
|
81
|
+
"ChatCompletion": ApiInfo("POST", "/api/knowledge/chat/completions", {}, {}, header),
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
def json_exception(self, api, params, body, headers=None, timeout=None):
|
|
85
|
+
try:
|
|
86
|
+
res = self._json(api, params, body, headers=headers, timeout=timeout)
|
|
87
|
+
except VikingException as exc:
|
|
88
|
+
raise promote_exception(exc, exception_map=EXCEPTION_MAP, default_cls=VikingKnowledgeException) from None
|
|
89
|
+
if res is None:
|
|
90
|
+
raise VikingKnowledgeException(1000028, "missed", "empty response due to unknown error", status_code=None)
|
|
91
|
+
return res
|
|
92
|
+
|
|
93
|
+
async def async_json_exception(self, api, params, body, headers=None, timeout=None):
|
|
94
|
+
try:
|
|
95
|
+
res = await self.async_json(api, params, body, headers=headers, timeout=timeout)
|
|
96
|
+
except VikingException as exc:
|
|
97
|
+
raise promote_exception(exc, exception_map=EXCEPTION_MAP, default_cls=VikingKnowledgeException) from None
|
|
98
|
+
if res is None:
|
|
99
|
+
raise VikingKnowledgeException(1000028, "missed", "empty response due to unknown error", status_code=None)
|
|
100
|
+
return res
|
|
101
|
+
|
|
102
|
+
def collection(
|
|
103
|
+
self,
|
|
104
|
+
*,
|
|
105
|
+
resource_id: Optional[str] = None,
|
|
106
|
+
collection_name: Optional[str] = None,
|
|
107
|
+
project_name: Optional[str] = None,
|
|
108
|
+
):
|
|
109
|
+
from .collection import KnowledgeCollection
|
|
110
|
+
|
|
111
|
+
meta = CollectionMeta(
|
|
112
|
+
resource_id=resource_id,
|
|
113
|
+
collection_name=collection_name,
|
|
114
|
+
project_name=project_name,
|
|
115
|
+
)
|
|
116
|
+
return KnowledgeCollection(self, meta)
|
|
117
|
+
|
|
118
|
+
def rerank(
|
|
119
|
+
self,
|
|
120
|
+
datas: Sequence[Union[RerankDataItem,Mapping[str, object]]],
|
|
121
|
+
*,
|
|
122
|
+
rerank_model: str = "Doubao-pro-4k-rerank",
|
|
123
|
+
rerank_instruction: Optional[str] = None,
|
|
124
|
+
endpoint_id: Optional[str] = None,
|
|
125
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
126
|
+
timeout: Optional[int] = None,
|
|
127
|
+
) -> RerankResponse:
|
|
128
|
+
items = [
|
|
129
|
+
(d.model_dump(by_alias=True, exclude_none=True) if isinstance(d, RerankDataItem) else dict(d))
|
|
130
|
+
for d in datas
|
|
131
|
+
]
|
|
132
|
+
if len(items) > 50:
|
|
133
|
+
raise VikingKnowledgeException(1000028, "missed", "datas list too large", status_code=None)
|
|
134
|
+
payload: dict = {"datas": items, "rerank_model": rerank_model}
|
|
135
|
+
if rerank_instruction is not None:
|
|
136
|
+
payload["rerank_instruction"] = rerank_instruction
|
|
137
|
+
if endpoint_id is not None:
|
|
138
|
+
payload["endpoint_id"] = endpoint_id
|
|
139
|
+
res = self.json_exception("Rerank", {}, json.dumps(payload), headers=headers, timeout=timeout)
|
|
140
|
+
response = RerankResponse.parse_with(res)
|
|
141
|
+
return response
|
|
142
|
+
|
|
143
|
+
def chat_completion(
|
|
144
|
+
self,
|
|
145
|
+
request: Union[ChatCompletionRequest, Mapping[str, object]],
|
|
146
|
+
*,
|
|
147
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
148
|
+
timeout: Optional[int] = None,
|
|
149
|
+
) -> ChatCompletionResponse:
|
|
150
|
+
payload = (
|
|
151
|
+
request.model_dump(by_alias=True, exclude_none=True) # type: ignore[attr-defined]
|
|
152
|
+
if isinstance(request, ChatCompletionRequest)
|
|
153
|
+
else dict(request)
|
|
154
|
+
)
|
|
155
|
+
res = self.json_exception("ChatCompletion", {}, json.dumps(payload), headers=headers, timeout=timeout)
|
|
156
|
+
response = ChatCompletionResponse.parse_with(res)
|
|
157
|
+
return response
|
|
158
|
+
|
|
159
|
+
def service_chat(
|
|
160
|
+
self,
|
|
161
|
+
request: Union[ServiceChatRequest, Mapping[str, object]],
|
|
162
|
+
*,
|
|
163
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
164
|
+
timeout: Optional[int] = None,
|
|
165
|
+
) -> ServiceChatResponse:
|
|
166
|
+
payload = (
|
|
167
|
+
request.model_dump(by_alias=True, exclude_none=True) # type: ignore[attr-defined]
|
|
168
|
+
if isinstance(request, ServiceChatRequest)
|
|
169
|
+
else dict(request)
|
|
170
|
+
)
|
|
171
|
+
req_headers = dict(headers or {})
|
|
172
|
+
res = self.json_exception("ServiceChat", {}, json.dumps(payload), headers=req_headers, timeout=timeout)
|
|
173
|
+
response = ServiceChatResponse.parse_with(res)
|
|
174
|
+
return response
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
# coding:utf-8
|
|
2
|
+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd.
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import json
|
|
8
|
+
from typing import Mapping, Optional, Union, List
|
|
9
|
+
import warnings
|
|
10
|
+
|
|
11
|
+
from .models.base import CollectionMeta, Model
|
|
12
|
+
from .models.doc import DocInfo, ListDocsResponse, AddDocRequest, ListDocsRequest, MetaItem, AddDocV2Request, AddDocResponse
|
|
13
|
+
from .models.point import (
|
|
14
|
+
ListPointsResponse,
|
|
15
|
+
PointInfo,
|
|
16
|
+
AddPointRequest,
|
|
17
|
+
UpdatePointRequest,
|
|
18
|
+
ListPointsRequest,
|
|
19
|
+
DeletePointRequest,
|
|
20
|
+
PointAddResponse
|
|
21
|
+
)
|
|
22
|
+
from .models.search import (
|
|
23
|
+
SearchCollectionRequest,
|
|
24
|
+
SearchKnowledgeResponse,
|
|
25
|
+
SearchResponse,
|
|
26
|
+
SearchKnowledgeRequest,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class KnowledgeCollection:
|
|
31
|
+
def __init__(self, client, meta: CollectionMeta):
|
|
32
|
+
self.client = client
|
|
33
|
+
self._meta = meta
|
|
34
|
+
self._meta_payload = meta.model_dump(by_alias=True, exclude_none=True)
|
|
35
|
+
|
|
36
|
+
def add_doc(
|
|
37
|
+
self,
|
|
38
|
+
request: Union[AddDocRequest, Mapping[str, object]],
|
|
39
|
+
*,
|
|
40
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
41
|
+
timeout: Optional[int] = None,
|
|
42
|
+
):
|
|
43
|
+
warnings.warn("add_doc 已废弃,请使用 add_doc_v2", DeprecationWarning, stacklevel=2)
|
|
44
|
+
req_payload = (
|
|
45
|
+
request.model_dump(by_alias=True, exclude_none=True) # type: ignore[attr-defined]
|
|
46
|
+
if isinstance(request, Model)
|
|
47
|
+
else dict(request)
|
|
48
|
+
)
|
|
49
|
+
payload = {**self._meta_payload, **req_payload}
|
|
50
|
+
res = self.client.json_exception("AddDoc", {}, json.dumps(payload), headers=headers, timeout=timeout)
|
|
51
|
+
response = AddDocResponse.parse_with(res)
|
|
52
|
+
return response
|
|
53
|
+
|
|
54
|
+
def add_doc_v2(
|
|
55
|
+
self,
|
|
56
|
+
request: Union[AddDocV2Request, Mapping[str, object]],
|
|
57
|
+
*,
|
|
58
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
59
|
+
timeout: Optional[int] = None,
|
|
60
|
+
) -> AddDocResponse:
|
|
61
|
+
req_payload = (
|
|
62
|
+
request.model_dump(by_alias=True, exclude_none=True) # type: ignore[attr-defined]
|
|
63
|
+
if isinstance(request, Model)
|
|
64
|
+
else dict(request)
|
|
65
|
+
)
|
|
66
|
+
payload = {**self._meta_payload, **req_payload}
|
|
67
|
+
res = self.client.json_exception("AddDocV2", {}, json.dumps(payload), headers=headers, timeout=timeout)
|
|
68
|
+
response = AddDocResponse.parse_with(res)
|
|
69
|
+
return response
|
|
70
|
+
|
|
71
|
+
def delete_doc(
|
|
72
|
+
self,
|
|
73
|
+
doc_id: str,
|
|
74
|
+
*,
|
|
75
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
76
|
+
timeout: Optional[int] = None,
|
|
77
|
+
):
|
|
78
|
+
payload = {**self._meta_payload, "doc_id": doc_id}
|
|
79
|
+
res = self.client.json_exception("DeleteDoc", {}, json.dumps(payload), headers=headers, timeout=timeout)
|
|
80
|
+
return res
|
|
81
|
+
|
|
82
|
+
def get_doc(
|
|
83
|
+
self,
|
|
84
|
+
doc_id: str,
|
|
85
|
+
*,
|
|
86
|
+
return_token_usage: bool = False,
|
|
87
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
88
|
+
timeout: Optional[int] = None,
|
|
89
|
+
) -> DocInfo:
|
|
90
|
+
payload = {**self._meta_payload, "doc_id": doc_id}
|
|
91
|
+
if return_token_usage:
|
|
92
|
+
payload["return_token_usage"] = True
|
|
93
|
+
res = self.client.json_exception("GetDocInfo", {}, json.dumps(payload), headers=headers, timeout=timeout)
|
|
94
|
+
data_obj = res["data"] if isinstance(res, dict) and "data" in res else {}
|
|
95
|
+
if not isinstance(data_obj, dict):
|
|
96
|
+
data_obj = {}
|
|
97
|
+
doc = DocInfo.model_validate(
|
|
98
|
+
{**data_obj, "project": self._meta.project_name or "default", "resource_id": self._meta.resource_id}
|
|
99
|
+
)
|
|
100
|
+
return doc
|
|
101
|
+
|
|
102
|
+
def list_docs(
|
|
103
|
+
self,
|
|
104
|
+
request: Union[ListDocsRequest, Mapping[str, object]],
|
|
105
|
+
*,
|
|
106
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
107
|
+
timeout: Optional[int] = None,
|
|
108
|
+
) -> ListDocsResponse:
|
|
109
|
+
req_payload = (
|
|
110
|
+
request.model_dump(by_alias=True, exclude_none=True) # type: ignore[attr-defined]
|
|
111
|
+
if isinstance(request, Model)
|
|
112
|
+
else dict(request)
|
|
113
|
+
)
|
|
114
|
+
payload: dict = {**self._meta_payload, **req_payload}
|
|
115
|
+
res = self.client.json_exception("ListDocs", {}, json.dumps(payload), headers=headers, timeout=timeout)
|
|
116
|
+
response = ListDocsResponse.parse_with(res)
|
|
117
|
+
return response
|
|
118
|
+
|
|
119
|
+
def update_doc_meta(
|
|
120
|
+
self,
|
|
121
|
+
doc_id: str,
|
|
122
|
+
meta: List[MetaItem],
|
|
123
|
+
*,
|
|
124
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
125
|
+
timeout: Optional[int] = None,
|
|
126
|
+
):
|
|
127
|
+
payload = {**self._meta_payload, "doc_id": doc_id, "meta": [item.model_dump(by_alias=True) for item in meta]}
|
|
128
|
+
res = self.client.json_exception("UpdateDocMeta", {}, json.dumps(payload), headers=headers, timeout=timeout)
|
|
129
|
+
return res
|
|
130
|
+
|
|
131
|
+
def update_doc(
|
|
132
|
+
self,
|
|
133
|
+
doc_id: str,
|
|
134
|
+
doc_name: str,
|
|
135
|
+
*,
|
|
136
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
137
|
+
timeout: Optional[int] = None,
|
|
138
|
+
):
|
|
139
|
+
payload = {**self._meta_payload, "doc_id": doc_id, "doc_name": doc_name}
|
|
140
|
+
res = self.client.json_exception("UpdateDoc", {}, json.dumps(payload), headers=headers, timeout=timeout)
|
|
141
|
+
return res
|
|
142
|
+
|
|
143
|
+
def get_point(
|
|
144
|
+
self,
|
|
145
|
+
point_id: str,
|
|
146
|
+
*,
|
|
147
|
+
get_attachment_link: bool = False,
|
|
148
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
149
|
+
timeout: Optional[int] = None,
|
|
150
|
+
) -> PointInfo:
|
|
151
|
+
payload = {**self._meta_payload, "point_id": point_id}
|
|
152
|
+
if get_attachment_link:
|
|
153
|
+
payload["get_attachment_link"] = True
|
|
154
|
+
res = self.client.json_exception("GetPointInfo", {}, json.dumps(payload), headers=headers, timeout=timeout)
|
|
155
|
+
data_obj = res["data"] if isinstance(res, dict) and "data" in res else {}
|
|
156
|
+
if not isinstance(data_obj, dict):
|
|
157
|
+
data_obj = {}
|
|
158
|
+
point = PointInfo.model_validate(
|
|
159
|
+
{**data_obj, "project": self._meta.project_name or "default", "resource_id": self._meta.resource_id}
|
|
160
|
+
)
|
|
161
|
+
return point
|
|
162
|
+
|
|
163
|
+
def list_points(
|
|
164
|
+
self,
|
|
165
|
+
request: Union[ListPointsRequest, Mapping[str, object]],
|
|
166
|
+
*,
|
|
167
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
168
|
+
timeout: Optional[int] = None,
|
|
169
|
+
) -> ListPointsResponse:
|
|
170
|
+
req_payload = (
|
|
171
|
+
request.model_dump(by_alias=True, exclude_none=True) # type: ignore[attr-defined]
|
|
172
|
+
if isinstance(request, Model)
|
|
173
|
+
else dict(request)
|
|
174
|
+
)
|
|
175
|
+
payload: dict = {**self._meta_payload, **req_payload}
|
|
176
|
+
res = self.client.json_exception("ListPoints", {}, json.dumps(payload), headers=headers, timeout=timeout)
|
|
177
|
+
response = ListPointsResponse.parse_with(res)
|
|
178
|
+
return response
|
|
179
|
+
|
|
180
|
+
def add_point(
|
|
181
|
+
self,
|
|
182
|
+
request: Union[AddPointRequest, Mapping[str, object]],
|
|
183
|
+
*,
|
|
184
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
185
|
+
timeout: Optional[int] = None,
|
|
186
|
+
):
|
|
187
|
+
req_payload = (
|
|
188
|
+
request.model_dump(by_alias=True, exclude_none=True) # type: ignore[attr-defined]
|
|
189
|
+
if isinstance(request, Model)
|
|
190
|
+
else dict(request)
|
|
191
|
+
)
|
|
192
|
+
payload = {**self._meta_payload, **req_payload}
|
|
193
|
+
res = self.client.json_exception("AddPoint", {}, json.dumps(payload), headers=headers, timeout=timeout)
|
|
194
|
+
response = PointAddResponse.parse_with(res)
|
|
195
|
+
return response
|
|
196
|
+
|
|
197
|
+
def update_point(
|
|
198
|
+
self,
|
|
199
|
+
point_id: str,
|
|
200
|
+
update: Union[UpdatePointRequest, Mapping[str, object]],
|
|
201
|
+
*,
|
|
202
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
203
|
+
timeout: Optional[int] = None,
|
|
204
|
+
):
|
|
205
|
+
upd_payload = (
|
|
206
|
+
update.model_dump(by_alias=True, exclude_none=True) # type: ignore[attr-defined]
|
|
207
|
+
if isinstance(update, Model)
|
|
208
|
+
else dict(update)
|
|
209
|
+
)
|
|
210
|
+
payload = {**self._meta_payload, "point_id": point_id, **upd_payload}
|
|
211
|
+
res = self.client.json_exception("UpdatePoint", {}, json.dumps(payload), headers=headers, timeout=timeout)
|
|
212
|
+
return res
|
|
213
|
+
|
|
214
|
+
def delete_point(
|
|
215
|
+
self,
|
|
216
|
+
request: Union[DeletePointRequest, Mapping[str, object]],
|
|
217
|
+
*,
|
|
218
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
219
|
+
timeout: Optional[int] = None,
|
|
220
|
+
):
|
|
221
|
+
req_payload = (
|
|
222
|
+
request.model_dump(by_alias=True, exclude_none=True) # type: ignore[attr-defined]
|
|
223
|
+
if isinstance(request, Model)
|
|
224
|
+
else dict(request)
|
|
225
|
+
)
|
|
226
|
+
payload = {**self._meta_payload, **req_payload}
|
|
227
|
+
res = self.client.json_exception("DeletePoint", {}, json.dumps(payload), headers=headers, timeout=timeout)
|
|
228
|
+
return res
|
|
229
|
+
|
|
230
|
+
def search_collection(
|
|
231
|
+
self,
|
|
232
|
+
request: Union[SearchCollectionRequest, Mapping[str, object]],
|
|
233
|
+
*,
|
|
234
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
235
|
+
timeout: Optional[int] = None,
|
|
236
|
+
) -> SearchResponse:
|
|
237
|
+
req_payload = (
|
|
238
|
+
request.model_dump(by_alias=True, exclude_none=True) # type: ignore[attr-defined]
|
|
239
|
+
if isinstance(request, Model)
|
|
240
|
+
else dict(request)
|
|
241
|
+
)
|
|
242
|
+
payload: dict = {**self._meta_payload, **req_payload, "name": self._meta.collection_name}
|
|
243
|
+
res = self.client.json_exception("SearchCollection", {}, json.dumps(payload), headers=headers, timeout=timeout)
|
|
244
|
+
response = SearchResponse.parse_with(res)
|
|
245
|
+
return response
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def search_knowledge(
|
|
249
|
+
self,
|
|
250
|
+
request: Union[SearchKnowledgeRequest, Mapping[str, object]],
|
|
251
|
+
*,
|
|
252
|
+
headers: Optional[Mapping[str, str]] = None,
|
|
253
|
+
timeout: Optional[int] = None,
|
|
254
|
+
) -> SearchKnowledgeResponse:
|
|
255
|
+
req_payload = (
|
|
256
|
+
request.model_dump(by_alias=True, exclude_none=True) # type: ignore[attr-defined]
|
|
257
|
+
if isinstance(request, Model)
|
|
258
|
+
else dict(request)
|
|
259
|
+
)
|
|
260
|
+
payload: dict = {**self._meta_payload, **req_payload, "name": self._meta.collection_name}
|
|
261
|
+
res = self.client.json_exception("SearchKnowledge", {}, json.dumps(payload), headers=headers, timeout=timeout)
|
|
262
|
+
response = SearchKnowledgeResponse.parse_with(res)
|
|
263
|
+
return response
|