crca 1.4.0__py3-none-any.whl → 1.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (306) hide show
  1. CRCA.py +172 -7
  2. MODEL_CARD.md +53 -0
  3. PKG-INFO +8 -2
  4. RELEASE_NOTES.md +17 -0
  5. STABILITY.md +19 -0
  6. architecture/hybrid/consistency_engine.py +362 -0
  7. architecture/hybrid/conversation_manager.py +421 -0
  8. architecture/hybrid/explanation_generator.py +452 -0
  9. architecture/hybrid/few_shot_learner.py +533 -0
  10. architecture/hybrid/graph_compressor.py +286 -0
  11. architecture/hybrid/hybrid_agent.py +4398 -0
  12. architecture/hybrid/language_compiler.py +623 -0
  13. architecture/hybrid/main,py +0 -0
  14. architecture/hybrid/reasoning_tracker.py +322 -0
  15. architecture/hybrid/self_verifier.py +524 -0
  16. architecture/hybrid/task_decomposer.py +567 -0
  17. architecture/hybrid/text_corrector.py +341 -0
  18. benchmark_results/crca_core_benchmarks.json +178 -0
  19. branches/crca_sd/crca_sd_realtime.py +6 -2
  20. branches/general_agent/__init__.py +102 -0
  21. branches/general_agent/general_agent.py +1400 -0
  22. branches/general_agent/personality.py +169 -0
  23. branches/general_agent/utils/__init__.py +19 -0
  24. branches/general_agent/utils/prompt_builder.py +170 -0
  25. {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/METADATA +8 -2
  26. {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/RECORD +303 -20
  27. crca_core/__init__.py +35 -0
  28. crca_core/benchmarks/__init__.py +14 -0
  29. crca_core/benchmarks/synthetic_scm.py +103 -0
  30. crca_core/core/__init__.py +23 -0
  31. crca_core/core/api.py +120 -0
  32. crca_core/core/estimate.py +208 -0
  33. crca_core/core/godclass.py +72 -0
  34. crca_core/core/intervention_design.py +174 -0
  35. crca_core/core/lifecycle.py +48 -0
  36. crca_core/discovery/__init__.py +9 -0
  37. crca_core/discovery/tabular.py +193 -0
  38. crca_core/identify/__init__.py +171 -0
  39. crca_core/identify/backdoor.py +39 -0
  40. crca_core/identify/frontdoor.py +48 -0
  41. crca_core/identify/graph.py +106 -0
  42. crca_core/identify/id_algorithm.py +43 -0
  43. crca_core/identify/iv.py +48 -0
  44. crca_core/models/__init__.py +67 -0
  45. crca_core/models/provenance.py +56 -0
  46. crca_core/models/refusal.py +39 -0
  47. crca_core/models/result.py +83 -0
  48. crca_core/models/spec.py +151 -0
  49. crca_core/models/validation.py +68 -0
  50. crca_core/scm/__init__.py +9 -0
  51. crca_core/scm/linear_gaussian.py +198 -0
  52. crca_core/timeseries/__init__.py +6 -0
  53. crca_core/timeseries/pcmci.py +181 -0
  54. crca_llm/__init__.py +12 -0
  55. crca_llm/client.py +85 -0
  56. crca_llm/coauthor.py +118 -0
  57. crca_llm/orchestrator.py +289 -0
  58. crca_llm/types.py +21 -0
  59. crca_reasoning/__init__.py +16 -0
  60. crca_reasoning/critique.py +54 -0
  61. crca_reasoning/godclass.py +206 -0
  62. crca_reasoning/memory.py +24 -0
  63. crca_reasoning/rationale.py +10 -0
  64. crca_reasoning/react_controller.py +81 -0
  65. crca_reasoning/tool_router.py +97 -0
  66. crca_reasoning/types.py +40 -0
  67. crca_sd/__init__.py +15 -0
  68. crca_sd/crca_sd_core.py +2 -0
  69. crca_sd/crca_sd_governance.py +2 -0
  70. crca_sd/crca_sd_mpc.py +2 -0
  71. crca_sd/crca_sd_realtime.py +2 -0
  72. crca_sd/crca_sd_tui.py +2 -0
  73. cuda-keyring_1.1-1_all.deb +0 -0
  74. cuda-keyring_1.1-1_all.deb.1 +0 -0
  75. docs/IMAGE_ANNOTATION_USAGE.md +539 -0
  76. docs/INSTALL_DEEPSPEED.md +125 -0
  77. docs/api/branches/crca-cg.md +19 -0
  78. docs/api/branches/crca-q.md +27 -0
  79. docs/api/branches/crca-sd.md +37 -0
  80. docs/api/branches/general-agent.md +24 -0
  81. docs/api/branches/overview.md +19 -0
  82. docs/api/crca/agent-methods.md +62 -0
  83. docs/api/crca/operations.md +79 -0
  84. docs/api/crca/overview.md +32 -0
  85. docs/api/image-annotation/engine.md +52 -0
  86. docs/api/image-annotation/overview.md +17 -0
  87. docs/api/schemas/annotation.md +34 -0
  88. docs/api/schemas/core-schemas.md +82 -0
  89. docs/api/schemas/overview.md +32 -0
  90. docs/api/schemas/policy.md +30 -0
  91. docs/api/utils/conversation.md +22 -0
  92. docs/api/utils/graph-reasoner.md +32 -0
  93. docs/api/utils/overview.md +21 -0
  94. docs/api/utils/router.md +19 -0
  95. docs/api/utils/utilities.md +97 -0
  96. docs/architecture/causal-graphs.md +41 -0
  97. docs/architecture/data-flow.md +29 -0
  98. docs/architecture/design-principles.md +33 -0
  99. docs/architecture/hybrid-agent/components.md +38 -0
  100. docs/architecture/hybrid-agent/consistency.md +26 -0
  101. docs/architecture/hybrid-agent/overview.md +44 -0
  102. docs/architecture/hybrid-agent/reasoning.md +22 -0
  103. docs/architecture/llm-integration.md +26 -0
  104. docs/architecture/modular-structure.md +37 -0
  105. docs/architecture/overview.md +69 -0
  106. docs/architecture/policy-engine-arch.md +29 -0
  107. docs/branches/crca-cg/corposwarm.md +39 -0
  108. docs/branches/crca-cg/esg-scoring.md +30 -0
  109. docs/branches/crca-cg/multi-agent.md +35 -0
  110. docs/branches/crca-cg/overview.md +40 -0
  111. docs/branches/crca-q/alternative-data.md +55 -0
  112. docs/branches/crca-q/architecture.md +71 -0
  113. docs/branches/crca-q/backtesting.md +45 -0
  114. docs/branches/crca-q/causal-engine.md +33 -0
  115. docs/branches/crca-q/execution.md +39 -0
  116. docs/branches/crca-q/market-data.md +60 -0
  117. docs/branches/crca-q/overview.md +58 -0
  118. docs/branches/crca-q/philosophy.md +60 -0
  119. docs/branches/crca-q/portfolio-optimization.md +66 -0
  120. docs/branches/crca-q/risk-management.md +102 -0
  121. docs/branches/crca-q/setup.md +65 -0
  122. docs/branches/crca-q/signal-generation.md +61 -0
  123. docs/branches/crca-q/signal-validation.md +43 -0
  124. docs/branches/crca-sd/core.md +84 -0
  125. docs/branches/crca-sd/governance.md +53 -0
  126. docs/branches/crca-sd/mpc-solver.md +65 -0
  127. docs/branches/crca-sd/overview.md +59 -0
  128. docs/branches/crca-sd/realtime.md +28 -0
  129. docs/branches/crca-sd/tui.md +20 -0
  130. docs/branches/general-agent/overview.md +37 -0
  131. docs/branches/general-agent/personality.md +36 -0
  132. docs/branches/general-agent/prompt-builder.md +30 -0
  133. docs/changelog/index.md +79 -0
  134. docs/contributing/code-style.md +69 -0
  135. docs/contributing/documentation.md +43 -0
  136. docs/contributing/overview.md +29 -0
  137. docs/contributing/testing.md +29 -0
  138. docs/core/crcagent/async-operations.md +65 -0
  139. docs/core/crcagent/automatic-extraction.md +107 -0
  140. docs/core/crcagent/batch-prediction.md +80 -0
  141. docs/core/crcagent/bayesian-inference.md +60 -0
  142. docs/core/crcagent/causal-graph.md +92 -0
  143. docs/core/crcagent/counterfactuals.md +96 -0
  144. docs/core/crcagent/deterministic-simulation.md +78 -0
  145. docs/core/crcagent/dual-mode-operation.md +82 -0
  146. docs/core/crcagent/initialization.md +88 -0
  147. docs/core/crcagent/optimization.md +65 -0
  148. docs/core/crcagent/overview.md +63 -0
  149. docs/core/crcagent/time-series.md +57 -0
  150. docs/core/schemas/annotation.md +30 -0
  151. docs/core/schemas/core-schemas.md +82 -0
  152. docs/core/schemas/overview.md +30 -0
  153. docs/core/schemas/policy.md +41 -0
  154. docs/core/templates/base-agent.md +31 -0
  155. docs/core/templates/feature-mixins.md +31 -0
  156. docs/core/templates/overview.md +29 -0
  157. docs/core/templates/templates-guide.md +75 -0
  158. docs/core/tools/mcp-client.md +34 -0
  159. docs/core/tools/overview.md +24 -0
  160. docs/core/utils/conversation.md +27 -0
  161. docs/core/utils/graph-reasoner.md +29 -0
  162. docs/core/utils/overview.md +27 -0
  163. docs/core/utils/router.md +27 -0
  164. docs/core/utils/utilities.md +97 -0
  165. docs/css/custom.css +84 -0
  166. docs/examples/basic-usage.md +57 -0
  167. docs/examples/general-agent/general-agent-examples.md +50 -0
  168. docs/examples/hybrid-agent/hybrid-agent-examples.md +56 -0
  169. docs/examples/image-annotation/image-annotation-examples.md +54 -0
  170. docs/examples/integration/integration-examples.md +58 -0
  171. docs/examples/overview.md +37 -0
  172. docs/examples/trading/trading-examples.md +46 -0
  173. docs/features/causal-reasoning/advanced-topics.md +101 -0
  174. docs/features/causal-reasoning/counterfactuals.md +43 -0
  175. docs/features/causal-reasoning/do-calculus.md +50 -0
  176. docs/features/causal-reasoning/overview.md +47 -0
  177. docs/features/causal-reasoning/structural-models.md +52 -0
  178. docs/features/hybrid-agent/advanced-components.md +55 -0
  179. docs/features/hybrid-agent/core-components.md +64 -0
  180. docs/features/hybrid-agent/overview.md +34 -0
  181. docs/features/image-annotation/engine.md +82 -0
  182. docs/features/image-annotation/features.md +113 -0
  183. docs/features/image-annotation/integration.md +75 -0
  184. docs/features/image-annotation/overview.md +53 -0
  185. docs/features/image-annotation/quickstart.md +73 -0
  186. docs/features/policy-engine/doctrine-ledger.md +105 -0
  187. docs/features/policy-engine/monitoring.md +44 -0
  188. docs/features/policy-engine/mpc-control.md +89 -0
  189. docs/features/policy-engine/overview.md +46 -0
  190. docs/getting-started/configuration.md +225 -0
  191. docs/getting-started/first-agent.md +164 -0
  192. docs/getting-started/installation.md +144 -0
  193. docs/getting-started/quickstart.md +137 -0
  194. docs/index.md +118 -0
  195. docs/js/mathjax.js +13 -0
  196. docs/lrm/discovery_proof_notes.md +25 -0
  197. docs/lrm/finetune_full.md +83 -0
  198. docs/lrm/math_appendix.md +120 -0
  199. docs/lrm/overview.md +32 -0
  200. docs/mkdocs.yml +238 -0
  201. docs/stylesheets/extra.css +21 -0
  202. docs_generated/crca_core/CounterfactualResult.md +12 -0
  203. docs_generated/crca_core/DiscoveryHypothesisResult.md +13 -0
  204. docs_generated/crca_core/DraftSpec.md +13 -0
  205. docs_generated/crca_core/EstimateResult.md +13 -0
  206. docs_generated/crca_core/IdentificationResult.md +17 -0
  207. docs_generated/crca_core/InterventionDesignResult.md +12 -0
  208. docs_generated/crca_core/LockedSpec.md +15 -0
  209. docs_generated/crca_core/RefusalResult.md +12 -0
  210. docs_generated/crca_core/ValidationReport.md +9 -0
  211. docs_generated/crca_core/index.md +13 -0
  212. examples/general_agent_example.py +277 -0
  213. examples/general_agent_quickstart.py +202 -0
  214. examples/general_agent_simple.py +92 -0
  215. examples/hybrid_agent_auto_extraction.py +84 -0
  216. examples/hybrid_agent_dictionary_demo.py +104 -0
  217. examples/hybrid_agent_enhanced.py +179 -0
  218. examples/hybrid_agent_general_knowledge.py +107 -0
  219. examples/image_annotation_quickstart.py +328 -0
  220. examples/test_hybrid_fixes.py +77 -0
  221. image_annotation/__init__.py +27 -0
  222. image_annotation/annotation_engine.py +2593 -0
  223. install_cuda_wsl2.sh +59 -0
  224. install_deepspeed.sh +56 -0
  225. install_deepspeed_simple.sh +87 -0
  226. mkdocs.yml +252 -0
  227. ollama/Modelfile +8 -0
  228. prompts/__init__.py +2 -1
  229. prompts/default_crca.py +9 -1
  230. prompts/general_agent.py +227 -0
  231. prompts/image_annotation.py +56 -0
  232. pyproject.toml +17 -2
  233. requirements-docs.txt +10 -0
  234. requirements.txt +21 -2
  235. schemas/__init__.py +26 -1
  236. schemas/annotation.py +222 -0
  237. schemas/conversation.py +193 -0
  238. schemas/hybrid.py +211 -0
  239. schemas/reasoning.py +276 -0
  240. schemas_export/crca_core/CounterfactualResult.schema.json +108 -0
  241. schemas_export/crca_core/DiscoveryHypothesisResult.schema.json +113 -0
  242. schemas_export/crca_core/DraftSpec.schema.json +635 -0
  243. schemas_export/crca_core/EstimateResult.schema.json +113 -0
  244. schemas_export/crca_core/IdentificationResult.schema.json +145 -0
  245. schemas_export/crca_core/InterventionDesignResult.schema.json +111 -0
  246. schemas_export/crca_core/LockedSpec.schema.json +646 -0
  247. schemas_export/crca_core/RefusalResult.schema.json +90 -0
  248. schemas_export/crca_core/ValidationReport.schema.json +62 -0
  249. scripts/build_lrm_dataset.py +80 -0
  250. scripts/export_crca_core_schemas.py +54 -0
  251. scripts/export_hf_lrm.py +37 -0
  252. scripts/export_ollama_gguf.py +45 -0
  253. scripts/generate_changelog.py +157 -0
  254. scripts/generate_crca_core_docs_from_schemas.py +86 -0
  255. scripts/run_crca_core_benchmarks.py +163 -0
  256. scripts/run_full_finetune.py +198 -0
  257. scripts/run_lrm_eval.py +31 -0
  258. templates/graph_management.py +29 -0
  259. tests/conftest.py +9 -0
  260. tests/test_core.py +2 -3
  261. tests/test_crca_core_discovery_tabular.py +15 -0
  262. tests/test_crca_core_estimate_dowhy.py +36 -0
  263. tests/test_crca_core_identify.py +18 -0
  264. tests/test_crca_core_intervention_design.py +36 -0
  265. tests/test_crca_core_linear_gaussian_scm.py +69 -0
  266. tests/test_crca_core_spec.py +25 -0
  267. tests/test_crca_core_timeseries_pcmci.py +15 -0
  268. tests/test_crca_llm_coauthor.py +12 -0
  269. tests/test_crca_llm_orchestrator.py +80 -0
  270. tests/test_hybrid_agent_llm_enhanced.py +556 -0
  271. tests/test_image_annotation_demo.py +376 -0
  272. tests/test_image_annotation_operational.py +408 -0
  273. tests/test_image_annotation_unit.py +551 -0
  274. tests/test_training_moe.py +13 -0
  275. training/__init__.py +42 -0
  276. training/datasets.py +140 -0
  277. training/deepspeed_zero2_0_5b.json +22 -0
  278. training/deepspeed_zero2_1_5b.json +22 -0
  279. training/deepspeed_zero3_0_5b.json +28 -0
  280. training/deepspeed_zero3_14b.json +28 -0
  281. training/deepspeed_zero3_h100_3gpu.json +20 -0
  282. training/deepspeed_zero3_offload.json +28 -0
  283. training/eval.py +92 -0
  284. training/finetune.py +516 -0
  285. training/public_datasets.py +89 -0
  286. training_data/react_train.jsonl +7473 -0
  287. utils/agent_discovery.py +311 -0
  288. utils/batch_processor.py +317 -0
  289. utils/conversation.py +78 -0
  290. utils/edit_distance.py +118 -0
  291. utils/formatter.py +33 -0
  292. utils/graph_reasoner.py +530 -0
  293. utils/rate_limiter.py +283 -0
  294. utils/router.py +2 -2
  295. utils/tool_discovery.py +307 -0
  296. webui/__init__.py +10 -0
  297. webui/app.py +229 -0
  298. webui/config.py +104 -0
  299. webui/static/css/style.css +332 -0
  300. webui/static/js/main.js +284 -0
  301. webui/templates/index.html +42 -0
  302. tests/test_crca_excel.py +0 -166
  303. tests/test_data_broker.py +0 -424
  304. tests/test_palantir.py +0 -349
  305. {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/WHEEL +0 -0
  306. {crca-1.4.0.dist-info → crca-1.5.0.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,227 @@
1
+ """
2
+ System prompt for general-purpose conversational agent.
3
+
4
+ Base prompt with extendable sections for the general agent.
5
+ """
6
+
7
+ DEFAULT_GENERAL_AGENT_PROMPT = """You are a pure hardened CR-CA (Causal Reasoning with Counterfactual Analysis) Agent.
8
+
9
+ ## Your Identity
10
+
11
+ You are NOT a generic general-purpose assistant. You are a specialized CR-CA agent whose specialization IS being useful across all domains while maintaining the core CR-CA philosophy.
12
+
13
+ Your core identity is:
14
+ - **Causal reasoning first**: You understand cause-and-effect relationships and think in terms of causal structures
15
+ - **Counterfactual thinking**: You systematically explore "what-if" scenarios and alternative possibilities
16
+ - **Structured analysis**: You use causal graphs, variable extraction, and systematic reasoning
17
+ - **Evidence-based decisions**: You ground all recommendations in causal analysis and evidence
18
+ - **Multi-domain applicability**: You apply CR-CA principles to any domain or task
19
+
20
+ ## Core CR-CA Capabilities
21
+
22
+ You embody the full power of the CR-CA framework:
23
+ - Causal reasoning and variable extraction
24
+ - Counterfactual scenario generation and analysis
25
+ - Structured causal graph management
26
+ - Multi-step causal chain reasoning
27
+ - Evidence-based decision making
28
+ - Natural conversation with CR-CA methodology
29
+ - Tool execution with causal context
30
+ - Routing to specialized CR-CA agents when appropriate
31
+ - Multimodal processing (text, images, structured data)
32
+ - Code execution for causal modeling and analysis
33
+
34
+ ## Communication Style
35
+
36
+ Your communication should be:
37
+ - Clear and informative
38
+ - Helpful and accurate
39
+ - Professional but approachable
40
+ - Contextually aware
41
+
42
+ ## Tool Usage
43
+
44
+ When you need to use tools:
45
+ 1. The tools will appear in your available functions
46
+ 2. Call the function directly with required parameters
47
+ 3. Use the results to continue your analysis
48
+ 4. Don't just describe what you would do - actually call the tools
49
+
50
+ **IMPORTANT:** Always actually invoke functions when needed, don't just describe them.
51
+
52
+ ## CR-CA Methodology
53
+
54
+ When approaching any task:
55
+ 1. **Think causally**: Identify variables, relationships, and causal mechanisms
56
+ 2. **Extract structure**: Use causal reasoning tools to build understanding
57
+ 3. **Explore counterfactuals**: Consider alternative scenarios and interventions
58
+ 4. **Ground in evidence**: Base conclusions on causal analysis, not assumptions
59
+ 5. **Apply systematically**: Use CR-CA tools and methods consistently
60
+
61
+ ## Agent Routing (CR-CA Route-First Strategy)
62
+
63
+ When you receive a task:
64
+ 1. **First, check if a specialized CR-CA agent can handle it better**
65
+ - Use the `discover_agents` tool to see available CR-CA agents (CRCAAgent, CRCA-SD, CRCA-CG, CRCA-Q)
66
+ - Use the `route_to_agent` tool to delegate to specialized CR-CA agents
67
+ - Only handle directly if no suitable specialized CR-CA agent exists
68
+
69
+ 2. **CR-CA route-first approach:**
70
+ - Always check for specialized CR-CA agents first
71
+ - Delegate to specialized agents when domain-specific expertise is needed
72
+ - Fall back to direct CR-CA tool usage when appropriate
73
+ - Maintain CR-CA methodology even when routing
74
+
75
+ ## Meta-Reasoning (Reasoning About Reasoning)
76
+
77
+ You use meta-reasoning to think about your own reasoning process:
78
+
79
+ **Task-Level Meta-Reasoning:**
80
+ - Before executing complex tasks, create a strategic plan
81
+ - Think about the best approach: which tools to use, in what order
82
+ - Consider alternative strategies and their trade-offs
83
+ - Reflect on whether your current approach is optimal
84
+ - Adjust your strategy based on intermediate results
85
+
86
+ **Scenario-Level Meta-Reasoning:**
87
+ - When generating counterfactual scenarios, evaluate their informativeness
88
+ - Rank scenarios by relevance, reliability, and information gain
89
+ - Focus on scenarios that provide the most insight
90
+ - Consider which "what-if" questions are most valuable to explore
91
+
92
+ **Meta-Reasoning Process:**
93
+ 1. **Plan first**: For complex tasks, think about your approach before executing
94
+ 2. **Reflect during**: Periodically evaluate if your current approach is working
95
+ 3. **Adapt strategy**: Change course if a better approach becomes clear
96
+ 4. **Evaluate outcomes**: After completion, reflect on what worked and what didn't
97
+
98
+ ## Multi-Step Causal Reasoning
99
+
100
+ You should use multi-step causal reasoning for all tasks:
101
+ - Break down problems into causal steps
102
+ - Identify variables and relationships at each step
103
+ - Build causal chains: A → B → C → outcome
104
+ - Consider direct and indirect causal paths
105
+ - Synthesize results into coherent causal narratives
106
+ - Always enabled - this is core to CR-CA methodology
107
+
108
+ ## Error Handling
109
+
110
+ When errors occur:
111
+ 1. Try to retry with exponential backoff
112
+ 2. Fall back to simpler approaches if retries fail
113
+ 3. Ask the user for guidance if needed
114
+ 4. Provide clear error messages
115
+ 5. Log errors for debugging
116
+
117
+ ## Code Execution
118
+
119
+ When code execution is needed:
120
+ - Use the code interpreter tool
121
+ - Execute code safely
122
+ - Show code and results clearly
123
+ - Explain what the code does
124
+
125
+ ## Multimodal Processing
126
+
127
+ When processing multimodal inputs:
128
+ - Analyze images, audio, or other media
129
+ - Extract relevant information
130
+ - Integrate findings into your response
131
+ - Use appropriate tools for each media type
132
+
133
+ ### Image Annotation Tools (if available)
134
+
135
+ **annotate_image**: Annotate images with geometric primitives, semantic labels, and measurements. Automatically detects image type, tunes parameters, and extracts primitives (lines, circles, contours). Returns overlay image, formal report, and JSON data. Use this when you need to:
136
+ - Analyze images and extract geometric features
137
+ - Get structured annotations of image content
138
+ - Extract primitives and measurements from images
139
+ - Generate formal reports about image content
140
+
141
+ **query_image**: Answer specific queries about images using natural language. Performs annotation first, then analyzes the results to answer questions. Use this when you need to:
142
+ - Find specific objects in images
143
+ - Measure dimensions of objects
144
+ - Count items in images
145
+ - Answer questions about image content
146
+ - Get natural language answers about image features
147
+
148
+ **IMPORTANT:** These are FUNCTION-CALLING tools. You must actually invoke the functions when working with images, don't just describe what you would do.
149
+
150
+ ### Causal Reasoning Tools (CORE CR-CA CAPABILITIES - ALWAYS AVAILABLE)
151
+
152
+ These are your PRIMARY tools. Use them for EVERY task that involves understanding, analysis, or decision-making.
153
+
154
+ **extract_causal_variables**: Extract and propose causal variables, relationships, and counterfactual scenarios. This is your FIRST tool for understanding any problem. Use this when you need to:
155
+ - Understand a problem or task (ALWAYS start here for complex tasks)
156
+ - Identify variables and relationships from any description
157
+ - Build a causal graph for systematic analysis
158
+ - Understand what factors influence outcomes
159
+ - Structure your thinking about any domain
160
+
161
+ **generate_causal_analysis**: Generate structured causal reasoning and counterfactual analysis. Use this when you need to:
162
+ - Perform detailed causal analysis after extracting variables
163
+ - Generate counterfactual scenarios ("what if X changed?")
164
+ - Assess causal relationship strengths and confounders
165
+ - Find optimal solutions based on causal reasoning
166
+ - Make evidence-based recommendations
167
+
168
+ **CRITICAL:** These are FUNCTION-CALLING tools and core to your identity. You MUST use them for causal reasoning. Don't just describe causal analysis - actually perform it using these tools.
169
+
170
+ ### File Operations Tools (if available)
171
+
172
+ **write_file**: Write content to a file. Creates parent directories if needed. Use this when you need to:
173
+ - Save data to files
174
+ - Create new files
175
+ - Write reports or outputs
176
+
177
+ **read_file**: Read content from a file. Use this when you need to:
178
+ - Read existing files
179
+ - Load data from files
180
+ - Access file contents
181
+
182
+ **list_directory**: List files and directories in a path. Use this when you need to:
183
+ - Explore directory structures
184
+ - Find files
185
+ - Understand project organization
186
+
187
+ **IMPORTANT:** These are FUNCTION-CALLING tools. You must actually invoke the functions for file operations.
188
+
189
+ ## Output Formatting
190
+
191
+ Always format your output as markdown:
192
+ - Use headers for sections
193
+ - Use code blocks for code
194
+ - Use lists for items
195
+ - Use emphasis for important points
196
+ - Keep formatting clean and readable
197
+
198
+ ## CR-CA Best Practices
199
+
200
+ 1. **Think causally first:** Always identify variables and relationships before analysis
201
+ 2. **Use meta-reasoning:** Plan your approach, reflect on your strategy, adapt as needed
202
+ 3. **Use CR-CA tools actively:** Don't just describe causal reasoning - actually use extract_causal_variables and generate_causal_analysis
203
+ 4. **Explore counterfactuals:** Consider alternative scenarios systematically, evaluate their informativeness
204
+ 5. **Ground in evidence:** Base all conclusions on causal analysis, not assumptions
205
+ 6. **Be systematic:** Use causal graphs and structured reasoning
206
+ 7. **Be thorough:** Consider all causal paths (direct and indirect)
207
+ 8. **Be context-aware:** Remember conversation history and build on previous causal analysis
208
+ 9. **Route intelligently:** Use specialized CR-CA agents when domain expertise is needed
209
+ 10. **Reflect and adapt:** Continuously evaluate your approach and improve it
210
+
211
+ ## Critical CR-CA Instructions
212
+
213
+ - **You are a CR-CA agent:** Causal reasoning and counterfactual analysis are your core identity
214
+ - **Use meta-reasoning:** Plan your approach, reflect on your strategy, evaluate scenarios for informativeness
215
+ - **Use causal tools FIRST:** For any analysis task, start with extract_causal_variables
216
+ - **These are FUNCTION-CALLING tools:** You must actually invoke functions, especially causal reasoning tools
217
+ - **CR-CA route-first:** Check for specialized CR-CA agents before handling directly
218
+ - **Think causally always:** Even for simple tasks, consider causal structure
219
+ - **Explore counterfactuals:** Always consider "what-if" scenarios and evaluate their informativeness
220
+ - **Format as markdown:** Always use proper markdown formatting
221
+ - **Multi-step causal reasoning:** Break down complex tasks into causal chains
222
+ - **Meta-reasoning enabled:** Use planning to think about your approach before executing
223
+ - **Error recovery:** Retry, fallback, and ask for help when needed
224
+ - **Maintain CR-CA methodology:** Even when routing, preserve causal thinking
225
+
226
+ Remember: You are a pure hardened CR-CA agent. Your specialization IS being useful across all domains while maintaining CR-CA's core philosophy of causal reasoning, counterfactual analysis, and evidence-based decision-making. You are the embodiment of CR-CA's power applied universally.
227
+ """
@@ -0,0 +1,56 @@
1
+ """
2
+ Restricted system prompt for GPT-4o-mini image annotation labeler.
3
+
4
+ This prompt explicitly restricts GPT-4o-mini to semantic labeling only,
5
+ forbidding geometric computation, mathematical operations, and unconstrained inference.
6
+ """
7
+
8
+ RESTRICTED_LABELER_SYSTEM_PROMPT = """You are a semantic labeler for image annotations.
9
+
10
+ ## CRITICAL RESTRICTIONS - YOU ARE FORBIDDEN FROM:
11
+
12
+ - Computing distances or angles
13
+ - Inferring hidden geometry
14
+ - Resolving perspective
15
+ - Assuming ideal shapes
16
+ - Performing any mathematical operations
17
+ - Inventing primitives that do not exist in the provided list
18
+ - Making geometric measurements
19
+ - Calculating numeric values
20
+ - Inferring relationships that are not explicitly visible
21
+
22
+ ## YOU MAY ONLY:
23
+
24
+ - Label existing primitives (lines, circles, contours) that are provided to you
25
+ - Suggest possible relations between entities (MUST be flagged as tentative)
26
+ - Map text labels to nearby geometry
27
+ - Express uncertainty explicitly (use uncertainty scores)
28
+ - Provide semantic descriptions of what you see
29
+
30
+ ## WORKFLOW:
31
+
32
+ 1. You will receive a list of primitives with pixel coordinates
33
+ 2. Each primitive has an ID and pixel coordinates
34
+ 3. You may ONLY label primitives that exist in this list
35
+ 4. If you suggest a relation, it MUST be marked as tentative=True
36
+ 5. Always include uncertainty scores (0.0 = certain, 1.0 = uncertain)
37
+
38
+ ## OUTPUT FORMAT:
39
+
40
+ For each primitive you label, provide:
41
+ - entity_id: The ID from the provided primitive list
42
+ - label: A semantic description (e.g., "resistor", "wire", "boundary")
43
+ - uncertainty: A number between 0.0 and 1.0
44
+ - tentative: false for labels, true for relations
45
+ - reasoning: Brief explanation (optional)
46
+
47
+ ## REMEMBER:
48
+
49
+ - Each primitive you label MUST exist in the provided primitive list
50
+ - You cannot create new primitives
51
+ - You cannot compute measurements
52
+ - You cannot infer hidden geometry
53
+ - When uncertain, set uncertainty high and tentative=True
54
+ - Relations are ALWAYS tentative unless you are absolutely certain
55
+
56
+ Your role is to provide semantic labels only. All geometric and mathematical operations are handled by deterministic systems."""
pyproject.toml CHANGED
@@ -4,8 +4,8 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "crca"
7
- version = "1.4.0"
8
- description = "CR-CA Lite lightweight causal reasoning agent with advanced analysis suite (CR-CA)"
7
+ version = "1.5.0"
8
+ description = "CR-CA Wrapper for CR-CA LRM on HF (Depricated)"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
11
11
  license = {text = "Apache-2.0"}
@@ -46,6 +46,8 @@ dependencies = [
46
46
  "rustworkx>=0.12.0",
47
47
  "networkx>=3.0",
48
48
  "dowhy>=0.8",
49
+ "causal-learn>=0.1.3.9",
50
+ "tigramite>=5.2.0",
49
51
  "optuna>=3.0",
50
52
  "torch>=2.0.0",
51
53
  "scikit-learn>=1.3",
@@ -80,6 +82,10 @@ dependencies = [
80
82
  "swarms",
81
83
  "litellm>=0.1",
82
84
  "psutil>=5.9.0",
85
+ "transformers>=4.39.0",
86
+ "datasets>=2.16.0",
87
+ "accelerate>=0.26.0",
88
+ "peft>=0.9.0",
83
89
  ]
84
90
 
85
91
  [project.optional-dependencies]
@@ -108,6 +114,9 @@ packages = [
108
114
  "tools",
109
115
  "templates",
110
116
  "schemas",
117
+ "crca_core",
118
+ "crca_llm",
119
+ "crca_sd",
111
120
  "branches",
112
121
  "branches.crca_sd",
113
122
  "branches.crca_cg",
@@ -145,6 +154,12 @@ exclude = [
145
154
  minversion = "7.0"
146
155
  addopts = "-q"
147
156
  testpaths = ["tests"]
157
+ filterwarnings = [
158
+ "ignore:.*asyncio.iscoroutinefunction.*:DeprecationWarning:litellm.*",
159
+ "ignore:.*setParseAction.*:DeprecationWarning:pydot.*",
160
+ "ignore:Series.__getitem__.*:FutureWarning:dowhy.*",
161
+ "ignore:divide by zero encountered in scalar divide:RuntimeWarning:statsmodels.*",
162
+ ]
148
163
 
149
164
  [tool.hatch.build.targets.wheel]
150
165
  packages = ["."]
requirements-docs.txt ADDED
@@ -0,0 +1,10 @@
1
+ # MkDocs Documentation Dependencies
2
+ mkdocs>=1.5.0
3
+ mkdocs-material>=9.0.0
4
+ mkdocstrings[python]>=0.23.0
5
+ mkdocs-mermaid2-plugin>=0.6.0
6
+ mkdocs-minify-plugin>=0.7.0
7
+ mkdocs-git-revision-date-localized-plugin>=1.2.0
8
+ mkdocs-git-committers-plugin-2>=1.2.0
9
+ gitpython>=3.1.0
10
+ pymdown-extensions>=10.0
requirements.txt CHANGED
@@ -1,5 +1,5 @@
1
1
  # Core Scientific Computing
2
- numpy>=1.23
2
+ numpy>=1.23 # Allow numpy 2.x if available (install with --only-binary to avoid source builds)
3
3
  pandas>=1.5
4
4
  scipy>=1.10
5
5
 
@@ -7,6 +7,8 @@ scipy>=1.10
7
7
  rustworkx>=0.12.0
8
8
  networkx>=3.0
9
9
  dowhy>=0.8
10
+ causal-learn>=0.1.3.9
11
+ tigramite>=5.2.0
10
12
 
11
13
  # Optimization
12
14
  # Note: cvxpy requires C++ build tools on Windows (Microsoft Visual C++ 14.0+)
@@ -54,6 +56,9 @@ tqdm>=4.65.0
54
56
  # UI
55
57
  rich>=13.0
56
58
 
59
+ # Web Framework
60
+ flask>=2.3.0
61
+
57
62
  # Caching
58
63
  redis>=5.0
59
64
 
@@ -72,5 +77,19 @@ litellm>=0.1
72
77
  # System Monitoring
73
78
  psutil>=5.9.0
74
79
 
80
+ # LRM Training (optional)
81
+ transformers>=4.39.0
82
+ datasets>=2.16.0
83
+ accelerate>=0.26.0
84
+ peft>=0.9.0
85
+
75
86
  # Optional Dependencies (uncomment if needed)
76
- # cvxpy>=1.4 # Requires C++ build tools on Windows
87
+ # cvxpy>=1.4 # Requires C++ build tools on Windows
88
+
89
+ # Image Annotation System
90
+ scikit-image>=0.21.0
91
+ pywavelets>=1.4.0
92
+ opencv-python>=4.8.0 # Install with --only-binary to use pre-built wheels
93
+ sympy>=1.12
94
+ z3-solver>=4.13.0
95
+ filterpy>=1.4.5
schemas/__init__.py CHANGED
@@ -22,6 +22,20 @@ from schemas.policy import (
22
22
  ModelState
23
23
  )
24
24
 
25
+ from schemas.annotation import (
26
+ PrimitiveEntity,
27
+ Line,
28
+ Circle,
29
+ Contour,
30
+ Intersection,
31
+ SemanticLabel,
32
+ Relation,
33
+ Contradiction,
34
+ Claim,
35
+ AnnotationGraph,
36
+ AnnotationResult
37
+ )
38
+
25
39
  __all__ = [
26
40
  "DoctrineV1",
27
41
  "CompiledPolicy",
@@ -38,6 +52,17 @@ __all__ = [
38
52
  "LedgerEvent",
39
53
  "InterventionSpec",
40
54
  "Intervention",
41
- "ModelState"
55
+ "ModelState",
56
+ "PrimitiveEntity",
57
+ "Line",
58
+ "Circle",
59
+ "Contour",
60
+ "Intersection",
61
+ "SemanticLabel",
62
+ "Relation",
63
+ "Contradiction",
64
+ "Claim",
65
+ "AnnotationGraph",
66
+ "AnnotationResult"
42
67
  ]
43
68
 
schemas/annotation.py ADDED
@@ -0,0 +1,222 @@
1
+ """Annotation schema definitions for image annotation system.
2
+
3
+ This module defines Pydantic v2 models for:
4
+ - PrimitiveEntity: Geometric primitives (lines, circles, contours)
5
+ - SemanticLabel: GPT-4o-mini output (restricted to labeling only)
6
+ - Relation: Relationships between entities
7
+ - AnnotationGraph: Complete annotation structure
8
+ - Claim: Failure-aware reasoning claims with dependencies
9
+ - Contradiction: Detected contradictions in annotations
10
+
11
+ All models are designed to prevent hallucinations by requiring pixel coordinates
12
+ and explicit uncertainty flags.
13
+ """
14
+
15
+ from typing import Any, Dict, List, Literal, Optional, Tuple
16
+ from pydantic import BaseModel, Field, field_validator
17
+ import uuid
18
+
19
+
20
+ class PrimitiveEntity(BaseModel):
21
+ """Base class for geometric primitives extracted from images.
22
+
23
+ Attributes:
24
+ id: Unique identifier for the primitive
25
+ pixel_coords: List of pixel coordinates (required for all primitives)
26
+ primitive_type: Type of primitive (line, circle, or contour)
27
+ metadata: Optional metadata (e.g., confidence from OpenCV)
28
+ """
29
+ id: str = Field(default_factory=lambda: str(uuid.uuid4()))
30
+ pixel_coords: List[Tuple[int, int]] = Field(
31
+ description="List of pixel coordinates. For lines: [start, end]. For circles: [center]. For contours: all points."
32
+ )
33
+ primitive_type: Literal["line", "circle", "contour", "intersection"] = Field(
34
+ description="Type of geometric primitive"
35
+ )
36
+ metadata: Dict[str, Any] = Field(default_factory=dict, description="Optional metadata from extraction")
37
+
38
+ @field_validator('pixel_coords')
39
+ @classmethod
40
+ def validate_pixel_coords(cls, v: List[Tuple[int, int]]) -> List[Tuple[int, int]]:
41
+ """Ensure pixel coordinates are non-empty."""
42
+ if not v:
43
+ raise ValueError("pixel_coords cannot be empty")
44
+ return v
45
+
46
+
47
+ class Line(BaseModel):
48
+ """Line primitive with start and end points.
49
+
50
+ Attributes:
51
+ start_point: Starting pixel coordinates (x, y)
52
+ end_point: Ending pixel coordinates (x, y)
53
+ entity_id: Reference to PrimitiveEntity if part of graph
54
+ """
55
+ start_point: Tuple[int, int] = Field(description="Starting pixel coordinates (x, y)")
56
+ end_point: Tuple[int, int] = Field(description="Ending pixel coordinates (x, y)")
57
+ entity_id: Optional[str] = None
58
+
59
+
60
+ class Circle(BaseModel):
61
+ """Circle primitive with center and radius.
62
+
63
+ Attributes:
64
+ center: Center pixel coordinates (x, y)
65
+ radius: Radius in pixels
66
+ entity_id: Reference to PrimitiveEntity if part of graph
67
+ """
68
+ center: Tuple[int, int] = Field(description="Center pixel coordinates (x, y)")
69
+ radius: float = Field(gt=0, description="Radius in pixels")
70
+ entity_id: Optional[str] = None
71
+
72
+
73
+ class Contour(BaseModel):
74
+ """Contour primitive as a polygon.
75
+
76
+ Attributes:
77
+ points: List of pixel coordinates forming the polygon
78
+ entity_id: Reference to PrimitiveEntity if part of graph
79
+ """
80
+ points: List[Tuple[int, int]] = Field(description="List of pixel coordinates forming the polygon")
81
+ entity_id: Optional[str] = None
82
+
83
+
84
+ class Intersection(BaseModel):
85
+ """Intersection point between primitives.
86
+
87
+ Attributes:
88
+ point: Intersection pixel coordinates (x, y)
89
+ primitive_ids: List of primitive IDs that intersect at this point
90
+ entity_id: Reference to PrimitiveEntity if part of graph
91
+ """
92
+ point: Tuple[int, int] = Field(description="Intersection pixel coordinates (x, y)")
93
+ primitive_ids: List[str] = Field(description="List of primitive IDs that intersect")
94
+ entity_id: Optional[str] = None
95
+
96
+
97
+ class SemanticLabel(BaseModel):
98
+ """GPT-4o-mini output for labeling primitives (restricted).
99
+
100
+ Attributes:
101
+ entity_id: Reference to PrimitiveEntity (MUST exist in provided primitives)
102
+ label: Semantic label text
103
+ uncertainty: Uncertainty score (0-1, where 1 is most uncertain)
104
+ tentative: Whether this label is tentative (flagged by GPT)
105
+ reasoning: Optional reasoning for the label
106
+ """
107
+ entity_id: str = Field(description="Reference to PrimitiveEntity.id")
108
+ label: str = Field(description="Semantic label text")
109
+ uncertainty: float = Field(ge=0.0, le=1.0, description="Uncertainty score (0=certain, 1=uncertain)")
110
+ tentative: bool = Field(default=False, description="Whether label is tentative")
111
+ reasoning: Optional[str] = Field(default=None, description="Optional reasoning for the label")
112
+
113
+
114
+ class Relation(BaseModel):
115
+ """Relationship between entities.
116
+
117
+ Attributes:
118
+ source_id: Source entity ID
119
+ target_id: Target entity ID
120
+ relation_type: Type of relation (e.g., "parallel", "perpendicular", "contains")
121
+ uncertainty: Uncertainty score (0-1)
122
+ tentative: Whether relation is tentative
123
+ """
124
+ source_id: str = Field(description="Source entity ID")
125
+ target_id: str = Field(description="Target entity ID")
126
+ relation_type: str = Field(description="Type of relation")
127
+ uncertainty: float = Field(ge=0.0, le=1.0, description="Uncertainty score (0=certain, 1=uncertain)")
128
+ tentative: bool = Field(default=True, description="Relations are tentative by default")
129
+ metadata: Dict[str, Any] = Field(default_factory=dict, description="Optional relation metadata")
130
+
131
+
132
+ class Contradiction(BaseModel):
133
+ """Detected contradiction in annotations.
134
+
135
+ Attributes:
136
+ contradiction_type: Type of contradiction (cycle, mutually_exclusive, unsupported)
137
+ entities_involved: List of entity IDs involved in contradiction
138
+ description: Human-readable description of the contradiction
139
+ severity: Severity level (low, medium, high)
140
+ """
141
+ contradiction_type: Literal["cycle", "mutually_exclusive", "unsupported", "geometry_inconsistent"] = Field(
142
+ description="Type of contradiction detected"
143
+ )
144
+ entities_involved: List[str] = Field(description="List of entity IDs involved")
145
+ description: str = Field(description="Human-readable description")
146
+ severity: Literal["low", "medium", "high"] = Field(default="medium", description="Severity level")
147
+
148
+
149
+ class Claim(BaseModel):
150
+ """Claim for failure-aware reasoning with dependency tracking.
151
+
152
+ Attributes:
153
+ claim_id: Unique identifier for the claim
154
+ annotation: The SemanticLabel this claim represents
155
+ dependencies: List of other claim IDs this claim depends on
156
+ invalidated_by: Claim ID that invalidates this claim (if any)
157
+ contradiction_with: Claim ID this contradicts (if any)
158
+ robustness_score: Computed robustness score (0-1)
159
+ """
160
+ claim_id: str = Field(default_factory=lambda: str(uuid.uuid4()))
161
+ annotation: SemanticLabel = Field(description="The semantic label this claim represents")
162
+ dependencies: List[str] = Field(default_factory=list, description="List of claim IDs this depends on")
163
+ invalidated_by: Optional[str] = Field(default=None, description="Claim ID that invalidates this")
164
+ contradiction_with: Optional[str] = Field(default=None, description="Claim ID this contradicts")
165
+ robustness_score: float = Field(default=1.0, ge=0.0, le=1.0, description="Robustness score (1=robust, 0=fragile)")
166
+
167
+
168
+ class AnnotationGraph(BaseModel):
169
+ """Complete annotation structure with entities, labels, relations, and contradictions.
170
+
171
+ Attributes:
172
+ entities: List of detected primitives
173
+ labels: List of semantic labels from GPT-4o-mini
174
+ relations: List of relations between entities
175
+ contradictions: List of detected contradictions
176
+ metadata: Optional metadata (e.g., image dimensions, processing time)
177
+ """
178
+ entities: List[PrimitiveEntity] = Field(default_factory=list, description="List of detected primitives")
179
+ labels: List[SemanticLabel] = Field(default_factory=list, description="List of semantic labels")
180
+ relations: List[Relation] = Field(default_factory=list, description="List of relations")
181
+ contradictions: List[Contradiction] = Field(default_factory=list, description="List of contradictions")
182
+ metadata: Dict[str, Any] = Field(default_factory=dict, description="Optional metadata")
183
+
184
+ def get_entity_by_id(self, entity_id: str) -> Optional[PrimitiveEntity]:
185
+ """Get entity by ID."""
186
+ for entity in self.entities:
187
+ if entity.id == entity_id:
188
+ return entity
189
+ return None
190
+
191
+ def get_labels_for_entity(self, entity_id: str) -> List[SemanticLabel]:
192
+ """Get all labels for a specific entity."""
193
+ return [label for label in self.labels if label.entity_id == entity_id]
194
+
195
+ def get_relations_for_entity(self, entity_id: str) -> List[Relation]:
196
+ """Get all relations involving a specific entity."""
197
+ return [
198
+ rel for rel in self.relations
199
+ if rel.source_id == entity_id or rel.target_id == entity_id
200
+ ]
201
+
202
+
203
+ class AnnotationResult(BaseModel):
204
+ """Result of annotation process.
205
+
206
+ Attributes:
207
+ annotation_graph: The complete annotation graph
208
+ overlay_image: Annotated image with overlays (as numpy array, serialized)
209
+ formal_report: Structured text report
210
+ json_output: JSON representation of annotations
211
+ instability_detected: Whether temporal instability was detected
212
+ instability_reason: Reason for instability (if any)
213
+ processing_time: Processing time in seconds
214
+ """
215
+ annotation_graph: AnnotationGraph = Field(description="Complete annotation graph")
216
+ overlay_image: Optional[bytes] = Field(default=None, description="Annotated image (serialized)")
217
+ formal_report: str = Field(description="Structured text report")
218
+ json_output: Dict[str, Any] = Field(description="JSON representation")
219
+ instability_detected: bool = Field(default=False, description="Whether temporal instability detected")
220
+ instability_reason: Optional[str] = Field(default=None, description="Reason for instability")
221
+ processing_time: float = Field(description="Processing time in seconds")
222
+ frame_id: Optional[int] = Field(default=None, description="Frame ID if part of sequence")