Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ai-help): avoid spawning thread for history if history is disabled #438

Merged
merged 5 commits into from
Mar 27, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 35 additions & 25 deletions src/api/ai_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ fn log_errors_and_record_response(
user_id: i64,
help_ids: HelpIds,
) -> Result<Option<mpsc::UnboundedSender<CreateChatCompletionStreamResponse>>, ApiError> {
if !history_enabled {
return Ok(None);
}
Comment on lines +286 to +288
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fiji-flo I have second thoughts about early returning if !history_enabled, because that this function not only records the history, but also logs errors (including the stream ended without a finish_reason one).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should happen somewhere else. As in the new code path in the meta data PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Let's wait for #424 to be merged and move the logic over in this PR afterwards.


let mut conn = pool.get()?;
let (tx, mut rx) = mpsc::unbounded_channel::<CreateChatCompletionStreamResponse>();
actix_web::rt::spawn(async move {
Expand All @@ -302,33 +306,31 @@ fn log_errors_and_record_response(
}

if !has_finish_reason {
error!("AI Help log: OpenAI stream ended without a finish_reason");
error!("AI Help log: OpenAI stream ended without a finish_reason (recorded)");
}

if history_enabled {
let HelpIds {
chat_id,
message_id,
parent_id,
} = help_ids;
let response = ChatCompletionRequestMessage {
role: Assistant,
content: Some(answer.join("")),
..Default::default()
};
let insert = AIHelpHistoryMessageInsert {
user_id,
chat_id,
message_id,
parent_id,
created_at: None,
sources: None,
request: None,
response: Some(serde_json::to_value(response).unwrap_or(Null)),
};
if let Err(err) = add_help_history_message(&mut conn, insert) {
error!("AI Help log: {err}");
}
let HelpIds {
chat_id,
message_id,
parent_id,
} = help_ids;
let response = ChatCompletionRequestMessage {
role: Assistant,
content: Some(answer.join("")),
..Default::default()
};
let insert = AIHelpHistoryMessageInsert {
user_id,
chat_id,
message_id,
parent_id,
created_at: None,
sources: None,
request: None,
response: Some(serde_json::to_value(response).unwrap_or(Null)),
};
if let Err(err) = add_help_history_message(&mut conn, insert) {
error!("AI Help log: {err}");
}
});
Ok(Some(tx))
Expand Down Expand Up @@ -487,6 +489,14 @@ pub async fn ai_help(
} else {
context.status
};
if status
== db::types::AiHelpMessageStatus::FinishedNoReason
{
error!(
"AI Help log: OpenAI stream ended without a finish_reason (streamed)"
);
}

let ai_help_message_meta = AiHelpMessageMetaInsert {
user_id,
chat_id,
Expand Down
Loading