Skip to content

Commit

Permalink
feat: Add verbose mode for debugging LLM prompts
Browse files Browse the repository at this point in the history
This commit introduces a verbose mode that displays the prompt sent to the LLM, the context used, and the git diff. This is useful for debugging and understanding how the LLM generates commit messages.
  • Loading branch information
suenot committed Feb 14, 2025
1 parent a5d1e0e commit 8e72b2d
Showing 1 changed file with 52 additions and 6 deletions.
58 changes: 52 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ struct Cli {
/// Display help information
#[arg(long = "help")]
help: bool,

/// Display verbose information
#[arg(long = "verbose")]
verbose: bool,
}

/// Increment version string (e.g., "0.0.37" -> "0.0.38")
Expand Down Expand Up @@ -600,11 +604,22 @@ async fn setup_openrouter_provider() -> Result<OpenRouterConfig, String> {
})
}

async fn generate_openrouter_commit_message(config: &OpenRouterConfig, diff: &str) -> Result<(String, UsageInfo), String> {
async fn generate_openrouter_commit_message(config: &OpenRouterConfig, diff: &str, cli: &Cli) -> Result<(String, UsageInfo), String> {
let client = reqwest::Client::new();

let prompt = format!("Here is the git diff, please generate a concise and descriptive commit message:\n\n{}", diff);

// Show context in verbose mode
if cli.verbose {
println!("\n=== Context for LLM ===");
println!("Provider: OpenRouter");
println!("Model: {}", config.model);
println!("Max tokens: {}", config.max_tokens);
println!("Temperature: {}", config.temperature);
println!("\n=== Prompt ===\n{}", prompt);
println!("\n=== Sending request to API ===");
}

let request_body = json!({
"model": &config.model,
"messages": [
Expand Down Expand Up @@ -655,9 +670,23 @@ async fn generate_openrouter_commit_message(config: &OpenRouterConfig, diff: &st
Ok((message, usage))
}

async fn generate_ollama_commit_message(config: &OllamaConfig, diff: &str) -> Result<(String, UsageInfo), String> {
async fn generate_ollama_commit_message(config: &OllamaConfig, diff: &str, cli: &Cli) -> Result<(String, UsageInfo), String> {
let client = reqwest::Client::new();

let prompt = format!("Write a clear and concise git commit message (one line, no technical terms) that describes these changes:\n\n{}", diff);

// Show context in verbose mode
if cli.verbose {
println!("\n=== Context for LLM ===");
println!("Provider: Ollama");
println!("Model: {}", config.model);
println!("URL: {}", config.url);
println!("Max tokens: {}", config.max_tokens);
println!("Temperature: {}", config.temperature);
println!("\n=== Prompt ===\n{}", prompt);
println!("\n=== Sending request to API ===");
}

let request_body = serde_json::json!({
"model": config.model,
"prompt": format!("Write a clear and concise git commit message (one line, no technical terms) that describes these changes:\n\n{}", diff),
Expand Down Expand Up @@ -782,11 +811,23 @@ async fn setup_openai_compatible_provider() -> Result<OpenAICompatibleConfig, St
})
}

async fn generate_openai_compatible_commit_message(config: &OpenAICompatibleConfig, diff: &str) -> Result<(String, UsageInfo), String> {
async fn generate_openai_compatible_commit_message(config: &OpenAICompatibleConfig, diff: &str, cli: &Cli) -> Result<(String, UsageInfo), String> {
let client = reqwest::Client::new();

let prompt = format!("Here is the git diff, please generate a concise and descriptive commit message:\n\n{}", diff);

// Show context in verbose mode
if cli.verbose {
println!("\n=== Context for LLM ===");
println!("Provider: OpenAI Compatible");
println!("Model: {}", config.model);
println!("API URL: {}", config.api_url);
println!("Max tokens: {}", config.max_tokens);
println!("Temperature: {}", config.temperature);
println!("\n=== Prompt ===\n{}", prompt);
println!("\n=== Sending request to API ===");
}

let request_body = json!({
"model": &config.model,
"messages": [
Expand Down Expand Up @@ -1165,6 +1206,11 @@ async fn run_commit(config: &Config, cli: &Cli) -> Result<(), String> {
// Get the diff (will handle git add if needed)
let diff = get_git_diff(cli)?;

// Show diff in verbose mode
if cli.verbose {
println!("\n=== Git Diff ===\n{}", diff);
}

// Generate commit message based on the active provider
let (message, usage_info) = {
let active_provider = config.providers.iter().find(|p| match p {
Expand All @@ -1174,9 +1220,9 @@ async fn run_commit(config: &Config, cli: &Cli) -> Result<(), String> {
}).ok_or("No active provider found")?;

match active_provider {
ProviderConfig::OpenRouter(c) => generate_openrouter_commit_message(c, &diff).await?,
ProviderConfig::Ollama(c) => generate_ollama_commit_message(c, &diff).await?,
ProviderConfig::OpenAICompatible(c) => generate_openai_compatible_commit_message(c, &diff).await?,
ProviderConfig::OpenRouter(c) => generate_openrouter_commit_message(c, &diff, cli).await?,
ProviderConfig::Ollama(c) => generate_ollama_commit_message(c, &diff, cli).await?,
ProviderConfig::OpenAICompatible(c) => generate_openai_compatible_commit_message(c, &diff, cli).await?,
}
};

Expand Down

0 comments on commit 8e72b2d

Please sign in to comment.