knowledge2 0.4.0__py3-none-any.whl
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.
- knowledge2-0.4.0.dist-info/METADATA +556 -0
- knowledge2-0.4.0.dist-info/RECORD +139 -0
- knowledge2-0.4.0.dist-info/WHEEL +5 -0
- knowledge2-0.4.0.dist-info/top_level.txt +1 -0
- sdk/__init__.py +70 -0
- sdk/_async_base.py +525 -0
- sdk/_async_paging.py +57 -0
- sdk/_base.py +541 -0
- sdk/_logging.py +41 -0
- sdk/_paging.py +73 -0
- sdk/_preview.py +70 -0
- sdk/_raw_response.py +25 -0
- sdk/_request_options.py +51 -0
- sdk/_transport.py +144 -0
- sdk/_validation.py +25 -0
- sdk/_validation_response.py +36 -0
- sdk/_version.py +3 -0
- sdk/async_client.py +320 -0
- sdk/async_resources/__init__.py +45 -0
- sdk/async_resources/_mixin_base.py +42 -0
- sdk/async_resources/a2a.py +230 -0
- sdk/async_resources/agents.py +489 -0
- sdk/async_resources/audit.py +145 -0
- sdk/async_resources/auth.py +133 -0
- sdk/async_resources/console.py +409 -0
- sdk/async_resources/corpora.py +276 -0
- sdk/async_resources/deployments.py +106 -0
- sdk/async_resources/documents.py +592 -0
- sdk/async_resources/feeds.py +248 -0
- sdk/async_resources/indexes.py +208 -0
- sdk/async_resources/jobs.py +165 -0
- sdk/async_resources/metadata.py +48 -0
- sdk/async_resources/models.py +102 -0
- sdk/async_resources/onboarding.py +538 -0
- sdk/async_resources/orgs.py +37 -0
- sdk/async_resources/pipelines.py +523 -0
- sdk/async_resources/projects.py +90 -0
- sdk/async_resources/search.py +262 -0
- sdk/async_resources/training.py +357 -0
- sdk/async_resources/usage.py +91 -0
- sdk/client.py +417 -0
- sdk/config.py +182 -0
- sdk/errors.py +178 -0
- sdk/examples/auth_factory.py +34 -0
- sdk/examples/batch_operations.py +57 -0
- sdk/examples/document_upload.py +56 -0
- sdk/examples/e2e_lifecycle.py +213 -0
- sdk/examples/error_handling.py +61 -0
- sdk/examples/pagination.py +64 -0
- sdk/examples/quickstart.py +36 -0
- sdk/examples/request_options.py +44 -0
- sdk/examples/search.py +64 -0
- sdk/integrations/__init__.py +57 -0
- sdk/integrations/_client.py +101 -0
- sdk/integrations/langchain/__init__.py +6 -0
- sdk/integrations/langchain/retriever.py +166 -0
- sdk/integrations/langchain/tools.py +108 -0
- sdk/integrations/llamaindex/__init__.py +11 -0
- sdk/integrations/llamaindex/filters.py +78 -0
- sdk/integrations/llamaindex/retriever.py +162 -0
- sdk/integrations/llamaindex/tools.py +109 -0
- sdk/integrations/llamaindex/vector_store.py +320 -0
- sdk/models/__init__.py +18 -0
- sdk/models/_base.py +24 -0
- sdk/models/_registry.py +457 -0
- sdk/models/a2a.py +92 -0
- sdk/models/agents.py +109 -0
- sdk/models/audit.py +28 -0
- sdk/models/auth.py +49 -0
- sdk/models/chunks.py +20 -0
- sdk/models/common.py +14 -0
- sdk/models/console.py +103 -0
- sdk/models/corpora.py +48 -0
- sdk/models/deployments.py +13 -0
- sdk/models/documents.py +126 -0
- sdk/models/embeddings.py +24 -0
- sdk/models/evaluation.py +17 -0
- sdk/models/feedback.py +9 -0
- sdk/models/feeds.py +57 -0
- sdk/models/indexes.py +36 -0
- sdk/models/jobs.py +52 -0
- sdk/models/models.py +26 -0
- sdk/models/onboarding.py +323 -0
- sdk/models/orgs.py +11 -0
- sdk/models/pipelines.py +147 -0
- sdk/models/projects.py +19 -0
- sdk/models/search.py +149 -0
- sdk/models/training.py +57 -0
- sdk/models/usage.py +39 -0
- sdk/namespaces.py +386 -0
- sdk/py.typed +0 -0
- sdk/resources/__init__.py +45 -0
- sdk/resources/_mixin_base.py +40 -0
- sdk/resources/a2a.py +230 -0
- sdk/resources/agents.py +487 -0
- sdk/resources/audit.py +144 -0
- sdk/resources/auth.py +138 -0
- sdk/resources/console.py +411 -0
- sdk/resources/corpora.py +269 -0
- sdk/resources/deployments.py +105 -0
- sdk/resources/documents.py +597 -0
- sdk/resources/feeds.py +246 -0
- sdk/resources/indexes.py +210 -0
- sdk/resources/jobs.py +164 -0
- sdk/resources/metadata.py +53 -0
- sdk/resources/models.py +99 -0
- sdk/resources/onboarding.py +542 -0
- sdk/resources/orgs.py +35 -0
- sdk/resources/pipeline_builder.py +257 -0
- sdk/resources/pipelines.py +520 -0
- sdk/resources/projects.py +87 -0
- sdk/resources/search.py +277 -0
- sdk/resources/training.py +358 -0
- sdk/resources/usage.py +92 -0
- sdk/types/__init__.py +366 -0
- sdk/types/a2a.py +88 -0
- sdk/types/agents.py +133 -0
- sdk/types/audit.py +26 -0
- sdk/types/auth.py +45 -0
- sdk/types/chunks.py +18 -0
- sdk/types/common.py +10 -0
- sdk/types/console.py +99 -0
- sdk/types/corpora.py +42 -0
- sdk/types/deployments.py +11 -0
- sdk/types/documents.py +104 -0
- sdk/types/embeddings.py +22 -0
- sdk/types/evaluation.py +15 -0
- sdk/types/feedback.py +7 -0
- sdk/types/feeds.py +61 -0
- sdk/types/indexes.py +30 -0
- sdk/types/jobs.py +50 -0
- sdk/types/models.py +22 -0
- sdk/types/onboarding.py +395 -0
- sdk/types/orgs.py +9 -0
- sdk/types/pipelines.py +177 -0
- sdk/types/projects.py +14 -0
- sdk/types/search.py +116 -0
- sdk/types/training.py +55 -0
- sdk/types/usage.py +37 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"""Fluent builder for Pipeline Spec topologies."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from typing import Any, Literal
|
|
7
|
+
|
|
8
|
+
from sdk._preview import preview_resource
|
|
9
|
+
|
|
10
|
+
try:
|
|
11
|
+
from typing import Self
|
|
12
|
+
except ImportError:
|
|
13
|
+
from typing_extensions import Self
|
|
14
|
+
|
|
15
|
+
_UUID_RE = re.compile(
|
|
16
|
+
r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", re.IGNORECASE
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@preview_resource
|
|
21
|
+
class PipelineBuilder:
|
|
22
|
+
"""Build a Pipeline Spec topology using a fluent API.
|
|
23
|
+
|
|
24
|
+
Example::
|
|
25
|
+
|
|
26
|
+
topology = (
|
|
27
|
+
PipelineBuilder()
|
|
28
|
+
.add_corpus("my-corpus", description="Main corpus")
|
|
29
|
+
.add_agent("my-agent", "summarize", corpus_ref="my-corpus")
|
|
30
|
+
.add_feed("my-feed", query="latest news", agent_ref="my-agent")
|
|
31
|
+
.subscribe(agent_ref="my-agent", feed_ref="my-feed", role="input")
|
|
32
|
+
.build()
|
|
33
|
+
)
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def __init__(self) -> None:
|
|
37
|
+
self._corpora: list[dict[str, Any]] = []
|
|
38
|
+
self._agents: list[dict[str, Any]] = []
|
|
39
|
+
self._feeds: list[dict[str, Any]] = []
|
|
40
|
+
self._subscriptions: list[dict[str, Any]] = []
|
|
41
|
+
self._metadata: dict[str, Any] | None = None
|
|
42
|
+
|
|
43
|
+
def add_corpus(
|
|
44
|
+
self, name: str, *, id: str | None = None, description: str | None = None
|
|
45
|
+
) -> Self:
|
|
46
|
+
"""Add a corpus node to the topology.
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
name: Name for the corpus.
|
|
50
|
+
id: Optional existing corpus ID to reference.
|
|
51
|
+
description: Optional description.
|
|
52
|
+
|
|
53
|
+
Returns:
|
|
54
|
+
Self for method chaining.
|
|
55
|
+
"""
|
|
56
|
+
node: dict[str, Any] = {"name": name}
|
|
57
|
+
if id is not None:
|
|
58
|
+
node["id"] = id
|
|
59
|
+
if description is not None:
|
|
60
|
+
node["description"] = description
|
|
61
|
+
self._corpora.append(node)
|
|
62
|
+
return self
|
|
63
|
+
|
|
64
|
+
def add_agent(
|
|
65
|
+
self,
|
|
66
|
+
name: str,
|
|
67
|
+
task_type: str,
|
|
68
|
+
*,
|
|
69
|
+
corpus_ref: str | None = None,
|
|
70
|
+
id: str | None = None,
|
|
71
|
+
description: str | None = None,
|
|
72
|
+
instructions: str | None = None,
|
|
73
|
+
model: str | None = None,
|
|
74
|
+
tool_config: dict[str, Any] | None = None,
|
|
75
|
+
) -> Self:
|
|
76
|
+
"""Add an agent node to the topology.
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
name: Name for the agent.
|
|
80
|
+
task_type: Task type for the agent (required).
|
|
81
|
+
corpus_ref: Optional reference to a corpus (name or existing ID).
|
|
82
|
+
id: Optional existing agent ID to reference.
|
|
83
|
+
description: Optional description.
|
|
84
|
+
instructions: Optional instructions for the agent.
|
|
85
|
+
model: Optional model identifier.
|
|
86
|
+
tool_config: Optional tool configuration dict.
|
|
87
|
+
|
|
88
|
+
Returns:
|
|
89
|
+
Self for method chaining.
|
|
90
|
+
"""
|
|
91
|
+
node: dict[str, Any] = {"name": name, "task_type": task_type}
|
|
92
|
+
if corpus_ref is not None:
|
|
93
|
+
node["corpus_ref"] = corpus_ref
|
|
94
|
+
if id is not None:
|
|
95
|
+
node["id"] = id
|
|
96
|
+
if description is not None:
|
|
97
|
+
node["description"] = description
|
|
98
|
+
if instructions is not None:
|
|
99
|
+
node["instructions"] = instructions
|
|
100
|
+
if model is not None:
|
|
101
|
+
node["model"] = model
|
|
102
|
+
if tool_config is not None:
|
|
103
|
+
node["tool_config"] = tool_config
|
|
104
|
+
self._agents.append(node)
|
|
105
|
+
return self
|
|
106
|
+
|
|
107
|
+
def add_feed(
|
|
108
|
+
self,
|
|
109
|
+
name: str,
|
|
110
|
+
*,
|
|
111
|
+
query: str | None = None,
|
|
112
|
+
agent_ref: str | None = None,
|
|
113
|
+
id: str | None = None,
|
|
114
|
+
persistent: bool = False,
|
|
115
|
+
target_corpus_ref: str | None = None,
|
|
116
|
+
reactive: bool = False,
|
|
117
|
+
schedule_interval: str | None = None,
|
|
118
|
+
) -> Self:
|
|
119
|
+
"""Add a feed node to the topology.
|
|
120
|
+
|
|
121
|
+
Args:
|
|
122
|
+
name: Name for the feed.
|
|
123
|
+
query: Optional query string for the feed.
|
|
124
|
+
agent_ref: Optional reference to an agent (name or existing ID).
|
|
125
|
+
id: Optional existing feed ID to reference.
|
|
126
|
+
persistent: Whether the feed persists results.
|
|
127
|
+
target_corpus_ref: Optional reference to a target corpus.
|
|
128
|
+
reactive: Whether the feed is reactive.
|
|
129
|
+
schedule_interval: Optional schedule interval.
|
|
130
|
+
|
|
131
|
+
Returns:
|
|
132
|
+
Self for method chaining.
|
|
133
|
+
"""
|
|
134
|
+
node: dict[str, Any] = {"name": name}
|
|
135
|
+
if query is not None:
|
|
136
|
+
node["query"] = query
|
|
137
|
+
if agent_ref is not None:
|
|
138
|
+
node["agent_ref"] = agent_ref
|
|
139
|
+
node["persistent"] = persistent
|
|
140
|
+
node["reactive"] = reactive
|
|
141
|
+
if id is not None:
|
|
142
|
+
node["id"] = id
|
|
143
|
+
if target_corpus_ref is not None:
|
|
144
|
+
node["target_corpus_ref"] = target_corpus_ref
|
|
145
|
+
if schedule_interval is not None:
|
|
146
|
+
node["schedule_interval"] = schedule_interval
|
|
147
|
+
self._feeds.append(node)
|
|
148
|
+
return self
|
|
149
|
+
|
|
150
|
+
def subscribe(self, agent_ref: str, feed_ref: str, role: Literal["input", "output"]) -> Self:
|
|
151
|
+
"""Add a subscription linking an agent and a feed.
|
|
152
|
+
|
|
153
|
+
Args:
|
|
154
|
+
agent_ref: Reference to an agent (name or existing ID).
|
|
155
|
+
feed_ref: Reference to a feed (name or existing ID).
|
|
156
|
+
role: The role of the subscription (``"input"`` or ``"output"``).
|
|
157
|
+
|
|
158
|
+
Returns:
|
|
159
|
+
Self for method chaining.
|
|
160
|
+
"""
|
|
161
|
+
self._subscriptions.append({"agent_ref": agent_ref, "feed_ref": feed_ref, "role": role})
|
|
162
|
+
return self
|
|
163
|
+
|
|
164
|
+
def set_metadata(self, metadata: dict[str, Any]) -> Self:
|
|
165
|
+
"""Set arbitrary metadata on the topology.
|
|
166
|
+
|
|
167
|
+
Args:
|
|
168
|
+
metadata: Metadata dictionary.
|
|
169
|
+
|
|
170
|
+
Returns:
|
|
171
|
+
Self for method chaining.
|
|
172
|
+
"""
|
|
173
|
+
self._metadata = metadata
|
|
174
|
+
return self
|
|
175
|
+
|
|
176
|
+
def _is_existing_id(self, ref: str) -> bool:
|
|
177
|
+
return bool(_UUID_RE.match(ref))
|
|
178
|
+
|
|
179
|
+
def _validate_ref(self, ref: str, field_name: str, allowed: set[str]) -> None:
|
|
180
|
+
if ref not in allowed and not self._is_existing_id(ref):
|
|
181
|
+
raise ValueError(
|
|
182
|
+
f"{field_name} '{ref}' does not match any defined node name "
|
|
183
|
+
f"and is not a valid existing entity ID (UUID)"
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
def build(self) -> dict[str, Any]:
|
|
187
|
+
"""Build and return a TopologyDocument dict.
|
|
188
|
+
|
|
189
|
+
Validates that all refs resolve to defined node names of the
|
|
190
|
+
correct entity type or existing entity IDs (UUIDs).
|
|
191
|
+
|
|
192
|
+
Raises:
|
|
193
|
+
ValueError: If topology is empty or has unresolved refs.
|
|
194
|
+
"""
|
|
195
|
+
if not self._corpora and not self._agents and not self._feeds:
|
|
196
|
+
raise ValueError("Topology must contain at least one entity")
|
|
197
|
+
|
|
198
|
+
corpus_names: set[str] = {c["name"] for c in self._corpora}
|
|
199
|
+
agent_names: set[str] = {a["name"] for a in self._agents}
|
|
200
|
+
feed_names: set[str] = {f["name"] for f in self._feeds}
|
|
201
|
+
|
|
202
|
+
# Validate agent corpus_ref (must reference a corpus)
|
|
203
|
+
for agent in self._agents:
|
|
204
|
+
if "corpus_ref" in agent:
|
|
205
|
+
self._validate_ref(agent["corpus_ref"], "corpus_ref", corpus_names)
|
|
206
|
+
|
|
207
|
+
# Validate feed agent_ref (must reference an agent) and target_corpus_ref (must reference a corpus)
|
|
208
|
+
for feed in self._feeds:
|
|
209
|
+
if "agent_ref" in feed:
|
|
210
|
+
self._validate_ref(feed["agent_ref"], "agent_ref", agent_names)
|
|
211
|
+
if "target_corpus_ref" in feed:
|
|
212
|
+
self._validate_ref(feed["target_corpus_ref"], "target_corpus_ref", corpus_names)
|
|
213
|
+
|
|
214
|
+
# Validate subscription refs (agent_ref → agent, feed_ref → feed)
|
|
215
|
+
for sub in self._subscriptions:
|
|
216
|
+
self._validate_ref(sub["agent_ref"], "agent_ref", agent_names)
|
|
217
|
+
self._validate_ref(sub["feed_ref"], "feed_ref", feed_names)
|
|
218
|
+
|
|
219
|
+
doc: dict[str, Any] = {
|
|
220
|
+
"corpora": list(self._corpora),
|
|
221
|
+
"agents": list(self._agents),
|
|
222
|
+
"feeds": list(self._feeds),
|
|
223
|
+
"subscriptions": list(self._subscriptions),
|
|
224
|
+
}
|
|
225
|
+
if self._metadata is not None:
|
|
226
|
+
doc["metadata"] = self._metadata
|
|
227
|
+
return doc
|
|
228
|
+
|
|
229
|
+
def create_and_apply(
|
|
230
|
+
self,
|
|
231
|
+
client: Any,
|
|
232
|
+
project_id: str,
|
|
233
|
+
name: str,
|
|
234
|
+
*,
|
|
235
|
+
description: str | None = None,
|
|
236
|
+
activate_entities: bool = True,
|
|
237
|
+
) -> dict[str, Any]:
|
|
238
|
+
"""Build topology, create a pipeline spec, and apply it.
|
|
239
|
+
|
|
240
|
+
Args:
|
|
241
|
+
client: A Knowledge2 client instance (sync).
|
|
242
|
+
project_id: Project to create the pipeline spec in.
|
|
243
|
+
name: Name for the pipeline spec.
|
|
244
|
+
description: Optional description.
|
|
245
|
+
activate_entities: Whether to activate created entities on apply.
|
|
246
|
+
|
|
247
|
+
Returns:
|
|
248
|
+
The ApplyResult dict from the apply call.
|
|
249
|
+
"""
|
|
250
|
+
topology = self.build()
|
|
251
|
+
spec = client.create_pipeline_spec(
|
|
252
|
+
project_id=project_id,
|
|
253
|
+
name=name,
|
|
254
|
+
topology=topology,
|
|
255
|
+
description=description,
|
|
256
|
+
)
|
|
257
|
+
return client.apply_pipeline_spec(spec["id"], activate_entities=activate_entities)
|