The ReasoningNode (also known as PromptRefinerNode) refines user prompts using the output schema and additional context. It creates precise prompts that explicitly link elements in the user’s original input to their corresponding representations in the JSON schema, improving extraction accuracy.
Generates a refined prompt for the reasoning task based on the user’s input and the JSON schema.
def execute(self, state: dict) -> dict: """ Generate a refined prompt for the reasoning task based on the user's input and the JSON schema. Args: state (dict): The current state of the graph. Returns: dict: The updated state with the output key containing the refined prompt. """
Source: scrapegraphai/nodes/reasoning_node.py:56Returns: Updated state dictionary with refined prompt
from scrapegraphai.nodes import ReasoningNodefrom langchain_openai import ChatOpenAIfrom pydantic import BaseModel, Fieldfrom typing import List# Define output schemaclass ProductInfo(BaseModel): name: str = Field(description="Product name") price: float = Field(description="Price in USD") features: List[str] = Field(description="Key features") rating: float = Field(description="Customer rating out of 5")# Create reasoning nodereasoning_node = ReasoningNode( input="user_prompt", output=["refined_prompt"], node_config={ "llm_model": ChatOpenAI(model="gpt-4"), "schema": ProductInfo, "verbose": True })# Execute nodestate = { "user_prompt": "Get me the product details"}updated_state = reasoning_node.execute(state)print(updated_state["refined_prompt"])# Output: "Extract the following product information: # - The product name (name field)# - The price in USD (price field as a float)# - A list of key product features (features field)# - The customer rating out of 5 stars (rating field as a float)"
class CompanyData(BaseModel): company_name: str = Field(description="Official company name") headquarters: str = Field(description="Location of headquarters") founded_year: int = Field(description="Year company was founded") employees: int = Field(description="Number of employees") revenue: float = Field(description="Annual revenue in millions USD") industries: List[str] = Field(description="Industries/sectors")reasoning_node = ReasoningNode( input="user_prompt", output=["refined_prompt"], node_config={ "llm_model": ChatOpenAI(model="gpt-4"), "schema": CompanyData, "verbose": True })state = { "user_prompt": "Get company info"}updated_state = reasoning_node.execute(state)# Refined prompt explicitly maps each field with type information
class Recipe(BaseModel): title: str = Field(description="Recipe title") description: str = Field(description="Recipe description") prep_time: int = Field(description="Preparation time in minutes") cook_time: int = Field(description="Cooking time in minutes") servings: int = Field(description="Number of servings") ingredients: List[str] = Field(description="List of ingredients with quantities") instructions: List[str] = Field(description="Step-by-step cooking instructions") difficulty: str = Field(description="Difficulty level: easy, medium, or hard") calories: int = Field(description="Calories per serving")reasoning_node = ReasoningNode( input="user_prompt", output=["refined_prompt"], node_config={ "llm_model": ChatOpenAI(model="gpt-4"), "schema": Recipe, "additional_info": """This is a recipe website. Times should be converted to minutes. Instructions should be numbered steps.""", "verbose": False })state = { "user_prompt": "Get the recipe details"}updated_state = reasoning_node.execute(state)
"""Given the user's input: {user_input}And the following JSON schema: {json_schema}Generate a refined prompt that explicitly maps the user's request to the schema fields.Provide clear instructions for extracting each field."""
"""Given the user's input: {user_input}And the following JSON schema: {json_schema}Additional context: {additional_context}Generate a refined prompt that explicitly maps the user's request to the schema fields.Consider the additional context when creating extraction instructions."""
"Extract the following product information from the page:- Product name (map to 'name' field as string)- Current price in USD (map to 'price' field as float)- List of key product features (map to 'features' field as list of strings)- Customer rating out of 5 (map to 'rating' field as float)Ensure all fields are extracted even if some values are not explicitly labeled."
"Locate and extract contact information:- Email address in standard email format (email field)- Phone number with country code if available (phone field)- Complete physical address including street, city, and postal code (address field)Look for these in footer, contact page sections, or header areas."