Spaces:
Runtime error
Runtime error
Cascade Bot
commited on
Commit
·
99c817e
1
Parent(s):
2625442
feat: add VentureAPI class and refactor endpoints
Browse files- Added VentureAPI class to encapsulate venture strategy operations
- Refactored FastAPI endpoints to use VentureAPI
- Added __all__ export for VentureAPI
- Converted request models to dataclasses
- Added proper error handling and response formatting
- api/venture_api.py +147 -97
api/venture_api.py
CHANGED
@@ -4,6 +4,7 @@ from fastapi import APIRouter, HTTPException, Depends
|
|
4 |
from typing import List, Dict, Any, Optional
|
5 |
from pydantic import BaseModel, Field
|
6 |
from datetime import datetime
|
|
|
7 |
|
8 |
from reasoning.venture_strategies import (
|
9 |
AIStartupStrategy, SaaSVentureStrategy, AutomationVentureStrategy,
|
@@ -28,17 +29,20 @@ class VentureRequest(BaseModel):
|
|
28 |
query: str
|
29 |
context: Dict[str, Any] = Field(default_factory=dict)
|
30 |
|
31 |
-
|
|
|
32 |
"""Market analysis request."""
|
33 |
segment: str
|
34 |
context: Dict[str, Any] = Field(default_factory=dict)
|
35 |
|
36 |
-
|
|
|
37 |
"""Portfolio optimization request."""
|
38 |
ventures: List[str]
|
39 |
context: Dict[str, Any] = Field(default_factory=dict)
|
40 |
|
41 |
-
|
|
|
42 |
"""Monetization optimization request."""
|
43 |
venture_type: str
|
44 |
context: Dict[str, Any] = Field(default_factory=dict)
|
@@ -61,137 +65,183 @@ VENTURE_STRATEGIES = {
|
|
61 |
"ai_marketplace": AIMarketplaceStrategy()
|
62 |
}
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
# Endpoints
|
65 |
@router.post("/analyze")
|
66 |
async def analyze_venture(request: VentureRequest):
|
67 |
"""Analyze venture opportunity."""
|
68 |
-
|
69 |
-
|
70 |
-
if not strategy:
|
71 |
-
raise HTTPException(
|
72 |
-
status_code=400,
|
73 |
-
detail=f"Invalid venture type: {request.venture_type}"
|
74 |
-
)
|
75 |
-
|
76 |
-
result = await strategy.reason(request.query, request.context)
|
77 |
-
return {
|
78 |
-
"success": True,
|
79 |
-
"result": result,
|
80 |
-
"timestamp": datetime.now().isoformat()
|
81 |
-
}
|
82 |
-
except Exception as e:
|
83 |
-
raise HTTPException(status_code=500, detail=str(e))
|
84 |
|
85 |
@router.post("/market")
|
86 |
async def analyze_market(request: MarketRequest):
|
87 |
"""Analyze market opportunity."""
|
88 |
-
|
89 |
-
|
90 |
-
result = await analyzer.analyze_market(request.segment, request.context)
|
91 |
-
return {
|
92 |
-
"success": True,
|
93 |
-
"result": result,
|
94 |
-
"timestamp": datetime.now().isoformat()
|
95 |
-
}
|
96 |
-
except Exception as e:
|
97 |
-
raise HTTPException(status_code=500, detail=str(e))
|
98 |
|
99 |
@router.post("/portfolio")
|
100 |
async def optimize_portfolio(request: PortfolioRequest):
|
101 |
"""Optimize venture portfolio."""
|
102 |
-
|
103 |
-
|
104 |
-
result = await optimizer.optimize_portfolio(request.ventures, request.context)
|
105 |
-
return {
|
106 |
-
"success": True,
|
107 |
-
"result": result,
|
108 |
-
"timestamp": datetime.now().isoformat()
|
109 |
-
}
|
110 |
-
except Exception as e:
|
111 |
-
raise HTTPException(status_code=500, detail=str(e))
|
112 |
|
113 |
@router.post("/monetization")
|
114 |
async def optimize_monetization(request: MonetizationRequest):
|
115 |
"""Optimize venture monetization."""
|
116 |
-
|
117 |
-
|
118 |
-
result = await optimizer.optimize_monetization(
|
119 |
-
request.venture_type, request.context)
|
120 |
-
return {
|
121 |
-
"success": True,
|
122 |
-
"result": result,
|
123 |
-
"timestamp": datetime.now().isoformat()
|
124 |
-
}
|
125 |
-
except Exception as e:
|
126 |
-
raise HTTPException(status_code=500, detail=str(e))
|
127 |
|
128 |
@router.get("/strategies")
|
129 |
async def list_strategies():
|
130 |
"""List available venture strategies."""
|
|
|
131 |
return {
|
132 |
"success": True,
|
133 |
-
"strategies":
|
134 |
"timestamp": datetime.now().isoformat()
|
135 |
}
|
136 |
|
137 |
@router.get("/metrics/{venture_type}")
|
138 |
async def get_venture_metrics(venture_type: str):
|
139 |
"""Get venture performance metrics."""
|
140 |
-
|
141 |
-
|
142 |
-
if not strategy:
|
143 |
-
raise HTTPException(
|
144 |
-
status_code=400,
|
145 |
-
detail=f"Invalid venture type: {venture_type}"
|
146 |
-
)
|
147 |
-
|
148 |
-
metrics = strategy.get_venture_metrics()
|
149 |
-
return {
|
150 |
-
"success": True,
|
151 |
-
"metrics": metrics,
|
152 |
-
"timestamp": datetime.now().isoformat()
|
153 |
-
}
|
154 |
-
except Exception as e:
|
155 |
-
raise HTTPException(status_code=500, detail=str(e))
|
156 |
|
157 |
@router.get("/insights")
|
158 |
async def get_market_insights():
|
159 |
"""Get comprehensive market insights."""
|
160 |
-
|
161 |
-
|
162 |
-
insights = analyzer.get_market_insights()
|
163 |
-
return {
|
164 |
-
"success": True,
|
165 |
-
"insights": insights,
|
166 |
-
"timestamp": datetime.now().isoformat()
|
167 |
-
}
|
168 |
-
except Exception as e:
|
169 |
-
raise HTTPException(status_code=500, detail=str(e))
|
170 |
|
171 |
@router.get("/portfolio/insights")
|
172 |
async def get_portfolio_insights():
|
173 |
"""Get comprehensive portfolio insights."""
|
174 |
-
|
175 |
-
|
176 |
-
insights = optimizer.get_portfolio_insights()
|
177 |
-
return {
|
178 |
-
"success": True,
|
179 |
-
"insights": insights,
|
180 |
-
"timestamp": datetime.now().isoformat()
|
181 |
-
}
|
182 |
-
except Exception as e:
|
183 |
-
raise HTTPException(status_code=500, detail=str(e))
|
184 |
|
185 |
@router.get("/monetization/metrics")
|
186 |
async def get_monetization_metrics():
|
187 |
"""Get comprehensive monetization metrics."""
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
"metrics": metrics,
|
194 |
-
"timestamp": datetime.now().isoformat()
|
195 |
-
}
|
196 |
-
except Exception as e:
|
197 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
4 |
from typing import List, Dict, Any, Optional
|
5 |
from pydantic import BaseModel, Field
|
6 |
from datetime import datetime
|
7 |
+
from dataclasses import dataclass
|
8 |
|
9 |
from reasoning.venture_strategies import (
|
10 |
AIStartupStrategy, SaaSVentureStrategy, AutomationVentureStrategy,
|
|
|
29 |
query: str
|
30 |
context: Dict[str, Any] = Field(default_factory=dict)
|
31 |
|
32 |
+
@dataclass
|
33 |
+
class MarketRequest:
|
34 |
"""Market analysis request."""
|
35 |
segment: str
|
36 |
context: Dict[str, Any] = Field(default_factory=dict)
|
37 |
|
38 |
+
@dataclass
|
39 |
+
class PortfolioRequest:
|
40 |
"""Portfolio optimization request."""
|
41 |
ventures: List[str]
|
42 |
context: Dict[str, Any] = Field(default_factory=dict)
|
43 |
|
44 |
+
@dataclass
|
45 |
+
class MonetizationRequest:
|
46 |
"""Monetization optimization request."""
|
47 |
venture_type: str
|
48 |
context: Dict[str, Any] = Field(default_factory=dict)
|
|
|
65 |
"ai_marketplace": AIMarketplaceStrategy()
|
66 |
}
|
67 |
|
68 |
+
class VentureAPI:
|
69 |
+
"""API for venture strategy operations."""
|
70 |
+
|
71 |
+
def __init__(self):
|
72 |
+
"""Initialize venture API components."""
|
73 |
+
self.market_analyzer = MarketAnalyzer()
|
74 |
+
self.portfolio_optimizer = PortfolioOptimizer()
|
75 |
+
self.monetization_optimizer = MonetizationOptimizer()
|
76 |
+
self.strategies = VENTURE_STRATEGIES
|
77 |
+
|
78 |
+
async def analyze_venture(self, request: VentureRequest) -> Dict[str, Any]:
|
79 |
+
"""Analyze venture opportunity."""
|
80 |
+
try:
|
81 |
+
strategy = self.strategies.get(request.venture_type)
|
82 |
+
if not strategy:
|
83 |
+
raise HTTPException(status_code=400, detail=f"Invalid venture type: {request.venture_type}")
|
84 |
+
|
85 |
+
result = await strategy.reason(request.query, request.context or {})
|
86 |
+
return {
|
87 |
+
"success": True,
|
88 |
+
"result": result,
|
89 |
+
"timestamp": datetime.now().isoformat()
|
90 |
+
}
|
91 |
+
except Exception as e:
|
92 |
+
raise HTTPException(status_code=500, detail=str(e))
|
93 |
+
|
94 |
+
async def analyze_market(self, request: MarketRequest) -> Dict[str, Any]:
|
95 |
+
"""Analyze market opportunity."""
|
96 |
+
try:
|
97 |
+
result = await self.market_analyzer.analyze_market(request.segment, request.context)
|
98 |
+
return {
|
99 |
+
"success": True,
|
100 |
+
"result": result,
|
101 |
+
"timestamp": datetime.now().isoformat()
|
102 |
+
}
|
103 |
+
except Exception as e:
|
104 |
+
raise HTTPException(status_code=500, detail=str(e))
|
105 |
+
|
106 |
+
async def optimize_portfolio(self, request: PortfolioRequest) -> Dict[str, Any]:
|
107 |
+
"""Optimize venture portfolio."""
|
108 |
+
try:
|
109 |
+
result = await self.portfolio_optimizer.optimize_portfolio(request.ventures, request.context)
|
110 |
+
return {
|
111 |
+
"success": True,
|
112 |
+
"result": result,
|
113 |
+
"timestamp": datetime.now().isoformat()
|
114 |
+
}
|
115 |
+
except Exception as e:
|
116 |
+
raise HTTPException(status_code=500, detail=str(e))
|
117 |
+
|
118 |
+
async def optimize_monetization(self, request: MonetizationRequest) -> Dict[str, Any]:
|
119 |
+
"""Optimize venture monetization."""
|
120 |
+
try:
|
121 |
+
result = await self.monetization_optimizer.optimize_monetization(request.venture_type, request.context)
|
122 |
+
return {
|
123 |
+
"success": True,
|
124 |
+
"result": result,
|
125 |
+
"timestamp": datetime.now().isoformat()
|
126 |
+
}
|
127 |
+
except Exception as e:
|
128 |
+
raise HTTPException(status_code=500, detail=str(e))
|
129 |
+
|
130 |
+
def list_strategies(self) -> List[str]:
|
131 |
+
"""List available venture strategies."""
|
132 |
+
return list(self.strategies.keys())
|
133 |
+
|
134 |
+
def get_venture_metrics(self, venture_type: str) -> Dict[str, Any]:
|
135 |
+
"""Get venture performance metrics."""
|
136 |
+
try:
|
137 |
+
strategy = self.strategies.get(venture_type)
|
138 |
+
if not strategy:
|
139 |
+
raise HTTPException(status_code=400, detail=f"Invalid venture type: {venture_type}")
|
140 |
+
|
141 |
+
metrics = strategy.get_venture_metrics()
|
142 |
+
return {
|
143 |
+
"success": True,
|
144 |
+
"venture_type": venture_type,
|
145 |
+
"metrics": metrics,
|
146 |
+
"timestamp": datetime.now().isoformat()
|
147 |
+
}
|
148 |
+
except Exception as e:
|
149 |
+
raise HTTPException(status_code=500, detail=str(e))
|
150 |
+
|
151 |
+
def get_market_insights(self) -> Dict[str, Any]:
|
152 |
+
"""Get comprehensive market insights."""
|
153 |
+
try:
|
154 |
+
insights = self.market_analyzer.get_market_insights()
|
155 |
+
return {
|
156 |
+
"success": True,
|
157 |
+
"insights": insights,
|
158 |
+
"timestamp": datetime.now().isoformat()
|
159 |
+
}
|
160 |
+
except Exception as e:
|
161 |
+
raise HTTPException(status_code=500, detail=str(e))
|
162 |
+
|
163 |
+
def get_portfolio_insights(self) -> Dict[str, Any]:
|
164 |
+
"""Get comprehensive portfolio insights."""
|
165 |
+
try:
|
166 |
+
insights = self.portfolio_optimizer.get_portfolio_insights()
|
167 |
+
return {
|
168 |
+
"success": True,
|
169 |
+
"insights": insights,
|
170 |
+
"timestamp": datetime.now().isoformat()
|
171 |
+
}
|
172 |
+
except Exception as e:
|
173 |
+
raise HTTPException(status_code=500, detail=str(e))
|
174 |
+
|
175 |
+
def get_monetization_metrics(self) -> Dict[str, Any]:
|
176 |
+
"""Get comprehensive monetization metrics."""
|
177 |
+
try:
|
178 |
+
metrics = self.monetization_optimizer.get_monetization_metrics()
|
179 |
+
return {
|
180 |
+
"success": True,
|
181 |
+
"metrics": metrics,
|
182 |
+
"timestamp": datetime.now().isoformat()
|
183 |
+
}
|
184 |
+
except Exception as e:
|
185 |
+
raise HTTPException(status_code=500, detail=str(e))
|
186 |
+
|
187 |
# Endpoints
|
188 |
@router.post("/analyze")
|
189 |
async def analyze_venture(request: VentureRequest):
|
190 |
"""Analyze venture opportunity."""
|
191 |
+
venture_api = VentureAPI()
|
192 |
+
return await venture_api.analyze_venture(request)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
|
194 |
@router.post("/market")
|
195 |
async def analyze_market(request: MarketRequest):
|
196 |
"""Analyze market opportunity."""
|
197 |
+
venture_api = VentureAPI()
|
198 |
+
return await venture_api.analyze_market(request)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
|
200 |
@router.post("/portfolio")
|
201 |
async def optimize_portfolio(request: PortfolioRequest):
|
202 |
"""Optimize venture portfolio."""
|
203 |
+
venture_api = VentureAPI()
|
204 |
+
return await venture_api.optimize_portfolio(request)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
|
206 |
@router.post("/monetization")
|
207 |
async def optimize_monetization(request: MonetizationRequest):
|
208 |
"""Optimize venture monetization."""
|
209 |
+
venture_api = VentureAPI()
|
210 |
+
return await venture_api.optimize_monetization(request)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
|
212 |
@router.get("/strategies")
|
213 |
async def list_strategies():
|
214 |
"""List available venture strategies."""
|
215 |
+
venture_api = VentureAPI()
|
216 |
return {
|
217 |
"success": True,
|
218 |
+
"strategies": venture_api.list_strategies(),
|
219 |
"timestamp": datetime.now().isoformat()
|
220 |
}
|
221 |
|
222 |
@router.get("/metrics/{venture_type}")
|
223 |
async def get_venture_metrics(venture_type: str):
|
224 |
"""Get venture performance metrics."""
|
225 |
+
venture_api = VentureAPI()
|
226 |
+
return venture_api.get_venture_metrics(venture_type)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
|
228 |
@router.get("/insights")
|
229 |
async def get_market_insights():
|
230 |
"""Get comprehensive market insights."""
|
231 |
+
venture_api = VentureAPI()
|
232 |
+
return venture_api.get_market_insights()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
|
234 |
@router.get("/portfolio/insights")
|
235 |
async def get_portfolio_insights():
|
236 |
"""Get comprehensive portfolio insights."""
|
237 |
+
venture_api = VentureAPI()
|
238 |
+
return venture_api.get_portfolio_insights()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
239 |
|
240 |
@router.get("/monetization/metrics")
|
241 |
async def get_monetization_metrics():
|
242 |
"""Get comprehensive monetization metrics."""
|
243 |
+
venture_api = VentureAPI()
|
244 |
+
return venture_api.get_monetization_metrics()
|
245 |
+
|
246 |
+
# Export VentureAPI
|
247 |
+
__all__ = ['VentureAPI']
|
|
|
|
|
|
|
|
|
|