Skip to content

Commit

Permalink
feature: add fall back response if there is no chunks above the score…
Browse files Browse the repository at this point in the history
… threshold
  • Loading branch information
densumesh committed Nov 21, 2024
1 parent e4f8e14 commit 1fc44e1
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
23 changes: 22 additions & 1 deletion clients/ts-sdk/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -6268,6 +6268,11 @@
],
"nullable": true
},
"no_result_message": {
"type": "string",
"description": "Options for handling the response for the llm to return when no results are found",
"nullable": true
},
"page_size": {
"type": "integer",
"format": "int64",
Expand Down Expand Up @@ -8539,6 +8544,11 @@
"type": "string",
"description": "The content of the user message to attach to the topic and then generate an assistant message in response to."
},
"no_result_message": {
"type": "string",
"description": "No result message for when there are no chunks found above the score threshold.",
"nullable": true
},
"page_size": {
"type": "integer",
"format": "int64",
Expand Down Expand Up @@ -9244,6 +9254,11 @@
"type": "string",
"description": "The new content of the message to replace the old content with."
},
"no_result_message": {
"type": "string",
"description": "No result message for when there are no chunks found above the score threshold.",
"nullable": true
},
"page_size": {
"type": "integer",
"format": "int64",
Expand Down Expand Up @@ -9476,7 +9491,8 @@
"group_chunks_updated",
"group_chunks_action_failed",
"crawl_completed",
"crawl_failed"
"crawl_failed",
"crawl_started"
]
},
"EventTypes": {
Expand Down Expand Up @@ -12699,6 +12715,11 @@
],
"nullable": true
},
"no_result_message": {
"type": "string",
"description": "No result message for when there are no chunks found above the score threshold.",
"nullable": true
},
"page_size": {
"type": "integer",
"format": "int64",
Expand Down
18 changes: 17 additions & 1 deletion clients/ts-sdk/src/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export type AddChunkToGroupReqPayload = {
export type ApiKeyRequestParams = {
filters?: ((ChunkFilter) | null);
highlight_options?: ((HighlightOptions) | null);
/**
* Options for handling the response for the llm to return when no results are found
*/
no_result_message?: (string) | null;
/**
* Page size is the number of chunks to fetch. This can be used to fetch more than 10 chunks at a time.
*/
Expand Down Expand Up @@ -707,6 +711,10 @@ export type CreateMessageReqPayload = {
* The content of the user message to attach to the topic and then generate an assistant message in response to.
*/
new_message_content: string;
/**
* No result message for when there are no chunks found above the score threshold.
*/
no_result_message?: (string) | null;
/**
* Page size is the number of chunks to fetch during RAG. If 0, then no search will be performed. If specified, this will override the N retrievals to include in the dataset configuration. Default is None.
*/
Expand Down Expand Up @@ -1012,6 +1020,10 @@ export type EditMessageReqPayload = {
* The new content of the message to replace the old content with.
*/
new_message_content: string;
/**
* No result message for when there are no chunks found above the score threshold.
*/
no_result_message?: (string) | null;
/**
* Page size is the number of chunks to fetch during RAG. If 0, then no search will be performed. If specified, this will override the N retrievals to include in the dataset configuration. Default is None.
*/
Expand Down Expand Up @@ -1120,7 +1132,7 @@ export type EventReturn = {
page_count: number;
};

export type EventTypeRequest = 'file_uploaded' | 'file_upload_failed' | 'chunks_uploaded' | 'chunk_action_failed' | 'chunk_updated' | 'bulk_chunks_deleted' | 'dataset_delete_failed' | 'qdrant_upload_failed' | 'bulk_chunk_upload_failed' | 'group_chunks_updated' | 'group_chunks_action_failed' | 'crawl_completed' | 'crawl_failed';
export type EventTypeRequest = 'file_uploaded' | 'file_upload_failed' | 'chunks_uploaded' | 'chunk_action_failed' | 'chunk_updated' | 'bulk_chunks_deleted' | 'dataset_delete_failed' | 'qdrant_upload_failed' | 'bulk_chunk_upload_failed' | 'group_chunks_updated' | 'group_chunks_action_failed' | 'crawl_completed' | 'crawl_failed' | 'crawl_started';

export type EventTypes = {
/**
Expand Down Expand Up @@ -2140,6 +2152,10 @@ export type RegenerateMessageReqPayload = {
filters?: ((ChunkFilter) | null);
highlight_options?: ((HighlightOptions) | null);
llm_options?: ((LLMOptions) | null);
/**
* No result message for when there are no chunks found above the score threshold.
*/
no_result_message?: (string) | null;
/**
* Page size is the number of chunks to fetch during RAG. If 0, then no search will be performed. If specified, this will override the N retrievals to include in the dataset configuration. Default is None.
*/
Expand Down
14 changes: 14 additions & 0 deletions frontends/chat/src/components/Layouts/MainLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const MainLayout = (props: LayoutProps) => {
const [minScore, setMinScore] = createSignal<number | null>(null);
const [streamingCompletion, setStreamingCompletion] =
createSignal<boolean>(false);
const [noResultMessage, setNoResultMessage] = createSignal<string | null>();
const [completionAbortController, setCompletionAbortController] =
createSignal<AbortController>(new AbortController());
const [showFilterModal, setShowFilterModal] = createSignal<boolean>(false);
Expand Down Expand Up @@ -231,6 +232,7 @@ const MainLayout = (props: LayoutProps) => {
use_images: useImages(),
},
},
no_result_message: noResultMessage(),
use_group_search: useGroupSearch(),
search_type: searchType(),
context_options: contextOptions(),
Expand Down Expand Up @@ -549,6 +551,18 @@ const MainLayout = (props: LayoutProps) => {
}}
/>
</div>
<div class="flex w-full items-center gap-x-2">
<label for="search_query">No Result Message:</label>
<input
type="text"
id="search_query"
class="w-3/4 rounded-md border border-neutral-300 bg-neutral-100 p-1 dark:border-neutral-900 dark:bg-neutral-700"
value={noResultMessage() ?? ""}
onChange={(e) => {
setNoResultMessage(e.target.value);
}}
/>
</div>
<div class="flex w-full items-center gap-x-2">
<label for="search_option">Search Type:</label>
<select
Expand Down
9 changes: 9 additions & 0 deletions server/src/data/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3245,6 +3245,8 @@ pub struct ApiKeyRequestParams {
pub remove_stop_words: Option<bool>,
/// Options for handling typos in the search query
pub typo_options: Option<TypoOptions>,
/// Options for handling the response for the llm to return when no results are found
pub no_result_message: Option<String>,
}

impl ApiKeyRequestParams {
Expand All @@ -3266,6 +3268,7 @@ impl ApiKeyRequestParams {
score_threshold: self.score_threshold.or(payload.score_threshold),
llm_options: payload.llm_options,
context_options: payload.context_options,
no_result_message: self.no_result_message.or(payload.no_result_message),
}
}

Expand Down Expand Up @@ -6747,6 +6750,7 @@ impl<'de> Deserialize<'de> for CreateMessageReqPayload {
pub user_id: Option<String>,
pub use_group_search: Option<bool>,
pub context_options: Option<ContextOptions>,
pub no_result_message: Option<String>,
#[serde(flatten)]
other: std::collections::HashMap<String, serde_json::Value>,
}
Expand Down Expand Up @@ -6778,6 +6782,7 @@ impl<'de> Deserialize<'de> for CreateMessageReqPayload {
llm_options,
user_id: helper.user_id,
context_options,
no_result_message: helper.no_result_message,
})
}
}
Expand All @@ -6801,6 +6806,7 @@ impl<'de> Deserialize<'de> for RegenerateMessageReqPayload {
pub user_id: Option<String>,
pub use_group_search: Option<bool>,
pub context_options: Option<ContextOptions>,
pub no_result_message: Option<String>,
#[serde(flatten)]
other: std::collections::HashMap<String, serde_json::Value>,
}
Expand Down Expand Up @@ -6831,6 +6837,7 @@ impl<'de> Deserialize<'de> for RegenerateMessageReqPayload {
llm_options,
user_id: helper.user_id,
context_options,
no_result_message: helper.no_result_message,
})
}
}
Expand All @@ -6856,6 +6863,7 @@ impl<'de> Deserialize<'de> for EditMessageReqPayload {
pub llm_options: Option<LLMOptions>,
pub user_id: Option<String>,
pub context_options: Option<ContextOptions>,
pub no_result_message: Option<String>,
#[serde(flatten)]
other: std::collections::HashMap<String, serde_json::Value>,
}
Expand Down Expand Up @@ -6888,6 +6896,7 @@ impl<'de> Deserialize<'de> for EditMessageReqPayload {
user_id: helper.user_id,
llm_options,
context_options,
no_result_message: helper.no_result_message,
})
}
}
Expand Down
8 changes: 8 additions & 0 deletions server/src/handlers/message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ pub struct CreateMessageReqPayload {
pub llm_options: Option<LLMOptions>,
/// Context options to use for the completion. If not specified, all options will default to false.
pub context_options: Option<ContextOptions>,
/// No result message for when there are no chunks found above the score threshold.
pub no_result_message: Option<String>,
}

