mirror of
https://github.com/VinciGit00/Scrapegraph-ai.git
synced 2026-07-09 21:19:20 +08:00
99 lines
2.3 KiB
Python
99 lines
2.3 KiB
Python
"""
|
|
Example of custom graph using existing nodes
|
|
"""
|
|
import os
|
|
from dotenv import load_dotenv
|
|
from langchain_openai import ChatOpenAI
|
|
from scrapegraphai.graphs import BaseGraph
|
|
from scrapegraphai.nodes import FetchNode, ParseNode, GenerateAnswerNode, RobotsNode
|
|
load_dotenv()
|
|
|
|
# ************************************************
|
|
# Define the configuration for the graph
|
|
# ************************************************
|
|
|
|
fireworks_api_key = os.getenv("FIREWORKS_APIKEY")
|
|
|
|
graph_config = {
|
|
"llm": {
|
|
"api_key": fireworks_api_key,
|
|
"model": "fireworks/accounts/fireworks/models/mixtral-8x7b-instruct"
|
|
},
|
|
"verbose": True,
|
|
"headless": False,
|
|
}
|
|
|
|
# ************************************************
|
|
# Define the graph nodes
|
|
# ************************************************
|
|
|
|
llm_model = ChatOpenAI(graph_config["llm"])
|
|
|
|
# define the nodes for the graph
|
|
robot_node = RobotsNode(
|
|
input="url",
|
|
output=["is_scrapable"],
|
|
node_config={
|
|
"llm_model": llm_model,
|
|
"force_scraping": True,
|
|
"verbose": True,
|
|
}
|
|
)
|
|
|
|
fetch_node = FetchNode(
|
|
input="url | local_dir",
|
|
output=["doc"],
|
|
node_config={
|
|
"verbose": True,
|
|
"headless": True,
|
|
}
|
|
)
|
|
parse_node = ParseNode(
|
|
input="doc",
|
|
output=["parsed_doc"],
|
|
node_config={
|
|
"chunk_size": 4096,
|
|
"verbose": True,
|
|
}
|
|
)
|
|
|
|
generate_answer_node = GenerateAnswerNode(
|
|
input="user_prompt & (relevant_chunks | parsed_doc | doc)",
|
|
output=["answer"],
|
|
node_config={
|
|
"llm_model": llm_model,
|
|
"verbose": True,
|
|
}
|
|
)
|
|
|
|
# ************************************************
|
|
# Create the graph by defining the connections
|
|
# ************************************************
|
|
|
|
graph = BaseGraph(
|
|
nodes=[
|
|
robot_node,
|
|
fetch_node,
|
|
parse_node,
|
|
],
|
|
edges=[
|
|
(robot_node, fetch_node),
|
|
(fetch_node, parse_node),
|
|
(parse_node, generate_answer_node)
|
|
],
|
|
entry_point=robot_node
|
|
)
|
|
|
|
# ************************************************
|
|
# Execute the graph
|
|
# ************************************************
|
|
|
|
result, execution_info = graph.execute({
|
|
"user_prompt": "Describe the content",
|
|
"url": "https://example.com/"
|
|
})
|
|
|
|
# get the answer from the result
|
|
result = result.get("answer", "No answer found.")
|
|
print(result)
|