vikingdb-python-sdk 0.1.12__tar.gz → 0.1.14__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.14}/PKG-INFO +84 -2
  2. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/README.md +81 -0
  3. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/__init__.py +7 -0
  4. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/_client.py +54 -5
  5. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/knowledge/client.py +30 -11
  6. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/knowledge/collection.py +0 -39
  7. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/knowledge/models/doc.py +0 -2
  8. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/vector/models/index.py +1 -0
  9. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/version.py +1 -1
  10. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb_python_sdk.egg-info/PKG-INFO +84 -2
  11. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/LICENSE.txt +0 -0
  12. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/pyproject.toml +0 -0
  13. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/setup.cfg +0 -0
  14. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/auth.py +0 -0
  15. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/exceptions.py +0 -0
  16. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/knowledge/__init__.py +0 -0
  17. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/knowledge/exceptions.py +0 -0
  18. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/knowledge/models/__init__.py +0 -0
  19. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/knowledge/models/base.py +0 -0
  20. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/knowledge/models/chat.py +0 -0
  21. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/knowledge/models/point.py +0 -0
  22. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/knowledge/models/rerank.py +0 -0
  23. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/knowledge/models/search.py +0 -0
  24. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/knowledge/models/service_chat.py +0 -0
  25. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/memory/__init__.py +0 -0
  26. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/memory/client.py +0 -0
  27. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/memory/collection.py +0 -0
  28. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/memory/exceptions.py +0 -0
  29. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/memory/types.py +0 -0
  30. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/request_options.py +0 -0
  31. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/vector/__init__.py +0 -0
  32. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/vector/base.py +0 -0
  33. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/vector/client.py +0 -0
  34. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/vector/collection.py +0 -0
  35. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/vector/embedding.py +0 -0
  36. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/vector/exceptions.py +0 -0
  37. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/vector/index.py +0 -0
  38. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/vector/models/__init__.py +0 -0
  39. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/vector/models/base.py +0 -0
  40. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/vector/models/collection.py +0 -0
  41. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/vector/models/embedding.py +0 -0
  42. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/vector/models/rerank.py +0 -0
  43. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb/vector/rerank.py +0 -0
  44. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb_python_sdk.egg-info/SOURCES.txt +0 -0
  45. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb_python_sdk.egg-info/dependency_links.txt +0 -0
  46. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb_python_sdk.egg-info/requires.txt +0 -0
  47. {vikingdb_python_sdk-0.1.12 → vikingdb_python_sdk-0.1.14}/vikingdb_python_sdk.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: vikingdb-python-sdk
3
- Version: 0.1.12
3
+ Version: 0.1.14
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
@@ -24,6 +24,7 @@ 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
27
28
 
28
29
  ## VikingDB Python SDK (v2)
29
30
 
@@ -33,6 +34,7 @@ This package provides an idiomatic Python interface to the VikingDB v2 data-plan
33
34
  - Simple client configuration with AK/SK signing (Volcano Engine V4) or API-key authentication.
34
35
  - **Vector Database**: Request envelope handling with typed request/response models covering collection, index, and embedding workflows.
35
36
  - **Memory Management**: Conversational memory APIs for managing user profiles, events, and session messages with semantic search capabilities.
37
+ - **Knowledge Base**: Document and point CRUD with typed models, hybrid retrieval (`search_collection`, `search_knowledge`), rerank, and chat-completion/service-chat orchestration.
36
38
  - Pluggable retry strategy (exponential backoff with jitter) and per-request overrides (`RequestOptions`).
37
39
  - 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
40
 
