vikingdb-python-sdk 0.1.11__tar.gz → 0.1.13__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.
Files changed (47) hide show
  1. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/PKG-INFO +84 -4
  2. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/README.md +81 -0
  3. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/pyproject.toml +1 -1
  4. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/__init__.py +7 -0
  5. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/_client.py +53 -0
  6. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/client.py +30 -11
  7. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/models/index.py +4 -0
  8. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/version.py +1 -1
  9. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb_python_sdk.egg-info/PKG-INFO +84 -4
  10. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/LICENSE.txt +0 -0
  11. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/setup.cfg +0 -0
  12. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/auth.py +0 -0
  13. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/exceptions.py +0 -0
  14. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/__init__.py +0 -0
  15. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/collection.py +0 -0
  16. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/exceptions.py +0 -0
  17. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/models/__init__.py +0 -0
  18. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/models/base.py +0 -0
  19. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/models/chat.py +0 -0
  20. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/models/doc.py +0 -0
  21. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/models/point.py +0 -0
  22. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/models/rerank.py +0 -0
  23. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/models/search.py +0 -0
  24. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/models/service_chat.py +0 -0
  25. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/memory/__init__.py +0 -0
  26. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/memory/client.py +0 -0
  27. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/memory/collection.py +0 -0
  28. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/memory/exceptions.py +0 -0
  29. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/memory/types.py +0 -0
  30. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/request_options.py +0 -0
  31. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/__init__.py +0 -0
  32. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/base.py +0 -0
  33. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/client.py +0 -0
  34. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/collection.py +0 -0
  35. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/embedding.py +0 -0
  36. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/exceptions.py +0 -0
  37. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/index.py +0 -0
  38. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/models/__init__.py +0 -0
  39. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/models/base.py +0 -0
  40. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/models/collection.py +0 -0
  41. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/models/embedding.py +0 -0
  42. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/models/rerank.py +0 -0
  43. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/rerank.py +0 -0
  44. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb_python_sdk.egg-info/SOURCES.txt +0 -0
  45. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb_python_sdk.egg-info/dependency_links.txt +0 -0
  46. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb_python_sdk.egg-info/requires.txt +0 -0
  47. {vikingdb_python_sdk-0.1.11 → vikingdb_python_sdk-0.1.13}/vikingdb_python_sdk.egg-info/top_level.txt +0 -0
@@ -1,8 +1,8 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.1
2
2
  Name: vikingdb-python-sdk
3
- Version: 0.1.11
3
+ Version: 0.1.13
4
4
  Summary: vikingdb Python SDK
5
- License-Expression: Apache-2.0
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
@@ -24,7 +24,6 @@ Requires-Dist: typing_extensions>=4.9.0
24
24
  Requires-Dist: urllib3>=1.21.1
25
25
  Requires-Dist: volcengine>=1.0.0
26
26
  Requires-Dist: aiohttp>=3.10.0
27
- Dynamic: license-file
28
27
 
29
28
  ## VikingDB Python SDK (v2)
30
29
 
@@ -34,6 +33,7 @@ This package provides an idiomatic Python interface to the VikingDB v2 data-plan
34
33
  - Simple client configuration with AK/SK signing (Volcano Engine V4) or API-key authentication.
35
34
  - **Vector Database**: Request envelope handling with typed request/response models covering collection, index, and embedding workflows.
36
35
  - **Memory Management**: Conversational memory APIs for managing user profiles, events, and session messages with semantic search capabilities.
36
+ - **Knowledge Base**: Document and point CRUD with typed models, hybrid retrieval (`search_collection`, `search_knowledge`), rerank, and chat-completion/service-chat orchestration.
37
37
  - Pluggable retry strategy (exponential backoff with jitter) and per-request overrides (`RequestOptions`).
38
38
  - Executable example guides (`pytest` integration tests and standalone scripts) that demonstrate connectivity, CRUD, search, analytics, embedding, and memory management scenarios against a real VikingDB environment.
