text2model 0.1.0__tar.gz

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 (143) hide show
  1. text2model-0.1.0/PKG-INFO +221 -0
  2. text2model-0.1.0/README.md +203 -0
  3. text2model-0.1.0/pyproject.toml +46 -0
  4. text2model-0.1.0/setup.cfg +4 -0
  5. text2model-0.1.0/tests/test_integration.py +107 -0
  6. text2model-0.1.0/tests/test_main.py +62 -0
  7. text2model-0.1.0/tests/test_utils.py +249 -0
  8. text2model-0.1.0/text2model/__init__.py +1 -0
  9. text2model-0.1.0/text2model/grammar.mzn +277 -0
  10. text2model-0.1.0/text2model/knowledge_graphs/CSPLib_12.ttl +41 -0
  11. text2model-0.1.0/text2model/knowledge_graphs/CSPLib_14.ttl +51 -0
  12. text2model-0.1.0/text2model/knowledge_graphs/CSPLib_2.ttl +59 -0
  13. text2model-0.1.0/text2model/knowledge_graphs/CSPLib_22.ttl +62 -0
  14. text2model-0.1.0/text2model/knowledge_graphs/CSPLib_28.ttl +28 -0
  15. text2model-0.1.0/text2model/knowledge_graphs/CSPLib_30.ttl +72 -0
  16. text2model-0.1.0/text2model/knowledge_graphs/CSPLib_34.ttl +52 -0
  17. text2model-0.1.0/text2model/knowledge_graphs/CSPLib_39.ttl +51 -0
  18. text2model-0.1.0/text2model/knowledge_graphs/CSPLib_54.ttl +50 -0
  19. text2model-0.1.0/text2model/knowledge_graphs/CSPLib_6.ttl +36 -0
  20. text2model-0.1.0/text2model/knowledge_graphs/CSPLib_8.ttl +53 -0
  21. text2model-0.1.0/text2model/knowledge_graphs/LPWP_128.ttl +79 -0
  22. text2model-0.1.0/text2model/knowledge_graphs/LPWP_232.ttl +95 -0
  23. text2model-0.1.0/text2model/knowledge_graphs/LPWP_53.ttl +58 -0
  24. text2model-0.1.0/text2model/knowledge_graphs/LPWP_66.ttl +52 -0
  25. text2model-0.1.0/text2model/knowledge_graphs/LPWP_85.ttl +95 -0
  26. text2model-0.1.0/text2model/knowledge_graphs/OR_LP_IP_Scheduling_problem_1.ttl +34 -0
  27. text2model-0.1.0/text2model/knowledge_graphs/OR_LP_IP_Scheduling_problem_18.ttl +59 -0
  28. text2model-0.1.0/text2model/knowledge_graphs/OR_LP_IP_Scheduling_problem_2.ttl +43 -0
  29. text2model-0.1.0/text2model/knowledge_graphs/OR_LP_IP_Scheduling_problem_28.ttl +42 -0
  30. text2model-0.1.0/text2model/knowledge_graphs/OR_LP_IP_Scheduling_problem_31.ttl +44 -0
  31. text2model-0.1.0/text2model/knowledge_graphs/OR_LP_IP_Scheduling_problem_4.ttl +48 -0
  32. text2model-0.1.0/text2model/knowledge_graphs/OR_LP_IP_Scheduling_problem_5.ttl +40 -0
  33. text2model-0.1.0/text2model/knowledge_graphs/OR_LP_IP_Scheduling_problem_6.ttl +61 -0
  34. text2model-0.1.0/text2model/knowledge_graphs/OR_LP_IP_Scheduling_problem_8.ttl +54 -0
  35. text2model-0.1.0/text2model/knowledge_graphs/combinatorial_problem_1.ttl +48 -0
  36. text2model-0.1.0/text2model/knowledge_graphs/combinatorial_problem_31.ttl +43 -0
  37. text2model-0.1.0/text2model/knowledge_graphs/combinatorial_problem_32.ttl +39 -0
  38. text2model-0.1.0/text2model/knowledge_graphs/combinatorial_problem_7.ttl +52 -0
  39. text2model-0.1.0/text2model/knowledge_graphs/complexor_AircraftAssignment.ttl +73 -0
  40. text2model-0.1.0/text2model/knowledge_graphs/complexor_AircraftLanding.ttl +53 -0
  41. text2model-0.1.0/text2model/knowledge_graphs/complexor_CFLP.ttl +43 -0
  42. text2model-0.1.0/text2model/knowledge_graphs/complexor_CapacitatedWarehouseLocation.ttl +59 -0
  43. text2model-0.1.0/text2model/knowledge_graphs/complexor_CarSelection.ttl +30 -0
  44. text2model-0.1.0/text2model/knowledge_graphs/complexor_TSP.ttl +52 -0
  45. text2model-0.1.0/text2model/knowledge_graphs/complexor_VRPTW.ttl +67 -0
  46. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_1.ttl +56 -0
  47. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_10.ttl +46 -0
  48. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_11.ttl +57 -0
  49. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_12.ttl +48 -0
  50. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_13.ttl +48 -0
  51. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_14.ttl +82 -0
  52. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_15.ttl +60 -0
  53. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_16.ttl +64 -0
  54. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_17.ttl +72 -0
  55. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_18.ttl +64 -0
  56. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_19.ttl +98 -0
  57. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_2.ttl +103 -0
  58. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_20.ttl +129 -0
  59. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_21.ttl +65 -0
  60. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_22.ttl +72 -0
  61. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_23.ttl +63 -0
  62. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_24.ttl +99 -0
  63. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_25.ttl +77 -0
  64. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_27.ttl +75 -0
  65. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_28.ttl +50 -0
  66. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_29.ttl +63 -0
  67. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_3.ttl +49 -0
  68. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_30.ttl +49 -0
  69. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_31.ttl +35 -0
  70. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_32.ttl +33 -0
  71. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_33.ttl +49 -0
  72. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_34.ttl +67 -0
  73. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_35.ttl +75 -0
  74. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_36.ttl +56 -0
  75. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_37.ttl +52 -0
  76. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_38.ttl +55 -0
  77. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_39.ttl +58 -0
  78. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_4.ttl +64 -0
  79. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_40.ttl +49 -0
  80. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_41.ttl +62 -0
  81. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_42.ttl +49 -0
  82. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_43.ttl +98 -0
  83. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_44.ttl +41 -0
  84. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_45.ttl +56 -0
  85. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_46.ttl +35 -0
  86. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_47.ttl +68 -0
  87. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_48.ttl +74 -0
  88. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_49.ttl +66 -0
  89. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_5.ttl +69 -0
  90. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_50.ttl +51 -0
  91. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_51.ttl +53 -0
  92. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_52.ttl +51 -0
  93. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_53.ttl +55 -0
  94. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_54.ttl +74 -0
  95. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_55.ttl +50 -0
  96. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_56.ttl +67 -0
  97. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_57.ttl +59 -0
  98. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_58.ttl +65 -0
  99. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_6.ttl +63 -0
  100. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_60.ttl +36 -0
  101. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_61.ttl +83 -0
  102. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_62.ttl +44 -0
  103. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_63.ttl +49 -0
  104. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_64.ttl +41 -0
  105. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_65.ttl +78 -0
  106. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_66.ttl +51 -0
  107. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_67.ttl +40 -0
  108. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_7.ttl +53 -0
  109. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_8.ttl +51 -0
  110. text2model-0.1.0/text2model/knowledge_graphs/nlp4lp_9.ttl +58 -0
  111. text2model-0.1.0/text2model/knowledge_graphs/non_linear_problem_10.ttl +57 -0
  112. text2model-0.1.0/text2model/knowledge_graphs/non_linear_problem_15.ttl +55 -0
  113. text2model-0.1.0/text2model/knowledge_graphs/non_linear_problem_23.ttl +42 -0
  114. text2model-0.1.0/text2model/knowledge_graphs/non_linear_problem_9.ttl +61 -0
  115. text2model-0.1.0/text2model/knowledge_graphs/puzzles_problem_1.ttl +64 -0
  116. text2model-0.1.0/text2model/knowledge_graphs/puzzles_problem_11.ttl +65 -0
  117. text2model-0.1.0/text2model/knowledge_graphs/puzzles_problem_12.ttl +79 -0
  118. text2model-0.1.0/text2model/knowledge_graphs/puzzles_problem_13.ttl +50 -0
  119. text2model-0.1.0/text2model/knowledge_graphs/puzzles_problem_31.ttl +62 -0
  120. text2model-0.1.0/text2model/main.py +771 -0
  121. text2model-0.1.0/text2model/prompts/assembler_prompt.txt +104 -0
  122. text2model-0.1.0/text2model/prompts/code_stitching_prompt.txt +49 -0
  123. text2model-0.1.0/text2model/prompts/code_validation_prompt.txt +36 -0
  124. text2model-0.1.0/text2model/prompts/constraint_generation_prompt.txt +35 -0
  125. text2model-0.1.0/text2model/prompts/cot_prompt.txt +45 -0
  126. text2model-0.1.0/text2model/prompts/global_constraint_prompts/all_different_prompt.txt +67 -0
  127. text2model-0.1.0/text2model/prompts/global_constraint_prompts/count_prompt.txt +75 -0
  128. text2model-0.1.0/text2model/prompts/global_constraint_prompts/cumulative_prompt.txt +88 -0
  129. text2model-0.1.0/text2model/prompts/global_constraint_prompts/global_cardinality_prompt.txt +76 -0
  130. text2model-0.1.0/text2model/prompts/global_constraint_prompts/increasing_prompt.txt +81 -0
  131. text2model-0.1.0/text2model/prompts/global_constraint_prompts/lex_less_prompt.txt +68 -0
  132. text2model-0.1.0/text2model/prompts/grammar_validation_prompt.txt +18 -0
  133. text2model-0.1.0/text2model/prompts/kg_code_generation_prompt.txt +20 -0
  134. text2model-0.1.0/text2model/prompts/kg_generation_prompt.txt +40 -0
  135. text2model-0.1.0/text2model/prompts/objective_generation_prompt.txt +34 -0
  136. text2model-0.1.0/text2model/prompts/parameter_and_variable_generation_prompt.txt +35 -0
  137. text2model-0.1.0/text2model/utils.py +350 -0
  138. text2model-0.1.0/text2model.egg-info/PKG-INFO +221 -0
  139. text2model-0.1.0/text2model.egg-info/SOURCES.txt +141 -0
  140. text2model-0.1.0/text2model.egg-info/dependency_links.txt +1 -0
  141. text2model-0.1.0/text2model.egg-info/entry_points.txt +2 -0
  142. text2model-0.1.0/text2model.egg-info/requires.txt +7 -0
  143. text2model-0.1.0/text2model.egg-info/top_level.txt +1 -0
