Skip to content

Improve AI prompt #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@ on:
push:
branches:
- main
pull_request:
workflow_dispatch:

concurrency:
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"
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:

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

12 changes: 9 additions & 3 deletions src/hook.rs
Original file line number Diff line number Diff line change
@@ -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();

@@ -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);
}
}

10 changes: 5 additions & 5 deletions src/model.rs
Original file line number Diff line number Diff line change
@@ -9,14 +9,14 @@ use tiktoken_rs::model::get_context_size;

const GPT4: &str = "gpt-4";
const GPT4O: &str = "gpt-4o";
const GPT4_TURBO: &str = "gpt-4-turbo-preview";
const GPT4OMINI: &str = "gpt-4o-mini";

#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, Serialize, Deserialize, Default)]
pub enum Model {
GPT4,
#[default]
GPT4o,
GPT4Turbo
#[default]
GPT4oMini
}

impl Model {
@@ -61,7 +61,7 @@ impl From<&Model> for &str {
match model {
Model::GPT4o => GPT4O,
Model::GPT4 => GPT4,
Model::GPT4Turbo => GPT4_TURBO
Model::GPT4oMini => GPT4OMINI
}
}
}
@@ -73,7 +73,7 @@ impl FromStr for Model {
match s.trim().to_lowercase().as_str() {
GPT4O => Ok(Model::GPT4o),
GPT4 => Ok(Model::GPT4),
GPT4_TURBO => Ok(Model::GPT4Turbo),
GPT4OMINI => Ok(Model::GPT4oMini),
model => bail!("Invalid model: {}", model)
}
}
3 changes: 2 additions & 1 deletion src/openai.rs
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ pub struct Response {
pub struct Request {
pub prompt: String,
pub system: String,
pub max_tokens: usize,
pub max_tokens: u16,
pub model: Model
}

@@ -30,6 +30,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)