39
39
 
@@ -114,6 +114,40 @@ result = collection.search_memory(
114
114
  print("search results:", result)
115
115
  ```
116
116
 
117
+ #### Knowledge Base
118
+
119
+ ```python
120
+ import os
121
+ from vikingdb import IAM, APIKey
122
+ from vikingdb.knowledge import (
123
+ VikingKnowledge,
124
+ KnowledgeCollection,
125
+ SearchKnowledgeRequest,
126
+ )
127
+
128
+ # IAM auth for collection-level operations
129
+ client = VikingKnowledge(
130
+ auth=IAM(ak=os.getenv("VOLC_AK"), sk=os.getenv("VOLC_SK")),
131
+ host="api-knowledgebase.mlp.cn-beijing.volces.com",
132
+ region="cn-beijing",
133
+ scheme="http",
134
+ )
135
+
136
+ collection = client.collection(
137
+ resource_id=os.getenv("VIKING_COLLECTION_RID"),
138
+ collection_name=os.getenv("VIKING_COLLECTION_NAME") or "financial_reports",
139
+ project_name=os.getenv("VIKING_PROJECT") or "default",
140
+ )
141
+
142
+ resp = collection.search_knowledge(
143
+ SearchKnowledgeRequest(query="2025 Q1 revenue growth", limit=5, dense_weight=0.5)
144
+ )
145
+ print(f"request_id={resp.request_id} hits={len(resp.result.result_list or [])}")
146
+
147
+ # API-key auth for service-level chat
148
+ svc_client = VikingKnowledge(auth=APIKey(api_key=os.getenv("VIKING_SERVICE_API_KEY")))
149
+ ```
150
+
117
151
  ### Example Guides
118
152
 
119
153
  #### Vector Examples
@@ -179,6 +213,40 @@ python examples/memory/02_add_session.py
179
213
  python examples/memory/03_search_memory.py
180
214
  ```
181
215
 
216
+ #### Knowledge Examples
217
+
218
+ The examples under `examples/knowledge` cover core knowledge base workflows:
219
+
220
+ 1. **01_init_client.py**: Initialize `VikingKnowledge` and obtain collections by resource ID or by name + project.
221
+ 2. **02_doc_crud.py**: Add documents via URL or TOS path (`add_doc_v2`), get/update metadata, list documents.
222
+ 3. **03_point_crud.py**: Add/update/delete points (chunks) within a document; list and fetch detailed point info.
223
+ 4. **04_search.py**: Perform collection search and knowledge search, invoke rerank, and orchestrate chat-completion or service-chat (including streaming).
224
+
225
+ Environment variables:
226
+
227
+ ```
228
+ VOLC_AK=your-access-key
229
+ VOLC_SK=your-secret-key
230
+ VIKING_PROJECT=project-name
231
+ VIKING_COLLECTION_RID=collection-resource-id
232
+ VIKING_COLLECTION_NAME=collection-name
233
+ # For chat/service features:
234
+ VIKING_CHAT_API_KEY=chat-api-key
235
+ VIKING_SERVICE_API_KEY=service-api-key
236
+ VIKING_SERVICE_RID=service-resource-id
237
+ # Optional for rerank:
238
+ VIKING_RERANK_INSTRUCTION=custom-instruction
239
+ ```
240
+
241
+ Run individual examples:
242
+
243
+ ```bash
244
+ python examples/knowledge/01_init_client.py
245
+ python examples/knowledge/02_doc_crud.py
246
+ python examples/knowledge/03_point_crud.py
247
+ python examples/knowledge/04_search.py
248
+ ```
249
+
182
250
  ### Architecture Overview
183
251
 
184
252
  - `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).
@@ -186,6 +254,7 @@ python examples/memory/03_search_memory.py
186
254
  - Vector request/response models now surface directly from `vikingdb.vector` (backed internally by `vikingdb/vector/models`).
187
255
  - Memory APIs return plain dictionaries without object encapsulation, providing a lightweight interface for conversational memory management (session, profile, event operations).
188
256
  - 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.
257
+ - Knowledge base APIs surface typed pydantic models under `vikingdb.knowledge` for documents, points, search, rerank, and chat/service chat, with IAM and API-key authentication modes.
189
258
 
190
259
  ### Project Structure
191
260
 
@@ -211,6 +280,12 @@ vikingdb/
211
280
  │ ├── collection.py # Memory collection operations
212
281
  │ ├── types.py # Type definitions for memory operations
213
282
  │ └── exceptions.py # Memory-specific exceptions
283
+ ├── knowledge/ # Knowledge base clients and models
284
+ │ ├── __init__.py # High-level knowledge client and exports
285
+ │ ├── client.py # VikingKnowledge service client
286
+ │ ├── collection.py # Document/point/search operations
287
+ │ ├── exceptions.py # Knowledge-specific exceptions
288
+ │ └── models/ # Typed pydantic models (doc, point, search, chat, rerank)
214
289
 
215
290
  examples/
216
291
  ├── vector/ # Vector integration guides (pytest)
@@ -222,6 +297,11 @@ examples/
222
297
  ├── 01_init_client_and_collection.py
223
298
  ├── 02_add_session.py
224
299
  └── 03_search_memory.py
300
+ └── knowledge/ # Knowledge base usage examples
301
+ ├── 01_init_client.py
302
+ ├── 02_doc_crud.py
303
+ ├── 03_point_crud.py
304
+ └── 04_search.py
225
305
  ```
226
306
 
227
307
  ### Contributing
@@ -6,6 +6,7 @@ This package provides an idiomatic Python interface to the VikingDB v2 data-plan
6
6
  - Simple client configuration with AK/SK signing (Volcano Engine V4) or API-key authentication.
7
7
  - **Vector Database**: Request envelope handling with typed request/response models covering collection, index, and embedding workflows.
8
8
  - **Memory Management**: Conversational memory APIs for managing user profiles, events, and session messages with semantic search capabilities.
9
+ - **Knowledge Base**: Document and point CRUD with typed models, hybrid retrieval (`search_collection`, `search_knowledge`), rerank, and chat-completion/service-chat orchestration.
9
10
  - Pluggable retry strategy (exponential backoff with jitter) and per-request overrides (`RequestOptions`).
10
11
  - Executable example guides (`pytest` integration tests and standalone scripts) that demonstrate connectivity, CRUD, search, analytics, embedding, and memory management scenarios against a real VikingDB environment.
11
12
 
@@ -86,6 +87,40 @@ result = collection.search_memory(
86
87
  print("search results:", result)
87
88
  ```
88
89
 
90
+ #### Knowledge Base
91
+
92
+ ```python
93
+ import os
94
+ from vikingdb import IAM, APIKey
95
+ from vikingdb.knowledge import (
96
+ VikingKnowledge,
97
+ KnowledgeCollection,
98
+ SearchKnowledgeRequest,
99
+ )
100
+
101
+ # IAM auth for collection-level operations
102
+ client = VikingKnowledge(
103
+ auth=IAM(ak=os.getenv("VOLC_AK"), sk=os.getenv("VOLC_SK")),
104
+ host="api-knowledgebase.mlp.cn-beijing.volces.com",
105
+ region="cn-beijing",
106
+ scheme="http",
107
+ )
108
+
109
+ collection = client.collection(
110
+ resource_id=os.getenv("VIKING_COLLECTION_RID"),
111
+ collection_name=os.getenv("VIKING_COLLECTION_NAME") or "financial_reports",
112
+ project_name=os.getenv("VIKING_PROJECT") or "default",
113
+ )
114
+
115
+ resp = collection.search_knowledge(
116
+ SearchKnowledgeRequest(query="2025 Q1 revenue growth", limit=5, dense_weight=0.5)
117
+ )
118
+ print(f"request_id={resp.request_id} hits={len(resp.result.result_list or [])}")
119
+
120
+ # API-key auth for service-level chat
121
+ svc_client = VikingKnowledge(auth=APIKey(api_key=os.getenv("VIKING_SERVICE_API_KEY")))
122
+ ```
123
+
89
124
  ### Example Guides
90
125
 
91
126
  #### Vector Examples
@@ -151,6 +186,40 @@ python examples/memory/02_add_session.py
151
186
  python examples/memory/03_search_memory.py
152
187
  ```
153
188
 
189
+ #### Knowledge Examples
190
+
191
+ The examples under `examples/knowledge` cover core knowledge base workflows:
192
+
193
+ 1. **01_init_client.py**: Initialize `VikingKnowledge` and obtain collections by resource ID or by name + project.
194
+ 2. **02_doc_crud.py**: Add documents via URL or TOS path (`add_doc_v2`), get/update metadata, list documents.
195
+ 3. **03_point_crud.py**: Add/update/delete points (chunks) within a document; list and fetch detailed point info.
196
+ 4. **04_search.py**: Perform collection search and knowledge search, invoke rerank, and orchestrate chat-completion or service-chat (including streaming).
197
+
198
+ Environment variables:
199
+
200
+ ```
201
+ VOLC_AK=your-access-key
202
+ VOLC_SK=your-secret-key
203
+ VIKING_PROJECT=project-name
204
+ VIKING_COLLECTION_RID=collection-resource-id
205
+ VIKING_COLLECTION_NAME=collection-name
206
+ # For chat/service features:
207
+ VIKING_CHAT_API_KEY=chat-api-key
208
+ VIKING_SERVICE_API_KEY=service-api-key
209
+ VIKING_SERVICE_RID=service-resource-id
210
+ # Optional for rerank:
211
+ VIKING_RERANK_INSTRUCTION=custom-instruction
212
+ ```
213
+
214
+ Run individual examples:
215
+
216
+ ```bash
217
+ python examples/knowledge/01_init_client.py
218
+ python examples/knowledge/02_doc_crud.py
219
+ python examples/knowledge/03_point_crud.py
220
+ python examples/knowledge/04_search.py
221
+ ```
222
+
154
223
  ### Architecture Overview
155
224
 
156
225
  - `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).
@@ -158,6 +227,7 @@ python examples/memory/03_search_memory.py
158
227
  - Vector request/response models now surface directly from `vikingdb.vector` (backed internally by `vikingdb/vector/models`).
159
228
  - Memory APIs return plain dictionaries without object encapsulation, providing a lightweight interface for conversational memory management (session, profile, event operations).
160
229
  - 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.
230
+ - Knowledge base APIs surface typed pydantic models under `vikingdb.knowledge` for documents, points, search, rerank, and chat/service chat, with IAM and API-key authentication modes.
161
231
 
162
232
  ### Project Structure
163
233
 
@@ -183,6 +253,12 @@ vikingdb/
183
253
  │ ├── collection.py # Memory collection operations
184
254
  │ ├── types.py # Type definitions for memory operations
185
255
  │ └── exceptions.py # Memory-specific exceptions
256
+ ├── knowledge/ # Knowledge base clients and models
257
+ │ ├── __init__.py # High-level knowledge client and exports
258
+ │ ├── client.py # VikingKnowledge service client
259
+ │ ├── collection.py # Document/point/search operations
260
+ │ ├── exceptions.py # Knowledge-specific exceptions
261
+ │ └── models/ # Typed pydantic models (doc, point, search, chat, rerank)
186
262
 
187
263
  examples/
188
264
  ├── vector/ # Vector integration guides (pytest)
@@ -194,6 +270,11 @@ examples/
194
270
  ├── 01_init_client_and_collection.py
195
271
  ├── 02_add_session.py
196
272
  └── 03_search_memory.py
273
+ └── knowledge/ # Knowledge base usage examples
274
+ ├── 01_init_client.py
275
+ ├── 02_doc_crud.py
276
+ ├── 03_point_crud.py
277
+ └── 04_search.py
197
278
  ```
198
279
 
199
280
  ### Contributing
@@ -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",
@@ -19,6 +19,11 @@ from .memory import (
19
19
  VikingMem,
20
20
  Collection,
21
21
  )
22
+ from .knowledge import (
23
+ VikingKnowledge,
24
+ KnowledgeCollection,
25
+ )
26
+
22
27
  from .version import __version__
23
28
  __all__ = [
24
29
  "IAM",
@@ -34,5 +39,7 @@ __all__ = [
34
39
  "memory",
35
40
  "VikingMem",
36
41
  "Collection",
42
+ "VikingKnowledge",
43
+ "KnowledgeCollection",
37
44
  "__version__",
38
45
  ]
@@ -260,3 +260,56 @@ class Client(Service, ABC):
260
260
  request_id=request_id,
261
261
  message=f"failed to run aiohttp {api}: {exc}",
262
262
  ) from exc
263
+
264
+
265
+ def _stream_json(self, api, params, body, headers=None, timeout=None):
266
+ if api not in self.api_info:
267
+ raise Exception("no such api")
268
+ api_info = self.api_info[api]
269
+ request = self.prepare_request(api_info, params)
270
+ if headers:
271
+ for key, value in headers.items():
272
+ request.headers[key] = value
273
+ request.headers["Content-Type"] = "application/json"
274
+ request.headers["Accept"] = "text/event-stream"
275
+ request.body = body
276
+ self.auth_provider.sign_request(request)
277
+ url = request.build()
278
+ request_id_value = request.headers.get(_REQUEST_ID_HEADER)
279
+ request_id = str(request_id_value) if request_id_value else "unknown"
280
+ if timeout is not None:
281
+ request_timeout = (timeout, timeout)
282
+ else:
283
+ request_timeout = (
284
+ self.service_info.connection_timeout,
285
+ self.service_info.socket_timeout,
286
+ )
287
+ response = self.session.post(
288
+ url,
289
+ headers=request.headers,
290
+ data=request.body,
291
+ stream=True,
292
+ timeout=request_timeout,
293
+ )
294
+ if response.status_code != 200:
295
+ payload_text_attr = getattr(response, "text", "")
296
+ payload_text = payload_text_attr if isinstance(payload_text_attr, str) else ""
297
+ payload_text = payload_text or ""
298
+ raise VikingAPIException.from_response(
299
+ payload_text,
300
+ request_id=request_id,
301
+ status_code=response.status_code,
302
+ )
303
+ for line in response.iter_lines():
304
+ s = line.decode("utf-8")
305
+ if not s:
306
+ continue
307
+ if s.startswith("data:"):
308
+ data_str = s.split("data:", 1)[1]
309
+ data = json.loads(data_str)
310
+ yield data
311
+ try:
312
+ if "end" in data.get("data", {}):
313
+ break
314
+ except Exception:
315
+ pass
@@ -5,14 +5,14 @@
5
5
  from __future__ import annotations
6
6
 
7
7
  import json
8
- from typing import Mapping, Optional, Union, Sequence, cast
8
+ from typing import Mapping, Optional, Union, Sequence, Iterable
9
9
 
10
10
  from volcengine.ApiInfo import ApiInfo
11
11
 
12
12
  from .. import APIKey
13
13
  from .._client import Client, _REQUEST_ID_HEADER
14
14
  from ..auth import Auth
15
- from ..exceptions import VikingException, promote_exception
15
+ from ..exceptions import VikingException, promote_exception, VikingAPIException
16
16
  from .exceptions import EXCEPTION_MAP, VikingKnowledgeException
17
17
  from ..version import __version__
18
18
  from .models.base import CollectionMeta, Model
@@ -90,6 +90,16 @@ class VikingKnowledge(Client):
90
90
  raise VikingKnowledgeException(1000028, "missed", "empty response due to unknown error", status_code=None)
91
91
  return res
92
92
 
93
+ def stream_json_exception(self, api, params, body, headers=None, timeout=None):
94
+ try:
95
+ for item in self._stream_json(api, params, body, headers=headers, timeout=timeout):
96
+ yield item
97
+ except VikingException as exc:
98
+ raise promote_exception(exc, exception_map=EXCEPTION_MAP, default_cls=VikingKnowledgeException) from None
99
+ except Exception as exc:
100
+ raise exc
101
+ return
102
+
93
103
  async def async_json_exception(self, api, params, body, headers=None, timeout=None):
94
104
  try:
95
105
  res = await self.async_json(api, params, body, headers=headers, timeout=timeout)
@@ -146,15 +156,20 @@ class VikingKnowledge(Client):
146
156
  *,
147
157
  headers: Optional[Mapping[str, str]] = None,
148
158
  timeout: Optional[int] = None,
149
- ) -> ChatCompletionResponse:
159
+ ) -> Union[ChatCompletionResponse, Iterable[ChatCompletionResponse]]:
150
160
  payload = (
151
161
  request.model_dump(by_alias=True, exclude_none=True) # type: ignore[attr-defined]
152
162
  if isinstance(request, ChatCompletionRequest)
153
163
  else dict(request)
154
164
  )
