Skip to content

Python scraper based on AI

added example multichunks rag

Perinim3236f6315a2e
diff --git a/examples/example_multichunks.py b/examples/example_multichunks.py
new file mode 100644
index 00000000..3790aa96
--- /dev/null
+++ b/examples/example_multichunks.py
@@ -0,0 +1,29 @@
+from langchain_community.document_transformers import Html2TextTransformer
+from langchain_community.document_loaders import AsyncHtmlLoader
+from langchain.text_splitter import RecursiveCharacterTextSplitter
+from langchain_community.embeddings import HuggingFaceEmbeddings
+from langchain_community.vectorstores import Chroma
+
+
+chroma = Chroma('test',HuggingFaceEmbeddings())
+
+# html2text 2020.1.16
+urls = ["https://www.mymovies.it/cinema/roma",'https://lurenss.github.io']
+loader = AsyncHtmlLoader(urls)
+docs = loader.load()
+text_splitter = RecursiveCharacterTextSplitter(
+ chunk_size=4000,
+ chunk_overlap=100,
+ length_function=len,
+ is_separator_regex=False,
+)
+
+docs_transformed = Html2TextTransformer().transform_documents(docs)
+
+list_texts = [] # is a list of lists, [i][j] where i is the website and j is the chunk
+for doc in docs_transformed:
+ doc.page_content = doc.page_content.replace('\n','')
+ chroma.add_documents(text_splitter.create_documents([doc.page_content]))
+ #list_texts.append(text_splitter.create_documents([doc.page_content]))
+
+chroma.similarity_search_with_relevance_scores('Find the cinema name', 10)
diff --git a/pyproject.toml b/pyproject.toml
index ede3db01..176d322e 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -26,6 +26,8 @@ langchain = "0.1.6"
langchain_community = "0.0.19"
langchain_core = "0.1.22"
langchain_openai = "0.0.5"
+chromadb = "0.4.22"
+html2text = "2020.1.16"
beautifulsoup4 = "4.12.3"
pandas = "2.0.3"
python-dotenv = "1.0.1"
@@ -35,6 +37,7 @@ graphviz = "0.20.1"
[tool.poetry.dev-dependencies]
pytest = "8.0.0"
+sentence_transformers = "2.3.1"
[tool.poetry.group.docs]
optional = true
diff --git a/requirements-dev.txt b/requirements-dev.txt
index 12d0e42f..63e96505 100644
--- a/requirements-dev.txt
+++ b/requirements-dev.txt
@@ -1,3 +1,4 @@
sphinx==7.1.2
sphinx-rtd-theme==2.0.0
pytest==8.0.0
+sentence_transformers==2.3.1
diff --git a/requirements.txt b/requirements.txt
index 816a95e6..eefb170a 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -2,6 +2,8 @@ langchain==0.1.6
langchain_community==0.0.19
langchain_core==0.1.22
langchain_openai==0.0.5
+chromadb==0.4.22
+html2text==2020.1.16
beautifulsoup4==4.12.3
pandas==2.0.3
python-dotenv==1.0.1