Skip to content

Commit ab34ca5

Browse files
authored
fix(generate): 移除stop words并改进消息提取逻辑 (#14)
* fix(generate): 移除stop words并改进消息提取逻辑 - 移除STOP_WORDS常量以避免AI思考过程中的干扰 - 将DEFAULT_MAX_TOKENS从2048增加到4096以支持更长的响应 - 重写extract_aicommit_message函数,改进提取逻辑以处理多个<aicommit>块 - 移除对lazy_static的依赖 Signed-off-by: longjin <longjin@DragonOS.org> * chore: 更新版本号至 0.2.2 Signed-off-by: longjin <longjin@DragonOS.org> --------- Signed-off-by: longjin <longjin@DragonOS.org>
1 parent d571a1a commit ab34ca5

File tree

7 files changed

+29
-31
lines changed

7 files changed

+29
-31
lines changed

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 = "fastcommit"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
description = "AI-based command line tool to quickly generate standardized commit messages."
55
edition = "2021"
66
authors = ["longjin <fslongjin@vip.qq.com>"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ You can install `fastcommit` using the following method:
1010

1111
```bash
1212
# Install using cargo
13-
cargo install --git https://github.com/fslongjin/fastcommit --tag v0.2.1
13+
cargo install --git https://github.com/fslongjin/fastcommit --tag v0.2.2
1414
```
1515

1616
## Usage

README_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
```bash
1010
# 使用 cargo 安装
11-
cargo install --git https://github.com/fslongjin/fastcommit --tag v0.2.1
11+
cargo install --git https://github.com/fslongjin/fastcommit --tag v0.2.2
1212
```
1313

1414
## 使用

src/constants.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,7 @@ impl PromptTemplateReplaceLabel {
9090
}
9191
}
9292

93-
lazy_static! {
94-
pub static ref STOP_WORDS: Vec<String> = vec![String::from("</aicommit>")];
95-
}
96-
97-
pub const DEFAULT_MAX_TOKENS: u32 = 2048;
93+
pub const DEFAULT_MAX_TOKENS: u32 = 4096;
9894

9995
pub const BRANCH_NAME_PROMPT: &str = r#"
10096
# 角色

src/generate.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use crate::cli;
66
use crate::config::{self, Config};
77

88
use crate::constants::BRANCH_NAME_PROMPT;
9-
use crate::constants::{
10-
DEFAULT_MAX_TOKENS, DEFAULT_OPENAI_MODEL, DEFAULT_PROMPT_TEMPLATE, STOP_WORDS,
11-
};
9+
use crate::constants::{DEFAULT_MAX_TOKENS, DEFAULT_OPENAI_MODEL, DEFAULT_PROMPT_TEMPLATE};
1210
use crate::template_engine::{render_template, TemplateContext};
1311

1412
async fn generate_commit_message(
@@ -59,7 +57,7 @@ async fn generate_commit_message(
5957
top_p: None,
6058
n: None,
6159
stream: Some(false),
62-
stop: Some(STOP_WORDS.to_owned()),
60+
stop: None, // 移除 stop words 以避免思考过程中的干扰
6361
max_tokens: Some(DEFAULT_MAX_TOKENS as i32),
6462
presence_penalty: None,
6563
frequency_penalty: None,
@@ -103,25 +101,32 @@ fn delete_thinking_contents(orig: &str) -> String {
103101
}
104102

105103
fn extract_aicommit_message(response: &str) -> anyhow::Result<String> {
106-
let start_tag = "<aicommit>";
107-
let end_tag = "</aicommit>";
108-
109104
let response = delete_thinking_contents(response);
110105

111-
let start_idx = response
112-
.find(start_tag)
113-
.ok_or(anyhow::anyhow!("Start tag <aicommit> not found"))?
114-
+ start_tag.len();
115-
let end_idx = response.find(end_tag).unwrap_or_else(|| response.len());
106+
// 查找所有 <aicommit>...</aicommit> 块
107+
let mut matches = Vec::new();
108+
let mut pos = 0;
109+
110+
while let Some(start_idx) = response[pos..].find("<aicommit>") {
111+
let absolute_start = pos + start_idx;
112+
let content_start = absolute_start + "<aicommit>".len();
116113

117-
if start_idx >= end_idx {
118-
return Err(anyhow::anyhow!(
119-
"End tag </aicommit> not found or misplaced"
120-
));
114+
if let Some(end_idx) = response[content_start..].find("</aicommit>") {
115+
let absolute_end = content_start + end_idx;
116+
let content = &response[content_start..absolute_end];
117+
matches.push(content.trim());
118+
pos = absolute_end + "</aicommit>".len();
119+
} else {
120+
break;
121+
}
121122
}
122123

123-
let commit_message = response[start_idx..end_idx].trim().to_string();
124-
Ok(commit_message)
124+
// 返回第一个匹配的内容
125+
matches
126+
.into_iter()
127+
.next()
128+
.map(|s| s.to_string())
129+
.ok_or(anyhow::anyhow!("Start tag <aicommit> not found"))
125130
}
126131

127132
fn get_diff(diff_file: Option<&str>, range: Option<&str>) -> anyhow::Result<String> {
@@ -186,7 +191,7 @@ async fn generate_branch_name_with_ai(
186191
top_p: None,
187192
n: None,
188193
stream: Some(false),
189-
stop: Some(STOP_WORDS.to_owned()),
194+
stop: None, // 移除 stop words 以避免思考过程中的干扰
190195
max_tokens: Some(DEFAULT_MAX_TOKENS as i32),
191196
presence_penalty: None,
192197
frequency_penalty: None,

src/main.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use clap::Parser;
22
use log::error;
33

4-
#[macro_use]
5-
extern crate lazy_static;
6-
74
mod cli;
85
mod config;
96
mod constants;

0 commit comments

Comments
 (0)