|
1 |
| -""" |
2 |
| -Example of custom graph using existing nodes |
3 |
| -""" |
4 |
| - |
5 |
| -import os |
6 |
| -from dotenv import load_dotenv |
7 |
| - |
8 |
| -from langchain_openai import ChatOpenAI |
9 |
| -from scrapegraphai.graphs import BaseGraph |
10 |
| -from scrapegraphai.nodes import FetchHTMLNode, ParseHTMLNode, GenerateAnswerNode |
11 |
| - |
12 |
| -# load the environment variables |
13 |
| -load_dotenv() |
14 |
| -openai_key = os.getenv("API_KEY") |
15 |
| -if not openai_key: |
16 |
| - print("Error: OpenAI API key not found in environment variables.") |
17 |
| - |
18 |
| -# Define the configuration for the language model |
19 |
| -llm_config = { |
20 |
| - "api_key": openai_key, |
21 |
| - "model_name": "gpt-3.5-turbo", |
22 |
| - "temperature": 0, |
23 |
| - "streaming": True |
24 |
| -} |
25 |
| -model = ChatOpenAI(**llm_config) |
26 |
| - |
27 |
| -# define the nodes for the graph |
28 |
| -fetch_html_node = FetchHTMLNode("fetch_html") |
29 |
| -parse_document_node = ParseHTMLNode("parse_document") |
30 |
| -generate_answer_node = GenerateAnswerNode(model, "generate_answer") |
31 |
| - |
32 |
| -# create the graph |
33 |
| -graph = BaseGraph( |
34 |
| - nodes={ |
35 |
| - fetch_html_node, |
36 |
| - parse_document_node, |
37 |
| - generate_answer_node |
38 |
| - }, |
39 |
| - edges={ |
40 |
| - (fetch_html_node, parse_document_node), |
41 |
| - (parse_document_node, generate_answer_node) |
42 |
| - }, |
43 |
| - entry_point=fetch_html_node |
44 |
| -) |
45 |
| - |
46 |
| -# execute the graph |
47 |
| -inputs = {"keys": {"user_input": "What is the title of the page?", "url": "https://example.com"}} |
48 |
| -result = graph.execute(inputs) |
49 |
| - |
50 |
| -# get the answer from the result |
51 |
| -answer = result["keys"].get("answer", "No answer found.") |
52 |
| -print(answer) |
| 1 | +""" |
| 2 | +Example of custom graph using existing nodes |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +from dotenv import load_dotenv |
| 7 | + |
| 8 | +from langchain_openai import ChatOpenAI |
| 9 | +from scrapegraphai.graphs import BaseGraph |
| 10 | +from scrapegraphai.nodes import FetchHTMLNode, ParseHTMLNode, GenerateAnswerNode |
| 11 | + |
| 12 | +# load the environment variables |
| 13 | +load_dotenv() |
| 14 | +openai_key = os.getenv("API_KEY") |
| 15 | +if not openai_key: |
| 16 | + print("Error: OpenAI API key not found in environment variables.") |
| 17 | + |
| 18 | +# Define the configuration for the language model |
| 19 | +llm_config = { |
| 20 | + "api_key": openai_key, |
| 21 | + "model_name": "gpt-3.5-turbo", |
| 22 | + "temperature": 0, |
| 23 | + "streaming": True |
| 24 | +} |
| 25 | +model = ChatOpenAI(**llm_config) |
| 26 | + |
| 27 | +# define the nodes for the graph |
| 28 | +fetch_html_node = FetchHTMLNode("fetch_html") |
| 29 | +parse_document_node = ParseHTMLNode("parse_document") |
| 30 | +generate_answer_node = GenerateAnswerNode(model, "generate_answer") |
| 31 | + |
| 32 | +# create the graph |
| 33 | +graph = BaseGraph( |
| 34 | + nodes={ |
| 35 | + fetch_html_node, |
| 36 | + parse_document_node, |
| 37 | + generate_answer_node |
| 38 | + }, |
| 39 | + edges={ |
| 40 | + (fetch_html_node, parse_document_node), |
| 41 | + (parse_document_node, generate_answer_node) |
| 42 | + }, |
| 43 | + entry_point=fetch_html_node |
| 44 | +) |
| 45 | + |
| 46 | +# execute the graph |
| 47 | +inputs = {"keys": {"user_input": "What is the title of the page?", "url": "https://example.com"}} |
| 48 | +result = graph.execute(inputs) |
| 49 | + |
| 50 | +# get the answer from the result |
| 51 | +answer = result["keys"].get("answer", "No answer found.") |
| 52 | +print(answer) |
0 commit comments