155
- res = self.json_exception("ChatCompletion", {}, json.dumps(payload), headers=headers, timeout=timeout)
156
- response = ChatCompletionResponse.parse_with(res)
157
- return response
165
+ if bool(payload.get("stream")):
166
+ def _gen():
167
+ for res in self.stream_json_exception("ChatCompletion", {}, json.dumps(payload), headers=headers, timeout=timeout):
168
+ yield ChatCompletionResponse.parse_with(res)
169
+ return _gen()
170
+ else:
171
+ res = self.json_exception("ChatCompletion", {}, json.dumps(payload), headers=headers, timeout=timeout)
172
+ return ChatCompletionResponse.parse_with(res)
158
173
 
159
174
  def service_chat(
160
175
  self,
@@ -162,13 +177,17 @@ class VikingKnowledge(Client):
162
177
  *,
163
178
  headers: Optional[Mapping[str, str]] = None,
164
179
  timeout: Optional[int] = None,
165
- ) -> ServiceChatResponse:
180
+ ) -> Union[ServiceChatResponse, Iterable[ServiceChatResponse]]:
166
181
  payload = (
167
182
  request.model_dump(by_alias=True, exclude_none=True) # type: ignore[attr-defined]
168
183
  if isinstance(request, ServiceChatRequest)
169
184
  else dict(request)
170
185
  )
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
186
+ if bool(payload.get("stream")):
187
+ def _gen():
188
+ for res in self.stream_json_exception("ServiceChat", {}, json.dumps(payload), headers=headers, timeout=timeout):
189
+ yield ServiceChatResponse.parse_with(res)
190
+ return _gen()
191
+ else:
192
+ res = self.json_exception("ServiceChat", {}, json.dumps(payload), headers=headers, timeout=timeout)
193
+ return ServiceChatResponse.parse_with(res)
@@ -95,9 +95,13 @@ class SearchByScalarRequest(SearchBase):
95
95
 
