Skip to content

Python scraper based on AI

created rag example

Perinimb0c75dc7ed7a
diff --git a/examples/custom_graph_example.py b/examples/custom_graph_example.py
index 3a55a5cc..080fe85f 100644
--- a/examples/custom_graph_example.py
+++ b/examples/custom_graph_example.py
@@ -6,7 +6,7 @@ import os
from dotenv import load_dotenv
from scrapegraphai.models import OpenAI
from scrapegraphai.graphs import BaseGraph
-from scrapegraphai.nodes import FetchHTMLNode, ParseHTMLNode, GenerateAnswerNode, TestRagNode
+from scrapegraphai.nodes import FetchHTMLNode, ParseHTMLNode, GenerateAnswerNode
load_dotenv()
@@ -22,7 +22,7 @@ model = OpenAI(llm_config)
# define the nodes for the graph
fetch_html_node = FetchHTMLNode("fetch_html")
-parse_document_node = TestRagNode(model, "parse_document")
+parse_document_node = ParseHTMLNode("parse_document")
generate_answer_node = GenerateAnswerNode(model, "generate_answer")
# create the graph
@@ -40,7 +40,7 @@ graph = BaseGraph(
)
# execute the graph
-inputs = {"user_input": "List me all the cinemas in Rome", "url": "https://www.mymovies.it/cinema/roma/"}
+inputs = {"user_input": "Give me the news", "url": "https://www.ansa.it/sito/notizie/topnews/index.shtml"}
result = graph.execute(inputs)
# get the answer from the result
diff --git a/examples/custom_graph_rag_example.py b/examples/custom_graph_rag_example.py
new file mode 100644
index 00000000..a8015d11
--- /dev/null
+++ b/examples/custom_graph_rag_example.py
@@ -0,0 +1,48 @@
+"""
+Example of custom graph using existing nodes
+"""
+
+import os
+from dotenv import load_dotenv
+from scrapegraphai.models import OpenAI
+from scrapegraphai.graphs import BaseGraph
+from scrapegraphai.nodes import FetchHTMLNode, RAGNode, GenerateAnswerNode
+
+load_dotenv()
+
+# Define the configuration for the language model
+openai_key = os.getenv("OPENAI_APIKEY")
+llm_config = {
+ "api_key": openai_key,
+ "model_name": "gpt-3.5-turbo",
+ "temperature": 0,
+ "streaming": True
+}
+model = OpenAI(llm_config)
+
+# define the nodes for the graph
+fetch_html_node = FetchHTMLNode("fetch_html")
+rag_node = RAGNode(model, "rag")
+generate_answer_node = GenerateAnswerNode(model, "generate_answer")
+
+# create the graph
+graph = BaseGraph(
+ nodes={
+ fetch_html_node,
+ rag_node,
+ generate_answer_node
+ },
+ edges={
+ (fetch_html_node, rag_node),
+ (rag_node, generate_answer_node)
+ },
+ entry_point=fetch_html_node
+)
+
+# execute the graph
+inputs = {"user_input": "Give me all the projects with their descriptions", "url": "https://perinim.github.io/projects/"}
+result = graph.execute(inputs)
+
+# get the answer from the result
+answer = result.get("answer", "No answer found.")
+print(answer)
diff --git a/pyproject.toml b/pyproject.toml
index 176d322e..ba54181e 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -26,8 +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"
+faiss-cpu = "1.7.4"
beautifulsoup4 = "4.12.3"
pandas = "2.0.3"
python-dotenv = "1.0.1"
diff --git a/requirements-dev.txt b/requirements-dev.txt
index 63e96505..12d0e42f 100644
--- a/requirements-dev.txt
+++ b/requirements-dev.txt
@@ -1,4 +1,3 @@
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 eefb170a..e11c029a 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -2,7 +2,7 @@ langchain==0.1.6
langchain_community==0.0.19
langchain_core==0.1.22
langchain_openai==0.0.5
-chromadb==0.4.22
+faiss-cpu==1.7.4
html2text==2020.1.16
beautifulsoup4==4.12.3
pandas==2.0.3
diff --git a/scrapegraphai/helpers/nodes_metadata.py b/scrapegraphai/helpers/nodes_metadata.py
index e5e6f243..cec408b7 100644
--- a/scrapegraphai/helpers/nodes_metadata.py
+++ b/scrapegraphai/helpers/nodes_metadata.py
@@ -29,6 +29,18 @@ nodes_metadata = {
},
"returns": "Updated state with extracted data under 'parsed_document' key."
},
+ "RAGNode": {
+ "description": """A node responsible for reducing the amount of text to be processed
+ by identifying and retrieving the most relevant chunks of text based on the user's query.
+ Utilizes RecursiveCharacterTextSplitter for chunking, Html2TextTransformer for HTML to text
+ conversion, and a combination of FAISS and OpenAIEmbeddings for efficient information retrieval.""",
+ "type": "node",
+ "args": {
+ "user_input": "The user's query or question guiding the retrieval.",
+ "document": "The HTML content to be processed and compressed."
+ },
+ "returns": "Updated state with 'relevant_chunks' key containing the most relevant text chunks."
+ },
"GenerateAnswerNode": {
"description": "Generates an answer based on the user's input and parsed document.",
"type": "node",
diff --git a/scrapegraphai/nodes/__init__.py b/scrapegraphai/nodes/__init__.py
index 2898fa16..c593c180 100644
--- a/scrapegraphai/nodes/__init__.py
+++ b/scrapegraphai/nodes/__init__.py
@@ -6,6 +6,6 @@ from .conditional_node import ConditionalNode
from .get_probable_tags_node import GetProbableTagsNode
from .generate_answer_node import GenerateAnswerNode
from .parse_html_node import ParseHTMLNode
-from .test_rag_node import TestRagNode
+from .rag_node import RAGNode
from .text_to_speech_node import TextToSpeechNode
from .image_to_text_node import ImageToTextNode
diff --git a/scrapegraphai/nodes/test_rag_node.py b/scrapegraphai/nodes/rag_node.py
similarity index 99%
rename from scrapegraphai/nodes/test_rag_node.py
rename to scrapegraphai/nodes/rag_node.py
index 90cc0796..db23c7d7 100644
--- a/scrapegraphai/nodes/test_rag_node.py
+++ b/scrapegraphai/nodes/rag_node.py
@@ -15,7 +15,7 @@ from langchain_community.document_transformers import EmbeddingsRedundantFilter
from .base_node import BaseNode
-class TestRagNode(BaseNode):
+class RAGNode(BaseNode):
"""
A node responsible for parsing HTML content from a document using specified tags.
It uses BeautifulSoupTransformer for parsing, providing flexibility in extracting