Skip to content

Commit

Permalink
chore: fetch rag only on open chat
Browse files Browse the repository at this point in the history
  • Loading branch information
richardshiue committed Dec 27, 2024
1 parent 36d8a32 commit 8246af9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ class ChatBloc extends Bloc<ChatEvent, ChatState> {
},
didReceiveChatSettings: (settings) {
emit(
state.copyWith(selectedSourceIds: settings.ragIds),
state.copyWith(
selectedSourceIds: settings.ragIds.ragIds,
onlyUseSelectedSources: settings.ragOnly,
),
);
},
updateSelectedSources: (selectedSourcesIds) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ class _PromptInputDesktopSelectSourcesButtonState
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
cubit.updateSelectedSources(
context.read<ChatBloc>().state.selectedSourceIds,
);
final chatBlocState = context.read<ChatBloc>().state;
cubit
..updateSelectedSources(chatBlocState.selectedSourceIds)
..updateOnlyUseSelectedSources(chatBlocState.onlyUseSelectedSources);
});
}

Expand Down
2 changes: 1 addition & 1 deletion frontend/resources/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
"addToPageTitle": "Add message to...",
"addToNewPage": "Add to a new page",
"addToNewPageName": "Messages extracted from \"{}\"",
"onlyUseRags": "Only use selected sources to generate response"
"onlyUseRags": "Selected sources only"
},
"trash": {
"text": "Trash",
Expand Down
10 changes: 4 additions & 6 deletions frontend/rust-lib/flowy-ai/src/ai_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,12 @@ impl AIManager {
Ok(())
}

pub async fn get_rag_ids(&self, chat_id: &str) -> FlowyResult<Vec<String>> {
pub async fn get_chat_settings(&self, chat_id: &str) -> FlowyResult<ChatSettingsPB> {
if let Some(settings) = self
.store_preferences
.get_object::<ChatSettings>(&setting_store_key(chat_id))
{
return Ok(settings.rag_ids);
return Ok(settings.into());
}

let settings = refresh_chat_setting(
Expand All @@ -360,7 +360,7 @@ impl AIManager {
chat_id,
)
.await?;
Ok(settings.rag_ids)
Ok(settings.into())
}

pub async fn update_settings(
Expand Down Expand Up @@ -448,9 +448,7 @@ async fn refresh_chat_setting(
}

chat_notification_builder(chat_id, ChatNotification::DidUpdateChatSettings)
.payload(ChatSettingsPB {
rag_ids: settings.rag_ids.clone(),
})
.payload(ChatSettingsPB::from(settings.clone()))
.send();

Ok(settings)
Expand Down
25 changes: 23 additions & 2 deletions frontend/rust-lib/flowy-ai/src/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use std::collections::HashMap;

use crate::local_ai::local_llm_resource::PendingResource;
use flowy_ai_pub::cloud::{
ChatMessage, LLMModel, RelatedQuestion, RepeatedChatMessage, RepeatedRelatedQuestion,
ChatMessage, ChatSettings, LLMModel, RelatedQuestion, RepeatedChatMessage,
RepeatedRelatedQuestion,
};
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use lib_infra::validator_fn::required_not_empty_str;
Expand Down Expand Up @@ -542,7 +543,27 @@ pub struct CreateChatContextPB {
#[derive(Default, ProtoBuf, Clone, Debug)]
pub struct ChatSettingsPB {
#[pb(index = 1)]
pub rag_ids: Vec<String>,
pub rag_ids: RepeatedRagId,

#[pb(index = 2)]
pub rag_only: bool,
}

impl From<ChatSettings> for ChatSettingsPB {
fn from(value: ChatSettings) -> Self {
let rag_ids = RepeatedRagId {
rag_ids: value.rag_ids.clone(),
};

let rag_only = value
.metadata
.as_object()
.and_then(|map| map.get("rag_only"))
.and_then(|value| value.as_bool())
.unwrap_or_default();

Self { rag_ids, rag_only }
}
}

#[derive(Default, ProtoBuf, Clone, Debug, Validate)]
Expand Down
3 changes: 1 addition & 2 deletions frontend/rust-lib/flowy-ai/src/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,7 @@ pub(crate) async fn get_chat_settings_handler(
) -> DataResult<ChatSettingsPB, FlowyError> {
let chat_id = data.try_into_inner()?.value;
let ai_manager = upgrade_ai_manager(ai_manager)?;
let rag_ids = ai_manager.get_rag_ids(&chat_id).await?;
let pb = ChatSettingsPB { rag_ids };
let pb = ai_manager.get_chat_settings(&chat_id).await?;
data_result_ok(pb)
}

Expand Down

0 comments on commit 8246af9

Please sign in to comment.