Skip to content

Python scraper based on AI

add multiple merging generate answer node

VinciGit00ff7f8105bbb1
diff --git a/examples/graph_examples/smart_scraper_example.py b/examples/graph_examples/smart_scraper_example.py
index adf949c9..70ff06e9 100644
--- a/examples/graph_examples/smart_scraper_example.py
+++ b/examples/graph_examples/smart_scraper_example.py
@@ -16,8 +16,8 @@ llm_config = {
}
# Define URL and PROMPT
-URL = "https://perinim.github.io/projects/"
-PROMPT = "List me all the titles and project descriptions"
+URL = "https://www.google.com/search?client=safari&rls=en&q=ristoranti+trento&ie=UTF-8&oe=UTF-8"
+PROMPT = "List me all the https inside the page"
# Create the SmartScraperGraph instance
smart_scraper_graph = SmartScraperGraph(PROMPT, URL, llm_config)
diff --git a/scrapegraphai/nodes/generate_answer_node.py b/scrapegraphai/nodes/generate_answer_node.py
index 2a3a1730..0c379cd4 100644
--- a/scrapegraphai/nodes/generate_answer_node.py
+++ b/scrapegraphai/nodes/generate_answer_node.py
@@ -11,6 +11,7 @@ from langchain_core.runnables import RunnableParallel
# Imports from the library
from .base_node import BaseNode
+from langchain.text_splitter import RecursiveCharacterTextSplitter
class GenerateAnswerNode(BaseNode):
@@ -114,23 +115,28 @@ class GenerateAnswerNode(BaseNode):
"chunk_id": i + 1, "format_instructions": format_instructions},
)
# Dynamically name the chains based on their index
- chain_name = f"chunk{i+1}"
- chains_dict[chain_name] = prompt | self.llm | output_parser
+ chains_dict[f"chunk{i+1}"] = prompt | self.llm | output_parser
- # Use dictionary unpacking to pass the dynamically named chains to RunnableParallel
- map_chain = RunnableParallel(**chains_dict)
- # Chain
- answer_map = map_chain.invoke({"question": user_input})
+ text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
+ chunk_size=4000,
+ chunk_overlap=0,
+ )
+
+ chunks = text_splitter.split_text(str(chains_dict))
- # Merge the answers from the chunks
merge_prompt = PromptTemplate(
template=template_merge,
input_variables=["context", "question"],
partial_variables={"format_instructions": format_instructions},
)
merge_chain = merge_prompt | self.llm | output_parser
- answer = merge_chain.invoke(
- {"context": answer_map, "question": user_input})
+
+ answer = []
+ for chunk in chunks:
+ answer_temp = merge_chain.invoke(
+ {"context": chunk, "question": user_input})
+
+ answer.append(answer_temp)
# Update the state with the generated answer
state.update({"answer": answer})