/// Create message
Expand Down Expand Up @@ -359,6 +361,8 @@ pub struct RegenerateMessageReqPayload {
pub user_id: Option<String>,
/// Context options to use for the completion. If not specified, all options will default to false.
pub context_options: Option<ContextOptions>,
/// No result message for when there are no chunks found above the score threshold.
pub no_result_message: Option<String>,
}

#[derive(Serialize, Debug, ToSchema)]
Expand Down Expand Up @@ -391,6 +395,8 @@ pub struct EditMessageReqPayload {
pub user_id: Option<String>,
/// Context options to use for the completion. If not specified, all options will default to false.
pub context_options: Option<ContextOptions>,
/// No result message for when there are no chunks found above the score threshold.
pub no_result_message: Option<String>,
}

impl From<EditMessageReqPayload> for CreateMessageReqPayload {
Expand All @@ -409,6 +415,7 @@ impl From<EditMessageReqPayload> for CreateMessageReqPayload {
llm_options: data.llm_options,
user_id: data.user_id,
context_options: data.context_options,
no_result_message: data.no_result_message,
}
}
}
Expand All @@ -429,6 +436,7 @@ impl From<RegenerateMessageReqPayload> for CreateMessageReqPayload {
llm_options: data.llm_options,
user_id: data.user_id,
context_options: data.context_options,
no_result_message: data.no_result_message,
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions server/src/operators/message_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,15 @@ pub async fn stream_response(
)
.await?;

if chunk_metadatas.is_empty() && create_message_req_payload.no_result_message.is_some() {
let response_stream = stream::iter(vec![Ok::<actix_web::web::Bytes, actix_web::Error>(
Bytes::from(create_message_req_payload.no_result_message.unwrap()),
)]);
return Ok(HttpResponse::Ok()
.insert_header(("TR-QueryID", search_id.to_string()))
.streaming(response_stream));
}

let chunk_data = chunk_metadatas
.clone()
.into_iter()
Expand Down

0 comments on commit 1fc44e1

Please sign in to comment.