@@ -113,6 +115,40 @@ result = collection.search_memory(
113
115
  print("search results:", result)
114
116
  ```
115
117
 
118
+ #### Knowledge Base
119
+
120
+ ```python
121
+ import os
122
+ from vikingdb import IAM, APIKey
123
+ from vikingdb.knowledge import (
124
+ VikingKnowledge,
125
+ KnowledgeCollection,
126
+ SearchKnowledgeRequest,
127
+ )
128
+
129
+ # IAM auth for collection-level operations
130
+ client = VikingKnowledge(
131
+ auth=IAM(ak=os.getenv("VOLC_AK"), sk=os.getenv("VOLC_SK")),
132
+ host="api-knowledgebase.mlp.cn-beijing.volces.com",
133
+ region="cn-beijing",
134
+ scheme="http",
135
+ )
136
+
137
+ collection = client.collection(
138
+ resource_id=os.getenv("VIKING_COLLECTION_RID"),
139
+ collection_name=os.getenv("VIKING_COLLECTION_NAME") or "financial_reports",
140
+ project_name=os.getenv("VIKING_PROJECT") or "default",
141
+ )
142
+
143
+ resp = collection.search_knowledge(
144
+ SearchKnowledgeRequest(query="2025 Q1 revenue growth", limit=5, dense_weight=0.5)
145
+ )
146
+ print(f"request_id={resp.request_id} hits={len(resp.result.result_list or [])}")
147
+
148
+ # API-key auth for service-level chat
149
+ svc_client = VikingKnowledge(auth=APIKey(api_key=os.getenv("VIKING_SERVICE_API_KEY")))
150
+ ```
151
+
116
152
  ### Example Guides
117
153
 
118
154
  #### Vector Examples
@@ -178,6 +214,40 @@ python examples/memory/02_add_session.py
178
214
  python examples/memory/03_search_memory.py
179
215
  ```
180
216
 
217
+ #### Knowledge Examples
218
+
219
+ The examples under `examples/knowledge` cover core knowledge base workflows:
220
+
221
+ 1. **01_init_client.py**: Initialize `VikingKnowledge` and obtain collections by resource ID or by name + project.
222
+ 2. **02_doc_crud.py**: Add documents via URL or TOS path (`add_doc_v2`), get/update metadata, list documents.
223
+ 3. **03_point_crud.py**: Add/update/delete points (chunks) within a document; list and fetch detailed point info.
224
+ 4. **04_search.py**: Perform collection search and knowledge search, invoke rerank, and orchestrate chat-completion or service-chat (including streaming).
225
+
226
+ Environment variables:
227
+
228
+ ```
229
+ VOLC_AK=your-access-key
230
+ VOLC_SK=your-secret-key
231
+ VIKING_PROJECT=project-name
232
+ VIKING_COLLECTION_RID=collection-resource-id
233
+ VIKING_COLLECTION_NAME=collection-name
234
+ # For chat/service features:
235
+ VIKING_CHAT_API_KEY=chat-api-key
236
+ VIKING_SERVICE_API_KEY=service-api-key
237
+ VIKING_SERVICE_RID=service-resource-id
238
+ # Optional for rerank:
239
+ VIKING_RERANK_INSTRUCTION=custom-instruction
240
+ ```
241
+
242
+ Run individual examples:
243
+
244
+ ```bash
245
+ python examples/knowledge/01_init_client.py
246
+ python examples/knowledge/02_doc_crud.py
247
+ python examples/knowledge/03_point_crud.py
248
+ python examples/knowledge/04_search.py
249
+ ```
250
+
181
251
  ### Architecture Overview
182
252
 
183
253
  - `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 +255,7 @@ python examples/memory/03_search_memory.py
185
255
  - Vector request/response models now surface directly from `vikingdb.vector` (backed internally by `vikingdb/vector/models`).
186
256
  - Memory APIs return plain dictionaries without object encapsulation, providing a lightweight interface for conversational memory management (session, profile, event operations).
187
257
  - 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.
258
+ - 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
259
 
189
260
  ### Project Structure
190
261
 
@@ -210,6 +281,12 @@ vikingdb/
210
281
  │ ├── collection.py # Memory collection operations
211
282
  │ ├── types.py # Type definitions for memory operations
212
283
  │ └── exceptions.py # Memory-specific exceptions
284
+ ├── knowledge/ # Knowledge base clients and models
285
+ │ ├── __init__.py # High-level knowledge client and exports
286
+ │ ├── client.py # VikingKnowledge service client
287
+ │ ├── collection.py # Document/point/search operations
288
+ │ ├── exceptions.py # Knowledge-specific exceptions
289
+ │ └── models/ # Typed pydantic models (doc, point, search, chat, rerank)
213
290
 
214
291
  examples/
215
292
  ├── vector/ # Vector integration guides (pytest)
@@ -221,6 +298,11 @@ examples/
221
298
  ├── 01_init_client_and_collection.py
222
299
  ├── 02_add_session.py
223
300
  └── 03_search_memory.py
301
+ └── knowledge/ # Knowledge base usage examples
302
+ ├── 01_init_client.py
303
+ ├── 02_doc_crud.py
304
+ ├── 03_point_crud.py
305
+ └── 04_search.py
224
306
  ```
225
307
 
226
308
  ### 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
  ]
@@ -144,7 +144,7 @@ class Client(Service, ABC):
144
144
  response = self.session.post(
145
145
  url,
146
146
  headers=request.headers,
147
- data=request.body,
147
+ data=request.body.encode("utf-8"),
148
148
  timeout=request_timeout,
149
149
  )
150
150
  except Exception as exc:
@@ -154,9 +154,7 @@ class Client(Service, ABC):
154
154
  message=f"failed to run session.post {api}: {exc}",
155
155
  ) from exc
156
156
 
157
- payload_text_attr = getattr(response, "text", "")
158
- payload_text = payload_text_attr if isinstance(payload_text_attr, str) else ""
159
- payload_text = payload_text or ""
157
+ payload_text = response.content.decode("utf-8", errors="replace") if response.content else ""
160
158
 
