Find answers from the community

Updated 3 months ago

Is there a way to define custom similarity scores for querying and index? I want to do us

Is there a way to define custom similarity scores for querying and index? I want to do use a weighted score like similarity_score = (0.25)similarity_with_content + (0.75)similarity_with_metadata
k
2 comments
Please stand by @node0 as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
Yes, you can define custom similarity scores in LlamaIndex. You can use a hybrid approach to compute the final similarity score by weighting the node similarity and document similarity. This is demonstrated in the Hybrid Retriever example () where the final similarity score is computed as (alpha * node similarity) + ((1-alpha) * document similarity).

Here is the relevant code snippet:

Plain Text
full_similarity = (self._alpha * node.score) + ((1 - self._alpha) * doc_similarity)


In this code, alpha is a parameter that determines the weighting. If you want to use a weighted score like similarity_score = (0.25)*similarity_with_content + (0.75)*similarity_with_metadata, you can set alpha to 0.25. The node.score can be considered as similarity_with_content and doc_similarity as similarity_with_metadata.

You can also customize the stages of querying in LlamaIndex using the low-level composition API. This gives you granular control over your querying. You can find more details about this in the Customizing the stages of querying () section.

Remember to sort the results based on the computed similarity score and return the top-k results as per your requirement.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Add a reply
Sign up and join the conversation on Discord