API Reference¶
Complete reference for the triadic-engine Python package.
neurosym¶
neurosym
¶
Triadic Neurosymbolic Engine — Core Package
Deterministic algebraic framework for neurosymbolic validation, semantic projection, and AI model auditing.
Install: pip install triadic-engine Docs: https://github.com/arturoornelasb/Triadic-Neurosymbolic-Engine
Encoder¶
Multi-backend embedding encoder with four LSH-to-prime projection modes.
neurosym.encoder
¶
BaseEncoder
¶
Bases: ABC
Abstract interface for all embedding backends. Subclass this to add new providers (OpenAI, Cohere, etc.)
Source code in engine-src/src/neurosym/encoder.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
name: str
abstractmethod
property
¶
Human-readable model identifier.
encode(concepts: List[str]) -> np.ndarray
abstractmethod
¶
Returns a 2D numpy array of shape (len(concepts), dim).
Source code in engine-src/src/neurosym/encoder.py
20 21 22 23 | |
ContinuousEncoder
¶
Bases: BaseEncoder
A lightweight wrapper around sentence-transformers to generate continuous vector embeddings for natural language concepts. Optimized for CPU usage. Runs entirely offline.
Source code in engine-src/src/neurosym/encoder.py
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 | |
OpenAIEncoder
¶
Bases: BaseEncoder
Embedding backend using OpenAI's text-embedding API. Requires: pip install openai Set OPENAI_API_KEY environment variable.
Source code in engine-src/src/neurosym/encoder.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
CohereEncoder
¶
Bases: BaseEncoder
Embedding backend using Cohere's embed API. Requires: pip install cohere Set COHERE_API_KEY environment variable.
Source code in engine-src/src/neurosym/encoder.py
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 | |
DiscreteMapper
¶
Maps continuous dense vectors to discrete integer space using Locality Sensitive Hashing (LSH) and Prime Factorization.
Each LSH hyperplane is assigned a unique prime number. A concept's discrete representation is the product of all primes corresponding to hyperplanes where its projection is positive.
Supports four projection modes
- 'random' (default): Random hyperplanes from N(0, I_d)
- 'pca': Principal Component directions from the corpus
- 'consensus': Multi-seed voting — keeps only stable primes
- 'contrastive': Trains hyperplanes on known hypernym pairs
Source code in engine-src/src/neurosym/encoder.py
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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | |
fit_transform(concepts: List[str], embeddings: np.ndarray) -> Dict[str, int]
¶
Maps a list of concepts and their continuous embeddings to discrete composite primes. Each LSH hyperplane corresponds to a unique prime number.
Source code in engine-src/src/neurosym/encoder.py
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | |
transform(concepts: List[str], embeddings: np.ndarray) -> Dict[str, int]
¶
Maps a list of new concepts to discrete composite primes using EXISTING planes. Does NOT update internal planes.
Source code in engine-src/src/neurosym/encoder.py
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | |
create_encoder(provider: str = 'local', model: str = None) -> BaseEncoder
¶
Factory function to create an encoder from a provider name.
Examples:
create_encoder("local", "all-MiniLM-L6-v2") create_encoder("openai", "text-embedding-3-large") create_encoder("cohere", "embed-english-v3.0")
Source code in engine-src/src/neurosym/encoder.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
Discrete Validator¶
Algebraic operations on prime-factor signatures: subsumption, composition, gap analysis, analogy.
neurosym.triadic
¶
ValidationResult
dataclass
¶
Result of a discrete algebraic validation.
Source code in engine-src/src/neurosym/triadic.py
8 9 10 11 12 13 14 15 16 17 18 | |
DiscreteValidator
¶
Abductive Algebraic Resolver for Relational Data. Merges Triadic Relational Framework and Shadow Engine logic. Project continuous latent spaces into discrete integer factors for rigorous validation.
Source code in engine-src/src/neurosym/triadic.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 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 | |
discover_missing_factor(numerator: int, denominator: int) -> int
staticmethod
¶
Abductive Discovery: Identifies the missing integer factor that prevents the equation from resolving to a clean integer state (Topological Obstruction).
Source code in engine-src/src/neurosym/triadic.py
37 38 39 40 41 42 43 44 45 | |
validate_relationship(source: int, transform1: int, transform2: int, rule_a: int = 1, rule_b: int = 1) -> ValidationResult
¶
Validates if the relationship: (rule_a * transform1 * transform2) / (rule_b * source) resolves cleanly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
C1
|
The base concept/entity (e.g. integer projection of a word) |
required |
transform1
|
C2
|
The first relational modifier |
required |
transform2
|
C3
|
The second relational modifier |
required |
rule_a, rule_b
|
The invariant ratio (default 1:1) |
required |
Returns:
| Type | Description |
|---|---|
ValidationResult
|
ValidationResult containing the expected target (C4) or the missing abductive factor. |
Source code in engine-src/src/neurosym/triadic.py
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 | |
analogy_prediction(source_a: int, source_b: int, target_a: int) -> ValidationResult
¶
Resolves semantic analogies (A:B :: C:D) Returns the integer prediction for D. Formula: D = (C * B) / A
Source code in engine-src/src/neurosym/triadic.py
114 115 116 117 118 119 120 121 122 123 124 125 126 | |
subsumes(a: int, b: int) -> bool
staticmethod
¶
Logical Subsumption: Does concept A contain ALL semantic features of B?
In prime space: A subsumes B iff B divides A (A % B == 0). Example: King(235) subsumes Male(3) → True (King contains 'male' feature)
This operation is IMPOSSIBLE with Hamming distance over bitstrings. Hamming only tells you "how many bits differ", not containment.
Source code in engine-src/src/neurosym/triadic.py
138 139 140 141 142 143 144 145 146 147 148 149 | |
compose(*concepts: int) -> int
staticmethod
¶
Algebraic Composition: Create a new concept by combining features.
In prime space: the union of all semantic features = LCM of the integers. This preserves uniqueness: compose(Royal, Male) contains all features of both.
Example: compose(Royal=25, Male=37) → 235*7 = 210
This operation is IMPOSSIBLE with vector cosine similarity.
Source code in engine-src/src/neurosym/triadic.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
explain_gap(a: int, b: int) -> dict
staticmethod
¶
Abductive Discovery: Explains exactly WHY two concepts differ.
Returns:
| Type | Description |
|---|---|
dict
|
|
dict
|
|
dict
|
|
explain_gap(King=30, Queen=10)
→ shared=10, only_in_king=3 (the 'male' factor), only_in_queen=1
This deterministic decomposition is IMPOSSIBLE with continuous vectors. Cosine similarity only says "0.87 similar" — not WHICH features differ.
Source code in engine-src/src/neurosym/triadic.py
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 | |
Graph Builder¶
Scalable graph construction with inverted prime index.
neurosym.graph
¶
Scalable Graph Builder for the Triadic Neurosymbolic Engine.
Replaces the naive O(N²) all-pairs GCD scan with an inverted prime index approach that only compares concepts sharing at least one prime factor.
O(N * F * B) where:
N = number of concepts
F = average number of prime factors per concept
B = average bucket size per prime factor
For sparse LSH projections (k=8-16), F ≈ 4-8 and B << N, so this is dramatically faster than O(N²) for large datasets.
ScalableGraphBuilder
¶
Builds semantic graphs using an inverted prime factor index instead of brute-force all-pairs comparison.
Source code in engine-src/src/neurosym/graph.py
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 | |
build_index(prime_map: Dict[str, int])
¶
Build the inverted index from a prime map. Maps each prime factor → set of concepts containing that factor.
Source code in engine-src/src/neurosym/graph.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
find_edges(prime_map: Dict[str, int], min_shared: int = 1) -> List[Tuple[str, str, int, List[int]]]
¶
Find all edges (concept pairs with shared prime factors) using the inverted index. Returns list of (concept_a, concept_b, weight, shared_factors).
This is O(N * F * B) instead of O(N²).
Source code in engine-src/src/neurosym/graph.py
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 | |
find_neighbors(concept: str, prime_map: Dict[str, int], min_shared: int = 1) -> List[Tuple[str, int, List[int]]]
¶
Find all neighbors of a single concept using the inverted index. Returns list of (neighbor, weight, shared_factors).
This is O(F * B) — constant relative to total dataset size.
Source code in engine-src/src/neurosym/graph.py
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 | |
get_stats() -> dict
¶
Return index statistics.
Source code in engine-src/src/neurosym/graph.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
Storage¶
SQLite persistence for prime indices and audit results.
neurosym.storage
¶
SQLite persistence layer for the Triadic Neurosymbolic Engine. Stores prime indexes so they survive server restarts.
PrimeIndexDB
¶
Persistent storage for discrete prime indexes using SQLite.
Default location: ~/.neurosym/index.db
Source code in engine-src/src/neurosym/storage.py
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 | |
save_index(prime_map: Dict[str, int], model: str, lsh_bits: int, seed: int)
¶
Save a prime map to the database. Upserts on conflict.
Source code in engine-src/src/neurosym/storage.py
62 63 64 65 66 67 68 69 70 71 72 | |
load_index(model: str, lsh_bits: int, seed: int) -> Dict[str, int]
¶
Load a previously saved prime map from the database.
Source code in engine-src/src/neurosym/storage.py
74 75 76 77 78 79 80 81 | |
save_audit(results: List[dict], model_a: str, model_b: str)
¶
Save audit discrepancy results.
Source code in engine-src/src/neurosym/storage.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
list_indexes() -> List[dict]
¶
List all stored indexes with their metadata.
Source code in engine-src/src/neurosym/storage.py
98 99 100 101 102 103 104 105 106 107 108 109 110 | |
export_csv(model: str = None) -> str
¶
Export concepts to CSV string.
Source code in engine-src/src/neurosym/storage.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
delete_index(model: str, lsh_bits: int, seed: int) -> int
¶
Delete all concepts for a given named index. Returns number of rows deleted.
Source code in engine-src/src/neurosym/storage.py
131 132 133 134 135 136 137 138 | |
concept_count() -> int
¶
Return total number of stored concepts.
Source code in engine-src/src/neurosym/storage.py
140 141 142 143 144 | |
Reports¶
Exportable reports in HTML, JSON, and CSV formats.
neurosym.reports
¶
Report Generator for the Triadic Neurosymbolic Engine.
Generates exportable audit reports in HTML, JSON, and CSV formats. No external dependencies — uses Python stdlib only.
ReportGenerator
¶
Generates structured audit reports from Triadic Engine results. Supports HTML (standalone, embeddable), JSON, and CSV output.
Source code in engine-src/src/neurosym/reports.py
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 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 | |
add_encoding_section(prime_map: Dict[str, int], model: str, lsh_bits: int, factorize_fn=None)
¶
Add the prime encoding results to the report.
Source code in engine-src/src/neurosym/reports.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
add_audit_section(discrepancies: List[dict], model_a: str, model_b: str, total_pairs: int, total_concepts: int)
¶
Add the model audit discrepancy results to the report.
Source code in engine-src/src/neurosym/reports.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 | |
add_graph_section(edges: list, node_count: int)
¶
Add graph statistics to the report.
Source code in engine-src/src/neurosym/reports.py
70 71 72 73 74 75 76 77 78 79 80 81 82 | |
to_json(indent: int = 2) -> str
¶
Export the full report as a JSON string.
Source code in engine-src/src/neurosym/reports.py
86 87 88 89 90 91 92 93 94 | |
to_csv() -> str
¶
Export encoding and audit rows as CSV.
Source code in engine-src/src/neurosym/reports.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 | |
to_html() -> str
¶
Export a standalone HTML report with embedded dark-theme styling.
Source code in engine-src/src/neurosym/reports.py
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 | |
save(path: str, format: str = 'html')
¶
Save the report to a file.
Source code in engine-src/src/neurosym/reports.py
283 284 285 286 287 288 289 290 291 292 293 294 295 | |
Ingest¶
DataFrame ingestion with inverted prime index and semantic search.
neurosym.ingest
¶
DatabaseIngestor
¶
Ingests tabular data (CSV) and builds a Discrete Prime Index for fast semantic search using integer arithmetic instead of vector distance.
Uses an inverted index keyed by prime factors for sub-linear lookup.
Source code in engine-src/src/neurosym/ingest.py
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 | |
ingest_dataframe(df: pd.DataFrame, text_column: str, id_column: str = None) -> Dict
¶
Takes a pandas DataFrame, vectorizes the text column, hashes it into composite prime factors, and builds an inverted index.
Complexity: O(N * E) where N = records and E = embedding time.
Source code in engine-src/src/neurosym/ingest.py
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 | |
triadic_search(query: str, top_k: int = 5) -> List[Tuple[int, str, int, int]]
¶
Semantic search using the inverted prime index.
- Encode the query into a composite prime.
- Factorize the query prime to get its active semantic features.
- Use the inverted index to find candidate records that share at least one prime factor (set union over factor buckets).
- Rank candidates by GCD-based distance.
Complexity: O(C * log(C)) where C = number of candidate records sharing at least one factor. In the best case C << N. Falls back to full scan if no candidates are found.
Source code in engine-src/src/neurosym/ingest.py
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 | |
Anomaly Detection¶
Multiplicative anomaly detection for tabular data.
neurosym.anomaly
¶
RelationalRule
dataclass
¶
Defines a multiplicative relationship between columns in a DataFrame.
Total = Quantity × Unit_Price × Tax_Rate
rule = RelationalRule( name="Invoice Total", factor_columns=["qty", "unit_price", "tax_rate"], result_column="total" )
Source code in engine-src/src/neurosym/anomaly.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
Anomaly
dataclass
¶
A detected anomaly in one row of data.
Source code in engine-src/src/neurosym/anomaly.py
27 28 29 30 31 32 33 34 35 36 37 | |
AnomalyDetector
¶
Detects anomalies in tabular data by verifying multiplicative relationships.
Uses the Shadow Engine's core insight: when a product relationship fails, GCD-based factor analysis identifies EXACTLY what is wrong and by how much.
This works on REAL numbers in your data — no embeddings, no LSH, no random primes.
Source code in engine-src/src/neurosym/anomaly.py
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 | |
add_rule(rule: RelationalRule)
¶
Register a multiplicative relationship rule.
Source code in engine-src/src/neurosym/anomaly.py
53 54 55 56 | |
scan(df: pd.DataFrame) -> List[Anomaly]
¶
Scan every row of the DataFrame against all registered rules. Returns a list of Anomaly objects for rows that violate any rule.
Source code in engine-src/src/neurosym/anomaly.py
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 | |