ai/
commit.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use anyhow::{bail, Result};

use crate::{config, openai};
use crate::model::Model;

fn instruction() -> String {
  format!("You are an AI assistant that generates concise and meaningful git commit messages based on provided diffs. Please adhere to the following guidelines:

  - Structure: Begin with a clear, present-tense summary.
  - Content: Emphasize the changes and their rationale, excluding irrelevant details.
  - Consistency: Maintain uniformity in tense, punctuation, and capitalization.
  - Accuracy: Ensure the message accurately reflects the changes and their purpose.
  - Present tense, imperative mood. (e.g., 'Add x to y' instead of 'Added x to y')
  - Max {} chars in the output

  ## Output:

  Your output should be a commit message generated from the input diff and nothing else.

  ## Input:

  INPUT:", config::APP.max_commit_length.unwrap_or(72))
}

pub fn token_used(model: &Model) -> Result<usize> {
  model.count_tokens(&instruction())
}

pub async fn generate(diff: String, max_tokens: usize, model: Model) -> Result<openai::Response> {
  if max_tokens == 0 {
    bail!("Max can't be zero (2)")
  }

  let request = openai::Request {
    system: instruction(),
    prompt: diff,
    max_tokens,
    model
  };

  openai::call(request).await
}