Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files
CriticalThinking/app/api/router.py
CHANGED
|
@@ -37,7 +37,7 @@ def run_analysis_task(task_id: str, repo_url: Optional[str], project_description
|
|
| 37 |
indexer.index_repository(repo_url)
|
| 38 |
|
| 39 |
# 2. Analyze
|
| 40 |
-
analysis_results = orchestrator.run_analysis(project_description)
|
| 41 |
weaknesses = analysis_results.get("weaknesses", [])
|
| 42 |
|
| 43 |
# 3. Improvements
|
|
|
|
| 37 |
indexer.index_repository(repo_url)
|
| 38 |
|
| 39 |
# 2. Analyze
|
| 40 |
+
analysis_results = orchestrator.run_analysis(project_description, has_code=bool(repo_url))
|
| 41 |
weaknesses = analysis_results.get("weaknesses", [])
|
| 42 |
|
| 43 |
# 3. Improvements
|
CriticalThinking/app/services/agent_orchestrator.py
CHANGED
|
@@ -50,25 +50,33 @@ class BaseAgent:
|
|
| 50 |
return "Mocked response"
|
| 51 |
|
| 52 |
class Planner(BaseAgent):
|
| 53 |
-
def plan(self, project_overview: str) -> Dict[str, Any]:
|
| 54 |
-
|
|
|
|
| 55 |
Your task is to decompose complex codebase investigations into sequential execution plans.
|
| 56 |
Guidelines:
|
| 57 |
- Create 2-5 steps that build on each other.
|
| 58 |
- Each step should have a clear sub-question targeting a specific architectural or logic component.
|
| 59 |
- Specify tool_type: doc_search (for code retrieval)."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
user_prompt = f"Decompose the following project overview into a sequential execution plan:\n\nProject Overview: {project_overview}\n\nRespond with valid JSON in this EXACT format:\n{{\n 'steps': [\n {{\n 'index': 0,\n 'sub_question': 'What specific architectural component needs analysis?',\n 'tool_type': 'doc_search',\n 'expected_outputs': ['finding 1', 'finding 2']\n }}\n ],\n 'reasoning': 'Explain why this plan will effectively find weaknesses.'\n}}"
|
| 61 |
return self._get_response(system_prompt, user_prompt, response_format={"type": "json_object"})
|
| 62 |
|
| 63 |
class WeaknessAnalyzer(BaseAgent):
|
| 64 |
-
def analyze(self,
|
| 65 |
-
system_prompt = """You are an AI senior engineer reviewing a project for critical weaknesses.
|
| 66 |
Be critical and cautious. Focus on:
|
| 67 |
-
- Architectural flaws (circular dependencies, lack of modularity).
|
| 68 |
-
-
|
| 69 |
-
-
|
| 70 |
- Redundant custom logic that could be replaced by standard libraries or models."""
|
| 71 |
-
user_prompt = f"Analyze the following code
|
| 72 |
return self._get_response(system_prompt, user_prompt, response_format={"type": "json_object"})
|
| 73 |
|
| 74 |
class AgentOrchestrator:
|
|
@@ -77,9 +85,9 @@ class AgentOrchestrator:
|
|
| 77 |
self.planner = Planner(openai_api_key=openai_api_key)
|
| 78 |
self.analyzer = WeaknessAnalyzer(openai_api_key=openai_api_key)
|
| 79 |
|
| 80 |
-
def run_analysis(self, project_overview: str) -> Dict[str, Any]:
|
| 81 |
# 1. Plan
|
| 82 |
-
plan = self.planner.plan(project_overview)
|
| 83 |
|
| 84 |
all_weaknesses = []
|
| 85 |
# 2. Execute steps
|
|
|
|
| 50 |
return "Mocked response"
|
| 51 |
|
| 52 |
class Planner(BaseAgent):
|
| 53 |
+
def plan(self, project_overview: str, has_code: bool = True) -> Dict[str, Any]:
|
| 54 |
+
if has_code:
|
| 55 |
+
system_prompt = """You are an expert query planner for a deep-thinking codebase analysis system.
|
| 56 |
Your task is to decompose complex codebase investigations into sequential execution plans.
|
| 57 |
Guidelines:
|
| 58 |
- Create 2-5 steps that build on each other.
|
| 59 |
- Each step should have a clear sub-question targeting a specific architectural or logic component.
|
| 60 |
- Specify tool_type: doc_search (for code retrieval)."""
|
| 61 |
+
else:
|
| 62 |
+
system_prompt = """You are an expert architect planning the design of a new modular software system.
|
| 63 |
+
Your task is to decompose the project description into sequential design steps.
|
| 64 |
+
Guidelines:
|
| 65 |
+
- Create 2-5 steps focusing on component decomposition and external service discovery.
|
| 66 |
+
- Each step should target a specific functional module or API integration.
|
| 67 |
+
- Specify tool_type: doc_search (for documentation or concept search)."""
|
| 68 |
user_prompt = f"Decompose the following project overview into a sequential execution plan:\n\nProject Overview: {project_overview}\n\nRespond with valid JSON in this EXACT format:\n{{\n 'steps': [\n {{\n 'index': 0,\n 'sub_question': 'What specific architectural component needs analysis?',\n 'tool_type': 'doc_search',\n 'expected_outputs': ['finding 1', 'finding 2']\n }}\n ],\n 'reasoning': 'Explain why this plan will effectively find weaknesses.'\n}}"
|
| 69 |
return self._get_response(system_prompt, user_prompt, response_format={"type": "json_object"})
|
| 70 |
|
| 71 |
class WeaknessAnalyzer(BaseAgent):
|
| 72 |
+
def analyze(self, context: str) -> Dict[str, Any]:
|
| 73 |
+
system_prompt = """You are an AI senior engineer and architect reviewing a project or requirements for critical weaknesses.
|
| 74 |
Be critical and cautious. Focus on:
|
| 75 |
+
- Architectural flaws (circular dependencies, lack of modularity, tight coupling).
|
| 76 |
+
- Missing high-value external integrations.
|
| 77 |
+
- Potential performance bottlenecks.
|
| 78 |
- Redundant custom logic that could be replaced by standard libraries or models."""
|
| 79 |
+
user_prompt = f"Analyze the following context (code or requirements) for weaknesses or missing components:\n\n{context}\n\nRespond in JSON format with fields: 'summary', 'weaknesses' (list of strings), 'severity' (high/medium/low)."
|
| 80 |
return self._get_response(system_prompt, user_prompt, response_format={"type": "json_object"})
|
| 81 |
|
| 82 |
class AgentOrchestrator:
|
|
|
|
| 85 |
self.planner = Planner(openai_api_key=openai_api_key)
|
| 86 |
self.analyzer = WeaknessAnalyzer(openai_api_key=openai_api_key)
|
| 87 |
|
| 88 |
+
def run_analysis(self, project_overview: str, has_code: bool = True) -> Dict[str, Any]:
|
| 89 |
# 1. Plan
|
| 90 |
+
plan = self.planner.plan(project_overview, has_code=has_code)
|
| 91 |
|
| 92 |
all_weaknesses = []
|
| 93 |
# 2. Execute steps
|
CriticalThinking/app/services/improvement_agent.py
CHANGED
|
@@ -3,28 +3,34 @@ from app.services.agent_orchestrator import BaseAgent
|
|
| 3 |
|
| 4 |
class ImprovementAgent(BaseAgent):
|
| 5 |
def generate_improvements(self, weaknesses: List[str]) -> Dict[str, Any]:
|
| 6 |
-
system_prompt = """You are an AI research scientist and senior architect.
|
| 7 |
-
Your goal is to generate impactful
|
| 8 |
-
|
| 9 |
-
-
|
| 10 |
-
-
|
| 11 |
-
-
|
| 12 |
-
-
|
| 13 |
-
user_prompt = f"Given these weaknesses:\n{weaknesses}\n\nPropose a
|
| 14 |
return self._get_response(system_prompt, user_prompt, response_format={"type": "json_object"})
|
| 15 |
|
| 16 |
def _mock_response(self, system_prompt: str) -> Any:
|
| 17 |
return {
|
| 18 |
"improvements": [
|
| 19 |
{
|
| 20 |
-
"
|
| 21 |
-
"proposal": "
|
| 22 |
-
"
|
|
|
|
|
|
|
|
|
|
| 23 |
},
|
| 24 |
{
|
| 25 |
-
"
|
| 26 |
-
"proposal": "
|
| 27 |
-
"
|
|
|
|
|
|
|
|
|
|
| 28 |
}
|
| 29 |
]
|
| 30 |
}
|
|
|
|
| 3 |
|
| 4 |
class ImprovementAgent(BaseAgent):
|
| 5 |
def generate_improvements(self, weaknesses: List[str]) -> Dict[str, Any]:
|
| 6 |
+
system_prompt = """You are an AI research scientist and senior architect with a focus on modular, API-driven design.
|
| 7 |
+
Your goal is to generate impactful ideas for improving a codebase or designing a new one by integrating high-value external components.
|
| 8 |
+
Key Principles:
|
| 9 |
+
- Decompose the project into independent, modular components that communicate via FastAPI endpoints.
|
| 10 |
+
- Identify state-of-the-art Hugging Face Models/Spaces or GitHub projects that can serve as functional building blocks.
|
| 11 |
+
- **Strict Critical Judgment**: Only suggest an external project if it is exceptionally useful and provides significant advantages over a custom implementation.
|
| 12 |
+
- Focus on reducing custom code and maintenance by leveraging established open-source ecosystems."""
|
| 13 |
+
user_prompt = f"Given these weaknesses or project requirements:\n{weaknesses}\n\nPropose a strategic improvement roadmap focusing on modularity and external integrations. Respond in JSON with format:\n{{\n 'improvements': [\n {{\n 'component_or_weakness': 'the target component or identified weakness',\n 'proposal': 'detailed plan for integration or improvement',\n 'justification': 'critical reasoning for why this external project or approach is high-value',\n 'replacement_search_query': 'specific query for Hugging Face or GitHub discovery',\n 'utility_score': 1-10,\n 'feasibility': 1-10\n }}\n ]\n}}"
|
| 14 |
return self._get_response(system_prompt, user_prompt, response_format={"type": "json_object"})
|
| 15 |
|
| 16 |
def _mock_response(self, system_prompt: str) -> Any:
|
| 17 |
return {
|
| 18 |
"improvements": [
|
| 19 |
{
|
| 20 |
+
"component_or_weakness": "Memory Management",
|
| 21 |
+
"proposal": "Integrate a specialized Rust-based memory optimizer via a Python wrapper.",
|
| 22 |
+
"justification": "Significant reduction in overhead for high-throughput data processing.",
|
| 23 |
+
"replacement_search_query": "python rust memory management",
|
| 24 |
+
"utility_score": 8,
|
| 25 |
+
"feasibility": 7
|
| 26 |
},
|
| 27 |
{
|
| 28 |
+
"component_or_weakness": "Sentiment Analysis",
|
| 29 |
+
"proposal": "Replace custom rule-based system with a hosted Hugging Face Space API.",
|
| 30 |
+
"justification": "LLM-based models provide far superior accuracy for nuanced text.",
|
| 31 |
+
"replacement_search_query": "sentiment analysis space",
|
| 32 |
+
"utility_score": 9,
|
| 33 |
+
"feasibility": 10
|
| 34 |
}
|
| 35 |
]
|
| 36 |
}
|