|
| 1 | +"""Research agent for improved information gathering.""" |
| 2 | + |
| 3 | +from modules import display_signature |
| 4 | +from prefect import task |
| 5 | +from prefect.cache_policies import INPUTS |
| 6 | +from pydantic import BaseModel, Field |
| 7 | +from pydantic_ai import Agent |
| 8 | +from pydantic_ai.models import Model |
| 9 | +from search import ( |
| 10 | + explore_module_offerings, |
| 11 | + get_latest_prefect_release_notes, |
| 12 | + review_common_3x_gotchas, |
| 13 | + review_top_level_prefect_api, |
| 14 | + search_controlflow_docs, |
| 15 | + search_prefect_2x_docs, |
| 16 | + search_prefect_3x_docs, |
| 17 | + verify_import_statements, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +class ResearchFindings(BaseModel): |
| 22 | + """Structured findings from research.""" |
| 23 | + |
| 24 | + main_findings: list[str] = Field( |
| 25 | + description="Key findings that answer the question" |
| 26 | + ) |
| 27 | + supporting_details: list[str] = Field(description="Additional context and details") |
| 28 | + confidence_level: str = Field( |
| 29 | + description="high/medium/low confidence in the answer" |
| 30 | + ) |
| 31 | + knowledge_gaps: list[str] = Field(description="What we still don't know") |
| 32 | + relevant_links: list[str] = Field(description="Links to documentation or resources") |
| 33 | + |
| 34 | + |
| 35 | +class ResearchContext(BaseModel): |
| 36 | + """Context for the research agent.""" |
| 37 | + |
| 38 | + namespace: str = "prefect-3" # default to Prefect 3.x docs |
| 39 | + |
| 40 | + |
| 41 | +def create_research_agent( |
| 42 | + model: Model | None = None, |
| 43 | +) -> Agent[ResearchContext, ResearchFindings]: |
| 44 | + """Create a specialized research agent for thorough information gathering.""" |
| 45 | + |
| 46 | + agent = Agent[ResearchContext, ResearchFindings]( |
| 47 | + model=model or "openai:gpt-4o", |
| 48 | + deps_type=ResearchContext, |
| 49 | + result_type=ResearchFindings, |
| 50 | + system_prompt="""You are a specialized research agent for Prefect documentation and knowledge. |
| 51 | +Your job is to thoroughly research topics by using ALL available tools to gather comprehensive, accurate information. |
| 52 | +
|
| 53 | +Your research process: |
| 54 | +1. Start with broad searches to understand the topic context |
| 55 | +2. Use multiple search queries with different keywords - don't stop at first result |
| 56 | +3. For code examples: ALWAYS verify imports with verify_import_statements |
| 57 | +4. Focus on Prefect 3.x documentation unless explicitly asked about 2.x or older versions |
| 58 | +5. Review gotchas and release notes for recent changes |
| 59 | +6. Explore relevant modules for deeper understanding |
| 60 | +7. Synthesize ALL findings into structured, confident answers |
| 61 | +
|
| 62 | +Remember: You are the research specialist. The main agent relies on you for accurate, comprehensive information. |
| 63 | +Be thorough - use tools repeatedly until you have complete information. |
| 64 | +Default to Prefect 3.x unless the user explicitly asks about 2.x or version compatibility. |
| 65 | +""", |
| 66 | + tools=[ |
| 67 | + get_latest_prefect_release_notes, |
| 68 | + search_prefect_2x_docs, |
| 69 | + display_signature, |
| 70 | + search_prefect_3x_docs, |
| 71 | + search_controlflow_docs, |
| 72 | + review_top_level_prefect_api, |
| 73 | + explore_module_offerings, |
| 74 | + review_common_3x_gotchas, |
| 75 | + verify_import_statements, |
| 76 | + ], |
| 77 | + ) |
| 78 | + |
| 79 | + return agent |
| 80 | + |
| 81 | + |
| 82 | +async def research_topic( |
| 83 | + question: str, namespace: str = "prefect-3", model: Model | None = None |
| 84 | +) -> ResearchFindings: |
| 85 | + """ |
| 86 | + Thoroughly research a topic using an intelligent agent. |
| 87 | + Args: |
| 88 | + question: The question to research |
| 89 | + namespace: The documentation namespace to search |
| 90 | + model: Optional model to use for the agent |
| 91 | +
|
| 92 | + Returns: |
| 93 | + ResearchFindings with comprehensive information |
| 94 | + """ |
| 95 | + context = ResearchContext(namespace=namespace) |
| 96 | + |
| 97 | + agent = create_research_agent(model) |
| 98 | + result = await agent.run(user_prompt=question, deps=context) |
| 99 | + |
| 100 | + return result.data |
| 101 | + |
| 102 | + |
| 103 | +def research_prefect_topic(question: str, topic: str, version: str = "3.x") -> str: |
| 104 | + """ |
| 105 | + Thoroughly research a Prefect topic using an intelligent research agent. |
| 106 | + This tool performs multiple searches and synthesizes comprehensive findings. |
| 107 | +
|
| 108 | + Args: |
| 109 | + question: The specific question or topic to research |
| 110 | + topic: A short display name for the topic based on the question (for bookkeeping) |
| 111 | + version: Prefect version ("2.x" or "3.x") |
| 112 | + """ |
| 113 | + namespace = f"prefect-{version[0]}" |
| 114 | + |
| 115 | + try: |
| 116 | + findings = ( |
| 117 | + task(task_run_name=f"Researching {topic}", cache_policy=INPUTS)( |
| 118 | + research_topic |
| 119 | + ) |
| 120 | + .submit(question, namespace) |
| 121 | + .result() |
| 122 | + ) |
| 123 | + |
| 124 | + result = f"**Research Findings** (Confidence: {findings.confidence_level})\n\n" |
| 125 | + |
| 126 | + result += "**Main Findings:**\n" |
| 127 | + for finding in findings.main_findings: |
| 128 | + result += f"- {finding}\n" |
| 129 | + |
| 130 | + if findings.supporting_details: |
| 131 | + result += "\n**Supporting Details:**\n" |
| 132 | + for detail in findings.supporting_details: |
| 133 | + result += f"- {detail}\n" |
| 134 | + |
| 135 | + if findings.relevant_links: |
| 136 | + result += "\n**Relevant Documentation:**\n" |
| 137 | + for link in findings.relevant_links: |
| 138 | + result += f"- {link}\n" |
| 139 | + |
| 140 | + if findings.knowledge_gaps: |
| 141 | + result += "\n**Note:** Some aspects could not be fully researched:\n" |
| 142 | + for gap in findings.knowledge_gaps: |
| 143 | + result += f"- {gap}\n" |
| 144 | + |
| 145 | + return result |
| 146 | + |
| 147 | + except Exception as e: |
| 148 | + return f"Research failed: {str(e)}. Falling back to standard search." |
0 commit comments