@@ -0,0 +1,221 @@
1
+ Metadata-Version: 2.4
2
+ Name: text2model
3
+ Version: 0.1.0
4
+ Summary: LLM Modeling Copilots for Text-to-Model Translation
5
+ License: MIT
6
+ Keywords: minizinc,constraint-programming,LLM,text-to-model
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
10
+ Requires-Python: >=3.8
11
+ Description-Content-Type: text/markdown
12
+ Requires-Dist: openai
13
+ Requires-Dist: datasets
14
+ Requires-Dist: tqdm
15
+ Requires-Dist: langchain-ollama
16
+ Provides-Extra: test
17
+ Requires-Dist: pytest; extra == "test"
18
+
19
+ # Text2Model: LLM Modeling Copilots for Text-to-Model Translation
20
+
21
+ [![Tests](https://github.com/skadio/text2model/actions/workflows/tests.yml/badge.svg)](https://github.com/skadio/text2model/actions/workflows/tests.yml)
22
+
23
+ Text-to-model translation is the task of converting natural language descriptions of combinatorial problems into formal constraint models.
24
+
25
+ [Text2Model](https://skadio.github.io/text2model/) is a suite of LLM modeling copilots, datasets, fined-tuned models, demos, interactive editor, and online leaderboard for translating natural language text into formal combinatorial constraint models.
26
+
27
+ Text2Model uses MiniZinc as the target modeling language which makes our copilots both **paradigm- and solver-agnostic**. Our copilots generate models that can be solved by any MiniZinc compatible solver including Gecode, Chuffed, OR-Tools, CBC, Gurobi, Cplex, HiGH. This covers a wide range of paradigms including CP, CP-SAT, and MIP. As such, Text2Model can address **both combinatorial satisfaction and optimization problems.**
28
+
29
+ Please visit [Text2Model](https://skadio.github.io/text2model/) for latest publications and resources.
30
+
31
+ ---
32
+
33
+ ## Text2Model Copilots
34
+
35
+ Text2Model offers different strategies, ranging from simple single-call approaches to sophisticated multi-agent systems. Each makes different trade-offs between speed and accuracy.
36
+
37
+ | Strategy | Description |
38
+ |----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
39
+ | `baseline` | Direct code generation from problem description. No special prompting. Good for simple problems or establishing a baseline. |
40
+ | `cot` | **Chain-of-Thought** prompting with guiding principles. The LLM reasons through the problem step-by-step before generating code. |
41
+ | `knowledge_graph` | First extracts structured information (entities, relationships) from the problem, then generates code from this intermediate representation. |
42
+ | `cot_with_code_validation` | Generates code with CoT, then validates and fixes any compilation errors. Good default choice. |
43
+ | `cot_with_grammar_validation` | Generates code with CoT, then checks against MiniZinc grammar rules. |
44
+ | `cot_with_code_and_grammar_validation` | Combines CoT generation with both grammar checking and code validation. |
45
+ | `agents` | Decomposes the task into specialized agents: (1) parameters & variables, (2) constraints, (3) objective, (4) assembler that stitches everything together. |
46
+ | `agents_with_code_validation` | Agents approach plus a final validation/fix step. |
47
+ | `gala` | Global Agents for different constraint types (all_different, cumulative, etc.) plus an assembler. See the [GALA paper](https://arxiv.org/abs/2509.08970). |
48
+
49
+
50
+ ---
51
+
52
+ ## Quick Start
53
+
54
+ ### 1. Install
55
+
56
+ ```bash
57
+ pip install text2model
58
+ ```
59
+
60
+ Or install from source for development:
61
+
62
+ ```bash
63
+ git clone https://github.com/skadio/text2model.git
64
+ cd text2model
65
+ pip install -e .
66
+ ```
67
+
68
+ ### 2. Set Your API Key
69
+
70
+ ```bash
71
+ export OPENAI_API_KEY="your-api-key-here"
72
+ ```
73
+
74
+ ### 3. Generate MiniZinc from a Problem Description
75
+
76
+ ```bash
77
+ # From a string
78
+ text2model --problem "A country produces fighter jets each year. Some of these jets must be set aside for pilot training instead of combat use. Year 1 production is 10 jets, and Year 2 production is 15 jets. Each training jet can train 5 pilots per year. Training runs for 2 years, starting in Year 1. Determine how many pilots will be trained in total by the end of Year 2."
79
+
80
+ # From a text file
81
+ text2model --problem my_problem.txt
82
+
83
+ # Choose a strategy (default: cot)
84
+ text2model --problem my_problem.txt --strategies agents_with_code_validation --model gpt-4o
85
+
86
+ # Redirect output to a file
87
+ text2model --problem my_problem.txt > model.mzn
88
+ ```
89
+
90
+ ### 4. Batch Mode on the Dataset
91
+
92
+ ```bash
93
+ # Try a quick test on specific problems
94
+ python main.py --strategies cot --problem-ids 0 1 2 --model gpt-4 --output-dir my_results
95
+
96
+ # Or run chain-of-thought on all problems
97
+ python main.py --strategies cot --model gpt-4 --output-dir my_results
98
+ ```
99
+
100
+ ---
101
+
102
+ ## Usage
103
+
104
+ ### Generate from a Problem Description
105
+
106
+ ```bash
107
+ # Inline description (prints MiniZinc to stdout)
108
+ text2model --problem "A country produces fighter jets each year. Some of these jets must be set aside for pilot training instead of combat use. Year 1 production is 10 jets, and Year 2 production is 15 jets. Each training jet can train 5 pilots per year. Training runs for 2 years, starting in Year 1. Determine how many pilots will be trained in total by the end of Year 2."
109
+
110
+ # From a file
111
+ text2model --problem problem.txt --strategies cot_with_code_validation
112
+ ```
113
+
114
+ > The `knowledge_graph` strategy is not available in this mode (it requires pre-built TTL files).
115
+ > The default strategy is `cot`.
116
+
117
+ ### Run Multiple Strategies
118
+
119
+ ```bash
120
+ # Compare baseline vs chain-of-thought
121
+ python main.py --strategies baseline cot --model gpt-4o --output-dir my_results
122
+
123
+ # Run all 9 strategies
124
+ python main.py --strategies all --model gpt-4 --output-dir my_results
125
+ ```
126
+
127
+ ### Filter by Problem Source
128
+
129
+ ```bash
130
+ # List available data sources
131
+ python main.py --list-sources
132
+
133
+ # Run on specific source
134
+ python main.py --strategies cot --model gpt-4 --source nlp4lp --output-dir my_results
135
+ ```
136
+
137
+ ### Advanced Options
138
+
139
+ ```bash
140
+ python main.py --strategies agents --model gpt-4 \
141
+ --output-dir my_results \
142
+ --temperature 0.7 \
143
+ --max-tokens 8192 \
144
+ --sleep-time 2 \
145
+ --include-unverified
146
+ ```
147
+
148
+ ---
149
+
150
+ ## Evaluation
151
+
152
+ After generating models, evaluate their correctness via `evaluate.py`. This script compiles and runs each generated MiniZinc model against test instances, checking for both execution success and solution correctness.
153
+
154
+ ### Prerequisites
155
+
156
+ Install MiniZinc solver: https://www.minizinc.org/doc-2.5.5/en/installation.html
157
+
158
+ ### Run Evaluation
159
+ ```bash
160
+ # Evaluate all generated code
161
+ python evaluate.py --output-dir my_results
162
+ ```
163
+
164
+ > **Note:** `--output-dir` is required. Point it at the directory produced by `main.py`.
165
+
166
+ ### Metrics
167
+
168
+ | Metric | Description |
169
+ |--------|-------------|
170
+ | **Execution Accuracy** | % of models that compile and run without errors |
171
+ | **Solution Accuracy** | % of models that produce correct solutions |
172
+ | **Average Score** | Average of execution and solution accuracy |
173
+
174
+ Results are broken down by problem type (satisfaction vs optimization).
175
+
176
+ ---
177
+
178
+ ## Testing
179
+
180
+ Install test dependencies with `pip install -e ".[test]"`.
181
+
182
+ **Offline tests** (`tests/test_main.py`, `tests/test_utils.py`) don't need an API key, network, or MiniZinc — they're pure logic tests with mocked API calls. This is what CI runs:
183
+
184
+ ```bash
185
+ pytest -m "not integration"
186
+ ```
187
+
188
+ **Integration tests** (`tests/test_integration.py`) hit real external dependencies and are opt-in only — never run in CI:
189
+ - MiniZinc tests run the real `minizinc` binary and are skipped unless it's on `PATH`.
190
+ - The OpenAI test makes exactly one real, cheap, token-capped call (`gpt-4o-mini`, `max_tokens=20`) and is skipped unless `OPENAI_API_KEY` is set. It's intentionally not exhaustive to avoid API costs.
191
+
192
+ To run everything locally (with `OPENAI_API_KEY` set and MiniZinc installed):
193
+
194
+ ```bash
195
+ pytest -m ""
196
+ ```
197
+
198
+ ---
199
+
200
+ ## Repository Structure
201
+
202
+ ```
203
+ text2model/
204
+ ├── text2model/ # Installable Python package
205
+ │ ├── prompts/ # Prompt templates for each strategy
206
+ │ │ ├── cot_prompt.txt
207
+ │ │ ├── code_validation_prompt.txt
208
+ │ │ ├── global_constraint_prompts/
209
+ │ │ └── ...
210
+ │ ├── knowledge_graphs/ # KG files (.ttl) for knowledge_graph strategy
211
+ │ ├── grammar.mzn # MiniZinc grammar for validation
212
+ │ ├── main.py # Copilot strategies and CLI entry point
213
+ │ └── utils.py # Shared utilities (API calls, validation)
214
+ ├── output/ # Generated models (created automatically)
215
+ │ ├── [model]/[strategy]/ # e.g., gpt-4/cot/problem_1.mzn
216
+ │ └── evaluation_results/ # Accuracy metrics and leaderboard
217
+ ├── evaluate.py # Evaluates generated MiniZinc models
218
+ ├── generate_knowledge_graph.py # Generates KGs for knowledge_graph strategy
219
+ ├── main.py # Backward-compatible entry point
220
+ └── pyproject.toml # Package metadata and install config
221
+ ```
@@ -0,0 +1,203 @@
1
+ # Text2Model: LLM Modeling Copilots for Text-to-Model Translation
2
+
3
+ [![Tests](https://github.com/skadio/text2model/actions/workflows/tests.yml/badge.svg)](https://github.com/skadio/text2model/actions/workflows/tests.yml)
4
+
5
+ Text-to-model translation is the task of converting natural language descriptions of combinatorial problems into formal constraint models.
6
+
7
+ [Text2Model](https://skadio.github.io/text2model/) is a suite of LLM modeling copilots, datasets, fined-tuned models, demos, interactive editor, and online leaderboard for translating natural language text into formal combinatorial constraint models.
8
+
9
+ Text2Model uses MiniZinc as the target modeling language which makes our copilots both **paradigm- and solver-agnostic**. Our copilots generate models that can be solved by any MiniZinc compatible solver including Gecode, Chuffed, OR-Tools, CBC, Gurobi, Cplex, HiGH. This covers a wide range of paradigms including CP, CP-SAT, and MIP. As such, Text2Model can address **both combinatorial satisfaction and optimization problems.**
10
+
11
+ Please visit [Text2Model](https://skadio.github.io/text2model/) for latest publications and resources.
12
+
13
+ ---
14
+
15
+ ## Text2Model Copilots
16
+
17
+ Text2Model offers different strategies, ranging from simple single-call approaches to sophisticated multi-agent systems. Each makes different trade-offs between speed and accuracy.
18
+
19
+ | Strategy | Description |
20
+ |----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
21
+ | `baseline` | Direct code generation from problem description. No special prompting. Good for simple problems or establishing a baseline. |
22
+ | `cot` | **Chain-of-Thought** prompting with guiding principles. The LLM reasons through the problem step-by-step before generating code. |
23
+ | `knowledge_graph` | First extracts structured information (entities, relationships) from the problem, then generates code from this intermediate representation. |
24
+ | `cot_with_code_validation` | Generates code with CoT, then validates and fixes any compilation errors. Good default choice. |
25
+ | `cot_with_grammar_validation` | Generates code with CoT, then checks against MiniZinc grammar rules. |
26
+ | `cot_with_code_and_grammar_validation` | Combines CoT generation with both grammar checking and code validation. |
27
+ | `agents` | Decomposes the task into specialized agents: (1) parameters & variables, (2) constraints, (3) objective, (4) assembler that stitches everything together. |
28
+ | `agents_with_code_validation` | Agents approach plus a final validation/fix step. |
29
+ | `gala` | Global Agents for different constraint types (all_different, cumulative, etc.) plus an assembler. See the [GALA paper](https://arxiv.org/abs/2509.08970). |
30
+
31
+
32
+ ---
33
+
34
+ ## Quick Start
35
+
36
+ ### 1. Install
37
+
38
+ ```bash
39
+ pip install text2model
40
+ ```
41
+
42
+ Or install from source for development:
43
+
44
+ ```bash
45
+ git clone https://github.com/skadio/text2model.git
46
+ cd text2model
47
+ pip install -e .
48
+ ```
49
+
50
+ ### 2. Set Your API Key
51
+
52
+ ```bash
53
+ export OPENAI_API_KEY="your-api-key-here"
54
+ ```
55
+
56
+ ### 3. Generate MiniZinc from a Problem Description
57
+
58
+ ```bash
59
+ # From a string
60
+ text2model --problem "A country produces fighter jets each year. Some of these jets must be set aside for pilot training instead of combat use. Year 1 production is 10 jets, and Year 2 production is 15 jets. Each training jet can train 5 pilots per year. Training runs for 2 years, starting in Year 1. Determine how many pilots will be trained in total by the end of Year 2."
61
+
62
+ # From a text file
63
+ text2model --problem my_problem.txt
64
+
65
+ # Choose a strategy (default: cot)
66
+ text2model --problem my_problem.txt --strategies agents_with_code_validation --model gpt-4o
67
+
68
+ # Redirect output to a file
69
+ text2model --problem my_problem.txt > model.mzn
70
+ ```
71
+
72
+ ### 4. Batch Mode on the Dataset
73
+
74
+ ```bash
75
+ # Try a quick test on specific problems
76
+ python main.py --strategies cot --problem-ids 0 1 2 --model gpt-4 --output-dir my_results
77
+
78
+ # Or run chain-of-thought on all problems
79
+ python main.py --strategies cot --model gpt-4 --output-dir my_results
80
+ ```
81
+
82
+ ---
83
+
84
+ ## Usage
85
+
86
+ ### Generate from a Problem Description
87
+
88
+ ```bash
89
+ # Inline description (prints MiniZinc to stdout)
90
+ text2model --problem "A country produces fighter jets each year. Some of these jets must be set aside for pilot training instead of combat use. Year 1 production is 10 jets, and Year 2 production is 15 jets. Each training jet can train 5 pilots per year. Training runs for 2 years, starting in Year 1. Determine how many pilots will be trained in total by the end of Year 2."
91
+
92
+ # From a file
93
+ text2model --problem problem.txt --strategies cot_with_code_validation
94
+ ```
95
+
96
+ > The `knowledge_graph` strategy is not available in this mode (it requires pre-built TTL files).
97
+ > The default strategy is `cot`.
98
+
99
+ ### Run Multiple Strategies
100
+
101
+ ```bash
102
+ # Compare baseline vs chain-of-thought
103
+ python main.py --strategies baseline cot --model gpt-4o --output-dir my_results
104
+
105
+ # Run all 9 strategies
106
+ python main.py --strategies all --model gpt-4 --output-dir my_results
107
+ ```
108
+
109
+ ### Filter by Problem Source
110
+
111
+ ```bash
112
+ # List available data sources
113
+ python main.py --list-sources
114
+
115
+ # Run on specific source
116
+ python main.py --strategies cot --model gpt-4 --source nlp4lp --output-dir my_results
117
+ ```
118
+
119
+ ### Advanced Options
120
+
121
+ ```bash
122
+ python main.py --strategies agents --model gpt-4 \
123
+ --output-dir my_results \
124
+ --temperature 0.7 \
125
+ --max-tokens 8192 \
126
+ --sleep-time 2 \
127
+ --include-unverified
128
+ ```
129
+
130
+ ---
131
+
132
+ ## Evaluation
133
+
134
+ After generating models, evaluate their correctness via `evaluate.py`. This script compiles and runs each generated MiniZinc model against test instances, checking for both execution success and solution correctness.
135
+
136
+ ### Prerequisites
137
+
138
+ Install MiniZinc solver: https://www.minizinc.org/doc-2.5.5/en/installation.html
139
+
140
+ ### Run Evaluation
141
+ ```bash
142
+ # Evaluate all generated code
143
+ python evaluate.py --output-dir my_results
144
+ ```
145
+
146
+ > **Note:** `--output-dir` is required. Point it at the directory produced by `main.py`.
147
+
148
+ ### Metrics
149
+
150
+ | Metric | Description |
151
+ |--------|-------------|
152
+ | **Execution Accuracy** | % of models that compile and run without errors |
153
+ | **Solution Accuracy** | % of models that produce correct solutions |
154
+ | **Average Score** | Average of execution and solution accuracy |
155
+
156
+ Results are broken down by problem type (satisfaction vs optimization).
157
+
158
+ ---
159
+
160
+ ## Testing
161
+
162
+ Install test dependencies with `pip install -e ".[test]"`.
163
+
164
+ **Offline tests** (`tests/test_main.py`, `tests/test_utils.py`) don't need an API key, network, or MiniZinc — they're pure logic tests with mocked API calls. This is what CI runs:
165
+
166
+ ```bash
167
+ pytest -m "not integration"
168
+ ```
169
+
170
+ **Integration tests** (`tests/test_integration.py`) hit real external dependencies and are opt-in only — never run in CI:
171
+ - MiniZinc tests run the real `minizinc` binary and are skipped unless it's on `PATH`.
172
+ - The OpenAI test makes exactly one real, cheap, token-capped call (`gpt-4o-mini`, `max_tokens=20`) and is skipped unless `OPENAI_API_KEY` is set. It's intentionally not exhaustive to avoid API costs.
173
+
174
+ To run everything locally (with `OPENAI_API_KEY` set and MiniZinc installed):
175
+
176
+ ```bash
177
+ pytest -m ""
178
+ ```
179
+
180
+ ---
181
+
182
+ ## Repository Structure
183
+
184
+ ```
185
+ text2model/
186
+ ├── text2model/ # Installable Python package
187
+ │ ├── prompts/ # Prompt templates for each strategy
188
+ │ │ ├── cot_prompt.txt
189
+ │ │ ├── code_validation_prompt.txt
190
+ │ │ ├── global_constraint_prompts/
191
+ │ │ └── ...
192
+ │ ├── knowledge_graphs/ # KG files (.ttl) for knowledge_graph strategy
193
+ │ ├── grammar.mzn # MiniZinc grammar for validation
194
+ │ ├── main.py # Copilot strategies and CLI entry point
195
+ │ └── utils.py # Shared utilities (API calls, validation)
196
+ ├── output/ # Generated models (created automatically)
197
+ │ ├── [model]/[strategy]/ # e.g., gpt-4/cot/problem_1.mzn
198
+ │ └── evaluation_results/ # Accuracy metrics and leaderboard
199
+ ├── evaluate.py # Evaluates generated MiniZinc models
200
+ ├── generate_knowledge_graph.py # Generates KGs for knowledge_graph strategy
201
+ ├── main.py # Backward-compatible entry point
202
+ └── pyproject.toml # Package metadata and install config
203
+ ```
@@ -0,0 +1,46 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "text2model"
7
+ version = "0.1.0"
8
+ description = "LLM Modeling Copilots for Text-to-Model Translation"
9
+ readme = "README.md"
10
+ requires-python = ">=3.8"
11
+ license = { text = "MIT" }
12
+ keywords = ["minizinc", "constraint-programming", "LLM", "text-to-model"]
13
+ classifiers = [
14
+ "Programming Language :: Python :: 3",
15
+ "License :: OSI Approved :: MIT License",
16
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
17
+ ]
18
+ dependencies = [
19
+ "openai",
20
+ "datasets",
21
+ "tqdm",
22
+ "langchain-ollama",
23
+ ]
24
+
25
+ [project.optional-dependencies]
26
+ test = ["pytest"]
27
+
28
+ [tool.pytest.ini_options]
29
+ markers = [
30
+ "integration: hits a real external dependency (OpenAI API, MiniZinc binary). Skipped automatically when the credential/binary is missing, and always excluded in CI.",
31
+ ]
32
+
33
+ [project.scripts]
34
+ text2model = "text2model.main:main"
35
+
36
+ [tool.setuptools.packages.find]
37
+ where = ["."]
38
+ include = ["text2model*"]
39
+
40
+ [tool.setuptools.package-data]
41
+ "text2model" = [
42
+ "prompts/*.txt",
43
+ "prompts/global_constraint_prompts/*.txt",
44
+ "grammar.mzn",
45
+ "knowledge_graphs/*.ttl",
46
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,107 @@
1
+ """Opt-in tests that hit real external dependencies.
2
+
3
+ These are NOT run in CI (see .github/workflows/tests.yml, which passes
4
+ `-m "not integration"`) and are skipped automatically whenever the
5
+ dependency they need isn't available locally:
6
+
7
+ - MiniZinc tests need the `minizinc` executable on PATH.
8
+ - OpenAI tests need OPENAI_API_KEY in the environment.
9
+
10
+ Run everything (offline + integration) locally with:
11
+ pytest -m ""
12
+
13
+ Run only the integration subset with:
14
+ pytest -m integration
15
+ """
16
+ import os
17
+ import shutil
18
+ import sys
19
+ from pathlib import Path
20
+
21
+ import pytest
22
+
23
+ from text2model import main, utils
24
+
25
+ pytestmark = pytest.mark.integration
26
+
27
+ requires_minizinc = pytest.mark.skipif(
28
+ shutil.which("minizinc") is None,
29
+ reason="minizinc executable not found on PATH",
30
+ )
31
+
32
+ requires_openai_key = pytest.mark.skipif(
33
+ not os.getenv("OPENAI_API_KEY"),
34
+ reason="OPENAI_API_KEY not set in the environment",
35
+ )
36
+
37
+
38
+ # ── MiniZinc: real binary, no network ────────────────────────────────────────
39
+
40
+ @requires_minizinc
41
+ def test_check_syntax_accepts_valid_model():
42
+ code = "var 1..10: x;\nconstraint x > 5;\nsolve satisfy;"
43
+ assert utils.check_syntax(code, "") is None
44
+
45
+
46
+ @requires_minizinc
47
+ def test_check_syntax_rejects_invalid_model():
48
+ code = "var int x;\nsolve satisfy" # missing colon and semicolon
49
+ error = utils.check_syntax(code, "")
50
+ assert error
51
+
52
+
53
+ # ── OpenAI: one cheap, bounded call — not run exhaustively ──────────────────
54
+
55
+ @requires_openai_key
56
+ def test_call_openai_api_live_returns_text():
57
+ import openai
58
+
59
+ client = openai.OpenAI()
60
+ original_config = dict(utils.API_CONFIG)
61
+ utils.API_CONFIG.update({"model": "gpt-4o-mini", "temperature": 0, "max_tokens": 20})
62
+ try:
63
+ result = utils.call_openai_api(client, "Reply with exactly: OK")
64
+ finally:
65
+ utils.API_CONFIG.clear()
66
+ utils.API_CONFIG.update(original_config)
67
+
68
+ assert result
69
+
70
+
71
+ # ── main --problem: end-to-end run from a plain-text description ───────────
72
+
73
+ @requires_openai_key
74
+ def test_run_problem_mode_generates_minizinc_from_description(monkeypatch, capsys):
75
+ problem_description = """A country produces fighter jets each year. Some of these jets must be set aside for pilot training instead of combat use. Year 1 production is 10 jets, and Year 2 production is 15 jets. Each training jet can train 5 pilots per year. Training runs for 2 years, starting in Year 1. Determine how many pilots will be trained in total by the end of Year 2."""
76
+
77
+ monkeypatch.setattr(sys, "argv", [
78
+ "text2model",
79
+ "--problem", problem_description,
80
+ "--model", "gpt-4o",
81
+ "--strategies", "cot",
82
+ ])
83
+
84
+ main.main()
85
+
86
+ generated_code = capsys.readouterr().out
87
+ assert generated_code.strip()
88
+
89
+
90
+ # ── main batch mode: one problem id, one strategy, real dataset + API ──────
91
+
92
+ @requires_openai_key
93
+ def test_main_batch_mode_single_problem_single_strategy(tmp_path, monkeypatch):
94
+ output_dir = str(tmp_path / "output")
95
+
96
+ monkeypatch.setattr(sys, "argv", [
97
+ "text2model",
98
+ "--model", "gpt-4o",
99
+ "--strategies", "baseline",
100
+ "--problem-ids", "0",
101
+ "--output-dir", output_dir,
102
+ ])
103
+
104
+ main.main()
105
+
106
+ summary_path = Path(output_dir) / "gpt-4o" / "summary.json"
107
+ assert summary_path.exists()
@@ -0,0 +1,62 @@
1
+ """Basic offline tests for text2model.main — no network, API key, or MiniZinc binary required."""
2
+ from text2model import main
3
+
4
+
5
+ def test_strategy_map_covers_all_documented_strategies():
6
+ expected = {
7
+ "baseline", "cot", "knowledge_graph",
8
+ "cot_with_code_validation", "cot_with_grammar_validation",
9
+ "cot_with_code_and_grammar_validation",
10
+ "agents", "agents_with_code_validation", "gala",
11
+ }
12
+ assert expected == set(main._STRATEGY_MAP.keys())
13
+
14
+
15
+ def test_check_already_processed_false_when_missing(tmp_path):
16
+ assert main.check_already_processed(str(tmp_path), "prob_1") is False
17
+
18
+
19
+ def test_check_already_processed_false_for_empty_file(tmp_path):
20
+ (tmp_path / "prob_1.mzn").write_text("")
21
+ assert main.check_already_processed(str(tmp_path), "prob_1") is False
22
+
23
+
24
+ def test_check_already_processed_true_when_present(tmp_path):
25
+ (tmp_path / "prob_1.mzn").write_text("var int: x;")
26
+ assert main.check_already_processed(str(tmp_path), "prob_1") is True
27
+
28
+
29
+ def test_run_baseline_strategy_saves_solution_on_success(tmp_path, monkeypatch):
30
+ problem = {
31
+ "input.json": repr({
32
+ "description": "desc",
33
+ "parameters": [],
34
+ "metadata": {"objective": "unknown", "identifier": "p1"},
35
+ }),
36
+ "data.dzn": "",
37
+ }
38
+
39
+ monkeypatch.setattr(main.utils, "call_api", lambda client, model, prompt: "var int: x;")
40
+
41
+ result = main.run_baseline_strategy(None, "cot", problem, "p1", str(tmp_path))
42
+
43
+ assert result is True
44
+ assert (tmp_path / "p1.mzn").read_text() == "var int: x;"
45
+
46
+
47
+ def test_run_baseline_strategy_returns_false_when_api_fails(tmp_path, monkeypatch):
48
+ problem = {
49
+ "input.json": repr({
50
+ "description": "desc",
51
+ "parameters": [],
52
+ "metadata": {"objective": "unknown", "identifier": "p1"},
53
+ }),
54
+ "data.dzn": "",
55
+ }
56
+
57
+ monkeypatch.setattr(main.utils, "call_api", lambda client, model, prompt: None)
58
+
59
+ result = main.run_baseline_strategy(None, "cot", problem, "p1", str(tmp_path))
60
+
61
+ assert result is False
62
+ assert not (tmp_path / "p1.mzn").exists()