-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
47 lines (38 loc) · 1.59 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from typing import Any, Dict, List, Optional
from modules.pocket import Pocket
from embedding.base import HuggingFaceEmbedder
from datasets import load_dataset
def fill_database_pocket(db, collection_name: str = "pocket") -> None:
"""
Fill the database with embedded documents from Pocket.
Args:
db (MongoDBVectorStore): MongoDB connection
"""
print("Load documents")
with open('data/ril_export.html', 'r', encoding='utf-8') as f:
html_content = f.read()
source: DataSource = Pocket(collection_name)
documents: List[Dict] = source.extract_documents(html_content, stop_after=3, follow_links=True)
print("Create embeddings")
hf = HuggingFaceEmbedder(
access_token=HERMES_CONFIG["hf_access_token"],
inference_url=HERMES_CONFIG["hf_inference_endpoint"])
for doc in documents:
# Choose which information should be embedded.
# Load whole HTMl page of link.
doc["vectorEmbedding"] = hf.generate(doc["content"])
print("Add documents...")
db.delete_many(collection_name, {})
db.add_many(collection_name, documents)
def fill_database_embedded_movies(db, collection_name: str = "movies") -> None:
"""
Fill the database with embedded movies from HuggingFace dataset
christophsonntag/gte_embedded_movies
Args:
db (MongoDBVectorStore): MongoDB connection
"""
dataset = load_dataset("christophsonntag/gte_embedded_movies")
documents: list[Dict] = dataset["train"]
print("Add documents...")
db.delete_many(collection_name, {})
db.add_many(collection_name, documents)