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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List, Literal, Dict, Optional
|
2 |
+
from agency_swarm import Agent, Agency, set_openai_key, BaseTool
|
3 |
+
from pydantic import Field, BaseModel
|
4 |
+
import streamlit as st
|
5 |
+
|
6 |
+
class AnalyzeProjectRequirements(BaseTool):
|
7 |
+
project_name: str = Field(..., description="Name of the project")
|
8 |
+
project_description: str = Field(..., description="Project description and goals")
|
9 |
+
project_type: Literal["Web Application", "Mobile App", "API Development",
|
10 |
+
"Data Analytics", "AI/ML Solution", "Other"] = Field(...,
|
11 |
+
description="Type of project")
|
12 |
+
budget_range: Literal["$10k-$25k", "$25k-$50k", "$50k-$100k", "$100k+"] = Field(...,
|
13 |
+
description="Budget range for the project")
|
14 |
+
|
15 |
+
class ToolConfig:
|
16 |
+
name = "analyze_project"
|
17 |
+
description = "Analyzes project requirements and feasibility"
|
18 |
+
one_call_at_a_time = True
|
19 |
+
|
20 |
+
def run(self) -> str:
|
21 |
+
"""Analyzes project and stores results in shared state"""
|
22 |
+
if self._shared_state.get("project_analysis", None) is not None:
|
23 |
+
raise ValueError("Project analysis already exists. Please proceed with technical specification.")
|
24 |
+
|
25 |
+
analysis = {
|
26 |
+
"name": self.project_name,
|
27 |
+
"type": self.project_type,
|
28 |
+
"complexity": "high",
|
29 |
+
"timeline": "6 months",
|
30 |
+
"budget_feasibility": "within range",
|
31 |
+
"requirements": ["Scalable architecture", "Security", "API integration"]
|
32 |
+
}
|
33 |
+
|
34 |
+
self._shared_state.set("project_analysis", analysis)
|
35 |
+
return "Project analysis completed. Please proceed with technical specification."
|
36 |
+
|
37 |
+
class CreateTechnicalSpecification(BaseTool):
|
38 |
+
architecture_type: Literal["monolithic", "microservices", "serverless", "hybrid"] = Field(
|
39 |
+
...,
|
40 |
+
description="Proposed architecture type"
|
41 |
+
)
|
42 |
+
core_technologies: str = Field(
|
43 |
+
...,
|
44 |
+
description="Comma-separated list of main technologies and frameworks"
|
45 |
+
)
|
46 |
+
scalability_requirements: Literal["high", "medium", "low"] = Field(
|
47 |
+
...,
|
48 |
+
description="Scalability needs"
|
49 |
+
)
|
50 |
+
|
51 |
+
class ToolConfig:
|
52 |
+
name = "create_technical_spec"
|
53 |
+
description = "Creates technical specifications based on project analysis"
|
54 |
+
one_call_at_a_time = True
|
55 |
+
|
56 |
+
def run(self) -> str:
|
57 |
+
"""Creates technical specification based on analysis"""
|
58 |
+
project_analysis = self._shared_state.get("project_analysis", None)
|
59 |
+
if project_analysis is None:
|
60 |
+
raise ValueError("Please analyze project requirements first using AnalyzeProjectRequirements tool.")
|
61 |
+
|
62 |
+
spec = {
|
63 |
+
"project_name": project_analysis["name"],
|
64 |
+
"architecture": self.architecture_type,
|
65 |
+
"technologies": self.core_technologies.split(","),
|
66 |
+
"scalability": self.scalability_requirements
|
67 |
+
}
|
68 |
+
|
69 |
+
self._shared_state.set("technical_specification", spec)
|
70 |
+
return f"Technical specification created for {project_analysis['name']}."
|
71 |
+
|
72 |
+
def init_session_state() -> None:
|
73 |
+
"""Initialize session state variables"""
|
74 |
+
if 'messages' not in st.session_state:
|
75 |
+
st.session_state.messages = []
|
76 |
+
if 'api_key' not in st.session_state:
|
77 |
+
st.session_state.api_key = None
|
78 |
+
|
79 |
+
def main() -> None:
|
80 |
+
st.set_page_config(page_title="AI Services Agency", layout="wide")
|
81 |
+
init_session_state()
|
82 |
+
|
83 |
+
st.title("🚀 AI Services Agency")
|
84 |
+
|
85 |
+
# API Configuration
|
86 |
+
with st.sidebar:
|
87 |
+
st.header("🔑 API Configuration")
|
88 |
+
openai_api_key = st.text_input(
|
89 |
+
"OpenAI API Key",
|
90 |
+
type="password",
|
91 |
+
help="Enter your OpenAI API key to continue"
|
92 |
+
)
|
93 |
+
|
94 |
+
if openai_api_key:
|
95 |
+
st.session_state.api_key = openai_api_key
|
96 |
+
st.success("API Key accepted!")
|
97 |
+
else:
|
98 |
+
st.warning("⚠️ Please enter your OpenAI API Key to proceed")
|
99 |
+
st.markdown("[Get your API key here](https://platform.openai.com/api-keys)")
|
100 |
+
return
|
101 |
+
|
102 |
+
# Initialize agents with the provided API key
|
103 |
+
set_openai_key(st.session_state.api_key)
|
104 |
+
api_headers = {"Authorization": f"Bearer {st.session_state.api_key}"}
|
105 |
+
|
106 |
+
# Project Input Form
|
107 |
+
with st.form("project_form"):
|
108 |
+
st.subheader("Project Details")
|
109 |
+
|
110 |
+
project_name = st.text_input("Project Name")
|
111 |
+
project_description = st.text_area(
|
112 |
+
"Project Description",
|
113 |
+
help="Describe the project, its goals, and any specific requirements"
|
114 |
+
)
|
115 |
+
|
116 |
+
col1, col2 = st.columns(2)
|
117 |
+
with col1:
|
118 |
+
project_type = st.selectbox(
|
119 |
+
"Project Type",
|
120 |
+
["Web Application", "Mobile App", "API Development",
|
121 |
+
"Data Analytics", "AI/ML Solution", "Other"]
|
122 |
+
)
|
123 |
+
timeline = st.selectbox(
|
124 |
+
"Expected Timeline",
|
125 |
+
["1-2 months", "3-4 months", "5-6 months", "6+ months"]
|
126 |
+
)
|
127 |
+
|
128 |
+
with col2:
|
129 |
+
budget_range = st.selectbox(
|
130 |
+
"Budget Range",
|
131 |
+
["$10k-$25k", "$25k-$50k", "$50k-$100k", "$100k+"]
|
132 |
+
)
|
133 |
+
priority = st.selectbox(
|
134 |
+
"Project Priority",
|
135 |
+
["High", "Medium", "Low"]
|
136 |
+
)
|
137 |
+
|
138 |
+
tech_requirements = st.text_area(
|
139 |
+
"Technical Requirements (optional)",
|
140 |
+
help="Any specific technical requirements or preferences"
|
141 |
+
)
|
142 |
+
|
143 |
+
special_considerations = st.text_area(
|
144 |
+
"Special Considerations (optional)",
|
145 |
+
help="Any additional information or special requirements"
|
146 |
+
)
|
147 |
+
|
148 |
+
submitted = st.form_submit_button("Analyze Project")
|
149 |
+
|
150 |
+
if submitted and project_name and project_description:
|
151 |
+
try:
|
152 |
+
# Set OpenAI key
|
153 |
+
set_openai_key(st.session_state.api_key)
|
154 |
+
|
155 |
+
# Create agents
|
156 |
+
ceo = Agent(
|
157 |
+
name="Project Director",
|
158 |
+
description="You are a CEO of multiple companies in the past and have a lot of experience in evaluating projects and making strategic decisions.",
|
159 |
+
instructions="""
|
160 |
+
You are an experienced CEO who evaluates projects. Follow these steps strictly:
|
161 |
+
|
162 |
+
1. FIRST, use the AnalyzeProjectRequirements tool with:
|
163 |
+
- project_name: The name from the project details
|
164 |
+
- project_description: The full project description
|
165 |
+
- project_type: The type of project (Web Application, Mobile App, etc)
|
166 |
+
- budget_range: The specified budget range
|
167 |
+
|
168 |
+
2. WAIT for the analysis to complete before proceeding.
|
169 |
+
|
170 |
+
3. Review the analysis results and provide strategic recommendations.
|
171 |
+
""",
|
172 |
+
tools=[AnalyzeProjectRequirements],
|
173 |
+
api_headers=api_headers,
|
174 |
+
temperature=0.7,
|
175 |
+
max_prompt_tokens=25000
|
176 |
+
)
|
177 |
+
|
178 |
+
cto = Agent(
|
179 |
+
name="Technical Architect",
|
180 |
+
description="Senior technical architect with deep expertise in system design.",
|
181 |
+
instructions="""
|
182 |
+
You are a technical architect. Follow these steps strictly:
|
183 |
+
|
184 |
+
1. WAIT for the project analysis to be completed by the CEO.
|
185 |
+
|
186 |
+
2. Use the CreateTechnicalSpecification tool with:
|
187 |
+
- architecture_type: Choose from monolithic/microservices/serverless/hybrid
|
188 |
+
- core_technologies: List main technologies as comma-separated values
|
189 |
+
- scalability_requirements: Choose high/medium/low based on project needs
|
190 |
+
|
191 |
+
3. Review the technical specification and provide additional recommendations.
|
192 |
+
""",
|
193 |
+
tools=[CreateTechnicalSpecification],
|
194 |
+
api_headers=api_headers,
|
195 |
+
temperature=0.5,
|
196 |
+
max_prompt_tokens=25000
|
197 |
+
)
|
198 |
+
|
199 |
+
product_manager = Agent(
|
200 |
+
name="Product Manager",
|
201 |
+
description="Experienced product manager focused on delivery excellence.",
|
202 |
+
instructions="""
|
203 |
+
- Manage project scope and timeline giving the roadmap of the project
|
204 |
+
- Define product requirements and you should give potential products and features that can be built for the startup
|
205 |
+
""",
|
206 |
+
api_headers=api_headers,
|
207 |
+
temperature=0.4,
|
208 |
+
max_prompt_tokens=25000
|
209 |
+
)
|
210 |
+
|
211 |
+
developer = Agent(
|
212 |
+
name="Lead Developer",
|
213 |
+
description="Senior developer with full-stack expertise.",
|
214 |
+
instructions="""
|
215 |
+
- Plan technical implementation
|
216 |
+
- Provide effort estimates
|
217 |
+
- Review technical feasibility
|
218 |
+
""",
|
219 |
+
api_headers=api_headers,
|
220 |
+
temperature=0.3,
|
221 |
+
max_prompt_tokens=25000
|
222 |
+
)
|
223 |
+
|
224 |
+
client_manager = Agent(
|
225 |
+
name="Client Success Manager",
|
226 |
+
description="Experienced client manager focused on project delivery.",
|
227 |
+
instructions="""
|
228 |
+
- Ensure client satisfaction
|
229 |
+
- Manage expectations
|
230 |
+
- Handle feedback
|
231 |
+
""",
|
232 |
+
api_headers=api_headers,
|
233 |
+
temperature=0.6,
|
234 |
+
max_prompt_tokens=25000
|
235 |
+
)
|
236 |
+
|
237 |
+
# Create agency
|
238 |
+
agency = Agency(
|
239 |
+
[
|
240 |
+
ceo, cto, product_manager, developer, client_manager,
|
241 |
+
[ceo, cto],
|
242 |
+
[ceo, product_manager],
|
243 |
+
[ceo, developer],
|
244 |
+
[ceo, client_manager],
|
245 |
+
[cto, developer],
|
246 |
+
[product_manager, developer],
|
247 |
+
[product_manager, client_manager]
|
248 |
+
],
|
249 |
+
async_mode='threading',
|
250 |
+
shared_files='shared_files'
|
251 |
+
)
|
252 |
+
|
253 |
+
# Prepare project info
|
254 |
+
project_info = {
|
255 |
+
"name": project_name,
|
256 |
+
"description": project_description,
|
257 |
+
"type": project_type,
|
258 |
+
"timeline": timeline,
|
259 |
+
"budget": budget_range,
|
260 |
+
"priority": priority,
|
261 |
+
"technical_requirements": tech_requirements,
|
262 |
+
"special_considerations": special_considerations
|
263 |
+
}
|
264 |
+
|
265 |
+
st.session_state.messages.append({"role": "user", "content": str(project_info)})
|
266 |
+
# Create tabs and run analysis
|
267 |
+
with st.spinner("AI Services Agency is analyzing your project..."):
|
268 |
+
try:
|
269 |
+
# Get analysis from each agent using agency.get_completion()
|
270 |
+
ceo_response = agency.get_completion(
|
271 |
+
message=f"""Analyze this project using the AnalyzeProjectRequirements tool:
|
272 |
+
Project Name: {project_name}
|
273 |
+
Project Description: {project_description}
|
274 |
+
Project Type: {project_type}
|
275 |
+
Budget Range: {budget_range}
|
276 |
+
|
277 |
+
Use these exact values with the tool and wait for the analysis results.""",
|
278 |
+
recipient_agent=ceo
|
279 |
+
)
|
280 |
+
|
281 |
+
cto_response = agency.get_completion(
|
282 |
+
message=f"""Review the project analysis and create technical specifications using the CreateTechnicalSpecification tool.
|
283 |
+
Choose the most appropriate:
|
284 |
+
- architecture_type (monolithic/microservices/serverless/hybrid)
|
285 |
+
- core_technologies (comma-separated list)
|
286 |
+
- scalability_requirements (high/medium/low)
|
287 |
+
|
288 |
+
Base your choices on the project requirements and analysis.""",
|
289 |
+
recipient_agent=cto
|
290 |
+
)
|
291 |
+
|
292 |
+
pm_response = agency.get_completion(
|
293 |
+
message=f"Analyze project management aspects: {str(project_info)}",
|
294 |
+
recipient_agent=product_manager,
|
295 |
+
additional_instructions="Focus on product-market fit and roadmap development, and coordinate with technical and marketing teams."
|
296 |
+
)
|
297 |
+
|
298 |
+
developer_response = agency.get_completion(
|
299 |
+
message=f"Analyze technical implementation based on CTO's specifications: {str(project_info)}",
|
300 |
+
recipient_agent=developer,
|
301 |
+
additional_instructions="Provide technical implementation details, optimal tech stack you would be using including the costs of cloud services (if any) and feasibility feedback, and coordinate with product manager and CTO to build the required products for the startup."
|
302 |
+
)
|
303 |
+
|
304 |
+
client_response = agency.get_completion(
|
305 |
+
message=f"Analyze client success aspects: {str(project_info)}",
|
306 |
+
recipient_agent=client_manager,
|
307 |
+
additional_instructions="Provide detailed go-to-market strategy and customer acquisition plan, and coordinate with product manager."
|
308 |
+
)
|
309 |
+
|
310 |
+
# Create tabs for different analyses
|
311 |
+
tabs = st.tabs([
|
312 |
+
"CEO's Project Analysis",
|
313 |
+
"CTO's Technical Specification",
|
314 |
+
"Product Manager's Plan",
|
315 |
+
"Developer's Implementation",
|
316 |
+
"Client Success Strategy"
|
317 |
+
])
|
318 |
+
|
319 |
+
with tabs[0]:
|
320 |
+
st.markdown("## CEO's Strategic Analysis")
|
321 |
+
st.markdown(ceo_response)
|
322 |
+
st.session_state.messages.append({"role": "assistant", "content": ceo_response})
|
323 |
+
|
324 |
+
with tabs[1]:
|
325 |
+
st.markdown("## CTO's Technical Specification")
|
326 |
+
st.markdown(cto_response)
|
327 |
+
st.session_state.messages.append({"role": "assistant", "content": cto_response})
|
328 |
+
|
329 |
+
with tabs[2]:
|
330 |
+
st.markdown("## Product Manager's Plan")
|
331 |
+
st.markdown(pm_response)
|
332 |
+
st.session_state.messages.append({"role": "assistant", "content": pm_response})
|
333 |
+
|
334 |
+
with tabs[3]:
|
335 |
+
st.markdown("## Lead Developer's Development Plan")
|
336 |
+
st.markdown(developer_response)
|
337 |
+
st.session_state.messages.append({"role": "assistant", "content": developer_response})
|
338 |
+
|
339 |
+
with tabs[4]:
|
340 |
+
st.markdown("## Client Success Strategy")
|
341 |
+
st.markdown(client_response)
|
342 |
+
st.session_state.messages.append({"role": "assistant", "content": client_response})
|
343 |
+
|
344 |
+
except Exception as e:
|
345 |
+
st.error(f"Error during analysis: {str(e)}")
|
346 |
+
st.error("Please check your inputs and API key and try again.")
|
347 |
+
|
348 |
+
except Exception as e:
|
349 |
+
st.error(f"Error during analysis: {str(e)}")
|
350 |
+
st.error("Please check your API key and try again.")
|
351 |
+
|
352 |
+
# Add history management in sidebar
|
353 |
+
with st.sidebar:
|
354 |
+
st.subheader("Options")
|
355 |
+
if st.checkbox("Show Analysis History"):
|
356 |
+
for message in st.session_state.messages:
|
357 |
+
with st.chat_message(message["role"]):
|
358 |
+
st.markdown(message["content"])
|
359 |
+
|
360 |
+
if st.button("Clear History"):
|
361 |
+
st.session_state.messages = []
|
362 |
+
st.rerun()
|
363 |
+
|
364 |
+
if __name__ == "__main__":
|
365 |
+
main()
|