96
96
 
97
97
  class SearchByKeywordsRequest(SearchBase):
98
+ mode: Optional[str] = Field(default=None, alias="mode")
98
99
  keywords: Optional[Sequence[str]] = Field(default=None, alias="keywords")
99
100
  query: Optional[str] = Field(default=None, alias="query")
100
101
  case_sensitive: Optional[bool] = Field(default=None, alias="case_sensitive")
102
+ fields: Optional[Sequence[str]] = Field(default=None, alias="fields")
103
+ bm25_k1: Optional[float] = Field(default=None, alias="bm25_k1")
104
+ bm25_b: Optional[float] = Field(default=None, alias="bm25_b")
101
105
 
102
106
 
103
107
  class SearchByRandomRequest(SearchBase):
@@ -1,4 +1,4 @@
1
1
  # Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd.
2
2
  # SPDX-License-Identifier: Apache-2.0
3
3
 
4
- __version__ = '0.1.11'
4
+ __version__ = '0.1.13'
@@ -1,8 +1,8 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.1
2
2
  Name: vikingdb-python-sdk
3
- Version: 0.1.11
3
+ Version: 0.1.13
4
4
  Summary: vikingdb Python SDK
5
- License-Expression: Apache-2.0
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
@@ -24,7 +24,6 @@ Requires-Dist: typing_extensions>=4.9.0
24
24
  Requires-Dist: urllib3>=1.21.1
