Skip to content

Commit

Permalink
Improve AI prompt (#26)
Browse files Browse the repository at this point in the history
* Change max_tokens type from usize to u16

* Bump git-ai version to 0.2.54 in Cargo.lock

* Refine context line handling in `PatchDiff` implementation

* Refactor `Model` enum with default attributes

* Remove pull_request trigger from cd workflow

* Update version to 0.2.56 in Cargo files
  • Loading branch information
oleander committed Feb 8, 2025
1 parent cf955f9 commit 18e020a
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 12 deletions.
1 change: 0 additions & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches:
- main
pull_request:
workflow_dispatch:

concurrency:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "git-ai"
version = "0.2.54"
version = "0.2.56"
edition = "2021"
description = "Git AI: Automates commit messages using ChatGPT. Stage your files, and Git AI generates the messages."
license = "MIT"
Expand Down
6 changes: 4 additions & 2 deletions resources/prompt.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
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.
- Content: While you should use the surrounding context to understand the changes, your commit message should ONLY describe the lines marked with + or -.
- Understanding: Use the context (unmarked lines) to understand the purpose and impact of the changes, but do not mention unchanged code in the commit message.
- Changes: Only describe what was actually changed (added, removed, or modified).
- 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 {{max_commit_length}} chars in the output

## Output:

Your output should be a commit message generated from the input diff and nothing else.
Your output should be a commit message generated from the input diff and nothing else. While you should use the surrounding context to understand the changes, your message should only describe what was actually modified (+ or - lines).

## Input:

Expand Down
2 changes: 1 addition & 1 deletion src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub async fn generate(diff: String, max_tokens: usize, model: Model) -> Result<R
let request = Request {
system: instruction(),
prompt: diff,
max_tokens,
max_tokens: max_tokens.try_into().unwrap_or(u16::MAX),
model
};

Expand Down
12 changes: 9 additions & 3 deletions src/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ pub trait PatchDiff {
}

impl PatchDiff for Diff<'_> {
// TODO: Grouo arguments
fn to_patch(&self, max_tokens: usize, model: Model) -> Result<String> {
let mut files: HashMap<PathBuf, String> = HashMap::new();

Expand All @@ -79,12 +78,19 @@ impl PatchDiff for Diff<'_> {
let content = line.content();
let string = content.to_utf8();

// Include both changes and context, but prefix context lines with "context: "
// This helps the model understand the context while still identifying actual changes
let line_content = match line.origin() {
'+' | '-' => string,
_ => format!("context: {}", string)
};

match files.get(&diff.path()) {
Some(file_acc) => {
files.insert(diff.path(), file_acc.to_owned() + &string);
files.insert(diff.path(), file_acc.to_owned() + &line_content);
}
None => {
files.insert(diff.path(), string);
files.insert(diff.path(), line_content);
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const GPT4: &str = "gpt-4";
const GPT4O: &str = "gpt-4o";
const GPT4_TURBO: &str = "gpt-4-turbo-preview";
const LLAMA3: &str = "llama3";
const GPT4OMINI: &str = "gpt-4o-mini";

#[derive(Debug, Clone, PartialEq)]
pub struct Response {
Expand All @@ -28,10 +29,11 @@ pub struct Request {
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, Serialize, Deserialize, Default)]
pub enum Model {
GPT4,
#[default]
GPT4o,
GPT4Turbo,
Llama3
Llama3,
#[default]
GPT4oMini
}

impl Model {
Expand Down Expand Up @@ -79,7 +81,8 @@ impl From<&Model> for &str {
Model::GPT4o => GPT4O,
Model::GPT4 => GPT4,
Model::GPT4Turbo => GPT4_TURBO,
Model::Llama3 => LLAMA3
Model::Llama3 => LLAMA3,
Model::GPT4oMini => GPT4OMINI
}
}
}
Expand All @@ -93,6 +96,7 @@ impl FromStr for Model {
GPT4 => Ok(Model::GPT4),
GPT4_TURBO => Ok(Model::GPT4Turbo),
LLAMA3 => Ok(Model::Llama3),
GPT4OMINI => Ok(Model::GPT4oMini),
model => bail!("Invalid model: {}", model)
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ use crate::config;
use crate::model::Response;
use crate::model::Request;

<<<<<<< HEAD
=======
#[derive(Debug, Clone, PartialEq)]
pub struct Response {
pub response: String
}

#[derive(Debug, Clone, PartialEq)]
pub struct Request {
pub prompt: String,
pub system: String,
pub max_tokens: u16,
pub model: Model
}
>>>>>>> 44782ec (Improve AI prompt (#26))

pub async fn call(request: Request) -> Result<Response> {
let api_key = config::APP
Expand All @@ -19,6 +34,7 @@ pub async fn call(request: Request) -> Result<Response> {

let request = CreateChatCompletionRequestArgs::default()
.model(request.model.to_string())
.max_tokens(request.max_tokens)
.messages([
ChatCompletionRequestSystemMessageArgs::default()
.content(request.system)
Expand Down

0 comments on commit 18e020a

Please sign in to comment.