mirror of
https://github.com/VinciGit00/Scrapegraph-ai.git
synced 2026-07-12 21:01:56 +08:00
feat: add reasoning integration
This commit is contained in:
parent
857f28dba0
commit
b2822f620a
46
examples/extras/reasoning.py
Normal file
46
examples/extras/reasoning.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
"""
|
||||||
|
Basic example of scraping pipeline using SmartScraper
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from scrapegraphai.graphs import SmartScraperGraph
|
||||||
|
from scrapegraphai.utils import prettify_exec_info
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
# ************************************************
|
||||||
|
# Define the configuration for the graph
|
||||||
|
# ************************************************
|
||||||
|
|
||||||
|
|
||||||
|
graph_config = {
|
||||||
|
"llm": {
|
||||||
|
"api_key": os.getenv("OPENAI_API_KEY"),
|
||||||
|
"model": "openai/gpt-4o",
|
||||||
|
},
|
||||||
|
"reasoning": True,
|
||||||
|
"verbose": True,
|
||||||
|
"headless": False,
|
||||||
|
}
|
||||||
|
|
||||||
|
# ************************************************
|
||||||
|
# Create the SmartScraperGraph instance and run it
|
||||||
|
# ************************************************
|
||||||
|
|
||||||
|
smart_scraper_graph = SmartScraperGraph(
|
||||||
|
prompt="List me what does the company do, the name and a contact email.",
|
||||||
|
source="https://scrapegraphai.com/",
|
||||||
|
config=graph_config
|
||||||
|
)
|
||||||
|
|
||||||
|
result = smart_scraper_graph.run()
|
||||||
|
print(json.dumps(result, indent=4))
|
||||||
|
|
||||||
|
# ************************************************
|
||||||
|
# Get graph execution info
|
||||||
|
# ************************************************
|
||||||
|
|
||||||
|
graph_exec_info = smart_scraper_graph.get_execution_info()
|
||||||
|
print(prettify_exec_info(graph_exec_info))
|
||||||
@ -9,6 +9,7 @@ from .abstract_graph import AbstractGraph
|
|||||||
from ..nodes import (
|
from ..nodes import (
|
||||||
FetchNode,
|
FetchNode,
|
||||||
ParseNode,
|
ParseNode,
|
||||||
|
ReasoningNode,
|
||||||
GenerateAnswerNode
|
GenerateAnswerNode
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -88,6 +89,33 @@ class SmartScraperGraph(AbstractGraph):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if self.config.get("reasoning"):
|
||||||
|
reasoning_node = ReasoningNode(
|
||||||
|
input="user_prompt & (relevant_chunks | parsed_doc | doc)",
|
||||||
|
output=["answer"],
|
||||||
|
node_config={
|
||||||
|
"llm_model": self.llm_model,
|
||||||
|
"additional_info": self.config.get("additional_info"),
|
||||||
|
"schema": self.schema,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return BaseGraph(
|
||||||
|
nodes=[
|
||||||
|
fetch_node,
|
||||||
|
parse_node,
|
||||||
|
reasoning_node,
|
||||||
|
generate_answer_node,
|
||||||
|
],
|
||||||
|
edges=[
|
||||||
|
(fetch_node, parse_node),
|
||||||
|
(parse_node, reasoning_node),
|
||||||
|
(reasoning_node, generate_answer_node)
|
||||||
|
],
|
||||||
|
entry_point=fetch_node,
|
||||||
|
graph_name=self.__class__.__name__
|
||||||
|
)
|
||||||
|
|
||||||
return BaseGraph(
|
return BaseGraph(
|
||||||
nodes=[
|
nodes=[
|
||||||
fetch_node,
|
fetch_node,
|
||||||
|
|||||||
@ -55,7 +55,8 @@ class ReasoningNode(BaseNode):
|
|||||||
|
|
||||||
def execute(self, state: dict) -> dict:
|
def execute(self, state: dict) -> dict:
|
||||||
"""
|
"""
|
||||||
Generate a refined prompt for the reasoning task based on the user's input and the JSON schema.
|
Generate a refined prompt for the reasoning task based
|
||||||
|
on the user's input and the JSON schema.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
state (dict): The current state of the graph. The input keys will be used
|
state (dict): The current state of the graph. The input keys will be used
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user