API Reference
This page provides the auto-generated API reference for the MiniLLM library, created directly from the source code's docstrings.
Core Components
safeagent.config
safeagent.governance
DataGovernanceError
Bases: Exception
Exception raised when governance policies are violated.
Source code in src/safeagent/governance.py
30 31 32 |
|
GovernanceManager
Manages data governance policies, including encryption, auditing, retention policies, and run ID management.
Source code in src/safeagent/governance.py
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 |
|
audit(user_id, action, resource, metadata=None)
Write an audit log entry for data actions, including the current run_id.
Source code in src/safeagent/governance.py
68 69 70 71 72 73 74 75 76 77 78 79 |
|
decrypt(token)
Decrypt sensitive data when needed.
Source code in src/safeagent/governance.py
64 65 66 |
|
encrypt(plaintext)
Encrypt sensitive data before storage.
Source code in src/safeagent/governance.py
60 61 62 |
|
get_current_run_id()
Returns the ID for the current run, creating one if it doesn't exist.
Source code in src/safeagent/governance.py
54 55 56 57 58 |
|
purge_old_logs()
Purge audit log entries older than retention period.
Source code in src/safeagent/governance.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
start_new_run()
Generates a new unique ID for a single, complete run of an orchestrator.
Source code in src/safeagent/governance.py
49 50 51 52 |
|
tag_lineage(record, source)
Attach lineage metadata to a record.
Source code in src/safeagent/governance.py
81 82 83 84 85 86 87 88 89 |
|
safeagent.llm_client
FrameworkError
Bases: Exception
Custom exception for framework-related errors.
Source code in src/safeagent/llm_client.py
13 14 15 |
|
LLMClient
Thin wrapper around any LLM provider with retries, error handling, and structured JSON logging.
Source code in src/safeagent/llm_client.py
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 |
|
__init__(provider, api_key, model, base_url=None)
Initialize the LLM client.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
provider
|
str
|
Name of the provider (e.g., 'openai', 'anthropic'). |
required |
api_key
|
str
|
API key or token for authentication. |
required |
model
|
str
|
Model identifier (e.g., 'gpt-4', 'claude-3-opus'). |
required |
base_url
|
str
|
Custom endpoint URL; defaults to provider-specific default. |
None
|
Source code in src/safeagent/llm_client.py
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 |
|
generate(prompt, max_tokens=512, temperature=0.7)
Call the underlying LLM API, with up to 3 retries.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
str
|
The textual prompt to send to the model. |
required |
max_tokens
|
int
|
Maximum number of tokens in the response. |
512
|
temperature
|
float
|
Sampling temperature. |
0.7
|
Returns:
Name | Type | Description |
---|---|---|
Dict |
Dict
|
A dictionary containing keys 'text', 'usage', and 'metadata'. |
Raises:
Type | Description |
---|---|
FrameworkError
|
If the API fails after retries. |
Source code in src/safeagent/llm_client.py
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 |
|
safeagent.memory_manager
MemoryManager
Minimal key-value memory store. Supports 'inmemory' or 'redis' backends and logs each read/write. Optionally, can summarize entire memory via an LLM.
Source code in src/safeagent/memory_manager.py
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 |
|
__init__(backend='inmemory', redis_url=None)
backend: "inmemory" (default) or "redis". redis_url: e.g., "redis://localhost:6379" if backend="redis".
Source code in src/safeagent/memory_manager.py
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 |
|
load(user_id, key)
Loads value for (user_id, key). Returns empty string if missing.
Source code in src/safeagent/memory_manager.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
|
save(user_id, key, value)
Saves value under (user_id, key).
Source code in src/safeagent/memory_manager.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
summarize(user_id, embed_fn, llm_client, max_tokens=256)
Reads all entries for user_id, concatenates them, and calls LLM to generate a summary. Stores the summary under key="summary" and returns it.
Source code in src/safeagent/memory_manager.py
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 |
|
safeagent.prompt_renderer
PromptRenderer
Jinja2-based templating engine with structured logging and lineage tagging.
Source code in src/safeagent/prompt_renderer.py
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 |
|
__init__(template_dir)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template_dir
|
Path
|
Path to the directory containing Jinja2 templates. |
required |
Source code in src/safeagent/prompt_renderer.py
45 46 47 48 49 50 51 52 53 54 |
|
render(template_name, **context)
Render a Jinja2 template with provided context, logging the event and tagging lineage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template_name
|
str
|
Filename of the template (e.g., 'qa_prompt.j2'). |
required |
**context
|
Key-value pairs to pass into the template rendering. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The rendered template as a string. |
Source code in src/safeagent/prompt_renderer.py
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 |
|
safeagent.embeddings
EmbeddingError
Bases: Exception
Custom exception for embedding-related failures.
Source code in src/safeagent/embeddings.py
20 21 22 |
|
gemini_embed(text, api_key, model='embedding-001')
Generates embeddings using the Google Gemini API.
This function now correctly formats the request for the embedding model, passing the API key as a URL parameter and avoiding conflicting headers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text to embed. |
required |
api_key
|
str
|
The Google API key. |
required |
model
|
str
|
The embedding model to use. |
'embedding-001'
|
Returns:
Type | Description |
---|---|
Optional[List[float]]
|
A list of floats representing the embedding, or None on failure. |
Raises:
Type | Description |
---|---|
EmbeddingError
|
If the API call fails after retries. |
Source code in src/safeagent/embeddings.py
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 |
|
Orchestrators
safeagent.orchestrator
SimpleOrchestrator
Minimal DAG runner: each node is a function, edges define dependencies, with audit and lineage tagging.
Source code in src/safeagent/orchestrator.py
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 |
|
add_edge(src, dest)
Specify that 'dest' depends on 'src'.
Source code in src/safeagent/orchestrator.py
19 20 21 22 23 |
|
add_node(name, func)
Register a function under the given node name.
Source code in src/safeagent/orchestrator.py
14 15 16 17 |
|
run(inputs)
Execute all nodes in topological order, audit pipeline start/end, and tag lineage on outputs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
Dict[str, Any]
|
Global inputs (e.g., 'user_input', 'user_id'). |
required |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Dict[str, Any]: Mapping of node name to its return value. |
Source code in src/safeagent/orchestrator.py
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 |
|
safeagent.stateful_orchestrator
EdgeRegistrationError
Bases: OrchestratorError
Raised during an invalid attempt to register an edge.
Source code in src/safeagent/stateful_orchestrator.py
19 20 21 22 23 |
|
NodeNotFoundError
Bases: OrchestratorError
Raised when a node name is not found in the graph.
Source code in src/safeagent/stateful_orchestrator.py
13 14 15 16 17 |
|
OrchestratorError
Bases: Exception
Base exception for all stateful orchestrator errors.
Source code in src/safeagent/stateful_orchestrator.py
9 10 11 |
|
StateValidationError
Bases: OrchestratorError
Raised when the state does not conform to the defined schema.
Source code in src/safeagent/stateful_orchestrator.py
25 26 27 28 |
|
StatefulOrchestrator
An orchestrator that manages a central state object, allowing for complex, cyclical, and conditional workflows with integrated governance, human-in-the-loop interrupts, and optional state schema validation.
Source code in src/safeagent/stateful_orchestrator.py
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 |
|
__init__(entry_node, state_schema=None)
Initializes the stateful orchestrator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entry_node
|
str
|
The name of the first node to execute in the graph. |
required |
state_schema
|
Optional[Dict[str, Type]]
|
An optional schema defining expected keys and their Python types in the state object. |
None
|
Source code in src/safeagent/stateful_orchestrator.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
resume(state, human_input, user_id='system', max_steps=15)
Resumes execution of a paused graph.
Source code in src/safeagent/stateful_orchestrator.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
run(inputs, user_id='system', max_steps=15)
Executes the graph starting from the entry node.
Returns:
Type | Description |
---|---|
str
|
A tuple containing the final status ('completed', 'paused', 'error') |
Dict[str, Any]
|
and the final state of the graph. |
Source code in src/safeagent/stateful_orchestrator.py
86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
Tooling
safeagent.tool_registry
AccessManager
A centralized class to manage Role-Based Access Control (RBAC).
This class can be initialized with a user role mapping, or it will use a default set of roles for demonstration purposes.
Source code in src/safeagent/tool_registry.py
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 |
|
__init__(role_config=None)
Initializes the AccessManager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
role_config
|
Optional[Dict[str, List[str]]]
|
A dictionary mapping user IDs to a list of their roles. If None, a default demo configuration is used. |
None
|
Source code in src/safeagent/tool_registry.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
check_access(user_id, required_role)
Checks if a user has a required role by looking them up in the internal role database.
Source code in src/safeagent/tool_registry.py
40 41 42 43 44 45 46 |
|
SimilarityMetric
Bases: Enum
Specifies the similarity metric for vector search.
Source code in src/safeagent/tool_registry.py
76 77 78 79 80 |
|
ToolExecutionError
Bases: ToolRegistryError
Raised when a tool fails to execute after all retries.
Source code in src/safeagent/tool_registry.py
91 92 93 |
|
ToolNotFoundError
Bases: ToolRegistryError
Raised when a tool is not found in the registry.
Source code in src/safeagent/tool_registry.py
87 88 89 |
|
ToolRegistry
A central, governed registry for tools that includes RBAC, automatic retries, circuit breakers, cost/latency tracking, caching, async support, output sinks, and dynamic schemas.
Source code in src/safeagent/tool_registry.py
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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
|
generate_tool_schema(tool_names)
Generates a JSON Schema-like description for a list of tools.
Source code in src/safeagent/tool_registry.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
|
get_governed_tool(name, user_id)
Retrieves a tool by name and wraps it in all registered governance policies. This method correctly handles both synchronous and asynchronous tools.
Source code in src/safeagent/tool_registry.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
|
get_relevant_tools(query, top_k=3)
Finds the most semantically relevant tools for a given query using a vector index.
Source code in src/safeagent/tool_registry.py
332 333 334 335 336 337 338 339 340 341 342 343 344 |
|
register(required_role=None, retry_attempts=0, retry_delay=1.0, circuit_breaker_threshold=0, cache_ttl_seconds=0, cost_per_call=None, cost_calculator=None, output_sinks=None)
A decorator to register a tool with advanced, governed execution policies.
Source code in src/safeagent/tool_registry.py
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 |
|
ToolRegistryError
Bases: Exception
Base class for tool registry exceptions.
Source code in src/safeagent/tool_registry.py
83 84 85 |
|
Retrievers
safeagent.retriever
BaseRetriever
Base interface for retrieval. Requires implementing index and query.
Source code in src/safeagent/retriever.py
59 60 61 62 63 64 65 |
|
GraphRetriever
Bases: BaseRetriever
Neo4j-backed GraphRAG retriever using GDS k-NN, with governance integration.
Source code in src/safeagent/retriever.py
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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
|
__init__(neo4j_uri, user, password, gds_graph_name, embed_model_fn)
Create the retriever. If neo4j_uri is falsy, the retriever is disabled.
Source code in src/safeagent/retriever.py
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 |
|
index(embeddings, metadata)
Ingest each document as a node with a 'vector' property and 'metadata' (with lineage tagging).
Source code in src/safeagent/retriever.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
|
query(query_text, top_k=5)
Compute embedding for query_text, run GDS K-NN, and return nearest documents (with lineage tagging).
Source code in src/safeagent/retriever.py
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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
|
VectorRetriever
Bases: BaseRetriever
FAISS-backed vector retriever. Uses an embedding function to map text to vectors, with governance integration.
Source code in src/safeagent/retriever.py
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 |
|
__init__(index_path, embed_model_fn)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index_path
|
str
|
Filesystem path to store/load FAISS index. |
required |
embed_model_fn
|
callable
|
Function that maps text (str) to a numpy ndarray vector. |
required |
Source code in src/safeagent/retriever.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
index(embeddings, metadata)
Add embeddings to the FAISS index and store metadata (with lineage tagging).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
embeddings
|
List[ndarray]
|
List of vectors. |
required |
metadata
|
List[Dict[str, Any]]
|
Corresponding metadata dicts (must include 'id'). |
required |
Source code in src/safeagent/retriever.py
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 |
|
query(query_text, top_k=5)
Perform KNN search on the FAISS index using the embedded query, with encryption and audit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query_text
|
str
|
The query string. |
required |
top_k
|
int
|
Number of nearest neighbors to return. |
5
|
Returns:
Type | Description |
---|---|
List[Dict[str, Any]]
|
List[Dict[str, Any]]: Each dict contains 'id', 'score', and 'metadata'. |
Source code in src/safeagent/retriever.py
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 |
|
register_retriever(name, cls)
Register a retriever class for dynamic loading.
Source code in src/safeagent/retriever.py
51 52 53 |
|
Protocols
safeagent.protocol_manager
PROTOCOLS
Bases: Enum
Defines the supported communication/execution protocols.
Source code in src/safeagent/protocol_manager.py
23 24 25 26 |
|
ProtocolManager
Manages the selection and execution of different agent workflows (protocols). This class acts as the main entry point for running a complete agent system.
Source code in src/safeagent/protocol_manager.py
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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|
run(inputs)
Executes the configured workflow based on the selected protocol.
Source code in src/safeagent/protocol_manager.py
54 55 56 57 58 59 60 61 62 63 |
|