-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknowledge_retrieval.py
More file actions
131 lines (112 loc) · 4.41 KB
/
knowledge_retrieval.py
File metadata and controls
131 lines (112 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""Example: Enterprise Knowledge Retrieval.
This example demonstrates how SIP processes an intent to retrieve the latest
approved architecture document and summarize its main design decisions.
Run with:
python examples/knowledge_retrieval.py
"""
from __future__ import annotations
import json
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from sip.broker.service import BrokerService
from sip.envelope.models import (
ActorDescriptor,
ActorType,
BindingType,
DesiredOutcome,
IntentEnvelope,
IntentPayload,
OperationClass,
ProtocolBinding,
TargetDescriptor,
TargetType,
TrustLevel,
)
from sip.registry.bootstrap import seed_registry
from sip.registry.service import CapabilityRegistryService
from sip.translator.rag_adapter import RagAdapter
from sip.translator.rest_adapter import RestAdapter
def main() -> None:
# --- Setup: build a seeded registry and broker ---
registry = CapabilityRegistryService()
seed_registry(registry)
broker = BrokerService(registry=registry)
print("=" * 70)
print("SIP Example: Enterprise Knowledge Retrieval")
print("=" * 70)
# --- Build the intent envelope ---
envelope = IntentEnvelope(
actor=ActorDescriptor(
actor_id="enterprise-portal",
actor_type=ActorType.SERVICE,
name="Enterprise Portal",
trust_level=TrustLevel.INTERNAL,
scopes=["sip:knowledge:read"],
),
target=TargetDescriptor(target_type=TargetType.CAPABILITY),
intent=IntentPayload(
intent_name="retrieve_document",
intent_domain="knowledge_management",
operation_class=OperationClass.RETRIEVE,
natural_language_hint=(
"Get the latest approved architecture document for private "
"service exchange and summarize the main design decisions."
),
parameters={
"query": "latest approved architecture document",
"collection": "architecture",
"output_format": "markdown",
},
),
desired_outcome=DesiredOutcome(
summary="Retrieve the architecture document and identify design decisions.",
output_format="markdown",
),
protocol_bindings=[
ProtocolBinding(binding_type=BindingType.RAG),
ProtocolBinding(binding_type=BindingType.REST),
],
)
print(f"\n[Intent]")
print(f" ID: {envelope.intent_id}")
print(f" Name: {envelope.intent.intent_name}")
print(f" Domain: {envelope.intent.intent_domain}")
print(f" Hint: {envelope.intent.natural_language_hint}")
# --- Process through the broker ---
result, translation = broker.translate(envelope)
print(f"\n[Negotiation]")
if result.negotiation_result:
nr = result.negotiation_result
if nr.selected_capability:
print(f" Selected: {nr.selected_capability.capability_id}")
print(f" Binding: {nr.selected_binding}")
print(f" Rationale: {nr.selection_rationale}")
print(f" Candidates: {[c.capability.capability_id for c in nr.ranked_candidates[:3]]}")
print(f"\n[Policy]")
if result.negotiation_result:
pd = result.negotiation_result.policy_decision
print(f" Allowed: {pd.allowed}")
print(f" Requires Approval: {pd.requires_approval}")
for note in pd.policy_notes:
print(f" Note: {note}")
print(f"\n[Execution Plan]")
if result.execution_plan:
plan = result.execution_plan
print(f" Plan ID: {plan.plan_id}")
print(f" Capability: {plan.selected_capability.capability_id}")
print(f" Binding: {plan.selected_binding}")
print(f" Approval: {plan.approval_required}")
print(f" Parameters: {json.dumps(plan.grounded_parameters, indent=4)}")
print(f"\n[Translation ({result.execution_plan.selected_binding if result.execution_plan else 'N/A'})]")
if translation:
print(json.dumps(translation.payload, indent=2))
print(f"\n[Audit Record]")
ar = result.audit_record
print(f" Outcome: {ar.outcome_summary}")
print(f" Action: {ar.action_taken}")
print(f" Policy: {'allowed' if ar.policy_allowed else 'denied'}")
print(f" Trace ID: {ar.trace_id}")
print()
if __name__ == "__main__":
main()