Skip to content

Commit 1777f7a

Browse files
committed
bugfix(server): rename get_random_chunk_metadatas_query to get_random_chunk_qdrant_point_id_query and update its logic to reduce latency
1 parent 6cc6e58 commit 1777f7a

File tree

3 files changed

+40
-51
lines changed

3 files changed

+40
-51
lines changed

clients/search-component/example/src/routeTree.gen.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
/* prettier-ignore-start */
2-
31
/* eslint-disable */
42

53
// @ts-nocheck
64

75
// noinspection JSUnusedGlobalSymbols
86

9-
// This file is auto-generated by TanStack Router
7+
// This file was automatically generated by TanStack Router.
8+
// You should NOT make any changes in this file as it will be overwritten.
9+
// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified.
1010

1111
import { createFileRoute } from "@tanstack/react-router";
1212

@@ -24,21 +24,25 @@ const IndexLazyImport = createFileRoute("/")();
2424
// Create/Update Routes
2525

2626
const SearchpageRoute = SearchpageImport.update({
27+
id: "/searchpage",
2728
path: "/searchpage",
2829
getParentRoute: () => rootRoute,
2930
} as any);
3031

3132
const RecsRoute = RecsImport.update({
33+
id: "/recs",
3234
path: "/recs",
3335
getParentRoute: () => rootRoute,
3436
} as any);
3537

3638
const EcommerceRoute = EcommerceImport.update({
39+
id: "/ecommerce",
3740
path: "/ecommerce",
3841
getParentRoute: () => rootRoute,
3942
} as any);
4043

4144
const IndexLazyRoute = IndexLazyImport.update({
45+
id: "/",
4246
path: "/",
4347
getParentRoute: () => rootRoute,
4448
} as any).lazy(() => import("./routes/index.lazy").then((d) => d.Route));
@@ -129,8 +133,6 @@ export const routeTree = rootRoute
129133
._addFileChildren(rootRouteChildren)
130134
._addFileTypes<FileRouteTypes>();
131135

