-
-
Notifications
You must be signed in to change notification settings - Fork 629
Implement Query Analyzer for NestBot #4303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
arkid15r
merged 11 commits into
OWASP:feature/nestbot-ai-assistant
from
rudransh-shrivastava:feature/nestbot-ai-assistant-work
Mar 27, 2026
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6df4888
implement query analyzer
rudransh-shrivastava ff8919f
update code
rudransh-shrivastava 916ac4b
update code
rudransh-shrivastava c4793ba
bot suggestion
rudransh-shrivastava d33af33
Merge branch 'feature/nestbot-ai-assistant' into feature/nestbot-ai-a…
rudransh-shrivastava 0192183
Merge branch 'feature/nestbot-ai-assistant' into feature/nestbot-ai-a…
rudransh-shrivastava 8c1734c
update sub_queries map to match agents
rudransh-shrivastava dd89a36
extract variable AGENT_SEPARATOR
rudransh-shrivastava 634b81e
bot suggestions
rudransh-shrivastava 03ec917
update code
rudransh-shrivastava 5e98fa3
update tests
rudransh-shrivastava File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,116 @@ | ||||||
| """Query analyzer for determining query complexity.""" | ||||||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||||||
|
|
||||||
| import contextlib | ||||||
| import logging | ||||||
|
|
||||||
| from crewai import Agent, Crew, Task | ||||||
|
|
||||||
| from apps.ai.common.llm_config import get_llm | ||||||
| from apps.ai.template_loader import env | ||||||
|
|
||||||
| logger = logging.getLogger(__name__) | ||||||
|
|
||||||
| AGENT_DESCRIPTIONS = { | ||||||
| "channel": "Routes users to appropriate Slack channels for their questions", | ||||||
| "chapter": "Finds OWASP chapters, chapter leaders, and chapter activities worldwide", | ||||||
| "community": "Finds community leaders, committees, and entity Slack channels", | ||||||
| "contribution": "Helps find contribution opportunities and GSoC program information", | ||||||
| "project": "Finds OWASP projects by topic, maturity level, or specific needs", | ||||||
| "rag": "Searches OWASP documentation, policies, and repositories for general information", | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| def create_query_analyzer_agent() -> Agent: | ||||||
| """Create query analyzer agent. | ||||||
|
|
||||||
| Returns: | ||||||
| Agent: Query analyzer agent configured for complexity analysis. | ||||||
|
|
||||||
| """ | ||||||
| return Agent( | ||||||
| role="Query Analyzer", | ||||||
| goal=( | ||||||
| "Analyze user queries to determine complexity, identify required expert " | ||||||
| "agents, and decompose complex queries into sub-queries when needed." | ||||||
| ), | ||||||
| backstory=env.get_template("query_analyzer/backstory.jinja") | ||||||
| .render(agent_names=", ".join(AGENT_DESCRIPTIONS)) # nosemgrep: direct-use-of-jinja2 | ||||||
| .strip(), | ||||||
| llm=get_llm(), | ||||||
| verbose=True, | ||||||
| allow_delegation=False, | ||||||
| memory=False, | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| def analyze_query(query: str) -> dict: | ||||||
| """Analyze query to determine complexity and required agents. | ||||||
|
|
||||||
| Args: | ||||||
| query (str): User's question. | ||||||
|
|
||||||
| Returns: | ||||||
| dict: Analysis with 'is_simple', 'sub_queries', and 'required_agents'. | ||||||
|
|
||||||
| """ | ||||||
| try: | ||||||
| analyzer_agent = create_query_analyzer_agent() | ||||||
|
|
||||||
| task_template = env.get_template("query_analyzer/tasks/analyze.jinja") | ||||||
| agents = [{"name": name, "description": desc} for name, desc in AGENT_DESCRIPTIONS.items()] | ||||||
| task_description = task_template.render( # nosemgrep: direct-use-of-jinja2 | ||||||
| query=query, agents=agents | ||||||
| ).strip() | ||||||
|
|
||||||
| analysis_task = Task( | ||||||
| description=task_description, | ||||||
| agent=analyzer_agent, | ||||||
| expected_output="Query analysis with complexity assessment and required agents", | ||||||
| ) | ||||||
|
|
||||||
| crew = Crew( | ||||||
| agents=[analyzer_agent], | ||||||
| tasks=[analysis_task], | ||||||
| verbose=True, | ||||||
| max_iter=5, | ||||||
| max_rpm=10, | ||||||
| ) | ||||||
| result = crew.kickoff() | ||||||
| except (RuntimeError, ValueError): | ||||||
| logger.exception("Query analysis failed for: %s", query) | ||||||
|
rudransh-shrivastava marked this conversation as resolved.
Outdated
|
||||||
| return { | ||||||
| "is_simple": True, | ||||||
| "sub_queries": [query], | ||||||
| "required_agents": [], | ||||||
| } | ||||||
|
|
||||||
| result_str = str(result) | ||||||
|
rudransh-shrivastava marked this conversation as resolved.
|
||||||
| is_simple = True | ||||||
| sub_queries = [query] | ||||||
| required_agents = [] | ||||||
|
|
||||||
| for line in result_str.split("\n"): | ||||||
| line_lower = line.lower().strip() | ||||||
| if line_lower.startswith("issimple:"): | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe IS_SIMPLE in the source would be more readable here (if we can name it as we want) |
||||||
| with contextlib.suppress(ValueError): | ||||||
| value = line.split(":", 1)[1].strip().lower() | ||||||
| is_simple = value in ("true", "yes", "1") | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| elif line_lower.startswith("subqueries:"): | ||||||
| value = line.split(":", 1)[1].strip() | ||||||
| if value and value.lower() != "none": | ||||||
| sub_queries = [q.strip() for q in value.split("|") if q.strip()] | ||||||
| elif line_lower.startswith("requiredagents:"): | ||||||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||||||
| value = line.split(":", 1)[1].strip().lower() | ||||||
| if value and value != "none": | ||||||
| required_agents = [ | ||||||
| a.strip() for a in value.split(",") if a.strip() in AGENT_DESCRIPTIONS | ||||||
| ] | ||||||
|
|
||||||
| if not required_agents: | ||||||
| logger.warning("Query analysis returned no valid agents for: %s", query) | ||||||
|
|
||||||
| return { | ||||||
| "is_simple": is_simple, | ||||||
| "sub_queries": sub_queries, | ||||||
| "required_agents": required_agents, | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| You are an expert at understanding query complexity and determining what resources are needed to answer questions about OWASP. You analyze queries to identify whether they are simple (answerable by one specialist) or complex (requiring multiple specialists). For complex queries, you decompose them into focused sub-queries that can be handled independently. You understand the capabilities of available agents ({{ agent_names }}), allowing you to determine which expert agents are best suited to answer each part of a query. |
19 changes: 19 additions & 0 deletions
19
backend/apps/ai/templates/query_analyzer/tasks/analyze.jinja
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| Analyze this query: {{ query }} | ||
|
rudransh-shrivastava marked this conversation as resolved.
|
||
|
|
||
| Determine the complexity of this query and what resources are needed to answer it. | ||
|
|
||
| Available expert agents: | ||
| {% for agent in agents %} | ||
| - {{ agent.name }}: {{ agent.description }} | ||
| {% endfor %} | ||
|
|
||
| A query is SIMPLE if it has a single clear intent and can be fully answered by ONE expert agent. | ||
|
|
||
| A query is COMPLEX if it combines multiple topics, requires information from multiple agents, or asks compound questions that need different expertise. | ||
|
|
||
| For complex queries, break them into independent sub-queries where each sub-query is answerable by a single agent. | ||
|
|
||
| Format your response as: | ||
| IsSimple: true or false | ||
| SubQueries: sub-query 1 | sub-query 2 | sub-query 3 (use pipe separator, or just the original query if simple) | ||
| RequiredAgents: agent1, agent2 (comma-separated list of agent names) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.