Code Summarization
In addition to the non-LLM based functions of the Repository
class,
kit
also integrates directly with LLMs via the Summarizer
class to provide intelligent code summarization capabilities. This helps you quickly understand the purpose and functionality of entire code files, specific functions, or classes.
Getting Started
Section titled “Getting Started”To use code summarization, you’ll need an LLM provider configured. Currently, OpenAI, Anthropic, and Google Cloud’s Generative AI models are supported.
-
Install Dependencies:
Terminal window # Ensure you are in your project's virtual environmentuv pip install cased-kitThe installation includes all dependencies for OpenAI, Anthropic, and Google Cloud’s Generative AI models.
-
Set API Key(s): Configure the API key(s) for your chosen provider(s) as environment variables:
Terminal window # For OpenAIexport OPENAI_API_KEY="sk-..."# For Anthropicexport ANTHROPIC_API_KEY="sk-ant-..."# For Googleexport GOOGLE_API_KEY="AIzaSy..."You only need to set the key for the provider(s) you intend to use. If no specific configuration is provided to the
Summarizer
(see ‘Configuration (Advanced)’ below),kit
defaults to using OpenAI viaOpenAIConfig()
, which expectsOPENAI_API_KEY
.For
OpenAIConfig
, you can also customize parameters such as themodel
,temperature
, orbase_url
(e.g., for connecting to services like OpenRouter). See the ‘Configuration (Advanced)’ section for detailed examples.
Basic Usage: Summarizing Files
Section titled “Basic Usage: Summarizing Files”The primary way to access summarization is through the Repository
object’s get_summarizer()
factory method.
import kitimport os
# Ensure API key is set (replace with your actual key handling)# os.environ["OPENAI_API_KEY"] = "YOUR_API_KEY"
try: # Load the repository repo = kit.Repository("/path/to/your/project")
# Get the summarizer instance (defaults to OpenAIConfig using env var OPENAI_API_KEY) # See 'Configuration (Advanced)' for using Anthropic or Google. summarizer = repo.get_summarizer()
# Summarize a specific file file_path = "src/main_logic.py" summary = summarizer.summarize_file(file_path)
print(f"Summary for {file_path}:\n{summary}")
except Exception as e: print(f"An error occurred with the LLM provider: {e}")except Exception as e: print(f"An error occurred: {e}")
How it Works
Section titled “How it Works”When you call summarize_file
, kit
performs the following steps:
- Retrieves the content of the specified file using
repo.get_file_content()
. - Constructs a prompt containing the file content, tailored for code summarization.
- Sends the prompt to the configured LLM provider and model (e.g., OpenAI’s GPT-4o).
- Parses the response and returns the summary text.
Configuration (Advanced)
Section titled “Configuration (Advanced)”While environment variables are the default, you can provide specific configuration:
from kit.summaries import OpenAIConfig, AnthropicConfig, GoogleConfig
# Load reporepo = kit.Repository("/path/to/your/project")
# Define custom OpenAI configurationopenai_custom_config = OpenAIConfig( api_key="sk-...", # Can be omitted if OPENAI_API_KEY is set model="gpt-4o-mini")# Get summarizer with specific OpenAI configopenai_summarizer = repo.get_summarizer(config=openai_custom_config)# Summarize using the custom OpenAI configurationopenai_summary = openai_summarizer.summarize_file("src/utils_openai.py")print(f"OpenAI Summary:\n{openai_summary}")
Using OpenAI-Compatible Endpoints (e.g., OpenRouter)
Section titled “Using OpenAI-Compatible Endpoints (e.g., OpenRouter)”The OpenAIConfig
also supports a base_url
parameter, allowing you to use any OpenAI-compatible API endpoint, such as OpenRouter. This is useful for accessing a wide variety of models through a single API key and endpoint.
To use OpenRouter:
- Your
api_key
inOpenAIConfig
should be your OpenRouter API key. - Set the
base_url
to OpenRouter’s API endpoint (e.g.,https://openrouter.ai/api/v1
). - The
model
parameter should be the specific model string as recognized by OpenRouter (e.g.,meta-llama/llama-3.3-70b-instruct
).
# Example for OpenRouteropenrouter_config = OpenAIConfig( api_key="YOUR_OPENROUTER_API_KEY", # Replace with your OpenRouter key model="meta-llama/llama-3.3-70b-instruct", # Example model on OpenRouter base_url="https://openrouter.ai/api/v1")
openrouter_summarizer = repo.get_summarizer(config=openrouter_config)
Additional Configs
Section titled “Additional Configs”Define custom Anthropic configuration
Section titled “Define custom Anthropic configuration”anthropic_config = AnthropicConfig( api_key=“sk-ant-…”, # Can be omitted if ANTHROPIC_API_KEY is set model=“claude-3-haiku-20240307” )
Define custom Google configuration
Section titled “Define custom Google configuration”google_config = GoogleConfig( api_key=“AIzaSy…”, # Can be omitted if GOOGLE_API_KEY is set model=“gemini-1.5-flash-latest” )
## Advanced Usage
### Summarizing Functions and Classes
Beyond entire files, you can target specific functions or classes:
```pythonimport kit
repo = kit.Repository("/path/to/your/project")summarizer = repo.get_summarizer() # Assumes OPENAI_API_KEY is set
# Summarize a specific functionfunction_summary = summarizer.summarize_function( file_path="src/core/processing.py", function_name="process_main_data")print(f"Function Summary:\n{function_summary}")
# Summarize a specific classclass_summary = summarizer.summarize_class( file_path="src/models/user.py", class_name="UserProfile")print(f"Class Summary:\n{class_summary}")
Combining with Other Repository Features
Section titled “Combining with Other Repository Features”You can combine the Summarizer
with other Repository
methods for powerful workflows. For example, find all classes in a file and then summarize each one:
import kit
repo = kit.Repository("/path/to/your/project")summarizer = repo.get_summarizer()
file_to_analyze = "src/services/notification_service.py"
# 1. Find all symbols in the filesymbols = repo.extract_symbols(file_path=file_to_analyze)
# 2. Filter for classesclass_symbols = [s for s in symbols if s.get('type') == 'class']
# 3. Summarize each classfor sym in class_symbols: class_name = sym.get('name') if class_name: print(f"--- Summarizing Class: {class_name} ---") try: summary = summarizer.summarize_class( file_path=file_to_analyze, class_name=class_name ) print(summary) except Exception as e: print(f"Could not summarize {class_name}: {e}") print("\n")