Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
neowu committed Jul 31, 2024
1 parent e8ee4ef commit 0484248
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/azure/chatgpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ async fn read_sse_response(http_response: Response, tx: &mpsc::Sender<String>) -
if let Some(finish_reason) = stream_choice.finish_reason {
choice.finish_reason = finish_reason;
if choice.finish_reason == "stop" {
// chatgpt doesn't return '\n' at end of message
tx.send("\n".to_string()).await?;
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/command/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl Complete {
if !message.is_empty() {
return Err(anyhow!("system message must be at first"));
}
if let Some(option) = parse_option(line) {
if let Some(option) = parse_option(line)? {
info!("option: {:?}", option);
model.option(option);
}
Expand Down Expand Up @@ -171,13 +171,13 @@ async fn add_message(model: &mut llm::Model, state: &ParserState, message: Strin
Ok(())
}

fn parse_option(line: &str) -> Option<ChatOption> {
let regex = Regex::new(r".*temperature=(\d+\.\d+).*").unwrap();
fn parse_option(line: &str) -> Result<Option<ChatOption>> {
let regex = Regex::new(r".*temperature=(\d+\.\d+).*")?;
if let Some(capture) = regex.captures(line) {
let temperature = f32::from_str(&capture[1]).unwrap();
Some(ChatOption { temperature })
let temperature = f32::from_str(&capture[1])?;
Ok(Some(ChatOption { temperature }))
} else {
None
Ok(None)
}
}

Expand All @@ -194,6 +194,6 @@ mod tests {
#[test]
fn parse_option() {
let option = super::parse_option("# system, temperature=2.0, top_p=0.95");
assert_eq!(option.unwrap().temperature, 2.0);
assert_eq!(option.unwrap().unwrap().temperature, 2.0);
}
}

0 comments on commit 0484248

Please sign in to comment.