-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.py
More file actions
53 lines (43 loc) · 1.42 KB
/
data_loader.py
File metadata and controls
53 lines (43 loc) · 1.42 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
import boto3
import json
from llama_index.readers.file import PDFReader
from llama_index.core.node_parser import SentenceSplitter
from dotenv import load_dotenv
load_dotenv()
bedrock_client = boto3.client(
service_name="bedrock-runtime",
region_name="us-east-1"
)
EMBED_MODEL = "amazon.titan-embed-text-v2:0"
EMBED_DIM = 1024
splitter = SentenceSplitter(chunk_size=1000, chunk_overlap=200)
def load_and_chunk_pdf(path: str):
docs = PDFReader().load_data(file=path)
texts = [d.text for d in docs if getattr(d, "text", None)]
chunks = []
for t in texts:
chunks.extend(splitter.split_text(t))
return chunks
def embed_texts(texts: list[str]) -> list[list[float]]:
"""
Embeds a list of texts using the Amazon Bedrock API.
Loops through each text and calls the API individually.
"""
all_embeddings = []
for text in texts:
# The body must be a JSON-formatted string
body = json.dumps({
"inputText": text
})
# Call the Bedrock API
response = bedrock_client.invoke_model(
body=body,
modelId=EMBED_MODEL,
accept="application/json",
contentType="application/json"
)
# Parse the response
response_body = json.loads(response.get("body").read())
embedding = response_body.get("embedding")
all_embeddings.append(embedding)
return all_embeddings