|
| 1 | +""" |
| 2 | +Example of custom graph using existing nodes |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +from dotenv import load_dotenv |
| 7 | +from scrapegraphai.models import OpenAI |
| 8 | +from scrapegraphai.graphs import BaseGraph |
| 9 | +from scrapegraphai.nodes import FetchNode, GenerateAnswerNode |
| 10 | +load_dotenv() |
| 11 | + |
| 12 | +# ************************************************ |
| 13 | +# Define the configuration for the graph |
| 14 | +# ************************************************ |
| 15 | + |
| 16 | +openai_key = os.getenv("OPENAI_APIKEY") |
| 17 | + |
| 18 | +graph_config = { |
| 19 | + "llm": { |
| 20 | + "api_key": openai_key, |
| 21 | + "model": "gpt-3.5-turbo", |
| 22 | + "temperature": 0, |
| 23 | + "streaming": True |
| 24 | + }, |
| 25 | +} |
| 26 | + |
| 27 | +# ************************************************ |
| 28 | +# Define the graph nodes |
| 29 | +# ************************************************ |
| 30 | + |
| 31 | +llm_model = OpenAI(graph_config["llm"]) |
| 32 | + |
| 33 | +# define the nodes for the graph |
| 34 | +fetch_node = FetchNode( |
| 35 | + input="url | local_dir", |
| 36 | + output=["doc"], |
| 37 | +) |
| 38 | +generate_answer_node = GenerateAnswerNode( |
| 39 | + input="user_prompt & (relevant_chunks | parsed_doc | doc)", |
| 40 | + output=["answer"], |
| 41 | + node_config={"llm": llm_model}, |
| 42 | +) |
| 43 | + |
| 44 | +# ************************************************ |
| 45 | +# Create the graph by defining the connections |
| 46 | +# ************************************************ |
| 47 | + |
| 48 | +graph = BaseGraph( |
| 49 | + nodes={ |
| 50 | + fetch_node, |
| 51 | + generate_answer_node, |
| 52 | + }, |
| 53 | + edges={ |
| 54 | + (fetch_node, generate_answer_node) |
| 55 | + }, |
| 56 | + entry_point=fetch_node |
| 57 | +) |
| 58 | + |
| 59 | +# ************************************************ |
| 60 | +# Execute the graph |
| 61 | +# ************************************************ |
| 62 | + |
| 63 | +subtree_text = ''' |
| 64 | +div>div -> "This is a paragraph" \n |
| 65 | +div>ul>li>a>span -> "This is a list item 1" \n |
| 66 | +div>ul>li>a>span -> "This is a list item 2" \n |
| 67 | +div>ul>li>a>span -> "This is a list item 3" |
| 68 | +''' |
| 69 | + |
| 70 | +subtree_simplified_html = ''' |
| 71 | +<div> |
| 72 | + <div>This is a paragraph</div> |
| 73 | + <ul> |
| 74 | + <li> |
| 75 | + <span>This is a list item 1</span> |
| 76 | + </li> |
| 77 | + <li> |
| 78 | + <span>This is a list item 2</span> |
| 79 | + </li> |
| 80 | + <li> |
| 81 | + <span>This is a list item 3</span> |
| 82 | + </li> |
| 83 | + </ul> |
| 84 | +</div> |
| 85 | +''' |
| 86 | + |
| 87 | +subtree_dict_simple = { |
| 88 | + "div": { |
| 89 | + "text": { |
| 90 | + "content": "This is a paragraph", |
| 91 | + "path_to_fork": "div>div", |
| 92 | + }, |
| 93 | + "ul": { |
| 94 | + "path_to_fork": "div>ul", |
| 95 | + "texts": [ |
| 96 | + { |
| 97 | + "content": "This is a list item 1", |
| 98 | + "path_to_fork": "ul>li>a>span", |
| 99 | + }, |
| 100 | + { |
| 101 | + "content": "This is a list item 2", |
| 102 | + "path_to_fork": "ul>li>a>span", |
| 103 | + }, |
| 104 | + { |
| 105 | + "content": "This is a list item 3", |
| 106 | + "path_to_fork": "ul>li>a>span", |
| 107 | + } |
| 108 | + ] |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +subtree_dict_complex = { |
| 115 | + "div": { |
| 116 | + "text": { |
| 117 | + "content": "This is a paragraph", |
| 118 | + "path_to_fork": "div>div", |
| 119 | + "attributes": { |
| 120 | + "classes": ["paragraph"], |
| 121 | + "ids": ["paragraph"], |
| 122 | + "hrefs": ["https://www.example.com"] |
| 123 | + } |
| 124 | + }, |
| 125 | + "ul": { |
| 126 | + "text1":{ |
| 127 | + "content": "This is a list item 1", |
| 128 | + "path_to_fork": "ul>li>a>span", |
| 129 | + "attributes": { |
| 130 | + "classes": ["list-item", "item-1"], |
| 131 | + "ids": ["item-1"], |
| 132 | + "hrefs": ["https://www.example.com"] |
| 133 | + } |
| 134 | + }, |
| 135 | + "text2":{ |
| 136 | + "content": "This is a list item 2", |
| 137 | + "path_to_fork": "ul>li>a>span", |
| 138 | + "attributes": { |
| 139 | + "classes": ["list-item", "item-2"], |
| 140 | + "ids": ["item-2"], |
| 141 | + "hrefs": ["https://www.example.com"] |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +result, execution_info = graph.execute({ |
| 149 | + "user_prompt": "How many list items are there in the document?", |
| 150 | + "local_dir": str(subtree_dict_simple) |
| 151 | +}) |
| 152 | + |
| 153 | +# get the answer from the result |
| 154 | +result = result.get("answer", "No answer found.") |
| 155 | +print(result) |
0 commit comments