|
| 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 OpenAIEmbeddings |
| 9 | +from scrapegraphai.models import OpenAI |
| 10 | +from scrapegraphai.graphs import BaseGraph |
| 11 | +from scrapegraphai.nodes import FetchNode, ParseNode, RAGNode, GenerateAnswerNode, RobotsNode |
| 12 | +load_dotenv() |
| 13 | + |
| 14 | +# ************************************************ |
| 15 | +# Define the configuration for the graph |
| 16 | +# ************************************************ |
| 17 | + |
| 18 | +fireworks_api_key = os.getenv("FIREWORKS_APIKEY") |
| 19 | + |
| 20 | +graph_config = { |
| 21 | + "llm": { |
| 22 | + "api_key": fireworks_api_key, |
| 23 | + "model": "fireworks/accounts/fireworks/models/mixtral-8x7b-instruct" |
| 24 | + }, |
| 25 | + "embeddings": { |
| 26 | + "model": "ollama/nomic-embed-text", |
| 27 | + "temperature": 0, |
| 28 | + # "base_url": "http://localhost:11434", # set ollama URL arbitrarily |
| 29 | + }, |
| 30 | + "verbose": True, |
| 31 | + "headless": False, |
| 32 | +} |
| 33 | + |
| 34 | +# ************************************************ |
| 35 | +# Define the graph nodes |
| 36 | +# ************************************************ |
| 37 | + |
| 38 | +llm_model = OpenAI(graph_config["llm"]) |
| 39 | +embedder = OpenAIEmbeddings(api_key=llm_model.openai_api_key) |
| 40 | + |
| 41 | +# define the nodes for the graph |
| 42 | +robot_node = RobotsNode( |
| 43 | + input="url", |
| 44 | + output=["is_scrapable"], |
| 45 | + node_config={ |
| 46 | + "llm_model": llm_model, |
| 47 | + "force_scraping": True, |
| 48 | + "verbose": True, |
| 49 | + } |
| 50 | +) |
| 51 | + |
| 52 | +fetch_node = FetchNode( |
| 53 | + input="url | local_dir", |
| 54 | + output=["doc", "link_urls", "img_urls"], |
| 55 | + node_config={ |
| 56 | + "verbose": True, |
| 57 | + "headless": True, |
| 58 | + } |
| 59 | +) |
| 60 | +parse_node = ParseNode( |
| 61 | + input="doc", |
| 62 | + output=["parsed_doc"], |
| 63 | + node_config={ |
| 64 | + "chunk_size": 4096, |
| 65 | + "verbose": True, |
| 66 | + } |
| 67 | +) |
| 68 | +rag_node = RAGNode( |
| 69 | + input="user_prompt & (parsed_doc | doc)", |
| 70 | + output=["relevant_chunks"], |
| 71 | + node_config={ |
| 72 | + "llm_model": llm_model, |
| 73 | + "embedder_model": embedder, |
| 74 | + "verbose": True, |
| 75 | + } |
| 76 | +) |
| 77 | +generate_answer_node = GenerateAnswerNode( |
| 78 | + input="user_prompt & (relevant_chunks | parsed_doc | doc)", |
| 79 | + output=["answer"], |
| 80 | + node_config={ |
| 81 | + "llm_model": llm_model, |
| 82 | + "verbose": True, |
| 83 | + } |
| 84 | +) |
| 85 | + |
| 86 | +# ************************************************ |
| 87 | +# Create the graph by defining the connections |
| 88 | +# ************************************************ |
| 89 | + |
| 90 | +graph = BaseGraph( |
| 91 | + nodes=[ |
| 92 | + robot_node, |
| 93 | + fetch_node, |
| 94 | + parse_node, |
| 95 | + rag_node, |
| 96 | + generate_answer_node, |
| 97 | + ], |
| 98 | + edges=[ |
| 99 | + (robot_node, fetch_node), |
| 100 | + (fetch_node, parse_node), |
| 101 | + (parse_node, rag_node), |
| 102 | + (rag_node, generate_answer_node) |
| 103 | + ], |
| 104 | + entry_point=robot_node |
| 105 | +) |
| 106 | + |
| 107 | +# ************************************************ |
| 108 | +# Execute the graph |
| 109 | +# ************************************************ |
| 110 | + |
| 111 | +result, execution_info = graph.execute({ |
| 112 | + "user_prompt": "Describe the content", |
| 113 | + "url": "https://example.com/" |
| 114 | +}) |
| 115 | + |
| 116 | +# get the answer from the result |
| 117 | +result = result.get("answer", "No answer found.") |
| 118 | +print(result) |
0 commit comments