forked from emarco177/langgaph-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathingestion.py
34 lines (28 loc) · 1.05 KB
/
ingestion.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
from dotenv import load_dotenv
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_chroma import Chroma
from langchain_community.document_loaders import WebBaseLoader
from langchain_openai import OpenAIEmbeddings
load_dotenv()
urls = [
"https://lilianweng.github.io/posts/2023-06-23-agent/",
"https://lilianweng.github.io/posts/2023-03-15-prompt-engineering/",
"https://lilianweng.github.io/posts/2023-10-25-adv-attack-llm/",
]
docs = [WebBaseLoader(url).load() for url in urls]
docs_list = [item for sublist in docs for item in sublist]
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
chunk_size=250, chunk_overlap=0
)
doc_splits = text_splitter.split_documents(docs_list)
# vectorstore = Chroma.from_documents(
# documents=doc_splits,
# collection_name="rag-chroma",
# embedding=OpenAIEmbeddings(),
# persist_directory="./.chroma",
# )
retriever = Chroma(
collection_name="rag-chroma",
persist_directory="./.chroma",
embedding_function=OpenAIEmbeddings(),
).as_retriever()