161
159
  if response.status_code != 200:
162
160
  error = VikingAPIException.from_response(
@@ -167,7 +165,7 @@ class Client(Service, ABC):
167
165
  raise error
168
166
 
169
167
  try:
170
- return response.json()
168
+ return json.loads(response.content.decode("utf-8"))
171
169
  except (ValueError, JSONDecodeError) as exc:
172
170
  raise VikingAPIException(
173
171
  DEFAULT_UNKNOWN_ERROR_CODE,
@@ -260,3 +258,54 @@ class Client(Service, ABC):
260
258
  request_id=request_id,
261
259
  message=f"failed to run aiohttp {api}: {exc}",
262
260
  ) from exc
261
+
262
+
263
+ def _stream_json(self, api, params, body, headers=None, timeout=None):
264
+ if api not in self.api_info:
265
+ raise Exception("no such api")
266
+ api_info = self.api_info[api]
267
+ request = self.prepare_request(api_info, params)
268
+ if headers:
269
+ for key, value in headers.items():
270
+ request.headers[key] = value
271
+ request.headers["Content-Type"] = "application/json"
272
+ request.headers["Accept"] = "text/event-stream"
273
+ request.body = body
274
+ self.auth_provider.sign_request(request)
275
+ url = request.build()
276
+ request_id_value = request.headers.get(_REQUEST_ID_HEADER)
277
+ request_id = str(request_id_value) if request_id_value else "unknown"
278
+ if timeout is not None:
279
+ request_timeout = (timeout, timeout)
280
+ else:
281
+ request_timeout = (
282
+ self.service_info.connection_timeout,
283
+ self.service_info.socket_timeout,
284
+ )
285
+ response = self.session.post(
286
+ url,
287
+ headers=request.headers,
288
+ data=request.body.encode("utf-8"),
289
+ stream=True,
290
+ timeout=request_timeout,
291
+ )
292
+ if response.status_code != 200:
293
+ payload_text = response.content.decode("utf-8", errors="replace") if response.content else ""
294
+ raise VikingAPIException.from_response(
295
+ payload_text,
296
+ request_id=request_id,
297
+ status_code=response.status_code,
298
+ )
299
+ for line in response.iter_lines():
300
+ s = line.decode("utf-8")
301
+ if not s:
302
+ continue
303
+ if s.startswith("data:"):
304
+ data_str = s.split("data:", 1)[1]
305
+ data = json.loads(data_str)
306
+ yield data
307
+ try:
308
+ if "end" in data.get("data", {}):
309
+ break
310
+ except Exception:
311
+ 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")
@@ -20,6 +20,7 @@ class FetchDataInIndexRequest(Model):
20
20
  class IndexDataItem(DataItem):
21
21
  dense_dim: Optional[int] = Field(default=None, alias="dense_dim")
22
22
  dense_vector: Optional[List[float]] = Field(default=None, alias="dense_vector")
23
+ sparse_vector: Optional[Dict[str, float]] = Field(default=None, alias="sparse_vector")
23
24
 
24
25
 
25
26
  class FetchDataInIndexResult(Model):
@@ -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.14'
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: vikingdb-python-sdk
3
- Version: 0.1.12
3
+ Version: 0.1.14
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
@@ -24,6 +24,7 @@ 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
27
28
 
28
29
  ## VikingDB Python SDK (v2)
29
30
 
@@ -33,6 +34,7 @@ This package provides an idiomatic Python interface to the VikingDB v2 data-plan
33
34
  - Simple client configuration with AK/SK signing (Volcano Engine V4) or API-key authentication.
34
35
  - **Vector Database**: Request envelope handling with typed request/response models covering collection, index, and embedding workflows.
35
36
  - **Memory Management**: Conversational memory APIs for managing user profiles, events, and session messages with semantic search capabilities.
37
+ - **Knowledge Base**: Document and point CRUD with typed models, hybrid retrieval (`search_collection`, `search_knowledge`), rerank, and chat-completion/service-chat orchestration.
36
38
  - Pluggable retry strategy (exponential backoff with jitter) and per-request overrides (`RequestOptions`).
37
39
  - 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
40
 
@@ -113,6 +115,40 @@ result = collection.search_memory(
113
115
  print("search results:", result)
114
116
  ```
115
117
 
118
+ #### Knowledge Base
119
+
120
+ ```python
121
+ import os
122
+ from vikingdb import IAM, APIKey
123
+ from vikingdb.knowledge import (
124
+ VikingKnowledge,
125
+ KnowledgeCollection,
126
+ SearchKnowledgeRequest,
127
+ )
128
+
129
+ # IAM auth for collection-level operations
130
+ client = VikingKnowledge(
131
+ auth=IAM(ak=os.getenv("VOLC_AK"), sk=os.getenv("VOLC_SK")),
132
+ host="api-knowledgebase.mlp.cn-beijing.volces.com",
133
+ region="cn-beijing",
134
+ scheme="http",
135
+ )
136
+
137
+ collection = client.collection(
138
+ resource_id=os.getenv("VIKING_COLLECTION_RID"),
139
+ collection_name=os.getenv("VIKING_COLLECTION_NAME") or "financial_reports",
140
+ project_name=os.getenv("VIKING_PROJECT") or "default",
141
+ )
142
+
143
+ resp = collection.search_knowledge(
144
+ SearchKnowledgeRequest(query="2025 Q1 revenue growth", limit=5, dense_weight=0.5)
145
+ )
146
+ print(f"request_id={resp.request_id} hits={len(resp.result.result_list or [])}")
147
+
148
+ # API-key auth for service-level chat
149
+ svc_client = VikingKnowledge(auth=APIKey(api_key=os.getenv("VIKING_SERVICE_API_KEY")))
150
+ ```
151
+
116
152
  ### Example Guides
117
153
 
118
154
  #### Vector Examples
@@ -178,6 +214,40 @@ python examples/memory/02_add_session.py
178
214
  python examples/memory/03_search_memory.py
179
215
  ```
180
216
 
217
+ #### Knowledge Examples
218
+
219
+ The examples under `examples/knowledge` cover core knowledge base workflows:
220
+
221
+ 1. **01_init_client.py**: Initialize `VikingKnowledge` and obtain collections by resource ID or by name + project.
222
+ 2. **02_doc_crud.py**: Add documents via URL or TOS path (`add_doc_v2`), get/update metadata, list documents.
223
+ 3. **03_point_crud.py**: Add/update/delete points (chunks) within a document; list and fetch detailed point info.
224
+ 4. **04_search.py**: Perform collection search and knowledge search, invoke rerank, and orchestrate chat-completion or service-chat (including streaming).
225
+
226
+ Environment variables:
227
+
228
+ ```
229
+ VOLC_AK=your-access-key
230
+ VOLC_SK=your-secret-key
231
+ VIKING_PROJECT=project-name
232
+ VIKING_COLLECTION_RID=collection-resource-id
233
+ VIKING_COLLECTION_NAME=collection-name
234
+ # For chat/service features:
235
+ VIKING_CHAT_API_KEY=chat-api-key
236
+ VIKING_SERVICE_API_KEY=service-api-key
237
+ VIKING_SERVICE_RID=service-resource-id
238
+ # Optional for rerank:
239
+ VIKING_RERANK_INSTRUCTION=custom-instruction
240
+ ```
241
+
242
+ Run individual examples:
243
+
244
+ ```bash
245
+ python examples/knowledge/01_init_client.py
246
+ python examples/knowledge/02_doc_crud.py
247
+ python examples/knowledge/03_point_crud.py
248
+ python examples/knowledge/04_search.py
249
+ ```
250
+
181
251
  ### Architecture Overview
182
252
 
183
253
  - `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 +255,7 @@ python examples/memory/03_search_memory.py
185
255
  - Vector request/response models now surface directly from `vikingdb.vector` (backed internally by `vikingdb/vector/models`).
186
256
  - Memory APIs return plain dictionaries without object encapsulation, providing a lightweight interface for conversational memory management (session, profile, event operations).
187
257
  - 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.
258
+ - 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
259
 
189
260
  ### Project Structure
190
261
 
@@ -210,6 +281,12 @@ vikingdb/
210
281
  │ ├── collection.py # Memory collection operations
211
282
  │ ├── types.py # Type definitions for memory operations
212
283
  │ └── exceptions.py # Memory-specific exceptions
284
+ ├── knowledge/ # Knowledge base clients and models
285
+ │ ├── __init__.py # High-level knowledge client and exports
286
+ │ ├── client.py # VikingKnowledge service client
287
+ │ ├── collection.py # Document/point/search operations
288
+ │ ├── exceptions.py # Knowledge-specific exceptions
289
+ │ └── models/ # Typed pydantic models (doc, point, search, chat, rerank)
213
290
 
214
291
  examples/
215
292
  ├── vector/ # Vector integration guides (pytest)
@@ -221,6 +298,11 @@ examples/
221
298
  ├── 01_init_client_and_collection.py
222
299
  ├── 02_add_session.py
223
300
  └── 03_search_memory.py
301
+ └── knowledge/ # Knowledge base usage examples
302
+ ├── 01_init_client.py
303
+ ├── 02_doc_crud.py
304
+ ├── 03_point_crud.py
305
+ └── 04_search.py
224
306
  ```
225
307
 
226
308
  ### Contributing