vikingdb-python-sdk 0.1.12__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.12 → vikingdb_python_sdk-0.1.13}/PKG-INFO +82 -1
  2. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/README.md +81 -0
  3. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/__init__.py +7 -0
  4. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/_client.py +53 -0
  5. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/client.py +30 -11
  6. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/collection.py +0 -39
  7. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/models/doc.py +0 -2
  8. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/version.py +1 -1
  9. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb_python_sdk.egg-info/PKG-INFO +82 -1
  10. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/LICENSE.txt +0 -0
  11. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/pyproject.toml +0 -0
  12. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/setup.cfg +0 -0
  13. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/auth.py +0 -0
  14. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/exceptions.py +0 -0
  15. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/__init__.py +0 -0
  16. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/exceptions.py +0 -0
  17. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/models/__init__.py +0 -0
  18. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/models/base.py +0 -0
  19. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/models/chat.py +0 -0
  20. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/models/point.py +0 -0
  21. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/models/rerank.py +0 -0
  22. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/models/search.py +0 -0
  23. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/knowledge/models/service_chat.py +0 -0
  24. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/memory/__init__.py +0 -0
  25. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/memory/client.py +0 -0
  26. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/memory/collection.py +0 -0
  27. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/memory/exceptions.py +0 -0
  28. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/memory/types.py +0 -0
  29. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/request_options.py +0 -0
  30. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/__init__.py +0 -0
  31. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/base.py +0 -0
  32. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/client.py +0 -0
  33. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/collection.py +0 -0
  34. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/embedding.py +0 -0
  35. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/exceptions.py +0 -0
  36. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/index.py +0 -0
  37. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/models/__init__.py +0 -0
  38. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/models/base.py +0 -0
  39. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/models/collection.py +0 -0
  40. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/models/embedding.py +0 -0
  41. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/models/index.py +0 -0
  42. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/models/rerank.py +0 -0
  43. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb/vector/rerank.py +0 -0
  44. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb_python_sdk.egg-info/SOURCES.txt +0 -0
  45. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb_python_sdk.egg-info/dependency_links.txt +0 -0
  46. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb_python_sdk.egg-info/requires.txt +0 -0
  47. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.13}/vikingdb_python_sdk.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: vikingdb-python-sdk
3
- Version: 0.1.12
3
+ Version: 0.1.13
4
4
  Summary: vikingdb Python SDK
5
5
  License: Apache-2.0
6
6
  Project-URL: Documentation, https://github.com/volcengine/volc-vikingdb-python-sdk/README.md
@@ -33,6 +33,7 @@ This package provides an idiomatic Python interface to the VikingDB v2 data-plan
33
33
  - Simple client configuration with AK/SK signing (Volcano Engine V4) or API-key authentication.
34
34
  - **Vector Database**: Request envelope handling with typed request/response models covering collection, index, and embedding workflows.
35
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.
36
37
  - Pluggable retry strategy (exponential backoff with jitter) and per-request overrides (`RequestOptions`).
37
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.
38
39
 
@@ -113,6 +114,40 @@ result = collection.search_memory(
113
114
  print("search results:", result)
114
115
  ```
115
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
+
116
151
  ### Example Guides
117
152
 
118
153
  #### Vector Examples
@@ -178,6 +213,40 @@ python examples/memory/02_add_session.py
178
213
  python examples/memory/03_search_memory.py
179
214
  ```
180
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
+
181
250
  ### Architecture Overview
182
251
 
183
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).
@@ -185,6 +254,7 @@ python examples/memory/03_search_memory.py
185
254
  - Vector request/response models now surface directly from `vikingdb.vector` (backed internally by `vikingdb/vector/models`).
186
255
  - Memory APIs return plain dictionaries without object encapsulation, providing a lightweight interface for conversational memory management (session, profile, event operations).
187
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.
188
258
 
189
259
  ### Project Structure
190
260
 
