Skip to content

Commit d620798

Browse files
welkeyeverAsterDY
authored andcommitted
optimize: prompt struct design (cloudwego#22)
* feat: structurize the object to be compressed * feat: add model file * optimzie: prompt engineering
1 parent 7d55839 commit d620798

2 files changed

Lines changed: 46 additions & 10 deletions

File tree

script/ModelFile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM codellama:latest
2+
3+
SYSTEM "You are an engineer who is proficient in Golang. You are responsible for summarizing the functions/methods given by the user(in JSON string format). Try to condense output into one sentence and retain key information as much as possible. DO NOT show any codes in your answer. The function/method to be summarized is composed of JSON. For an example:\n
4+
{\"Content\":\"func unifyPath(path string) string {\\n\\tif IsWindows() {\\n\\t\\tpath = strings.ReplaceAll(path, \\\"\\\\\\\\\\\", \\\"/\\\")\\n\\t}\\n\\treturn path\\n}\",\"related_func\":[{\"CallName\":\"IsWindows\",\"Description\":\"\\nThis function checks whether the system is Windows or not. \"}]}\n
5+
\"Content\" contains the content of the main function/method which is to be summarized.\n
6+
\"related_func\" is a list for all the functions or methods which are called in the main function/method. Each Element of the list is an object: \"related_func\" shows the name which is called in the main function/method. \"Description\" is the summarized description of it. If \"related_func\" is null means no any other context is added to the summary of the mean function/method. DO NOT mention \"JSON string\" in your answer, just focus on the function/method. The format of the output should follow like this: XXX is for XXX..."
7+
8+
PARAMETER temperature 0.7

src/compress/compress.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ pub struct Identity {
7878
pub name: String,
7979
}
8080

81+
#[derive(Serialize, Deserialize, Debug)]
82+
struct ToCompressFunc {
83+
#[serde(rename = "Content")]
84+
content: String,
85+
#[serde(rename = "related_func")]
86+
related_func: Option<Vec<CalledFunc>>,
87+
}
88+
89+
90+
#[derive(Serialize, Deserialize, Debug)]
91+
struct CalledFunc {
92+
#[serde(rename = "CallName")]
93+
pub call_name: String,
94+
#[serde(rename = "Description")]
95+
pub description: String,
96+
}
97+
8198
pub fn from_json(json: &str) -> Result<Repository, Box<dyn Error>> {
8299
let f: Repository = serde_json::from_str(json)?;
83100
Ok(f)
@@ -149,10 +166,20 @@ pub async fn cascade_compress_function(id: &Identity, repo: &mut Repository) {
149166
}
150167

151168
println!("start to compress function: {}", func_opt.name);
152-
llm_compress(func_opt.content.as_str(), map).await
169+
if func_opt.content.is_empty() {
170+
println!("content is empty skip it");
171+
Some("".to_string())
172+
} else {
173+
llm_compress(func_opt.content.as_str(), map).await
174+
}
153175
};
154176

155177
let mut func_opt = repo.packages.get_mut(id.pkg_path.as_str()).unwrap().functions.get_mut(id.name.as_str()).unwrap();
178+
if content.is_some() {
179+
let content = content.unwrap().trim().to_string();
180+
func_opt.compress_data = Some(content);
181+
return;
182+
}
156183
func_opt.compress_data = content;
157184
}
158185

@@ -161,24 +188,25 @@ async fn llm_compress(func: &str, extra: HashMap<String, String>) -> Option<Stri
161188
Option::from(compress_data)
162189
}
163190

191+
164192
pub async fn _ollama_compress(func: String, ctx: HashMap<String, String>) -> String {
165193
let request_url = format!("http://localhost:11434/api/generate");
166194

167-
let mut prompt = r#"You are an engineer who is proficient in Golang. You are responsible for summarizing the functions/methods given by the user, DO NOT put anything that is not mentioned in the function.Try to condense output into one sentence and retain key information as much as possible. DO NOT show any codes in your answer. Function/methods content is as follow:"#.to_string();
168-
169-
prompt.push_str("\n```\n");
170-
prompt.push_str(func.as_str());
171-
prompt.push_str("\n```\n");
195+
let mut compress_func = ToCompressFunc { content: func, related_func: None };
172196
if !ctx.is_empty() {
173-
prompt.push_str("\nRelated function:\n");
197+
let mut related_func = Vec::new();
174198
for (name, compressed_data) in ctx {
175-
prompt.push_str(&*(name + ": " + &*compressed_data + "\n"));
199+
let re = CalledFunc { call_name: name, description: compressed_data };
200+
related_func.push(re);
176201
}
202+
compress_func.related_func = Some(related_func);
177203
}
178204

205+
let to_compress_func = serde_json::to_string(&compress_func).unwrap();
206+
179207

180-
println!("use prompt:\n{}", prompt);
181-
let req_body: ollama_req = ollama_req { model: "codellama".to_string(), prompt };
208+
println!("use prompt:\n{}", to_compress_func);
209+
let req_body: ollama_req = ollama_req { model: "codellama-private".to_string(), prompt: to_compress_func };
182210
let client = reqwest::Client::new();
183211
let mut response = client
184212
.post(&request_url)

0 commit comments

Comments
 (0)