Skip to content

Commit 44782ec

Browse files
authored
Improve AI prompt (#26)
* 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
1 parent 9ffc513 commit 44782ec

File tree

8 files changed

+23
-15
lines changed

8 files changed

+23
-15
lines changed

.github/workflows/cd.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ on:
44
push:
55
branches:
66
- main
7-
pull_request:
87
workflow_dispatch:
98

109
concurrency:

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "git-ai"
3-
version = "0.2.54"
3+
version = "0.2.56"
44
edition = "2021"
55
description = "Git AI: Automates commit messages using ChatGPT. Stage your files, and Git AI generates the messages."
66
license = "MIT"

resources/prompt.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
You are an AI assistant that generates concise and meaningful git commit messages based on provided diffs. Please adhere to the following guidelines:
22

33
- Structure: Begin with a clear, present-tense summary.
4-
- Content: Emphasize the changes and their rationale, excluding irrelevant details.
4+
- Content: While you should use the surrounding context to understand the changes, your commit message should ONLY describe the lines marked with + or -.
5+
- 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.
6+
- Changes: Only describe what was actually changed (added, removed, or modified).
57
- Consistency: Maintain uniformity in tense, punctuation, and capitalization.
68
- Accuracy: Ensure the message accurately reflects the changes and their purpose.
79
- Present tense, imperative mood. (e.g., "Add x to y" instead of "Added x to y")
810
- Max {{max_commit_length}} chars in the output
911

1012
## Output:
1113

12-
Your output should be a commit message generated from the input diff and nothing else.
14+
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).
1315

1416
## Input:
1517

src/commit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub async fn generate(diff: String, max_tokens: usize, model: Model) -> Result<o
3434
let request = openai::Request {
3535
system: instruction(),
3636
prompt: diff,
37-
max_tokens,
37+
max_tokens: max_tokens.try_into().unwrap_or(u16::MAX),
3838
model
3939
};
4040

src/hook.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ pub trait PatchDiff {
7070
}
7171

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

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

81+
// Include both changes and context, but prefix context lines with "context: "
82+
// This helps the model understand the context while still identifying actual changes
83+
let line_content = match line.origin() {
84+
'+' | '-' => string,
85+
_ => format!("context: {}", string)
86+
};
87+
8288
match files.get(&diff.path()) {
8389
Some(file_acc) => {
84-
files.insert(diff.path(), file_acc.to_owned() + &string);
90+
files.insert(diff.path(), file_acc.to_owned() + &line_content);
8591
}
8692
None => {
87-
files.insert(diff.path(), string);
93+
files.insert(diff.path(), line_content);
8894
}
8995
}
9096

src/model.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ use tiktoken_rs::model::get_context_size;
99

1010
const GPT4: &str = "gpt-4";
1111
const GPT4O: &str = "gpt-4o";
12-
const GPT4_TURBO: &str = "gpt-4-turbo-preview";
12+
const GPT4OMINI: &str = "gpt-4o-mini";
1313

1414
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, Serialize, Deserialize, Default)]
1515
pub enum Model {
1616
GPT4,
17-
#[default]
1817
GPT4o,
19-
GPT4Turbo
18+
#[default]
19+
GPT4oMini
2020
}
2121

2222
impl Model {
@@ -61,7 +61,7 @@ impl From<&Model> for &str {
6161
match model {
6262
Model::GPT4o => GPT4O,
6363
Model::GPT4 => GPT4,
64-
Model::GPT4Turbo => GPT4_TURBO
64+
Model::GPT4oMini => GPT4OMINI
6565
}
6666
}
6767
}
@@ -73,7 +73,7 @@ impl FromStr for Model {
7373
match s.trim().to_lowercase().as_str() {
7474
GPT4O => Ok(Model::GPT4o),
7575
GPT4 => Ok(Model::GPT4),
76-
GPT4_TURBO => Ok(Model::GPT4Turbo),
76+
GPT4OMINI => Ok(Model::GPT4oMini),
7777
model => bail!("Invalid model: {}", model)
7878
}
7979
}

src/openai.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct Response {
1515
pub struct Request {
1616
pub prompt: String,
1717
pub system: String,
18-
pub max_tokens: usize,
18+
pub max_tokens: u16,
1919
pub model: Model
2020
}
2121

@@ -30,6 +30,7 @@ pub async fn call(request: Request) -> Result<Response> {
3030

3131
let request = CreateChatCompletionRequestArgs::default()
3232
.model(request.model.to_string())
33+
.max_tokens(request.max_tokens)
3334
.messages([
3435
ChatCompletionRequestSystemMessageArgs::default()
3536
.content(request.system)

0 commit comments

Comments
 (0)