Skip to content

Commit 308c2d2

Browse files
committed
fixed text input example
1 parent 55702b2 commit 308c2d2

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

examples/graph_examples/graph_from_text_example.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from dotenv import load_dotenv
77
from scrapegraphai.models import OpenAI
88
from scrapegraphai.graphs import BaseGraph
9-
from scrapegraphai.nodes import FetchTextNode, ParseTextNode, GenerateAnswerNode
9+
from scrapegraphai.nodes import FetchTextNode, ParseNode, RAGNode, GenerateAnswerNode
1010

1111
load_dotenv()
1212

@@ -20,25 +20,31 @@
2020
}
2121
model = OpenAI(llm_config)
2222

23-
with open("text_example.txt", "r", encoding="utf-8") as file:
23+
curr_dir = os.path.dirname(__file__)
24+
file_path = os.path.join(curr_dir, "text_example.txt")
25+
26+
with open(file_path, "r", encoding="utf-8") as file:
2427
text = file.read()
2528

2629

2730
# define the nodes for the graph
28-
fetch_html_node = FetchTextNode("load_html")
29-
parse_document_node = ParseTextNode("parse_document")
31+
fetch_html_node = FetchTextNode("load_html_from_text")
32+
parse_document_node = ParseNode(doc_type="text", chunks_size=4000, node_name="parse_document")
33+
rag_node = RAGNode(model, "rag")
3034
generate_answer_node = GenerateAnswerNode(model, "generate_answer")
3135

3236
# create the graph
3337
graph = BaseGraph(
3438
nodes={
3539
fetch_html_node,
3640
parse_document_node,
41+
rag_node,
3742
generate_answer_node
3843
},
3944
edges={
4045
(fetch_html_node, parse_document_node),
41-
(parse_document_node, generate_answer_node)
46+
(parse_document_node, rag_node),
47+
(rag_node, generate_answer_node)
4248
},
4349
entry_point=fetch_html_node
4450
)

scrapegraphai/nodes/fetch_text_node.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Module for FetchTextNode
33
"""
44
from .base_node import BaseNode
5-
5+
from langchain_core.documents import Document
66

77
class FetchTextNode(BaseNode):
88
"""
@@ -53,5 +53,5 @@ def execute(self, state: dict) -> dict:
5353
if 'text' not in state:
5454
raise KeyError("The 'url' key is required to load the text.")
5555

56-
state["document"] = state["text"]
56+
state["document"] = Document(page_content=state["text"])
5757
return state

0 commit comments

Comments
 (0)