-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocces-jsons.py
More file actions
42 lines (31 loc) · 1.07 KB
/
preprocces-jsons.py
File metadata and controls
42 lines (31 loc) · 1.07 KB
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
import requests
import os
import joblib
import json
import numpy as np
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
def create_embedding(text_list):
# https://github.com/ollama/ollama/blob/main/docs/api.md#generate-embeddings
r = requests.post("http://localhost:11434/api/embed", json={
"model": "bge-m3",
"input": text_list
})
embedding = r.json()["embeddings"]
return embedding
jsons = os.listdir("newjsons") # List all the jsons
my_dicts = []
chunk_id = 0
for json_file in jsons:
with open(f"newjsons/{json_file}") as f:
content = json.load(f)
print(f"Creating Embeddings for {json_file}")
embeddings = create_embedding([c['text'] for c in content['chunks']])
for i, chunk in enumerate(content['chunks']):
chunk['chunk_id'] = chunk_id
chunk['embedding'] = embeddings[i]
chunk_id += 1
my_dicts.append(chunk)
# print(my_dicts)
df = pd.DataFrame.from_records(my_dicts)
joblib.dump(df,'embeddings.joblib')