Agent Framework for Docstring Generation
April 17, 2025 ยท View on GitHub
This directory contains the core components of the multi-agent system responsible for generating high-quality docstrings for code components.
Overview
The system employs a collaborative workflow involving several specialized agents, managed by an Orchestrator. The goal is to analyze code, gather necessary context (both internal and external), generate a docstring, and verify its quality before finalizing.
The main workflow is initiated via the generate_docstring function in workflow.py.
Agents
-
BaseAgent(base.py)- Role: Abstract base class for all agents.
- Functionality: Provides common infrastructure including LLM initialization (using
LLMFactory), configuration loading, memory management (storing conversation history), and basic LLM interaction (generate_response). Ensures consistency across agents.
-
Reader(reader.py)- Role: Contextual Analysis and Information Needs Assessment.
- Functionality: Analyzes the input code component (
focal_component) and any existing context. Determines if additional information is required to write a comprehensive docstring. If more information is needed, it generates a structured request specifying whether internal codebase details (e.g., callers, callees) or external web search results are required.
-
Searcher(searcher.py)- Role: Information Retrieval.
- Functionality: Acts upon the requests generated by the
Reader. It retrieves the specified information by:- Querying the internal codebase using AST analysis (
ASTNodeAnalyzer) and dependency graphs. - Performing external web searches via APIs (e.g.,
PerplexityAPI).
- Querying the internal codebase using AST analysis (
- Returns the gathered context in a structured format.
-
Writer(writer.py)- Role: Docstring Generation.
- Functionality: Takes the original code component and the accumulated context (provided by the
OrchestratorafterReaderandSearchersteps) as input. Uses its configured LLM and detailed prompts (tailored for classes vs. functions/methods, adhering to Google style guide) to generate the docstring. Outputs the generated docstring within specific XML tags (<DOCSTRING>).
-
Verifier(verifier.py)- Role: Quality Assurance.
- Functionality: Evaluates the docstring produced by the
Writeragainst the original code and the context used. Checks for clarity, accuracy, completeness, information value (avoiding redundancy), and appropriate level of detail. Determines if the docstring meets quality standards or requires revision. If revision is needed, it specifies whether more context is required or provides direct suggestions for improvement.
-
Orchestrator(orchestrator.py)- Role: Workflow Management.
- Functionality: Coordinates the entire process. It manages the sequence of agent interactions:
- Calls
Readerto assess context needs. - Calls
Searcheriteratively if more context is requested (up to a limit). - Calls
Writerto generate the docstring. - Calls
Verifierto evaluate the docstring. - Manages revision loops based on
Verifierfeedback, potentially involving further searches or refinement by theWriter(up to a limit).
- Calls
- Handles context accumulation, token limit constraints, and status visualization.
Supporting Files
workflow.py: Provides the primary entry point functiongenerate_docstringto initiate the docstring generation process for a given code component.__init__.py: Makes theagentdirectory a Python package.llm/: Contains LLM-related code, including theLLMFactoryand base LLM classes.tool/: Contains tools used by agents, such as theASTNodeAnalyzerfor internal code traversal and thePerplexityAPIwrapper for external search.