Add support for rerank operation type

Created on 18 November 2024, about 1 month ago

Problem/Motivation

A semantic search response precision can be enhanced by a rerank algorithm.
Some providers exist that provide document re-ranking, or a specific query can be sent to a general-purpose LLM to ask for a re-rank

Proposed resolution

Add a new ReRank operation type

Remaining tasks

Review the MR

User interface changes

None

API changes

A new operation type called 'rerank' that can be implemented in AI providers

Data model changes

None

Feature request
Status

Active

Version

1.0

Component

AI Core module

Created by

🇮🇹Italy lussoluca Italy

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @lussoluca
  • Pipeline finished with Failed
    about 1 month ago
    Total: 176s
    #342012
  • Pipeline finished with Success
    about 1 month ago
    Total: 260s
    #342018
  • 🇮🇹Italy lussoluca Italy

    The first example of an AI provider that supports re-ranking: https://www.drupal.org/project/ai_provider_cohere

  • 🇬🇧United Kingdom scott_euser

    Hi! I think I don't understand it enough - I don't see where it fits in to AI Search for example, of the intention is to use it elsewhere. Can you provide clear steps on how to test this out please?

  • 🇮🇹Italy lussoluca Italy

    Hi,
    Yes, one use case could be to enhance a semantic search response; you can look at this page from Cohere: https://cohere.com/rerank

    An example can be like:

    $client = $backend->getTypesenseClient();
    
     $response = $client->searchDocuments('images', [
        'q' => $query,
        'query_by' => 'embedding',
        'exclude_fields' => 'embedding',
        'prefix' => FALSE,
        'vector_query' => 'embedding:([], distance_threshold:0.70, k:'.$num_results.')',
    ]);
    $hits = $response['hits'];
    
    $default_provider = $this->aiProviderManager->getDefaultProviderForOperationType('rerank');
    if ($default_provider != NULL) {
      try {
        $provider = $this
           ->aiProviderManager
            ->createInstance($default_provider['provider_id']);
    
        $response_reranked = $provider->rerank(
           new ReRankInput(
             model: $default_provider['model_id'],
             query: $query,
             top_n: $num_results,
             texts: array_map(
               static fn($hit) => $hit['document']['chunk'],
               $hits
             ),
             documents: $hits,
           ),
           $default_provider['model_id'],
         );
    
       $hits = $response_reranked->results;
    }
    
  • 🇬🇧United Kingdom yautja_cetanu
    • What do they mean by "re-rank" in this context.
    • Could you help explain the context by giving a specific real-world use-case?
    • How would this be used by the AI module specifically, how would Search API / ai Search or Assistants make use of this or is it intended for other things?
    • Why use "operation type" as the abstraction here.
    • Is there anything else cohere can do?
    • Where would this fit within the UI of the things we currently do?

    My thinking is that it would be a setting in an index in Search API. You could click "Rerank with AI" and then type in a prompt. I can't quite tell if its a opertation type or if its a something we should create in the vector database abstraction layer.

    It seems other VDBs can do this but can other LLMs do this? Could you decide to use ChatGPT to do the reranking?

    Also I think it might need to be AI_Reranking as reranking is a common thing for search that doesn't use AI a lot of the time right? It just uses a variety of algorithms.

  • 🇮🇹Italy lussoluca Italy

    Thanks, James, for your questions; I'll try to answer them below.

    What do they mean by "re-rank" in this context.

    This article explains in depth what I mean by re-ranking: https://www.pinecone.io/learn/series/rag/rerankers/
    It is a two-step process to enhance the result of a vector-based search. Some vector databases (like Pinecone) implement a re-ranking feature, but always in the second step, after you've retrieved an initial set of documents by using some kind of search.

    Could you help explain the context by giving a specific real-world use-case?

    I'm implementing a site where the client needs to find the best image between thousands. We saw that using a re-ranking algorithm on the retrieved results improved the quality of the result (we're using Cohere for that).

    How would this be used by the AI module specifically, how would Search API / ai Search or Assistants make use of this or is it intended for other things?

    Why use "operation type" as the abstraction here.

    I've implemented it as an operation type because (as for embedding or chat) you can use different algorithms from a different provider to perform a re-rank. You can use a generic LLM for that; take a look, for example, how LLPhant implements re-ranking: https://github.com/theodo-group/LLPhant/blob/main/src/Query/SemanticSear...
    As you wrote later, probably it makes sense to also expose this feature as a Search API postprocess_query processor plugin. However, I do not want to limit it to Search API only, as I may have retrieved the set of documents to re-rank from a different source.

    Is there anything else cohere can do?

    Yes, it can also be used to compute embeddings. I plan to add this feature to the ai_provider_cohere module.

    Where would this fit within the UI of the things we currently do?

    I think that re-ranking is not something the final users may be aware of. It can be limited to the processor configuration page from Search API.

    Also I think it might need to be AI_Reranking as reranking is a common thing for search that doesn't use AI a lot of the time right? It just uses a variety of algorithms.

    I agree with you, I'll update the MR.

Production build 0.71.5 2024