FlashRank.jl terinspirasi oleh paket FlashRank Python yang mengagumkan, awalnya dikembangkan oleh Prithiviraj Damodaran. Paket ini memanfaatkan bobot model dari repo HF Prithiviraj dan repo HF Svilupp untuk menyediakan cara yang cepat dan efisien untuk menentukan peringkat dokumen yang relevan dengan kueri tertentu tanpa GPU dan ketergantungan besar .
Hal ini meningkatkan alur Retrieval Augmented Generation (RAG) dengan memprioritaskan dokumen yang paling sesuai. Model terkecil dapat dijalankan di hampir semua mesin.
:tiny
):mini4
):mini6
):mini
atau mini12
)Seberapa cepat? Dengan model Tiny, Anda dapat menentukan peringkat 100 dokumen dalam ~0,1 detik di laptop. Dengan model MiniLM (12 lapisan), Anda dapat menentukan peringkat 100 dokumen dalam ~0,4 detik.
Tip: Pilih model terbesar yang mampu Anda beli dengan anggaran latensi Anda, misalnya MiniLM L-12 adalah yang paling lambat namun memiliki akurasi terbaik.
Perhatikan bahwa kami menggunakan model BERT dengan ukuran potongan maksimum 512 token (lebih dari itu akan terpotong).
Tambahkan ke lingkungan Anda hanya dengan:
using Pkg
Pkg . activate ( " . " )
Pkg . add ( " FlashRank " )
Memberi peringkat pada dokumen Anda untuk kueri tertentu semudah:
ENV [ " DATADEPS_ALWAYS_ACCEPT " ] = " true "
using FlashRank
ranker = RankerModel () # Defaults to model = `:tiny`
query = " How to speedup LLMs? "
passages = [
" Introduce *lookahead decoding*: - a parallel decoding algo to accelerate LLM inference - w/o the need for a draft model or a data store - linearly decreases # decoding steps relative to log(FLOPs) used per decoding step. " ,
" LLM inference efficiency will be one of the most crucial topics for both industry and academia, simply because the more efficient you are, the more $$$ you will save. vllm project is a must-read for this direction, and now they have just released the paper " ,
" There are many ways to increase LLM inference throughput (tokens/second) and decrease memory footprint, sometimes at the same time. Here are a few methods I’ve found effective when working with Llama 2. These methods are all well-integrated with Hugging Face. This list is far from exhaustive; some of these techniques can be used in combination with each other and there are plenty of others to try. - Bettertransformer (Optimum Library): Simply call `model.to_bettertransformer()` on your Hugging Face model for a modest improvement in tokens per second. - Fp4 Mixed-Precision (Bitsandbytes): Requires minimal configuration and dramatically reduces the model's memory footprint. - AutoGPTQ: Time-consuming but leads to a much smaller model and faster inference. The quantization is a one-time cost that pays off in the long run. " ,
" Ever want to make your LLM inference go brrrrr but got stuck at implementing speculative decoding and finding the suitable draft model? No more pain! Thrilled to unveil Medusa, a simple framework that removes the annoying draft model while getting 2x speedup. " ,
" vLLM is a fast and easy-to-use library for LLM inference and serving. vLLM is fast with: State-of-the-art serving throughput Efficient management of attention key and value memory with PagedAttention Continuous batching of incoming requests Optimized CUDA kernels " ,
];
result = rank (ranker, query, passages)
result
bertipe RankResult
dan berisi bagian yang diurutkan, skornya (0-1, di mana 1 adalah yang terbaik) dan posisi dokumen yang diurutkan (mengacu pada vektor passages
asli).
Berikut ini garis besar singkat tentang bagaimana Anda dapat mengintegrasikan FlashRank.jl ke dalam pipeline RAG PromptingTools.jl Anda.
Untuk contoh selengkapnya, lihat examples/prompting_tools_integration.jl
.
using FlashRank
using PromptingTools
using PromptingTools . Experimental . RAGTools
const RT = PromptingTools . Experimental . RAGTools
# Wrap the model to be a valid Ranker recognized by RAGTools
# It will be provided to the airag/rerank function to avoid instantiating it on every call
struct FlashRanker <: RT.AbstractReranker
model :: RankerModel
end
reranker = RankerModel ( :tiny ) |> FlashRanker
# Define the method for ranking with it
function RT . rerank (
reranker :: FlashRanker , index :: RT.AbstractDocumentIndex , question :: AbstractString ,
candidates :: RT.AbstractCandidateChunks ; kwargs ... )
# # omitted for brevity
# # See examples/prompting_tools_integration.jl for details
end
# # Apply to the pipeline configuration, eg,
cfg = RAGConfig (; retriever = RT . AdvancedRetriever (; reranker))
# # assumes existing index
question = " Tell me about prehistoric animals "
result = airag (cfg, index; question, return_all = true )
Anda juga dapat memanfaatkan penyematan yang cukup "kasar" namun cepat dengan model tiny_embed
(Bert-L4).
embedder = FlashRank . EmbedderModel ( :tiny_embed )
passages = [ " This is a test " , " This is another test " ]
result = FlashRank . embed (embedder, passages)