-
Notifications
You must be signed in to change notification settings - Fork 57
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
feat: Add RAG LLMReranker #784
Open
ishaansehgal99
wants to merge
12
commits into
main
Choose a base branch
from
Ishaan/rerank
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+175
−10
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1c5bf10
feat: Add Reranker
ishaansehgal99 24b5796
feat: cleanup reranker code
ishaansehgal99 ecb24a3
feat: cleanup reranker code
ishaansehgal99 9d65d7d
Merge branch 'main' of https://github.com/kaito-project/kaito into Is…
ishaansehgal99 5ba8b4c
fix: tests
ishaansehgal99 ae7a9d5
remove
ishaansehgal99 312ed1f
Merge branch 'main' of https://github.com/kaito-project/kaito into Is…
ishaansehgal99 7309edd
Merge branch 'main' of https://github.com/kaito-project/kaito into Is…
ishaansehgal99 715afdb
Merge branch 'main' into Ishaan/rerank
ishaansehgal99 7ebe956
Merge branch 'Ishaan/rerank' of https://github.com/kaito-project/kait…
ishaansehgal99 fbf2a38
fix: Reranker tests
ishaansehgal99 405b28d
fix: typo
ishaansehgal99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ def test_query_index_success(mock_post): | |
"result": "This is the completion from the API" | ||
} | ||
mock_post.return_value.json.return_value = mock_response | ||
# Index | ||
# Index | ||
request_data = { | ||
"index_name": "test_index", | ||
"documents": [ | ||
|
@@ -74,6 +74,100 @@ def test_query_index_success(mock_post): | |
assert response.json()["source_nodes"][0]["metadata"] == {} | ||
assert mock_post.call_count == 1 | ||
|
||
|
||
@patch('requests.post') | ||
def test_reranker_and_query_with_index(mock_post): | ||
""" | ||
Test reranker and query functionality with indexed documents. | ||
|
||
This test ensures the following: | ||
1. The custom reranker returns a relevance-sorted list of documents. | ||
2. The query response matches the expected format and contains the correct top results. | ||
|
||
Template for reranker input: | ||
A list of documents is shown below. Each document has a number next to it along with a summary of the document. | ||
A question is also provided. Respond with the numbers of the documents you should consult to answer the question, | ||
in order of relevance, as well as the relevance score. The relevance score is a number from 1-10 based on how | ||
relevant you think the document is to the question. Do not include any documents that are not relevant. | ||
|
||
Example format: | ||
Document 1: <summary of document 1> | ||
Document 2: <summary of document 2> | ||
... | ||
Document 10: <summary of document 10> | ||
|
||
Question: <question> | ||
Answer: | ||
Doc: 9, Relevance: 7 | ||
Doc: 3, Relevance: 4 | ||
Doc: 7, Relevance: 3 | ||
""" | ||
# Mock responses for the reranker and query API calls | ||
reranker_mock_response = "Doc: 4, Relevance: 10\nDoc: 5, Relevance: 10" | ||
query_mock_response = {"result": "This is the completion from the API"} | ||
mock_http_responses = [reranker_mock_response, query_mock_response] | ||
|
||
mock_post.return_value.json.side_effect = mock_http_responses | ||
|
||
# Define input documents for indexing | ||
documents = [ | ||
"The capital of France is great.", | ||
"The capital of France is huge.", | ||
"The capital of France is beautiful.", | ||
"""Have you ever visited Paris? It is a beautiful city where you can eat delicious food and see the Eiffel Tower. | ||
I really enjoyed all the cities in France, but its capital with the Eiffel Tower is my favorite city.""", | ||
"I really enjoyed my trip to Paris, France. The city is beautiful and the food is delicious. I would love to visit again. " | ||
"Such a great capital city." | ||
] | ||
|
||
# Indexing request payload | ||
index_request_payload = { | ||
"index_name": "test_index", | ||
"documents": [{"text": doc} for doc in documents] | ||
} | ||
|
||
# Perform indexing | ||
response = client.post("/index", json=index_request_payload) | ||
assert response.status_code == 200 | ||
|
||
# Query request payload with reranking | ||
top_n = len(reranker_mock_response.split("\n")) # Extract top_n from mock reranker response | ||
query_request_payload = { | ||
"index_name": "test_index", | ||
"query": "what is the capital of france?", | ||
"top_k": 5, | ||
"llm_params": {"temperature": 0.7}, | ||
"rerank_params": {"top_n": top_n} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are the list of parameters that we can change here? |
||
} | ||
|
||
# Perform query | ||
response = client.post("/query", json=query_request_payload) | ||
assert response.status_code == 200 | ||
query_response = response.json() | ||
|
||
# Validate query response | ||
assert query_response["response"] == query_response["result"] | ||
assert len(query_response["source_nodes"]) == top_n | ||
|
||
# Validate each source node in the query response | ||
expected_source_nodes = [ | ||
{"text": "Have you ever visited Paris? It is a beautiful city where you can eat " | ||
"delicious food and see the Eiffel Tower. I really enjoyed all the cities in " | ||
"France, but its capital with the Eiffel Tower is my favorite city.", | ||
"score": 10.0, "metadata": {}}, | ||
{"text": "I really enjoyed my trip to Paris, France. The city is beautiful and the " | ||
"food is delicious. I would love to visit again. Such a great capital city.", | ||
"score": 10.0, "metadata": {}}, | ||
] | ||
for i, expected_node in enumerate(expected_source_nodes): | ||
actual_node = query_response["source_nodes"][i] | ||
assert actual_node["text"] == expected_node["text"] | ||
assert actual_node["score"] == expected_node["score"] | ||
assert actual_node["metadata"] == expected_node["metadata"] | ||
|
||
# Verify the number of mock API calls | ||
assert mock_post.call_count == len(mock_http_responses) | ||
|
||
def test_query_index_failure(): | ||
# Prepare request data for querying. | ||
request_data = { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
low priority: we can reorg these params to cli args and using structured params instead of raw key-value dict.