132-
/* prettier-ignore-end */
133-
134136
/* ROUTE_MANIFEST_START
135137
{
136138
"routes": {

server/src/operators/chunk_operator.rs

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,10 @@ pub async fn get_content_chunk_from_point_ids_query(
286286
Ok(content_chunks)
287287
}
288288

289-
pub async fn get_random_chunk_metadatas_query(
289+
pub async fn get_random_chunk_qdrant_point_id_query(
290290
dataset_id: uuid::Uuid,
291-
limit: u64,
292291
pool: web::Data<Pool>,
293-
) -> Result<Vec<ChunkMetadata>, ServiceError> {
292+
) -> Result<uuid::Uuid, ServiceError> {
294293
use crate::data::schema::chunk_metadata::dsl as chunk_metadata_columns;
295294

296295
let mut conn = pool
@@ -303,53 +302,39 @@ pub async fn get_random_chunk_metadatas_query(
303302
.select(chunk_metadata_columns::id)
304303
.order_by(chunk_metadata_columns::id.asc())
305304
.first::<uuid::Uuid>(&mut conn);
306-
let get_highest_ids_future = chunk_metadata_columns::chunk_metadata
305+
let get_highest_id_future = chunk_metadata_columns::chunk_metadata
307306
.filter(chunk_metadata_columns::dataset_id.eq(dataset_id))
308307
.select(chunk_metadata_columns::id)
309308
.order_by(chunk_metadata_columns::id.desc())
310-
.limit(limit.try_into().unwrap_or(10))
311-
.load::<uuid::Uuid>(&mut conn);
312-
let (lowest_id, highest_ids) = futures::join!(get_lowest_id_future, get_highest_ids_future);
309+
.first::<uuid::Uuid>(&mut conn);
310+
let (lowest_id, highest_id) = futures::join!(get_lowest_id_future, get_highest_id_future);
313311
let lowest_id: uuid::Uuid = lowest_id.map_err(|err| {
314312
ServiceError::BadRequest(format!(
315313
"Failed to load chunk with the lowest id in the dataset for random range: {:?}",
316314
err
317315
))
318316
})?;
319-
let highest_ids: Vec<uuid::Uuid> = highest_ids.map_err(|err| {
317+
let highest_id: uuid::Uuid = highest_id.map_err(|err| {
320318
ServiceError::BadRequest(format!(
321-
"Failed to load chunks with the highest id in the dataset for random range: {:?}",
319+
"Failed to load chunk with the lowest id in the dataset for random range: {:?}",
322320
err
323321
))
324322
})?;
325-
let highest_id = match highest_ids.get(0) {
326-
Some(id) => *id,
327-
None => {
328-
return Err(ServiceError::NotFound(
329-
"Chunk with the highest id in the dataset not found for random range".to_string(),
330-
))
331-
}
332-
};
333323
let random_uuid = uuid_between(lowest_id, highest_id);
334324

335-
let chunk_metadatas: Vec<ChunkMetadataTable> = chunk_metadata_columns::chunk_metadata
325+
let qdrant_point_id: uuid::Uuid = chunk_metadata_columns::chunk_metadata
336326
.filter(chunk_metadata_columns::dataset_id.eq(dataset_id))
337327
.filter(chunk_metadata_columns::id.gt(random_uuid))
338328
.order_by(chunk_metadata_columns::id.asc())
339-
.limit(limit.try_into().unwrap_or(10))
340-
.select(ChunkMetadataTable::as_select())
341-
.load::<ChunkMetadataTable>(&mut conn)
329+
.select(chunk_metadata_columns::qdrant_point_id)
330+
.first::<uuid::Uuid>(&mut conn)
342331
.await
343-
.map_err(|_| ServiceError::BadRequest("Failed to load metadata".to_string()))?;
344-
345-
let chunk_metadatas = chunk_metadatas
346-
.into_iter()
347-
.map(|chunk_metadata_table| {
348-
ChunkMetadata::from_table_and_tag_set(chunk_metadata_table, vec![])
349-
})
350-
.collect();
332+
.map_err(|e| {
333+
log::error!("Failed to load random qdrant_point_ids: {:?}", e);
334+
ServiceError::BadRequest("Failed to load random qdrant_point_ids".to_string())
335+
})?;
351336

352-
Ok(chunk_metadatas)
337+
Ok(qdrant_point_id)
353338
}
354339

355340
pub async fn get_metadata_from_id_query(

server/src/operators/message_operator.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::operators::chunk_operator::get_random_chunk_metadatas_query;
1+
use crate::operators::chunk_operator::get_random_chunk_qdrant_point_id_query;
22
use crate::operators::message_operator::models::DatasetAndOrgWithSubAndPlan;
33
use itertools::Itertools;
44
use simple_server_timing_header::Timer;
@@ -1849,20 +1849,24 @@ pub async fn suggested_new_queries(
18491849
}
18501850
None => {
18511851
let random_offset_id = match payload.filters {
1852-
Some(_) => Some(uuid::Uuid::nil()),
1853-
None => get_random_chunk_metadatas_query(dataset_id, 1, pool.clone())
1854-
.await?
1855-
.clone()
1856-
.get(0)
1857-
.cloned()
1858-
.map(|chunk| chunk.qdrant_point_id),
1852+
Some(_) => uuid::Uuid::nil(),
1853+
None => get_random_chunk_qdrant_point_id_query(dataset_id, pool.clone()).await?,
18591854
};
18601855
let filter =
18611856
assemble_qdrant_filter(filters, None, None, dataset_id, pool.clone()).await?;
18621857

1863-
let (search_results, _) =
1864-
scroll_dataset_points(10, random_offset_id, None, dataset_config.clone(), filter)
1865-
.await?;
1858+
let (search_results, _) = scroll_dataset_points(
1859+
payload
1860+
.suggestions_to_create
1861+
.unwrap_or(5)
1862+
.try_into()
1863+
.unwrap(),
1864+
Some(random_offset_id),
1865+
None,
1866+
dataset_config.clone(),
1867+
filter,
1868+
)
1869+
.await?;
18661870
if qdrant_only {
18671871
search_results
18681872
.iter()
@@ -1890,11 +1894,9 @@ pub async fn suggested_new_queries(
18901894

18911895
let rag_content = chunk_metadatas
18921896
.iter()
1893-
.enumerate()
1894-
.map(|(idx, chunk)| {
1897+
.map(|chunk| {
18951898
format!(
1896-
"Doc {}: {}",
1897-
idx + 1,
1899+
"- {}",
18981900
convert_html_to_text(&(chunk.chunk_html.clone().unwrap_or_default()))
18991901
)
19001902
})
@@ -1920,7 +1922,7 @@ pub async fn suggested_new_queries(
19201922
None => "".to_string(),
19211923
};
19221924

1923-
let number_of_suggestions_to_create = payload.suggestions_to_create.unwrap_or(10);
1925+
let number_of_suggestions_to_create = payload.suggestions_to_create.unwrap_or(5);
19241926

19251927
let content = ChatMessageContent::Text(format!(
19261928
"Here is some content which the user might be looking for: {rag_content}{context_sentence}. Generate {number_of_suggestions_to_create} varied followup {query_style} style queries based off the domain of this dataset. Your only response should be the {number_of_suggestions_to_create} followup {query_style} style queries which are separated by new lines and are just text and you do not add any other context or information about the followup {query_style} style queries. This should not be a list, so do not number each {query_style} style queries.",

0 commit comments

Comments
 (0)