API: Core Components
This section covers the fundamental building blocks of the SafeAgent framework.
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 |
|
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 |
|