-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunking.py
More file actions
32 lines (25 loc) · 920 Bytes
/
Copy pathchunking.py
File metadata and controls
32 lines (25 loc) · 920 Bytes
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
import os
from langchain_experimental.text_splitter import SemanticChunker
from langchain_openai import OpenAIEmbeddings
from Load_file import documents
from dotenv import load_dotenv
load_dotenv()
GITHUB_TOKEN = os.getenv("GITHUB_RAG_TOKEN")
INFERENCE_URL="https://models.github.ai/inference"
embeddings_model = OpenAIEmbeddings(
base_url=INFERENCE_URL,
model="openai/text-embedding-3-small",
api_key=GITHUB_TOKEN
)
semantic_splitter = SemanticChunker(
embeddings_model,
breakpoint_threshold_amount=95,
breakpoint_threshold_type="percentile"
)
semantic_chunks = semantic_splitter.split_documents(documents)
if __name__=="__main__":
print(f" Semantic Chunks: {len(semantic_chunks)} created\n")
for idx, chunk in enumerate(semantic_chunks):
print(f"----- CHUNK {idx+1} ------")
print(f"SOURCE : {chunk.metadata.get('source')}")
print(chunk.page_content)