Parte 3: Configuración del ejemplo RAG y validación del pipeline de recuperación
Parte 3: Configuración del ejemplo RAG y validación de nuestro pipeline de recuperación
Estamos listos para el final, pero recapitulemos:
- Empezamos con un conjunto de datos de ejemplo de 5000 imágenes.
- En el primer notebook, lo limpiamos para etiquetado y usamos el modelo
Llama-3.2-11Bpara etiquetar. - En el segundo notebook, limpiamos algunas alucinaciones del modelo y preprocesamos las descripciones que se generaron sintéticamente.
El paso 3 es configurar un pipeline RAG y obtener beneficios.
Usaremos lance-db ya que es de código abierto y Llama y el código abierto van bien juntos 🤝
También nos encantan los créditos de API gratuitos y Together aloja el modelo 11B de forma gratuita. Para nuestra demostración final, usaremos su API y validaremos lo mismo en este ejemplo.
#!pip install lancedb rerankers together -q
Cargando el conjunto de datos y creando embeddings
import pandas as pd
import os
from together import Together
os.environ["TOGETHER_API_KEY"] = ""
client = Together(api_key=os.environ.get('TOGETHER_API_KEY'))
df = pd.read_csv("./MM-Demo/final_balanced_sample_dataset.csv")
df.head()
Filename \
0 d7ed1d64-2c65-427f-9ae4-eb4aaa3e2389.jpg
1 5c1b7a77-1fa3-4af8-9722-cd38e45d89da.jpg
2 b2e084c7-e3a0-4182-8671-b908544a7cf2.jpg
3 87846aa9-86cc-404a-af2c-7e8fe941081d.jpg
4 04fa06fb-d71a-4293-9804-fe799375a682.jpg
Title Size Gender \
0 Stylish and Trendy Tank Top with Celestial Design M F
1 Classic White Sweatshirt M F
2 Grey T-shirt M Unisex
3 Long-Sleeved V-Neck Shirt L U
4 Silver Metallic Buckle Sandals L F
Description Category Type
0 This white tank top is a stylish and trendy pi... Tops Casual
1 This classic white sweatshirt is a timeless pi... Tops Casual
2 This is a short-sleeved, crew neck t-shirt tha... T-Shirt Casual
3 A long-sleeved, V-neck shirt with a solid purp... Tops Casual
4 These silver metallic buckle sandals feature a... Shoes Casual
| Filename | Title | Size | Gender | Description | Category | Type | |
|---|---|---|---|---|---|---|---|
| 0 | d7ed1d64-2c65-427f-9ae4-eb4aaa3e2389.jpg | Stylish and Trendy Tank Top with Celestial Design | M | F | This white tank top is a stylish and trendy pi... | Tops | Casual |
| 1 | 5c1b7a77-1fa3-4af8-9722-cd38e45d89da.jpg | Classic White Sweatshirt | M | F | This classic white sweatshirt is a timeless pi... | Tops | Casual |
| 2 | b2e084c7-e3a0-4182-8671-b908544a7cf2.jpg | Grey T-shirt | M | Unisex | This is a short-sleeved, crew neck t-shirt tha... | T-Shirt | Casual |
| 3 | 87846aa9-86cc-404a-af2c-7e8fe941081d.jpg | Long-Sleeved V-Neck Shirt | L | U | A long-sleeved, V-neck shirt with a solid purp... | Tops | Casual |
| 4 | 04fa06fb-d71a-4293-9804-fe799375a682.jpg | Silver Metallic Buckle Sandals | L | F | These silver metallic buckle sandals feature a... | Shoes | Casual |
df["Description"][0]
'This white tank top is a stylish and trendy piece of clothing that features a captivating celestial design. The design showcases a mesmerizing array of stars, planets, and moons in black and white, creating a visually appealing and eye-catching pattern. The tank top has a relaxed fit with a round neckline and sleeveless design, making it a comfortable and versatile piece that can be worn on its own or layered under a cardigan or jacket. Perfect for a casual day out or a night on the town, this tank top is a must-have for any fashion-conscious woman.'
Creando embeddings:
Definiremos un esquema y usaremos embeddings BAAI/bge-small-en-v1.5 para crear nuestros embeddings vectoriales. Este es un primer paso; podemos iterar con más embeddings en nuestra aplicación final más adelante.
Para la recuperación, incrustaremos las descripciones de toda la ropa y las usaremos.
import lancedb
from lancedb.pydantic import LanceModel, Vector
from lancedb.embeddings import get_registry
from lancedb.rerankers import ColbertReranker
from pathlib import Path
from PIL import Image
import pandas as pd
model = get_registry().get("sentence-transformers").create(name="BAAI/bge-small-en-v1.5", device="cuda")
class Schema(LanceModel):
Filename: str
Title: str
Size: str
Gender: str
Description: str = model.SourceField()
Category: str
Type: str
vector: Vector(model.ndims()) = model.VectorField()
db = lancedb.connect("~/.lancedb")
tbl = db.create_table(name="clothes", schema=Schema, mode="overwrite")
# Function to clean and convert data types
def clean_data(df):
# Convert all columns to string type
for col in df.columns:
df[col] = df[col].astype(str)
# Remove any rows with NaN values
df = df.dropna()
return df
# Clean the data
cleaned_df = clean_data(df)
# Convert cleaned DataFrame to list of dictionaries
data = cleaned_df.to_dict('records')
empty_count = len([d for d in data if d.get('Description', '').strip() == ''])
print(f"Number of empty descriptions: {empty_count}")
# automatically generate vectors
tbl.add(data)
tbl.search().to_pandas()
def show_image_from_response(rs):
names = rs["Filename"].to_list()
return [Image.open(Path("archive") / "images_compressed" / name) for name in names]
Number of empty descriptions: 0
# automatically generate vectors
tbl.add(data)
tbl.search().to_pandas()
Filename \
0 d7ed1d64-2c65-427f-9ae4-eb4aaa3e2389.jpg
1 5c1b7a77-1fa3-4af8-9722-cd38e45d89da.jpg
2 b2e084c7-e3a0-4182-8671-b908544a7cf2.jpg
3 87846aa9-86cc-404a-af2c-7e8fe941081d.jpg
4 04fa06fb-d71a-4293-9804-fe799375a682.jpg
5 8f576f1a-839d-4fb2-a224-a4700b2d05da.jpg
6 e976a8f6-6731-485f-8a9a-2872a5208818.jpg
7 bbf0d9c7-663d-46d1-a9f8-66e8e5678541.jpg
8 e25a7faa-7a49-4e72-a7ef-e74427f77784.jpg
9 d995ac1f-fbd0-482c-a308-dafb6a93cfd0.jpg
Title Size Gender \
0 Stylish and Trendy Tank Top with Celestial Design M F
1 Classic White Sweatshirt M F
2 Grey T-shirt M Unisex
3 Long-Sleeved V-Neck Shirt L U
4 Silver Metallic Buckle Sandals L F
5 Orange Long Sleeve T-Shirt S U
6 Green T-Shirt L M
7 White Quarter Zip Pullover Top L U
8 Aviator Sunglasses T-Shirt M F
9 Beige Top M F
Description Category Type \
0 This white tank top is a stylish and trendy pi... Tops Casual
1 This classic white sweatshirt is a timeless pi... Tops Casual
2 This is a short-sleeved, crew neck t-shirt tha... T-Shirt Casual
3 A long-sleeved, V-neck shirt with a solid purp... Tops Casual
4 These silver metallic buckle sandals feature a... Shoes Casual
5 This long sleeve t-shirt is made of a lightwei... Tops Casual
6 The green t-shirt is a relaxed fit with a V-ne... Tops Casual
7 This pullover top has a quarter zip closure at... Tops Casual
8 This sleeveless red T-shirt features a large b... Tops Casual
9 This beige top has a boat neck, a pocket in fr... Tops Casual
vector
0 [-0.06355423, 0.02385288, 0.03382446, -0.00212...
1 [-0.011691423, 0.049270794, 0.030319242, -0.02...
2 [-0.010150914, 0.09637642, -0.0012558334, 0.04...
3 [-0.058710814, 0.053951632, -0.047531255, 0.03...
4 [0.0123484805, 0.02398385, 0.059779372, -0.006...
5 [-0.016778396, 0.06664105, -0.010597771, 0.052...
6 [0.015946185, 0.07543863, 0.032447945, 0.00442...
7 [-0.05592024, 0.03401515, -0.0057456787, 0.030...
8 [-0.040058654, 0.090077035, -0.020889325, 0.03...
9 [-0.020475557, 0.032990508, 0.0145089375, -0.0...
| Filename | Title | Size | Gender | Description | Category | Type | vector | |
|---|---|---|---|---|---|---|---|---|
| 0 | d7ed1d64-2c65-427f-9ae4-eb4aaa3e2389.jpg | Stylish and Trendy Tank Top with Celestial Design | M | F | This white tank top is a stylish and trendy pi... | Tops | Casual | [-0.06355423, 0.02385288, 0.03382446, -0.00212... |
| 1 | 5c1b7a77-1fa3-4af8-9722-cd38e45d89da.jpg | Classic White Sweatshirt | M | F | This classic white sweatshirt is a timeless pi... | Tops | Casual | [-0.011691423, 0.049270794, 0.030319242, -0.02... |
| 2 | b2e084c7-e3a0-4182-8671-b908544a7cf2.jpg | Grey T-shirt | M | Unisex | This is a short-sleeved, crew neck t-shirt tha... | T-Shirt | Casual | [-0.010150914, 0.09637642, -0.0012558334, 0.04... |
| 3 | 87846aa9-86cc-404a-af2c-7e8fe941081d.jpg | Long-Sleeved V-Neck Shirt | L | U | A long-sleeved, V-neck shirt with a solid purp... | Tops | Casual | [-0.058710814, 0.053951632, -0.047531255, 0.03... |
| 4 | 04fa06fb-d71a-4293-9804-fe799375a682.jpg | Silver Metallic Buckle Sandals | L | F | These silver metallic buckle sandals feature a... | Shoes | Casual | [0.0123484805, 0.02398385, 0.059779372, -0.006... |
| 5 | 8f576f1a-839d-4fb2-a224-a4700b2d05da.jpg | Orange Long Sleeve T-Shirt | S | U | This long sleeve t-shirt is made of a lightwei... | Tops | Casual | [-0.016778396, 0.06664105, -0.010597771, 0.052... |
| 6 | e976a8f6-6731-485f-8a9a-2872a5208818.jpg | Green T-Shirt | L | M | The green t-shirt is a relaxed fit with a V-ne... | Tops | Casual | [0.015946185, 0.07543863, 0.032447945, 0.00442... |
| 7 | bbf0d9c7-663d-46d1-a9f8-66e8e5678541.jpg | White Quarter Zip Pullover Top | L | U | This pullover top has a quarter zip closure at... | Tops | Casual | [-0.05592024, 0.03401515, -0.0057456787, 0.030... |
| 8 | e25a7faa-7a49-4e72-a7ef-e74427f77784.jpg | Aviator Sunglasses T-Shirt | M | F | This sleeveless red T-shirt features a large b... | Tops | Casual | [-0.040058654, 0.090077035, -0.020889325, 0.03... |
| 9 | d995ac1f-fbd0-482c-a308-dafb6a93cfd0.jpg | Beige Top | M | F | This beige top has a boat neck, a pocket in fr... | Tops | Casual | [-0.020475557, 0.032990508, 0.0145089375, -0.0... |