Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- __init__.py +2 -0
- start_vector_db.py +169 -0
__init__.py
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from start_vector_db import setup_chroma_db
|
| 2 |
+
from tools import setup_tools, setup_llm
|
start_vector_db.py
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import chromadb
|
| 4 |
+
|
| 5 |
+
from datasets import load_dataset
|
| 6 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 7 |
+
from llama_index.core import VectorStoreIndex
|
| 8 |
+
from llama_index.vector_stores.chroma import ChromaVectorStore
|
| 9 |
+
from tqdm import tqdm
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
dataset_id = "gaia-benchmark/GAIA"
|
| 14 |
+
gaia_dataset = load_dataset('gaia-benchmark/GAIA', '2023_all')
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
print("Dataset loaded successfully!")
|
| 19 |
+
print(gaia_dataset)
|
| 20 |
+
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"Error loading the dataset: {e}")
|
| 23 |
+
print("Make sure to:")
|
| 24 |
+
print("1. Have logged in with 'huggingface-cli login'.")
|
| 25 |
+
print("2. Have been granted access to the dataset on Hugging Face.")
|
| 26 |
+
print(f"3. That the dataset ID '{dataset_id}' is correct.")
|
| 27 |
+
print("4. That you've added 'trust_remote_code=True' to the load_dataset call.")
|
| 28 |
+
|
| 29 |
+
def setup_chroma_db():
|
| 30 |
+
"""Configure ChromaDB as the vector database."""
|
| 31 |
+
# Set up ChromaDB client
|
| 32 |
+
db_path = os.path.join(os.getcwd(), "chroma_db")
|
| 33 |
+
# Ensure directory exists
|
| 34 |
+
os.makedirs(db_path, exist_ok=True)
|
| 35 |
+
|
| 36 |
+
# First step: Initialize ChromaDB client
|
| 37 |
+
db = chromadb.PersistentClient(path=db_path)
|
| 38 |
+
|
| 39 |
+
# Second step: Set up embedding model
|
| 40 |
+
embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-mpnet-base-v2")
|
| 41 |
+
|
| 42 |
+
# Third step: Create or get the collection
|
| 43 |
+
try:
|
| 44 |
+
chroma_collection = db.get_collection("gaia_examples")
|
| 45 |
+
print("Existing collection found...")
|
| 46 |
+
except:
|
| 47 |
+
chroma_collection = db.create_collection(
|
| 48 |
+
"gaia_examples",
|
| 49 |
+
metadata={"description": "GAIA benchmark examples for agent training"}
|
| 50 |
+
)
|
| 51 |
+
print("New collection created...")
|
| 52 |
+
|
| 53 |
+
# Fourth step: Set up the vector store
|
| 54 |
+
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
|
| 55 |
+
|
| 56 |
+
# Fifth step: Create the index
|
| 57 |
+
index = VectorStoreIndex.from_vector_store(
|
| 58 |
+
vector_store=vector_store,
|
| 59 |
+
embed_model=embed_model
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
return db, chroma_collection, vector_store, index
|
| 63 |
+
|
| 64 |
+
def load_gaia_to_chroma():
|
| 65 |
+
"""Load the GAIA dataset into ChromaDB."""
|
| 66 |
+
# Configure ChromaDB
|
| 67 |
+
_, collection, _, index = setup_chroma_db()
|
| 68 |
+
|
| 69 |
+
# Load dataset
|
| 70 |
+
print("Loading GAIA dataset...")
|
| 71 |
+
gaia_dataset = load_dataset('gaia-benchmark/GAIA', '2023_all')
|
| 72 |
+
|
| 73 |
+
# Total number of examples to process
|
| 74 |
+
total_examples = len(gaia_dataset['validation']) + len(gaia_dataset['test'])
|
| 75 |
+
print(f"Total examples to process: {total_examples}")
|
| 76 |
+
|
| 77 |
+
# Global counter for unique ID
|
| 78 |
+
example_counter = 0
|
| 79 |
+
|
| 80 |
+
# Process validation and test sets
|
| 81 |
+
for split in ['validation', 'test']:
|
| 82 |
+
print(f"\nProcessing {split} set...")
|
| 83 |
+
|
| 84 |
+
# Lists for batch loading
|
| 85 |
+
ids = []
|
| 86 |
+
texts = []
|
| 87 |
+
metadatas = []
|
| 88 |
+
|
| 89 |
+
# Use tqdm to show progress
|
| 90 |
+
for idx, example in enumerate(tqdm(gaia_dataset[split], desc=f"Processing {split}")):
|
| 91 |
+
# Unique ID
|
| 92 |
+
doc_id = f"{split}_{example['task_id']}"
|
| 93 |
+
|
| 94 |
+
# Text for embedding
|
| 95 |
+
text_content = f"""
|
| 96 |
+
Question: {example['Question']}
|
| 97 |
+
Level: {example['Level']}
|
| 98 |
+
Steps to solve:
|
| 99 |
+
{example['Annotator Metadata']['Steps']}
|
| 100 |
+
|
| 101 |
+
Tools used:
|
| 102 |
+
{example['Annotator Metadata']['Tools']}
|
| 103 |
+
|
| 104 |
+
Final Answer: {example['Final answer']}
|
| 105 |
+
"""
|
| 106 |
+
|
| 107 |
+
# Metadata
|
| 108 |
+
metadata = {
|
| 109 |
+
"task_id": example['task_id'],
|
| 110 |
+
"level": example['Level'],
|
| 111 |
+
"type": "gaia_example",
|
| 112 |
+
"split": split
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
# Add to lists
|
| 116 |
+
ids.append(doc_id)
|
| 117 |
+
texts.append(text_content)
|
| 118 |
+
metadatas.append(metadata)
|
| 119 |
+
|
| 120 |
+
# Load in batches every 50 examples or in the last batch
|
| 121 |
+
if len(ids) >= 50 or idx == len(gaia_dataset[split]) - 1:
|
| 122 |
+
# Add documents in batches - ChromaDB will calculate embeddings automatically
|
| 123 |
+
collection.add(
|
| 124 |
+
ids=ids,
|
| 125 |
+
documents=texts,
|
| 126 |
+
metadatas=metadatas
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
print(f"Batch of {len(ids)} examples loaded...")
|
| 130 |
+
example_counter += len(ids)
|
| 131 |
+
|
| 132 |
+
# Reset lists
|
| 133 |
+
ids = []
|
| 134 |
+
texts = []
|
| 135 |
+
metadatas = []
|
| 136 |
+
|
| 137 |
+
print(f"\nLoad complete. {example_counter} examples stored in ChromaDB.")
|
| 138 |
+
print(f"Data is saved at: {os.path.join(os.getcwd(), 'chroma_db')}")
|
| 139 |
+
|
| 140 |
+
def test_chroma_search():
|
| 141 |
+
"""Test the search in ChromaDB."""
|
| 142 |
+
_, collection, _, index = setup_chroma_db()
|
| 143 |
+
|
| 144 |
+
# Example query
|
| 145 |
+
test_query = "What is the last word before the second chorus of a famous song?"
|
| 146 |
+
|
| 147 |
+
# Perform search
|
| 148 |
+
results = collection.query(
|
| 149 |
+
query_texts=[test_query],
|
| 150 |
+
n_results=2,
|
| 151 |
+
where={"type": "gaia_example"}
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
# Show results
|
| 155 |
+
print("\n=== Example search results ===")
|
| 156 |
+
for i in range(len(results["ids"][0])):
|
| 157 |
+
print(f"\nResult #{i+1}:")
|
| 158 |
+
print(f"ID: {results['ids'][0][i]}")
|
| 159 |
+
print(f"Metadata: {results['metadatas'][0][i]}")
|
| 160 |
+
print(f"Content: {results['documents'][0][i][:200]}...") # Show first 200 characters
|
| 161 |
+
|
| 162 |
+
print("\n=== End of results ===")
|
| 163 |
+
|
| 164 |
+
# Run the process
|
| 165 |
+
if __name__ == "__main__":
|
| 166 |
+
print("Starting GAIA data load to ChromaDB...")
|
| 167 |
+
load_gaia_to_chroma()
|
| 168 |
+
print("\nTesting search...")
|
| 169 |
+
test_chroma_search()
|