File size: 18,114 Bytes
0c591a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
---
title: Instant SWOT Agent
emoji: πŸ“Š
colorFrom: blue
colorTo: purple
sdk: docker
pinned: false
short_description: Instant SWOT Agent with self-correcting feedback
---

# Instant SWOT Agent

**Multi-agent workflow with self-correcting quality control for strategic analysis.**

[![Python 3.11+](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

| Resource | Link |
|----------|------|
| Live Demo | [huggingface.co/spaces/vn6295337/Instant-SWOT-Agent](https://huggingface.co/spaces/vn6295337/Instant-SWOT-Agent) |
| Product Demo Video | [Pre-recorded Demo](https://github.com/vn6295337/Instant-SWOT-Agent/issues/1) |
| Business Guide | [BUSINESS_README.md](BUSINESS_README.md) |

---

## The Problem

Strategic analysis is time-consuming and quality varies widely. Analysts spend hours gathering data and drafting reports, with no systematic quality checks until peer reviewβ€”often too late in the process.

## The Solution

This demo implements an **agentic AI pattern** where specialized agents collaborate autonomously: one gathers data, another drafts analysis, a third evaluates quality, and a fourth revises until standards are met. The self-correcting loop eliminates the "first draft = final draft" problem common in LLM applications.

## Why This Matters

Most enterprise AI deployments fail not from bad models, but from lack of quality gates. This architecture demonstrates how to build reliability into AI workflowsβ€”a pattern applicable to any domain requiring consistent output quality.

---

## Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                              USER INTERFACE                                  β”‚
β”‚                             (React + Vite)                                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                  β”‚
                                  β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                          ORCHESTRATION (LangGraph)                          β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚  Researcher  │─▢│   Analyst    │─▢│    Critic    │─▢│    Editor    β”‚    β”‚
β”‚  β”‚              β”‚  β”‚  (SWOT Gen)  β”‚  β”‚  (Scoring)   β”‚  β”‚  (Revision)  β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚                                             β”‚   score < 7      β”‚           β”‚
β”‚                                             β”‚β—€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β”‚
β”‚                                             β–Ό                              β”‚
β”‚                                      score β‰₯ 7 or 3 revisions β†’ [END]      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                              β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚                         β”‚                         β”‚
                    β–Ό                         β–Ό                         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  1. FINANCIALS BASKET     β”‚ β”‚  2. VOLATILITY BASKET     β”‚ β”‚  3. MACRO BASKET          β”‚
β”‚     (MCP Server) βœ“        β”‚ β”‚     (MCP Server) βœ“        β”‚ β”‚     (MCP Server) βœ“        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β€’ get_financials          β”‚ β”‚ β€’ get_vix                 β”‚ β”‚ β€’ get_gdp                 β”‚
β”‚ β€’ get_debt_metrics        β”‚ β”‚ β€’ get_beta                β”‚ β”‚ β€’ get_interest_rates      β”‚
β”‚ β€’ get_cash_flow           β”‚ β”‚ β€’ get_historical_vol      β”‚ β”‚ β€’ get_cpi                 β”‚
β”‚ β€’ get_material_events     β”‚ β”‚ β€’ get_implied_vol         β”‚ β”‚ β€’ get_unemployment        β”‚
β”‚ β€’ get_ownership_filings   β”‚ β”‚ β€’ get_volatility_basket   β”‚ β”‚ β€’ get_macro_basket        β”‚
β”‚ β€’ get_going_concern       β”‚ β”‚                           β”‚ β”‚                           β”‚
β”‚ β€’ get_sec_fundamentals    β”‚ β”‚                           β”‚ β”‚                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              β”‚                             β”‚                             β”‚
              β–Ό                             β–Ό                             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚      SEC EDGAR API        β”‚ β”‚  FRED API + Yahoo Finance β”‚ β”‚         FRED API          β”‚
β”‚      (Free, Public)       β”‚ β”‚  (Free with API Key)      β”‚ β”‚      (Free with Key)      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  4. VALUATION BASKET      β”‚ β”‚  5. NEWS BASKET           β”‚ β”‚  6. SENTIMENT BASKET      β”‚
β”‚     (MCP Server) βœ“        β”‚ β”‚     (MCP Server) βœ“        β”‚ β”‚     (MCP Server) βœ“        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β€’ get_pe_ratio            β”‚ β”‚ β€’ search_company_news     β”‚ β”‚ β€’ get_social_sentiment    β”‚
β”‚ β€’ get_ps_ratio            β”‚ β”‚ β€’ search_going_concern    β”‚ β”‚ β€’ get_analyst_ratings     β”‚
β”‚ β€’ get_pb_ratio            β”‚ β”‚ β€’ search_industry_trends  β”‚ β”‚                           β”‚
β”‚ β€’ get_ev_ebitda           β”‚ β”‚ β€’ search_competitor_news  β”‚ β”‚                           β”‚
β”‚ β€’ get_valuation_basket    β”‚ β”‚ β€’ tavily_search           β”‚ β”‚                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              β”‚                             β”‚                             β”‚
              β–Ό                             β–Ό                             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    Yahoo + SEC EDGAR      β”‚ β”‚       Tavily API          β”‚ β”‚     Finnhub API           β”‚
β”‚      (Free/Public)        β”‚ β”‚  (Free 1,000/month)       β”‚ β”‚   (Free with API Key)     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

```

### MCP Baskets Summary

| # | Basket | Status | Source | Key Metrics |
|---|--------|--------|--------|-------------|
| 1 | Financials | βœ“ Done | SEC EDGAR | Revenue, Margins, Debt, Cash Flow, 8-K, Ownership |
| 2 | Volatility | βœ“ Done | FRED + Yahoo | VIX, Beta, Historical Vol, Implied Vol |
| 3 | Macro | βœ“ Done | FRED | GDP, CPI, Interest Rates, Unemployment |
| 4 | Valuation | βœ“ Done | Yahoo + SEC | P/E, P/S, P/B, EV/EBITDA, PEG |
| 5 | News | βœ“ Done | Tavily | Company News, Industry Trends, Competitors |
| 6 | Sentiment | βœ“ Done | Finnhub | Social Sentiment, Analyst Ratings |

### Data Flow

```
User Input (Company) β†’ Researcher β†’ [MCP Servers] β†’ Raw Data
                                         ↓
Raw Data β†’ Analyst β†’ SWOT Draft β†’ Critic β†’ Score
                                     ↓
                        Score < 7 β†’ Editor β†’ Revised Draft β†’ Critic
                        Score β‰₯ 7 β†’ Final Output β†’ User
```

## Features

| Agent | Role | Implementation |
|-------|------|----------------|
| **Researcher** | Gathers real-time company data | 6 MCP servers (financials, volatility, macro, valuation, news, sentiment) |
| **Analyst** | Drafts SWOT based on selected strategy | Prompt-engineered generation |
| **Critic** | Scores output 1-10 with reasoning | Rubric-based evaluation |
| **Editor** | Revises based on critique | Targeted improvement |

**Supported Strategies:** Cost Leadership, Differentiation, Focus/Niche

## MCP Data Servers

Model Context Protocol (MCP) servers provide structured data access for AI agents. See [BUSINESS_README.md](BUSINESS_README.md) for detailed explanation.

### Financials Basket

| Location | Metric | Tool |
|----------|--------|------|
| `mcp-servers/financials-basket/` | Revenue, Net Income, Margins | `get_financials` |
| | Debt, Debt-to-Equity | `get_debt_metrics` |
| | Operating CF, CapEx, FCF, R&D | `get_cash_flow` |
| | 8-K Material Events | `get_material_events` |
| | 13D/13G, Form 4 (Ownership) | `get_ownership_filings` |
| | Going Concern Warnings | `get_going_concern` |
| | All Metrics + SWOT | `get_sec_fundamentals` |

### Volatility Basket

| Location | Metric | Tool |
|----------|--------|------|
| `mcp-servers/volatility-basket/` | VIX Index | `get_vix` |
| | Beta (vs S&P 500) | `get_beta` |
| | Historical Volatility | `get_historical_volatility` |
| | Implied Volatility | `get_implied_volatility` |
| | All Metrics + SWOT | `get_volatility_basket` |

### Macro Basket

| Location | Metric | Tool |
|----------|--------|------|
| `mcp-servers/macro-basket/` | GDP Growth Rate | `get_gdp` |
| | Federal Funds Rate | `get_interest_rates` |
| | CPI / Inflation | `get_cpi` |
| | Unemployment Rate | `get_unemployment` |
| | All Metrics + SWOT | `get_macro_basket` |

### Valuation Basket

| Location | Metric | Tool |
|----------|--------|------|
| `mcp-servers/valuation-basket/` | P/E Ratio | `get_pe_ratio` |
| | P/S Ratio | `get_ps_ratio` |
| | P/B Ratio | `get_pb_ratio` |
| | EV/EBITDA | `get_ev_ebitda` |
| | PEG Ratio | `get_peg_ratio` |
| | All Metrics + SWOT | `get_valuation_basket` |

### News Basket

| Location | Metric | Tool |
|----------|--------|------|
| `mcp-servers/news-basket/` | General Web Search | `tavily_search` |
| | Company News | `search_company_news` |
| | Going Concern News | `search_going_concern_news` |
| | Industry Trends | `search_industry_trends` |
| | Competitor News | `search_competitor_news` |

### Sentiment Basket

| Location | Metric | Tool |
|----------|--------|------|
| `mcp-servers/sentiment-basket/` | Social Sentiment | `get_social_sentiment` |
| | Analyst Ratings | `get_analyst_ratings` |

### API Endpoints

| MCP Server | API | Endpoint | Auth |
|------------|-----|----------|------|
| **Financials Basket** | SEC EDGAR | `https://data.sec.gov/api/xbrl/companyfacts/CIK{cik}.json` | None (free) |
| | | `https://data.sec.gov/submissions/CIK{cik}.json` | |
| **Volatility Basket** | FRED | `https://api.stlouisfed.org/fred/series/observations` | API Key |
| | Yahoo Finance | `https://query1.finance.yahoo.com/v8/finance/chart/{ticker}` | None |
| **Macro Basket** | FRED | `https://api.stlouisfed.org/fred/series/observations` | API Key |
| **Valuation Basket** | Yahoo Finance | `https://query1.finance.yahoo.com/v10/finance/quoteSummary/{ticker}` | None |
| **News Basket** | Tavily | `https://api.tavily.com/search` | API Key |
| **Sentiment Basket** | Finnhub | `https://finnhub.io/api/v1/` | API Key |

### API Keys

| Key | Environment Variable | Get From |
|-----|---------------------|----------|
| FRED | `FRED_VIX_API_KEY` | https://fred.stlouisfed.org/docs/api/api_key.html |
| Tavily | `TAVILY_API_KEY` | https://tavily.com |
| Finnhub | `FINNHUB_API_KEY` | https://finnhub.io |

Store in `.env`:
```
FRED_VIX_API_KEY=your_key
TAVILY_API_KEY=tvly-your_key
FINNHUB_API_KEY=your_key
```

## Installation & Setup

### Local Development

```bash
# Clone the repository
git clone https://github.com/vn6295337/Instant-SWOT-Agent.git
cd Instant-SWOT-Agent

# Create and activate virtual environment
python3 -m venv .venv
source .venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# Set up environment variables
cp .env.example .env
# Edit .env with your API keys

# Run the application (FastAPI + React UI)
python -m src.main api
# Or use make
make api
```

### Hugging Face Spaces Deployment

1. **Create a new Space** (Docker SDK)
2. **Add this repository** as the source
3. **Set up Secrets** (at least one LLM provider required):
   - `GROQ_API_KEY` (primary, recommended)
   - `GEMINI_API_KEY` (fallback)
   - `OPENROUTER_API_KEY` (fallback)
   - `TAVILY_API_KEY` (for live search data)
4. The system automatically falls back through providers if one fails

## Requirements

- Python 3.11+
- At least one LLM API key (Groq, Gemini, or OpenRouter)
- Tavily API key (optional, for live search data)

## Usage Examples

### Web UI
```bash
# Start the FastAPI server with React frontend
python -m src.main api
```
Open http://localhost:7860, enter a company name (e.g., "Tesla", "NVIDIA", "Microsoft") and click "Generate SWOT".

### CLI Usage
```bash
# Analyze a company from command line
python -m src.main analyze --company "Apple" --strategy "Differentiation"
```

### Programmatic Usage
```python
from src.workflow.runner import run_self_correcting_workflow

# Generate SWOT analysis with specific strategy
result = run_self_correcting_workflow(company_name="Apple", strategy_focus="Differentiation")

print(f"Score: {result['score']}/10")
print(f"Revisions: {result['revision_count']}")
print(f"SWOT Analysis:\n{result['draft_report']}")
```

## Testing

```bash
# Run tests
make test
# Or directly
python3 tests/test_self_correcting_loop.py
```

## Technical Characteristics

- **Analysis Time**: Typically under 10 seconds (depends on API latency)
- **Quality Loop**: Iterates until score β‰₯ 7/10 or max 3 revisions
- **LLM Providers**: Groq (primary) β†’ Gemini β†’ OpenRouter (cascading fallback)
- **Data Sources**: 6 MCP servers aggregating SEC EDGAR, FRED, Yahoo Finance, Tavily, and Finnhub APIs
- **Frontend**: React + TypeScript + Vite + Tailwind CSS
- **Backend**: FastAPI with async workflow execution

## Design Decisions

| Decision | Choice | Rationale |
|----------|--------|-----------|
| **Orchestration** | LangGraph | Native support for cyclic workflows; cleaner than raw LangChain for multi-agent patterns |
| **LLM Provider** | Groq (Llama 3.1 8B) | Sub-second inference enables tight feedback loops; cost-effective for demos |
| **Quality Threshold** | 7/10 | Balances quality vs. latency; lower values cause excessive loops, higher values rarely achievable |
| **Max Revisions** | 3 | Empirically, quality plateaus after 2-3 iterations; prevents infinite loops |
| **Same Model for Critic** | Intentional tradeoff | Production would use a stronger model for evaluation; kept simple for demo cost management |
| **Web Search** | Tavily API | Purpose-built for LLM applications; returns clean, structured content |

### Known Limitations

- **Self-evaluation bias**: The critic uses the same model family as the analyst. A production system would use a more capable evaluator model or human-in-the-loop for high-stakes decisions.
- **Mock data visibility**: When Tavily API is unavailable, the UI clearly indicates cached data is being used.

## Contributing

Contributions are welcome! Please follow these steps:

1. Fork the repository
2. Create a feature branch
3. Implement your changes
4. Add tests for new functionality
5. Submit a pull request

## License

This project is licensed under the MIT License.

---