Skip to content

Commit

Permalink
♻️ Refactor LLM response handling and prompt instructions
Browse files Browse the repository at this point in the history
Improve error handling and message cleaning in LLM module

Enhance JSON cleaning function to handle non-ASCII characters
Update deserialization error logging with full message content
Refine system prompt instructions for commit message formatting
Adjust line wrapping guideline for improved readability
  • Loading branch information
hyperb1iss committed Aug 11, 2024
1 parent 67545cf commit 000519a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
16 changes: 7 additions & 9 deletions src/llm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,8 @@ where
log_debug!("User prompt: {}", user_prompt);

// Call get_refined_message_with_provider
let result = get_refined_message_with_provider::<T>(
llm_provider,
system_prompt,
user_prompt,
)
.await?;
let result =
get_refined_message_with_provider::<T>(llm_provider, system_prompt, user_prompt).await?;

Ok(result)
}
Expand Down Expand Up @@ -90,7 +86,7 @@ where
match serde_json::from_str::<T>(&cleaned_message) {
Ok(message) => Ok(message),
Err(e) => {
log_debug!("Deserialization error: {}", e);
log_debug!("Deserialization error: {} message: {}", e, cleaned_message);
Err(anyhow!("Deserialization error: {}", e))
}
}
Expand Down Expand Up @@ -205,8 +201,10 @@ pub fn get_combined_config(
}

fn clean_json_from_llm(json_str: &str) -> String {
// Remove potential leading/trailing whitespace
let trimmed = json_str.trim();
// Remove potential leading/trailing whitespace and invisible characters
let trimmed = json_str
.trim_start_matches(|c: char| c.is_whitespace() || !c.is_ascii())
.trim_end_matches(|c: char| c.is_whitespace() || !c.is_ascii());

// If wrapped in code block, remove the markers
let without_codeblock = if trimmed.starts_with("```") && trimmed.ends_with("```") {
Expand Down
5 changes: 4 additions & 1 deletion src/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn create_system_prompt(config: &Config) -> String {
3. Capitalize the subject line.
4. Do not end the subject line with a period.
5. Separate subject from body with a blank line.
6. Wrap the body at 72 characters.
6. Ensure that each line of the message body does not exceed 72 characters.
7. Use the body to explain what changes were made and their impact, not how they were implemented.
8. Be specific and avoid vague language.
9. Focus on the concrete changes and their effects, not assumptions about intent.
Expand All @@ -42,6 +42,9 @@ pub fn create_system_prompt(config: &Config) -> String {
\"title\": \"<title>\",
\"message\": \"<message>\"
}
Be sure to quote newlines and any other control characters in your response.
The message should be based entirely on the information provided in the context,
without any speculation or assumptions."
);
Expand Down

0 comments on commit 000519a

Please sign in to comment.