25
25
  Requires-Dist: volcengine>=1.0.0
26
26
  Requires-Dist: aiohttp>=3.10.0
27
- Dynamic: license-file
28
27
 
29
28
  ## VikingDB Python SDK (v2)
30
29
 
@@ -34,6 +33,7 @@ This package provides an idiomatic Python interface to the VikingDB v2 data-plan
34
33
  - Simple client configuration with AK/SK signing (Volcano Engine V4) or API-key authentication.
35
34
  - **Vector Database**: Request envelope handling with typed request/response models covering collection, index, and embedding workflows.
36
35
  - **Memory Management**: Conversational memory APIs for managing user profiles, events, and session messages with semantic search capabilities.
36
+ - **Knowledge Base**: Document and point CRUD with typed models, hybrid retrieval (`search_collection`, `search_knowledge`), rerank, and chat-completion/service-chat orchestration.
37
37
  - Pluggable retry strategy (exponential backoff with jitter) and per-request overrides (`RequestOptions`).
38
38
  - Executable example guides (`pytest` integration tests and standalone scripts) that demonstrate connectivity, CRUD, search, analytics, embedding, and memory management scenarios against a real VikingDB environment.
39
39
 
@@ -114,6 +114,40 @@ result = collection.search_memory(
114
114
  print("search results:", result)
115
115
  ```
116
116
 
117
+ #### Knowledge Base
118
+
119
+ ```python
120
+ import os
121
+ from vikingdb import IAM, APIKey
122
+ from vikingdb.knowledge import (
123
+ VikingKnowledge,
124
+ KnowledgeCollection,
125
+ SearchKnowledgeRequest,
126
+ )
127
+
128
+ # IAM auth for collection-level operations
129
+ client = VikingKnowledge(
130
+ auth=IAM(ak=os.getenv("VOLC_AK"), sk=os.getenv("VOLC_SK")),
131
+ host="api-knowledgebase.mlp.cn-beijing.volces.com",
132
+ region="cn-beijing",
133
+ scheme="http",
134
+ )
135
+
136
+ collection = client.collection(
137
+ resource_id=os.getenv("VIKING_COLLECTION_RID"),
138
+ collection_name=os.getenv("VIKING_COLLECTION_NAME") or "financial_reports",
139
+ project_name=os.getenv("VIKING_PROJECT") or "default",
140
+ )
141
+
142
+ resp = collection.search_knowledge(
143
+ SearchKnowledgeRequest(query="2025 Q1 revenue growth", limit=5, dense_weight=0.5)
144
+ )
145
+ print(f"request_id={resp.request_id} hits={len(resp.result.result_list or [])}")
146
+
147
+ # API-key auth for service-level chat
148
+ svc_client = VikingKnowledge(auth=APIKey(api_key=os.getenv("VIKING_SERVICE_API_KEY")))
149
+ ```
150
+
117
151
  ### Example Guides
118
152
 
119
153
  #### Vector Examples
@@ -179,6 +213,40 @@ python examples/memory/02_add_session.py
179
213
  python examples/memory/03_search_memory.py
180
214
  ```
181
215
 
216
+ #### Knowledge Examples
217
+
218
+ The examples under `examples/knowledge` cover core knowledge base workflows:
219
+
220
+ 1. **01_init_client.py**: Initialize `VikingKnowledge` and obtain collections by resource ID or by name + project.
221
+ 2. **02_doc_crud.py**: Add documents via URL or TOS path (`add_doc_v2`), get/update metadata, list documents.
222
+ 3. **03_point_crud.py**: Add/update/delete points (chunks) within a document; list and fetch detailed point info.
223
+ 4. **04_search.py**: Perform collection search and knowledge search, invoke rerank, and orchestrate chat-completion or service-chat (including streaming).
224
+
225
+ Environment variables:
226
+
227
+ ```
228
+ VOLC_AK=your-access-key
229
+ VOLC_SK=your-secret-key
230
+ VIKING_PROJECT=project-name
231
+ VIKING_COLLECTION_RID=collection-resource-id
232
+ VIKING_COLLECTION_NAME=collection-name
233
+ # For chat/service features:
234
+ VIKING_CHAT_API_KEY=chat-api-key
235
+ VIKING_SERVICE_API_KEY=service-api-key
236
+ VIKING_SERVICE_RID=service-resource-id
237
+ # Optional for rerank:
238
+ VIKING_RERANK_INSTRUCTION=custom-instruction
239
+ ```
240
+
241
+ Run individual examples:
242
+
243
+ ```bash
244
+ python examples/knowledge/01_init_client.py
245
+ python examples/knowledge/02_doc_crud.py
246
+ python examples/knowledge/03_point_crud.py
247
+ python examples/knowledge/04_search.py
248
+ ```
249
+
182
250
  ### Architecture Overview
183
251
 
184
252
  - `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).
@@ -186,6 +254,7 @@ python examples/memory/03_search_memory.py
186
254
  - Vector request/response models now surface directly from `vikingdb.vector` (backed internally by `vikingdb/vector/models`).
187
255
  - Memory APIs return plain dictionaries without object encapsulation, providing a lightweight interface for conversational memory management (session, profile, event operations).
188
256
  - 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.
257
+ - Knowledge base APIs surface typed pydantic models under `vikingdb.knowledge` for documents, points, search, rerank, and chat/service chat, with IAM and API-key authentication modes.
189
258
 
190
259
  ### Project Structure
191
260
 
@@ -211,6 +280,12 @@ vikingdb/
211
280
  │ ├── collection.py # Memory collection operations
212
281
  │ ├── types.py # Type definitions for memory operations
213
282
  │ └── exceptions.py # Memory-specific exceptions
283
+ ├── knowledge/ # Knowledge base clients and models
284
+ │ ├── __init__.py # High-level knowledge client and exports
285
+ │ ├── client.py # VikingKnowledge service client
286
+ │ ├── collection.py # Document/point/search operations
287
+ │ ├── exceptions.py # Knowledge-specific exceptions
288
+ │ └── models/ # Typed pydantic models (doc, point, search, chat, rerank)
214
289
 
215
290
  examples/
216
291
  ├── vector/ # Vector integration guides (pytest)
@@ -222,6 +297,11 @@ examples/
222
297
  ├── 01_init_client_and_collection.py
223
298
  ├── 02_add_session.py
224
299
  └── 03_search_memory.py
300
+ └── knowledge/ # Knowledge base usage examples
301
+ ├── 01_init_client.py
302
+ ├── 02_doc_crud.py
303
+ ├── 03_point_crud.py
304
+ └── 04_search.py
225
305
  ```
226
306
 
227
307
  ### Contributing