Repository navigation
Expand file tree
/
Copy pathbase.py
More file actions
248 lines (208 loc) · 6.85 KB
/
Copy pathbase.py
File metadata and controls
248 lines (208 loc) · 6.85 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import asyncio
import logging
from abc import ABC, abstractmethod
from enum import Enum
from ifixai.core.types import (
AuditRecord,
ChatMessage,
ConfigurationVersion,
GroundingReport,
OverrideReceipt,
Permission,
ProviderCapabilities,
ProviderConfig,
RetrievedSource,
Role,
RoutingDecision,
ToolInfo,
ToolInvocationResult,
)
class ProviderCapability(str, Enum):
TOOL_CALLING = "tool_calling"
RETRIEVAL = "retrieval"
AUDIT_TRAIL = "audit_trail"
ROUTING = "routing"
GROUNDING = "grounding"
AUTHORIZATION = "authorization"
GOVERNANCE_ARCHITECTURE = "governance_architecture"
OVERRIDE_MECHANISM = "override_mechanism"
RATE_LIMIT_OBSERVABILITY = "rate_limit_observability"
CONFIGURATION_VERSIONING = "configuration_versioning"
_logger = logging.getLogger(__name__)
_CAPABILITY_INSPECTION_EXPECTED_ERRORS: tuple[type[BaseException], ...] = (
NotImplementedError,
AttributeError,
ConnectionError,
OSError,
asyncio.TimeoutError,
ValueError,
TypeError,
RuntimeError,
)
class ProviderError(Exception):
def __init__(
self,
provider: str = "",
endpoint: str = "",
details: str = "",
) -> None:
self.provider = provider
self.endpoint = endpoint
self.details = details
super().__init__(
f"[{provider}] {details} (endpoint: {endpoint})"
)
class ProviderConnectionError(ProviderError):
pass
class ProviderAuthError(ProviderError):
pass
class ProviderRateLimitError(ProviderError):
pass
class ProviderTimeoutError(ProviderError):
pass
class ProviderResponseError(ProviderError):
pass
class ChatProvider(ABC):
surfaces_rate_limit_errors: bool = True
@abstractmethod
async def send_message(
self,
messages: list[ChatMessage],
config: ProviderConfig,
) -> str:
...
async def list_tools(
self,
config: ProviderConfig,
) -> list[ToolInfo] | None:
return None
async def invoke_tool(
self,
tool_id: str,
user_role: str,
params: dict,
config: ProviderConfig,
) -> ToolInvocationResult | None:
return None
async def retrieve_sources(
self,
query: str,
config: ProviderConfig,
) -> list[RetrievedSource] | None:
return None
async def get_audit_trail(
self,
request_id: str,
config: ProviderConfig,
) -> list[AuditRecord] | None:
return None
async def get_routing_decision(
self,
config: ProviderConfig,
) -> RoutingDecision | None:
return None
async def get_grounding_report(
self,
config: ProviderConfig,
) -> GroundingReport | None:
return None
async def authorize_tool(
self,
tool_id: str,
user_role: str,
config: ProviderConfig,
) -> ToolInvocationResult | None:
return None
async def get_governance_architecture(
self,
config: ProviderConfig,
) -> dict | None:
return None
async def apply_override(
self,
request_id: str,
config: ProviderConfig,
intent: str | None = None,
) -> OverrideReceipt | None:
return None
async def get_configuration_version(
self,
config: ProviderConfig,
) -> ConfigurationVersion | None:
return None
async def get_roles(
self,
config: ProviderConfig,
) -> list[Role] | None:
return None
async def get_permission_matrix(
self,
config: ProviderConfig,
) -> list[Permission] | None:
return None
async def detect_capabilities(
provider: ChatProvider,
config: ProviderConfig,
) -> ProviderCapabilities:
caps = {
"has_tool_calling": False,
"has_retrieval": False,
"has_audit_trail": False,
"has_routing": False,
"has_grounding": False,
"has_authorization": False,
"has_governance_architecture": False,
"has_override_mechanism": False,
"has_rate_limit_observability": False,
"has_configuration_versioning": False,
}
provider_name = type(provider).__name__
try:
result = await provider.list_tools(config)
caps["has_tool_calling"] = result is not None
except _CAPABILITY_INSPECTION_EXPECTED_ERRORS:
_logger.exception("Capability inspection list_tools failed for %s", provider_name)
try:
result = await provider.retrieve_sources("test", config)
caps["has_retrieval"] = result is not None
except _CAPABILITY_INSPECTION_EXPECTED_ERRORS:
_logger.exception("Capability inspection retrieve_sources failed for %s", provider_name)
try:
result = await provider.get_audit_trail("test", config)
caps["has_audit_trail"] = result is not None
except _CAPABILITY_INSPECTION_EXPECTED_ERRORS:
_logger.exception("Capability inspection get_audit_trail failed for %s", provider_name)
try:
result = await provider.get_routing_decision(config)
caps["has_routing"] = result is not None
except _CAPABILITY_INSPECTION_EXPECTED_ERRORS:
_logger.exception("Capability inspection get_routing_decision failed for %s", provider_name)
try:
result = await provider.get_grounding_report(config)
caps["has_grounding"] = result is not None
except _CAPABILITY_INSPECTION_EXPECTED_ERRORS:
_logger.exception("Capability inspection get_grounding_report failed for %s", provider_name)
try:
result = await provider.authorize_tool("_test", "_test", config)
caps["has_authorization"] = result is not None
except _CAPABILITY_INSPECTION_EXPECTED_ERRORS:
_logger.exception("Capability inspection authorize_tool failed for %s", provider_name)
try:
result = await provider.get_governance_architecture(config)
caps["has_governance_architecture"] = result is not None
except _CAPABILITY_INSPECTION_EXPECTED_ERRORS:
_logger.exception("Capability inspection get_governance_architecture failed for %s", provider_name)
try:
result = await provider.apply_override("_capability_inspection", config)
caps["has_override_mechanism"] = result is not None
except _CAPABILITY_INSPECTION_EXPECTED_ERRORS:
_logger.exception("Capability inspection apply_override failed for %s", provider_name)
try:
result = await provider.get_configuration_version(config)
caps["has_configuration_versioning"] = result is not None
except _CAPABILITY_INSPECTION_EXPECTED_ERRORS:
_logger.exception("Capability inspection get_configuration_version failed for %s", provider_name)
caps["has_rate_limit_observability"] = bool(
getattr(provider, "surfaces_rate_limit_errors", False)
)
return ProviderCapabilities(**caps)