Elffuss BraidHub

kikocis/lancedb public

main · 3.4 KiBRenderedRawPermalink
1## Finetuning the Embedding Model
2Try it yourself - <a href="https://colab.research.google.com/github/lancedb/lancedb/blob/main/docs/src/notebooks/embedding_tuner.ipynb"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"></a><br/>
3
4Another way to improve retriever performance is to fine-tune the embedding model itself. Fine-tuning the embedding model can help in learning better representations for the documents and queries in the dataset. This can be particularly useful when the dataset is very different from the pre-trained data used to train the embedding model.
5
6We'll use the same dataset as in the previous sections. Start off by splitting the dataset into training and validation sets:
7```python
8from sklearn.model_selection import train_test_split
9
10train_df, validation_df = train_test_split("data_qa.csv", test_size=0.2, random_state=42)
11
12train_df.to_csv("data_train.csv", index=False)
13validation_df.to_csv("data_val.csv", index=False)
14```
15
16You can use any tuning API to fine-tune embedding models. In this example, we'll utilise Llama-index as it also comes with utilities for synthetic data generation and training the model.
17
18
19Then parse the dataset as llama-index text nodes and generate synthetic QA pairs from each node.
20```python
21from llama_index.core.node_parser import SentenceSplitter
22from llama_index.readers.file import PagedCSVReader
23from llama_index.finetuning import generate_qa_embedding_pairs
24from llama_index.core.evaluation import EmbeddingQAFinetuneDataset
25
26def load_corpus(file):
27 loader = PagedCSVReader(encoding="utf-8")
28 docs = loader.load_data(file=Path(file))
29
30 parser = SentenceSplitter()
31 nodes = parser.get_nodes_from_documents(docs)
32
33 return nodes
34
35from llama_index.llms.openai import OpenAI
36
37
38train_dataset = generate_qa_embedding_pairs(
39 llm=OpenAI(model="gpt-3.5-turbo"), nodes=train_nodes, verbose=False
40)
41val_dataset = generate_qa_embedding_pairs(
42 llm=OpenAI(model="gpt-3.5-turbo"), nodes=val_nodes, verbose=False
43)
44```
45
46Now we'll use `SentenceTransformersFinetuneEngine` engine to fine-tune the model. You can also use `sentence-transformers` or `transformers` library to fine-tune the model.
47
48```python
49from llama_index.finetuning import SentenceTransformersFinetuneEngine
50
51finetune_engine = SentenceTransformersFinetuneEngine(
52 train_dataset,
53 model_id="BAAI/bge-small-en-v1.5",
54 model_output_path="tuned_model",
55 val_dataset=val_dataset,
56)
57finetune_engine.finetune()
58embed_model = finetune_engine.get_finetuned_model()
59```
60This saves the fine tuned embedding model in `tuned_model` folder. This al
61
62# Evaluation results
63In order to eval the retriever, you can either use this model to ingest the data into LanceDB directly or llama-index's LanceDB integration to create a `VectorStoreIndex` and use it as a retriever.
64On performing the same hit-rate evaluation as before, we see a significant improvement in the hit-rate across all query types.
65
66### Baseline
67| Query Type | Hit-rate@5 |
68| --- | --- |
69| Vector Search | 0.640 |
70| Full-text Search | 0.595 |
71| Reranked Vector Search | 0.677 |
72| Reranked Full-text Search | 0.672 |
73| Hybrid Search (w/ CohereReranker) | 0.759|
74
75### Fine-tuned model ( 2 iterations )
76| Query Type | Hit-rate@5 |
77| --- | --- |
78| Vector Search | 0.672 |
79| Full-text Search | 0.595 |
80| Reranked Vector Search | 0.754 |
81| Reranked Full-text Search | 0.672|
82| Hybrid Search (w/ CohereReranker) | 0.768 |