magi-core 0.3.1__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.
- magi_core-0.3.1/LICENSE +21 -0
- magi_core-0.3.1/PKG-INFO +641 -0
- magi_core-0.3.1/README.md +591 -0
- magi_core-0.3.1/magi_core/__init__.py +11 -0
- magi_core-0.3.1/magi_core/cli.py +256 -0
- magi_core-0.3.1/magi_core/core.py +883 -0
- magi_core-0.3.1/magi_core/prompts.yaml +137 -0
- magi_core-0.3.1/magi_core/utils.py +43 -0
- magi_core-0.3.1/magi_core.egg-info/PKG-INFO +641 -0
- magi_core-0.3.1/magi_core.egg-info/SOURCES.txt +17 -0
- magi_core-0.3.1/magi_core.egg-info/dependency_links.txt +1 -0
- magi_core-0.3.1/magi_core.egg-info/entry_points.txt +2 -0
- magi_core-0.3.1/magi_core.egg-info/requires.txt +7 -0
- magi_core-0.3.1/magi_core.egg-info/top_level.txt +1 -0
- magi_core-0.3.1/pyproject.toml +53 -0
- magi_core-0.3.1/setup.cfg +4 -0
- magi_core-0.3.1/tests/test_cli.py +639 -0
- magi_core-0.3.1/tests/test_core.py +907 -0
- magi_core-0.3.1/tests/test_integration_matrix.py +243 -0
magi_core-0.3.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jason Chao
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
magi_core-0.3.1/PKG-INFO
ADDED
|
@@ -0,0 +1,641 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: magi-core
|
|
3
|
+
Version: 0.3.1
|
|
4
|
+
Summary: Multi-Agent Group Intelligence: orchestrate a council of LLMs to deliberate on complex questions
|
|
5
|
+
License: MIT License
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2026 Jason Chao
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
|
|
27
|
+
Project-URL: Homepage, https://github.com/jason-chao/magi
|
|
28
|
+
Project-URL: Repository, https://github.com/jason-chao/magi
|
|
29
|
+
Project-URL: Issues, https://github.com/jason-chao/magi/issues
|
|
30
|
+
Keywords: llm,ai,decision-making,multi-agent,deliberation
|
|
31
|
+
Classifier: Development Status :: 3 - Alpha
|
|
32
|
+
Classifier: Intended Audience :: Developers
|
|
33
|
+
Classifier: Intended Audience :: Science/Research
|
|
34
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
35
|
+
Classifier: Programming Language :: Python :: 3
|
|
36
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
39
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
40
|
+
Requires-Python: >=3.10
|
|
41
|
+
Description-Content-Type: text/markdown
|
|
42
|
+
License-File: LICENSE
|
|
43
|
+
Requires-Dist: litellm>=1.44.12
|
|
44
|
+
Requires-Dist: pyyaml>=6.0.1
|
|
45
|
+
Requires-Dist: python-dotenv>=1.0.0
|
|
46
|
+
Provides-Extra: dev
|
|
47
|
+
Requires-Dist: pytest>=8.0.0; extra == "dev"
|
|
48
|
+
Requires-Dist: pytest-asyncio>=0.23.0; extra == "dev"
|
|
49
|
+
Dynamic: license-file
|
|
50
|
+
|
|
51
|
+
# MAGI: Multi-Agent Group Intelligence
|
|
52
|
+
|
|
53
|
+
**What if you could consult a council of diverse artificial minds before making a tough decision?**
|
|
54
|
+
|
|
55
|
+
MAGI is a decision-support system inspired by the **MAGI supercomputer from *Neon Genesis Evangelion***. In the anime, three distinct AI personas deliberate to govern Tokyo-3. Similarly, this project orchestrates a council of multiple LLMs to deliberate, vote, and reason about complex moral, ethical, and practical questions. By aggregating diverse AI perspectives, MAGI simulates a more human-like deliberation process — revealing consensus, surfacing minority risks, and synthesising collective wisdom.
|
|
56
|
+
|
|
57
|
+
## How It Works
|
|
58
|
+
|
|
59
|
+
MAGI sends a question to all configured models in parallel. Each model responds in a structured JSON format (answer + reasoning + confidence score). A **Rapporteur** — the most confident model — then synthesises the group's findings into a final report. An optional **Deliberative Round** lets agents read each other's initial responses before finalising their own, mimicking human group deliberation. During deliberation, each model is assigned a random anonymous ID (e.g. `Participant X7K2`) to reduce brand bias in peer review.
|
|
60
|
+
|
|
61
|
+
## Decision Modes
|
|
62
|
+
|
|
63
|
+
| Mode | What it does |
|
|
64
|
+
|------|-------------|
|
|
65
|
+
| `VoteYesNo` | Democratic Yes / No / Abstain vote |
|
|
66
|
+
| `VoteOptions` | Vote on a custom set of options |
|
|
67
|
+
| `Majority` | Summarises the prevailing opinion |
|
|
68
|
+
| `Consensus` | Finds common ground across all views |
|
|
69
|
+
| `Minority` | Surfaces dissenting and overlooked perspectives |
|
|
70
|
+
| `Probability` | Estimates the likelihood of a statement being true |
|
|
71
|
+
| `Compose` | Generates content and ranks it via blind peer review |
|
|
72
|
+
| `Synthesis` | Comprehensively combines **all** perspectives into one unified response |
|
|
73
|
+
|
|
74
|
+
**Synthesis** is the most inclusive mode — unlike `Majority` (which amplifies the dominant view) or `Consensus` (which finds the lowest common denominator), `Synthesis` instructs the rapporteur to weave every argument, nuance, and disagreement into a single coherent narrative.
|
|
75
|
+
|
|
76
|
+
## Decision Flows
|
|
77
|
+
|
|
78
|
+
### Standard flow (all methods)
|
|
79
|
+
|
|
80
|
+
Every deliberation follows this pipeline regardless of mode:
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
┌──────────────────────────────────────────────────┐
|
|
84
|
+
│ User Prompt │
|
|
85
|
+
└────────────────────────┬─────────────────────────┘
|
|
86
|
+
│ dispatched in parallel
|
|
87
|
+
┌───────────────┼───────────────┐
|
|
88
|
+
▼ ▼ ▼
|
|
89
|
+
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
|
|
90
|
+
│ LLM 1 │ │ LLM 2 │ │ LLM N │
|
|
91
|
+
│ response │ │ response │ │ response │
|
|
92
|
+
│ reason │ │ reason │ │ reason │
|
|
93
|
+
│ confidence │ │ confidence │ │ confidence │
|
|
94
|
+
└──────┬──────┘ └──────┬──────┘ └──────┬──────┘
|
|
95
|
+
└────────────────┼────────────────┘
|
|
96
|
+
│ results collected
|
|
97
|
+
▼
|
|
98
|
+
┌────────────────────────┐
|
|
99
|
+
│ Aggregate │
|
|
100
|
+
│ (method-dependent) │
|
|
101
|
+
└────────────┬───────────┘
|
|
102
|
+
│
|
|
103
|
+
▼
|
|
104
|
+
┌────────────────────────┐
|
|
105
|
+
│ Rapporteur selected │
|
|
106
|
+
│ (by confidence score) │
|
|
107
|
+
└────────────┬───────────┘
|
|
108
|
+
│
|
|
109
|
+
▼
|
|
110
|
+
┌────────────────────────┐
|
|
111
|
+
│ Final Report │
|
|
112
|
+
│ text │ JSON │
|
|
113
|
+
└────────────────────────┘
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Aggregation by method
|
|
117
|
+
|
|
118
|
+
The "Aggregate" step is what distinguishes each mode:
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
Responses collected
|
|
122
|
+
│
|
|
123
|
+
├─ VoteYesNo / VoteOptions ──► tally votes ──► declare winner (if > threshold)
|
|
124
|
+
│ │
|
|
125
|
+
│ rapporteur summarises vote
|
|
126
|
+
│
|
|
127
|
+
├─ Majority ──────────────────────────────► highest-confidence model
|
|
128
|
+
│ summarises prevailing view
|
|
129
|
+
│
|
|
130
|
+
├─ Consensus ─────────────────────────────► highest-confidence model
|
|
131
|
+
│ identifies common ground
|
|
132
|
+
│
|
|
133
|
+
├─ Minority ──────────────────────────────► lowest-confidence model
|
|
134
|
+
│ surfaces dissent and gaps
|
|
135
|
+
│
|
|
136
|
+
├─ Probability ───────────────────────────► compute average / median score
|
|
137
|
+
│ median model writes analysis
|
|
138
|
+
│
|
|
139
|
+
├─ Compose ───────────────────────────────► generate texts (Round 1)
|
|
140
|
+
│ │
|
|
141
|
+
│ blind peer rating (Round 2)
|
|
142
|
+
│ │
|
|
143
|
+
│ ranked output, no rapporteur
|
|
144
|
+
│
|
|
145
|
+
└─ Synthesis ─────────────────────────────► highest-confidence model
|
|
146
|
+
weaves ALL views into one narrative
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Deliberative mode (`--deliberative`)
|
|
150
|
+
|
|
151
|
+
An optional second round where each agent reads its peers' anonymous responses before finalising:
|
|
152
|
+
|
|
153
|
+
```
|
|
154
|
+
┌──────────────────────────────────────────────────────────────┐
|
|
155
|
+
│ Round 1 │
|
|
156
|
+
│ │
|
|
157
|
+
│ Prompt ──► LLM 1 ──► response1 │
|
|
158
|
+
│ ──► LLM 2 ──► response2 │
|
|
159
|
+
│ ──► LLM N ──► responseN │
|
|
160
|
+
│ │ │
|
|
161
|
+
│ Aggregate + Rapporteur ──► Pre-Deliberation Report │
|
|
162
|
+
└──────────────────────────────────────────────────────────────┘
|
|
163
|
+
│ responses shared anonymously
|
|
164
|
+
▼ (agents see peers' views, not their names)
|
|
165
|
+
┌──────────────────────────────────────────────────────────────┐
|
|
166
|
+
│ Round 2 │
|
|
167
|
+
│ │
|
|
168
|
+
│ Prompt + peers' Round 1 responses │
|
|
169
|
+
│ ──► LLM 1 (sees 2..N) ──► response1' │
|
|
170
|
+
│ ──► LLM 2 (sees 1,3..N) ──► response2' │
|
|
171
|
+
│ ──► LLM N (sees 1..N-1) ──► responseN' │
|
|
172
|
+
│ │ │
|
|
173
|
+
│ Aggregate + Rapporteur ──► Post-Deliberation Report │
|
|
174
|
+
└──────────────────────────────────────────────────────────────┘
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Installation
|
|
178
|
+
|
|
179
|
+
### From PyPI
|
|
180
|
+
|
|
181
|
+
```bash
|
|
182
|
+
pip install magi-core
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### From source
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
git clone https://github.com/jason-chao/magi
|
|
189
|
+
cd magi
|
|
190
|
+
pip install -e .
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## Configuration
|
|
194
|
+
|
|
195
|
+
### API Keys
|
|
196
|
+
|
|
197
|
+
You only need a key for each provider you actually use.
|
|
198
|
+
|
|
199
|
+
**CLI:** The `magi` command automatically loads a `.env` file from your working directory. Copy `.env.example` and fill in your keys:
|
|
200
|
+
|
|
201
|
+
```bash
|
|
202
|
+
cp .env.example .env
|
|
203
|
+
# then edit .env with your actual keys
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
Or set them directly as environment variables:
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
export OPENAI_API_KEY=your-key-here
|
|
210
|
+
export ANTHROPIC_API_KEY=your-key-here
|
|
211
|
+
export GEMINI_API_KEY=your-key-here
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
**Package:** API keys must be set as environment variables before calling `run()` or `run_structured()`. The package does **not** auto-load `.env` — call `load_dotenv()` yourself if needed:
|
|
215
|
+
|
|
216
|
+
```python
|
|
217
|
+
from dotenv import load_dotenv
|
|
218
|
+
load_dotenv() # reads .env from the current directory
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### `config.yaml`
|
|
222
|
+
|
|
223
|
+
**CLI:** Required (unless you pass `--llms` on the command line). Place it in your working directory:
|
|
224
|
+
|
|
225
|
+
```yaml
|
|
226
|
+
llms:
|
|
227
|
+
- openai/gpt-4.1
|
|
228
|
+
- anthropic/claude-haiku-4-5-20251001
|
|
229
|
+
- gemini/gemini-2.5-flash
|
|
230
|
+
|
|
231
|
+
defaults:
|
|
232
|
+
max_retries: 2
|
|
233
|
+
min_models: 2 # abort if fewer than this many models respond
|
|
234
|
+
request_timeout: 60 # seconds before an individual LLM call is abandoned
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Fallback chains are also supported — if the primary model fails, MAGI automatically tries the next:
|
|
238
|
+
|
|
239
|
+
```yaml
|
|
240
|
+
llms:
|
|
241
|
+
- - openai/gpt-4.1
|
|
242
|
+
- openai/gpt-4o # fallback if gpt-4.1 is unavailable
|
|
243
|
+
- anthropic/claude-haiku-4-5-20251001
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
**Package:** Not required — pass the model list directly as a Python dict. No file needed:
|
|
247
|
+
|
|
248
|
+
```python
|
|
249
|
+
config = {
|
|
250
|
+
"llms": [
|
|
251
|
+
"openai/gpt-4.1",
|
|
252
|
+
"anthropic/claude-haiku-4-5-20251001",
|
|
253
|
+
"gemini/gemini-2.5-flash",
|
|
254
|
+
]
|
|
255
|
+
}
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### `magi_core/prompts.yaml`
|
|
259
|
+
|
|
260
|
+
The bundled prompt templates are used by default and work out of the box. Override them only if you want to customise system prompts or method instructions.
|
|
261
|
+
|
|
262
|
+
## CLI Usage
|
|
263
|
+
|
|
264
|
+
After installation, the `magi` command is available:
|
|
265
|
+
|
|
266
|
+
```bash
|
|
267
|
+
magi "Your question here" --method Synthesis
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
Or run directly from the repository:
|
|
271
|
+
|
|
272
|
+
```bash
|
|
273
|
+
python magi-cli.py "Your question here" --method VoteYesNo
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### Examples
|
|
277
|
+
|
|
278
|
+
**1. VoteYesNo — The Self-Driving Car Dilemma**
|
|
279
|
+
```bash
|
|
280
|
+
magi "A self-driving car's brakes have failed. It will kill five pedestrians unless its AI swerves onto the pavement, killing the single passenger inside. Should the AI be programmed to sacrifice its passenger?" \
|
|
281
|
+
--method VoteYesNo
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
**2. VoteOptions — Organ Allocation**
|
|
285
|
+
```bash
|
|
286
|
+
magi "A hospital has one donor heart. Who should receive it?" \
|
|
287
|
+
--method VoteOptions \
|
|
288
|
+
--options "A 10-year-old child with decades ahead,A 45-year-old surgeon who saves hundreds of lives per year,The patient who has waited longest on the list,Whoever has the highest chance of survival post-transplant"
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
**3. Majority — Capital Punishment After Mass Atrocity**
|
|
292
|
+
```bash
|
|
293
|
+
magi "Should capital punishment be re-introduced for terrorist attacks that cause mass civilian casualties, even knowing that wrongful convictions are statistically inevitable?" \
|
|
294
|
+
--method Majority
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
**4. Consensus — Abortion**
|
|
298
|
+
```bash
|
|
299
|
+
magi "At what point, if any, does terminating a pregnancy become morally impermissible, and who — if anyone — has the authority to enforce that line?" \
|
|
300
|
+
--method Consensus
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
**5. Minority — The Demanding Conclusion of Effective Altruism**
|
|
304
|
+
```bash
|
|
305
|
+
magi "Anyone who spends money on luxuries while children die of preventable diseases is morally equivalent to letting a drowning child die. Is this argument sound?" \
|
|
306
|
+
--method Minority
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
**6. Probability — Moral Luck**
|
|
310
|
+
```bash
|
|
311
|
+
magi "Two drivers drink the same amount and drive home. One kills a pedestrian by chance; the other arrives safely. The lucky driver deserves the same moral blame and legal punishment as the unlucky one." \
|
|
312
|
+
--method Probability
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
**7. Compose — Steel-Manning Open Borders**
|
|
316
|
+
```bash
|
|
317
|
+
magi "Write the strongest possible moral argument for the claim that wealthy nations have an absolute obligation to accept unlimited refugees, regardless of cultural, economic, or security consequences." \
|
|
318
|
+
--method Compose
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
**8. Synthesis — Parfit's Repugnant Conclusion**
|
|
322
|
+
```bash
|
|
323
|
+
magi "Derek Parfit's Repugnant Conclusion: a world of a trillion people living lives barely worth living is morally preferable to ten billion living very happy lives. Is this repugnant, unavoidable, or does it reveal a flaw in utilitarian reasoning?" \
|
|
324
|
+
--method Synthesis
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
**9. Deliberative Round — Capital Punishment**
|
|
328
|
+
```bash
|
|
329
|
+
magi "Should capital punishment be re-introduced for terrorist attacks that cause mass civilian casualties, even knowing that wrongful convictions are statistically inevitable?" \
|
|
330
|
+
--method VoteYesNo --deliberative
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
**10. JSON output — pipe into other tools**
|
|
334
|
+
```bash
|
|
335
|
+
magi "Should you pull the lever?" --method VoteYesNo --output-format json | jq '.rounds[0].aggregate'
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
### CLI Reference
|
|
339
|
+
|
|
340
|
+
| Argument | Description |
|
|
341
|
+
|----------|-------------|
|
|
342
|
+
| `prompt` | The question or issue to deliberate on |
|
|
343
|
+
| `--method` | `VoteYesNo` (default), `VoteOptions`, `Majority`, `Consensus`, `Minority`, `Probability`, `Compose`, `Synthesis` |
|
|
344
|
+
| `--llms` | Comma-separated model names (overrides `config.yaml`) |
|
|
345
|
+
| `--options` | Custom options for `VoteOptions` |
|
|
346
|
+
| `--vote-threshold` | Fraction of votes to declare a winner (default: `0.5`) |
|
|
347
|
+
| `--no-abstain` | Disallow abstaining in `VoteYesNo` / `Probability` |
|
|
348
|
+
| `--deliberative` | Enable deliberative second round |
|
|
349
|
+
| `--rapporteur-prompt` | Additional instructions for the rapporteur |
|
|
350
|
+
| `--system-prompt` | Context prepended to every agent's system prompt |
|
|
351
|
+
| `--output-format` | `text` (default) or `json` |
|
|
352
|
+
| `--config` | Path to a custom `config.yaml` |
|
|
353
|
+
| `--prompts` | Path to a custom `prompts.yaml` |
|
|
354
|
+
| `--check-models` | Probe each model with a live API call and report availability, then exit |
|
|
355
|
+
|
|
356
|
+
## JSON Output
|
|
357
|
+
|
|
358
|
+
Both the CLI and the Python package support structured JSON output, designed for integration with UIs and downstream applications.
|
|
359
|
+
|
|
360
|
+
### CLI
|
|
361
|
+
|
|
362
|
+
```bash
|
|
363
|
+
magi "Your question" --method VoteYesNo --output-format json
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
### Package
|
|
367
|
+
|
|
368
|
+
```python
|
|
369
|
+
result = await magi.run_structured(
|
|
370
|
+
user_prompt="Your question",
|
|
371
|
+
method="VoteYesNo",
|
|
372
|
+
)
|
|
373
|
+
import json
|
|
374
|
+
print(json.dumps(result, indent=2))
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
### JSON Schema
|
|
378
|
+
|
|
379
|
+
```json
|
|
380
|
+
{
|
|
381
|
+
"schema_version": "1.0",
|
|
382
|
+
"method": "VoteYesNo",
|
|
383
|
+
"prompt": "Should you pull the lever?",
|
|
384
|
+
"system_prompt": null,
|
|
385
|
+
"deliberative": false,
|
|
386
|
+
"models": ["openai/gpt-4.1", "anthropic/claude-haiku-4-5-20251001"],
|
|
387
|
+
"rounds": [
|
|
388
|
+
{
|
|
389
|
+
"round": 1,
|
|
390
|
+
"responses": [
|
|
391
|
+
{
|
|
392
|
+
"model": "openai/gpt-4.1",
|
|
393
|
+
"pseudonym": "Participant X7K2",
|
|
394
|
+
"response": "yes",
|
|
395
|
+
"reason": "Utilitarian reasoning: saving five outweighs saving one.",
|
|
396
|
+
"confidence_score": 0.9,
|
|
397
|
+
"fallback_for": null
|
|
398
|
+
}
|
|
399
|
+
],
|
|
400
|
+
"errors": [
|
|
401
|
+
{
|
|
402
|
+
"model": "gemini/gemini-2.5-flash",
|
|
403
|
+
"error": "Model not found",
|
|
404
|
+
"error_category": "not_found",
|
|
405
|
+
"attempted_fallbacks": ["gemini/gemini-2.5-flash", "gemini/gemini-1.5-pro"]
|
|
406
|
+
}
|
|
407
|
+
],
|
|
408
|
+
"aggregate": { },
|
|
409
|
+
"rapporteur": {
|
|
410
|
+
"model": "openai/gpt-4.1",
|
|
411
|
+
"summary": "The council voted yes by a clear majority..."
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
]
|
|
415
|
+
}
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
#### `aggregate` by method
|
|
419
|
+
|
|
420
|
+
| Method | `aggregate` fields |
|
|
421
|
+
|--------|-------------------|
|
|
422
|
+
| `VoteYesNo`, `VoteOptions` | `votes` (object), `winner` (string), `threshold` (float) |
|
|
423
|
+
| `Probability` | `average`, `median`, `min`, `max` (all floats), `abstained_count` (int) |
|
|
424
|
+
| `Compose` | `ranked_candidates` (array — see below) |
|
|
425
|
+
| `Majority`, `Consensus`, `Minority`, `Synthesis` | `null` — result is in `rapporteur.summary` |
|
|
426
|
+
|
|
427
|
+
#### `ranked_candidates` (Compose)
|
|
428
|
+
|
|
429
|
+
```json
|
|
430
|
+
[
|
|
431
|
+
{
|
|
432
|
+
"rank": 1,
|
|
433
|
+
"model": "openai/gpt-4.1",
|
|
434
|
+
"pseudonym": "Participant X7K2",
|
|
435
|
+
"average_score": 8.5,
|
|
436
|
+
"text": "The composed paragraph text...",
|
|
437
|
+
"peer_reviews": [
|
|
438
|
+
{
|
|
439
|
+
"reviewer_model": "anthropic/claude-haiku-4-5-20251001",
|
|
440
|
+
"score": 8.0,
|
|
441
|
+
"justification": "Well-structured and compelling."
|
|
442
|
+
}
|
|
443
|
+
]
|
|
444
|
+
}
|
|
445
|
+
]
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
#### Error responses
|
|
449
|
+
|
|
450
|
+
When deliberation cannot proceed (empty prompt, no models, quorum failure), `run_structured()` returns a dict with a top-level `error` key instead of `rounds`:
|
|
451
|
+
|
|
452
|
+
```json
|
|
453
|
+
{
|
|
454
|
+
"error": "Quorum not met — 1 of 3 model(s) responded (minimum required: 2).",
|
|
455
|
+
"failed_models": [
|
|
456
|
+
{ "model": "gemini/gemini-2.5-flash", "error_category": "not_found", "error": "Model not found" }
|
|
457
|
+
]
|
|
458
|
+
}
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
`run()` converts these to a plain `"Error: ..."` string.
|
|
462
|
+
|
|
463
|
+
## Package API
|
|
464
|
+
|
|
465
|
+
No `config.yaml` file needed — pass models as a dict. API keys must be set as environment variables (or loaded via `load_dotenv()`) before calling `run()` or `run_structured()`.
|
|
466
|
+
|
|
467
|
+
```python
|
|
468
|
+
import asyncio
|
|
469
|
+
from magi_core import Magi
|
|
470
|
+
from magi_core.utils import load_yaml, get_default_prompts_path
|
|
471
|
+
|
|
472
|
+
config = {
|
|
473
|
+
"llms": [
|
|
474
|
+
"openai/gpt-4.1",
|
|
475
|
+
"anthropic/claude-haiku-4-5-20251001",
|
|
476
|
+
"gemini/gemini-2.5-flash",
|
|
477
|
+
]
|
|
478
|
+
}
|
|
479
|
+
prompts = load_yaml(get_default_prompts_path()) # bundled prompts, no file needed
|
|
480
|
+
|
|
481
|
+
magi = Magi(config, prompts)
|
|
482
|
+
|
|
483
|
+
# Text output
|
|
484
|
+
result = asyncio.run(magi.run(
|
|
485
|
+
user_prompt="A runaway trolley will kill five people. Should you pull the lever?",
|
|
486
|
+
method="Synthesis",
|
|
487
|
+
deliberative=True,
|
|
488
|
+
))
|
|
489
|
+
print(result)
|
|
490
|
+
|
|
491
|
+
# Structured JSON output
|
|
492
|
+
import json
|
|
493
|
+
result = asyncio.run(magi.run_structured(
|
|
494
|
+
user_prompt="A runaway trolley will kill five people. Should you pull the lever?",
|
|
495
|
+
method="VoteYesNo",
|
|
496
|
+
))
|
|
497
|
+
print(json.dumps(result, indent=2))
|
|
498
|
+
```
|
|
499
|
+
|
|
500
|
+
### `Magi(config, prompts)`
|
|
501
|
+
|
|
502
|
+
| Parameter | Type | Description |
|
|
503
|
+
|-----------|------|-------------|
|
|
504
|
+
| `config` | `dict` | Configuration dict with `llms` list and optional `defaults` |
|
|
505
|
+
| `prompts` | `dict` | Prompt templates — load from bundled file via `load_yaml(get_default_prompts_path())` |
|
|
506
|
+
|
|
507
|
+
**`config` keys:**
|
|
508
|
+
|
|
509
|
+
| Key | Type | Default | Description |
|
|
510
|
+
|-----|------|---------|-------------|
|
|
511
|
+
| `llms` | `list` | — | Model names or fallback lists |
|
|
512
|
+
| `defaults.max_retries` | `int` | `3` | Retries per model on transient errors |
|
|
513
|
+
| `defaults.min_models` | `int` | `1` | Minimum responding models before aborting |
|
|
514
|
+
| `defaults.request_timeout` | `float` | `60` | Seconds before an LLM call is abandoned |
|
|
515
|
+
| `defaults.vote_threshold` | `float` | `0.5` | Minimum vote fraction for a winner |
|
|
516
|
+
| `litellm_debug_mode` | `bool` | `false` | Enable verbose litellm logging |
|
|
517
|
+
|
|
518
|
+
Config is validated at construction time — invalid values raise `ValueError` immediately.
|
|
519
|
+
|
|
520
|
+
### `await magi.run(...)` / `await magi.run_structured(...)`
|
|
521
|
+
|
|
522
|
+
Both methods accept identical parameters:
|
|
523
|
+
|
|
524
|
+
| Parameter | Type | Default | Description |
|
|
525
|
+
|-----------|------|---------|-------------|
|
|
526
|
+
| `user_prompt` | `str` | — | The question or statement to deliberate on |
|
|
527
|
+
| `system_prompt` | `str \| None` | `None` | Extra context prepended to the system prompt |
|
|
528
|
+
| `selected_llms` | `list \| None` | config value | Model names or fallback lists to use |
|
|
529
|
+
| `method` | `str` | `"VoteYesNo"` | One of the eight methods listed above |
|
|
530
|
+
| `method_options` | `dict` | `{}` | Method-specific options (see below) |
|
|
531
|
+
| `deliberative` | `bool` | `False` | Enable a second round where agents review peer responses |
|
|
532
|
+
|
|
533
|
+
**`method_options` keys:**
|
|
534
|
+
|
|
535
|
+
| Key | Type | Description |
|
|
536
|
+
|-----|------|-------------|
|
|
537
|
+
| `vote_threshold` | `float` | Minimum fraction of votes to declare a winner (default `0.5`) |
|
|
538
|
+
| `allow_abstain` | `bool` | Allow abstaining in `VoteYesNo` / `-1.0` probability in `Probability` (default `True`) |
|
|
539
|
+
| `options` | `list[str]` | Choices for `VoteOptions` |
|
|
540
|
+
| `rapporteur_prompt` | `str` | Additional instructions appended to the rapporteur prompt |
|
|
541
|
+
|
|
542
|
+
**Returns:**
|
|
543
|
+
- `run()` → `str` — Markdown-formatted report
|
|
544
|
+
- `run_structured()` → `dict` — JSON-serialisable structured result (see [JSON Schema](#json-schema))
|
|
545
|
+
|
|
546
|
+
### Method Quick Reference
|
|
547
|
+
|
|
548
|
+
```python
|
|
549
|
+
# Vote — trolley problem
|
|
550
|
+
await magi.run("Should you pull the lever?", method="VoteYesNo")
|
|
551
|
+
|
|
552
|
+
# Custom options — pandemic triage
|
|
553
|
+
await magi.run(
|
|
554
|
+
"Who should receive the last ventilator?",
|
|
555
|
+
method="VoteOptions",
|
|
556
|
+
method_options={"options": ["Young Child", "Frontline Doctor", "Elderly Patient", "Random Lottery"]},
|
|
557
|
+
)
|
|
558
|
+
|
|
559
|
+
# Synthesis — comprehensive view of all perspectives
|
|
560
|
+
await magi.run(
|
|
561
|
+
"Is it ever justified to lie to protect someone's feelings?",
|
|
562
|
+
method="Synthesis",
|
|
563
|
+
)
|
|
564
|
+
|
|
565
|
+
# Probability — simulation hypothesis
|
|
566
|
+
await magi.run("We are living in a computer simulation.", method="Probability")
|
|
567
|
+
|
|
568
|
+
# Compose — generate and peer-review ethical arguments
|
|
569
|
+
await magi.run(
|
|
570
|
+
"Write a paragraph arguing that lying is sometimes morally permissible.",
|
|
571
|
+
method="Compose",
|
|
572
|
+
)
|
|
573
|
+
|
|
574
|
+
# Structured output for downstream use
|
|
575
|
+
result = await magi.run_structured("Should you pull the lever?", method="VoteYesNo")
|
|
576
|
+
winner = result["rounds"][0]["aggregate"]["winner"]
|
|
577
|
+
```
|
|
578
|
+
|
|
579
|
+
## Architecture
|
|
580
|
+
|
|
581
|
+
```
|
|
582
|
+
magi_core/
|
|
583
|
+
__init__.py Public API (Magi class, load_yaml, get_default_prompts_path)
|
|
584
|
+
core.py Orchestration, aggregation, rapporteur logic, and renderers
|
|
585
|
+
cli.py Console entry point (installed as `magi` command)
|
|
586
|
+
utils.py Pseudonym generation and YAML loading helpers
|
|
587
|
+
prompts.yaml System prompts and per-method instructions
|
|
588
|
+
config.yaml Default model selection
|
|
589
|
+
```
|
|
590
|
+
|
|
591
|
+
**Key design decisions:**
|
|
592
|
+
- All LLM calls are async (`asyncio.gather`) — models are queried in parallel.
|
|
593
|
+
- Agents use random anonymous IDs (`Participant X7K2`) during deliberation to reduce brand bias.
|
|
594
|
+
- The rapporteur is selected by confidence score; ties are broken randomly.
|
|
595
|
+
- `Synthesis` uses the same rapporteur selection as `Majority` but with a prompt that mandates comprehensive inclusion of all perspectives.
|
|
596
|
+
- `run()` and `run_structured()` share a single `_deliberate()` engine; the only difference is whether the result dict is rendered to Markdown or returned as-is.
|
|
597
|
+
- Fallback chains trigger on permanent errors (model not found, deprecated, auth) and rate limits; timeouts and unknown errors retry the primary only.
|
|
598
|
+
|
|
599
|
+
### Fallback chain
|
|
600
|
+
|
|
601
|
+
Each slot in `config.yaml` can be a list; MAGI walks the list when a model is permanently unavailable:
|
|
602
|
+
|
|
603
|
+
```
|
|
604
|
+
Slot: [primary, fallback-1, fallback-2, …]
|
|
605
|
+
|
|
606
|
+
┌───────────┐ deprecated / ┌─────────────┐ error again ┌─────────────┐
|
|
607
|
+
│ Primary │ not found / ──► │ Fallback 1 │ ──────────► │ Fallback 2 │
|
|
608
|
+
│ Model │ auth error / │ │ │ │
|
|
609
|
+
└───────────┘ rate limit └─────────────┘ └──────┬──────┘
|
|
610
|
+
│ ok │ ok │ all failed
|
|
611
|
+
▼ ▼ ▼
|
|
612
|
+
result used result used + error logged in
|
|
613
|
+
fallback noted round errors[]
|
|
614
|
+
in report
|
|
615
|
+
```
|
|
616
|
+
|
|
617
|
+
Timeout and unknown errors retry the **primary only** — they do not burn through the fallback chain.
|
|
618
|
+
|
|
619
|
+
## Security
|
|
620
|
+
|
|
621
|
+
`litellm<1.44.12` is affected by **CVE-2024-8938** (arbitrary code execution via `eval()`). This package pins `litellm>=1.44.12`. Keep your dependencies up to date.
|
|
622
|
+
|
|
623
|
+
## Testing
|
|
624
|
+
|
|
625
|
+
### Unit tests (no API keys required)
|
|
626
|
+
|
|
627
|
+
```bash
|
|
628
|
+
pytest tests/test_core.py -v
|
|
629
|
+
```
|
|
630
|
+
|
|
631
|
+
### Integration tests (live LLM calls — incurs API costs)
|
|
632
|
+
|
|
633
|
+
```bash
|
|
634
|
+
pytest tests/test_integration_matrix.py -v
|
|
635
|
+
```
|
|
636
|
+
|
|
637
|
+
Integration test outputs are saved as Markdown in `test_results/`.
|
|
638
|
+
|
|
639
|
+
## License
|
|
640
|
+
|
|
641
|
+
MIT — see [LICENSE](LICENSE).
|