@@ -210,6 +280,12 @@ vikingdb/
210
280
  │ ├── collection.py # Memory collection operations
211
281
  │ ├── types.py # Type definitions for memory operations
212
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)
213
289
 
214
290
  examples/
215
291
  ├── vector/ # Vector integration guides (pytest)
@@ -221,6 +297,11 @@ examples/
221
297
  ├── 01_init_client_and_collection.py
222
298
  ├── 02_add_session.py
223
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
224
305
  ```
225
306
 
226
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
@@ -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)
@@ -63,12 +63,6 @@ class KnowledgeCollection:
63
63
  if isinstance(request, Model)
64
64
  else dict(request)
65
65
  )
66
- if request.file_path:
67
-
68
-
69
-
70
- pass
71
-
72
66
  payload = {**self._meta_payload, **req_payload}
73
67
  res = self.client.json_exception("AddDocV2", {}, json.dumps(payload), headers=headers, timeout=timeout)
74
68
  response = AddDocResponse.parse_with(res)
@@ -267,36 +261,3 @@ class KnowledgeCollection:
267
261
  res = self.client.json_exception("SearchKnowledge", {}, json.dumps(payload), headers=headers, timeout=timeout)
268
262
  response = SearchKnowledgeResponse.parse_with(res)
269
263
  return response
270
-
271
- def _upload_file(self, file_path: str) -> str:
272
- with open(file_path, "rb") as f:
273
- res = self.client.json_exception("UploadFile", {}, f.read())
274
-
275
- # https://console.volcengine.com/api/top/vikingdb/cn-beijing/2025-04-01/GetKnowledgeBasePreSignedUrl?KnowledgeBaseId=kb-1e04481ba6a823c2&ProjectName=default
276
- # {"TosPaths": ["2026_02_05_21_40_30rvbjCrtJycnAGsOFvXrphLN7F7VRsP2e/核心流程.md"]}
277
- """
278
- {
279
- "Result": {
280
- "PreSignedUrls": [
281
- "https://knowledgebase-fileupload.tos-cn-beijing.volces.com/ADD_DOC/2100655893/2026_02_05_21_40_30rvbjCrtJycnAGsOFvXrphLN7F7VRsP2e/%E6%A0%B8%E5%BF%83%E6%B5%81%E7%A8%8B.md?X-Tos-Algorithm=TOS4-HMAC-SHA256&X-Tos-Content-Sha256=UNSIGNED-PAYLOAD&X-Tos-Credential=AKLTOTg3ODc0NTAyYzU1NDIxNmJmODg1MWUyYTkwZDY3ZGM%2F20260205%2Ftos-cn-beijing.volces.com%2Ftos%2Frequest&X-Tos-Date=20260205T134045Z&X-Tos-Expires=300&X-Tos-SignedHeaders=host&X-Tos-Signature=712e8572edc6d251bf234cdb580246714c1ac9414e824ac1fe110772a6372fbc"
282
- ],
283
- "BucketName": "knowledgebase-fileupload"
284
- },
285
- "ResponseMetadata": {
286
- "RequestId": "202602052140452A8D3265DCDE1911D17B",
287
- "Action": "GetKnowledgeBasePreSignedUrl",
288
- "Version": "2025-04-01",
289
- "Service": "vikingdb",
290
- "Region": "cn-beijing"
291
- }
292
- }
293
- """
294
-
295
- """
296
- https://knowledgebase-fileupload.tos-cn-beijing.volces.com/ADD_DOC/2100655893/2026_02_05_21_40_30rvbjCrtJycnAGsOFvXrphLN7F7VRsP2e/%E6%A0%B8%E5%BF%83%E6%B5%81%E7%A8%8B.md?X-Tos-Algorithm=TOS4-HMAC-SHA256&X-Tos-Content-Sha256=UNSIGNED-PAYLOAD&X-Tos-Credential=AKLTOTg3ODc0NTAyYzU1NDIxNmJmODg1MWUyYTkwZDY3ZGM%2F20260205%2Ftos-cn-beijing.volces.com%2Ftos%2Frequest&X-Tos-Date=20260205T134045Z&X-Tos-Expires=300&X-Tos-SignedHeaders=host&X-Tos-Signature=712e8572edc6d251bf234cdb580246714c1ac9414e824ac1fe110772a6372fbc
297
- """
298
-
299
-
300
-
301
- return res["data"]["uri"]
302
-
@@ -41,7 +41,6 @@ class AddDocRequest(Model):
41
41
  doc_type: Optional[str] = Field(default=None, alias="doc_type")
42
42
  description: Optional[str] = Field(default=None, alias="description")
43
43
  tos_path: Optional[str] = Field(default=None, alias="tos_path")
44
- file_path: Optional[str] = Field(default=None, alias="file_path")
45
44
  url: Optional[str] = Field(default=None, alias="url")
46
45
  lark_file: Optional[str] = Field(default=None, alias="lark_file")
47
46
  meta: Optional[List["MetaItem"]] = Field(default=None, alias="meta")
@@ -64,7 +63,6 @@ class AddDocV2Request(Model):
64
63
  description: Optional[str] = Field(default=None, alias="description")
65
64
  tag_list: Optional[List[MetaItem]] = Field(default=None, alias="tag_list")
66
65
  uri: Optional[str] = Field(default=None, alias="uri")
67
- file_path: Optional[str] = Field(default=None, alias="file_path")
68
66
 
69
67
  class DedupInfo(Model):
70
68
  skip: Optional[bool] = Field(default=None, alias="skip")
@@ -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.12'
4
+ __version__ = '0.1.13'
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: vikingdb-python-sdk
3
- Version: 0.1.12
3
+ Version: 0.1.13
4
4
  Summary: vikingdb Python SDK
5
5
  License: Apache-2.0
6
6
  Project-URL: Documentation, https://github.com/volcengine/volc-vikingdb-python-sdk/README.md
@@ -33,6 +33,7 @@ This package provides an idiomatic Python interface to the VikingDB v2 data-plan
33
33
  - Simple client configuration with AK/SK signing (Volcano Engine V4) or API-key authentication.
34
34
  - **Vector Database**: Request envelope handling with typed request/response models covering collection, index, and embedding workflows.
35
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.
36
37
  - Pluggable retry strategy (exponential backoff with jitter) and per-request overrides (`RequestOptions`).
37
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.
38
39
 
@@ -113,6 +114,40 @@ result = collection.search_memory(
113
114
  print("search results:", result)
114
115
  ```
115
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
+
116
151
  ### Example Guides
117
152
 
118
153
  #### Vector Examples
@@ -178,6 +213,40 @@ python examples/memory/02_add_session.py
178
213
  python examples/memory/03_search_memory.py
179
214
  ```
180
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
+
181
250
  ### Architecture Overview
182
251
 
183
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).
@@ -185,6 +254,7 @@ python examples/memory/03_search_memory.py
185
254
  - Vector request/response models now surface directly from `vikingdb.vector` (backed internally by `vikingdb/vector/models`).
186
255
  - Memory APIs return plain dictionaries without object encapsulation, providing a lightweight interface for conversational memory management (session, profile, event operations).
187
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.
188
258
 
189
259
  ### Project Structure
190
260
 
@@ -210,6 +280,12 @@ vikingdb/
210
280
  │ ├── collection.py # Memory collection operations
211
281
  │ ├── types.py # Type definitions for memory operations
212
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)
213
289
 
214
290
  examples/
215
291
  ├── vector/ # Vector integration guides (pytest)
@@ -221,6 +297,11 @@ examples/
221
297
  ├── 01_init_client_and_collection.py
222
298
  ├── 02_add_session.py
223
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
224
305
  ```
225
306
 
226
307
  ### Contributing