-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_data.py
More file actions
59 lines (46 loc) · 1.35 KB
/
Copy pathsync_data.py
File metadata and controls
59 lines (46 loc) · 1.35 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from qdrant_client import QdrantClient
from qdrant_client.http import models
import streamlit as st
from qdrant_client.http.models import PointStruct
import openai
openai.api_key = st.secrets["OPENAI_API_KEY"]
collection_name = st.secrets["QDRANT_COLLECTION_NAME"]
qdrant_client = QdrantClient(
url=st.secrets["QDRANT_HOST"],
api_key=st.secrets["QDRANT_API_KEY"],
)
qdrant_client.recreate_collection(
collection_name=collection_name,
vectors_config=models.VectorParams(
size=1536, distance=models.Distance.COSINE),
)
with open("data/test.json") as f:
text = f.read()
def get_chunks(text):
chunks = []
while len(text) > 500:
last_period_index = text[:500].rfind('.')
if last_period_index == -1:
last_period_index = 500
chunks.append(text[:last_period_index])
text = text[last_period_index+1:]
chunks.append(text)
return chunks
chunks = get_chunks(text)
points = []
i = 1
for chunk in chunks:
i += 1
response = openai.Embedding.create(
input=chunk,
model="text-embedding-ada-002"
)
embeddings = response['data'][0]['embedding']
points.append(PointStruct(
id=i, vector=embeddings, payload={"text": chunk}))
operation_info = qdrant_client.upsert(
collection_name=collection_name,
wait=True,
points=points
)
print(operation_info)