Skip to content

Commit 6624ebe

Browse files
speedstorm1copybara-github
authored andcommitted
feat: Add vector search alpha to rag retrieval for hybrid search ranking
PiperOrigin-RevId: 670703417
1 parent 37627de commit 6624ebe

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

tests/unit/vertex_rag/test_rag_retrieval.py

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def test_retrieval_query_rag_resources_success(self):
7676
text=tc.TEST_QUERY_TEXT,
7777
similarity_top_k=2,
7878
vector_distance_threshold=0.5,
79+
vector_search_alpha=0.5,
7980
)
8081
retrieve_contexts_eq(response, tc.TEST_RETRIEVAL_RESPONSE)
8182

vertexai/preview/rag/rag_retrieval.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def retrieval_query(
3737
rag_corpora: Optional[List[str]] = None,
3838
similarity_top_k: Optional[int] = 10,
3939
vector_distance_threshold: Optional[float] = 0.3,
40+
vector_search_alpha: Optional[float] = 0.5,
4041
) -> RetrieveContextsResponse:
4142
"""Retrieve top k relevant docs/chunks.
4243
@@ -54,6 +55,7 @@ def retrieval_query(
5455
)],
5556
similarity_top_k=2,
5657
vector_distance_threshold=0.5,
58+
vector_search_alpha=0.5,
5759
)
5860
```
5961
@@ -67,6 +69,10 @@ def retrieval_query(
6769
similarity_top_k: The number of contexts to retrieve.
6870
vector_distance_threshold: Optional. Only return contexts with vector
6971
distance smaller than the threshold.
72+
vector_search_alpha: Optional. Controls the weight between dense and
73+
sparse vector search results. The range is [0, 1], where 0 means
74+
sparse vector search only and 1 means dense vector search only.
75+
The default value is 0.5.
7076
7177
Returns:
7278
RetrieveContextsResonse.
@@ -111,7 +117,13 @@ def retrieval_query(
111117
)
112118

113119
vertex_rag_store.vector_distance_threshold = vector_distance_threshold
114-
query = RagQuery(text=text, similarity_top_k=similarity_top_k)
120+
query = RagQuery(
121+
text=text,
122+
similarity_top_k=similarity_top_k,
123+
ranking=RagQuery.Ranking(
124+
alpha=vector_search_alpha,
125+
),
126+
)
115127
request = RetrieveContextsRequest(
116128
vertex_rag_store=vertex_rag_store,
117129
parent=parent,

0 commit comments

Comments
 (0)