|
| 1 | +""" |
| 2 | +Example of custom graph for creating a knowledge graph |
| 3 | +""" |
| 4 | + |
| 5 | +import os, json |
| 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, SmartScraperGraph |
| 11 | +from scrapegraphai.nodes import GraphIteratorNode, MergeAnswersNode, KnowledgeGraphNode |
| 12 | + |
| 13 | +load_dotenv() |
| 14 | + |
| 15 | +# ************************************************ |
| 16 | +# Define the output schema |
| 17 | +# ************************************************ |
| 18 | + |
| 19 | +schema= """{ |
| 20 | + "Job Postings": { |
| 21 | + "Company x": [ |
| 22 | + { |
| 23 | + "title": "...", |
| 24 | + "description": "...", |
| 25 | + "location": "...", |
| 26 | + "date_posted": "..", |
| 27 | + "requirements": ["...", "...", "..."] |
| 28 | + }, |
| 29 | + { |
| 30 | + "title": "...", |
| 31 | + "description": "...", |
| 32 | + "location": "...", |
| 33 | + "date_posted": "..", |
| 34 | + "requirements": ["...", "...", "..."] |
| 35 | + } |
| 36 | + ], |
| 37 | + "Company y": [ |
| 38 | + { |
| 39 | + "title": "...", |
| 40 | + "description": "...", |
| 41 | + "location": "...", |
| 42 | + "date_posted": "..", |
| 43 | + "requirements": ["...", "...", "..."] |
| 44 | + } |
| 45 | + ] |
| 46 | + } |
| 47 | +}""" |
| 48 | + |
| 49 | +# ************************************************ |
| 50 | +# Define the configuration for the graph |
| 51 | +# ************************************************ |
| 52 | + |
| 53 | +openai_key = os.getenv("OPENAI_APIKEY") |
| 54 | + |
| 55 | +graph_config = { |
| 56 | + "llm": { |
| 57 | + "api_key": openai_key, |
| 58 | + "model": "gpt-4o", |
| 59 | + }, |
| 60 | + "verbose": True, |
| 61 | + "headless": False, |
| 62 | +} |
| 63 | + |
| 64 | +# ************************************************ |
| 65 | +# Define the graph nodes |
| 66 | +# ************************************************ |
| 67 | + |
| 68 | +llm_model = OpenAI(graph_config["llm"]) |
| 69 | +embedder = OpenAIEmbeddings(api_key=llm_model.openai_api_key) |
| 70 | + |
| 71 | +smart_scraper_instance = SmartScraperGraph( |
| 72 | + prompt="", |
| 73 | + source="", |
| 74 | + config=graph_config, |
| 75 | +) |
| 76 | + |
| 77 | +# ************************************************ |
| 78 | +# Define the graph nodes |
| 79 | +# ************************************************ |
| 80 | + |
| 81 | +graph_iterator_node = GraphIteratorNode( |
| 82 | + input="user_prompt & urls", |
| 83 | + output=["results"], |
| 84 | + node_config={ |
| 85 | + "graph_instance": smart_scraper_instance, |
| 86 | + } |
| 87 | +) |
| 88 | + |
| 89 | +merge_answers_node = MergeAnswersNode( |
| 90 | + input="user_prompt & results", |
| 91 | + output=["answer"], |
| 92 | + node_config={ |
| 93 | + "llm_model": llm_model, |
| 94 | + "schema": schema |
| 95 | + } |
| 96 | +) |
| 97 | + |
| 98 | +knowledge_graph_node = KnowledgeGraphNode( |
| 99 | + input="user_prompt & answer", |
| 100 | + output=["kg"], |
| 101 | + node_config={ |
| 102 | + "llm_model": llm_model, |
| 103 | + } |
| 104 | +) |
| 105 | + |
| 106 | +graph = BaseGraph( |
| 107 | + nodes=[ |
| 108 | + graph_iterator_node, |
| 109 | + merge_answers_node, |
| 110 | + knowledge_graph_node |
| 111 | + ], |
| 112 | + edges=[ |
| 113 | + (graph_iterator_node, merge_answers_node), |
| 114 | + (merge_answers_node, knowledge_graph_node) |
| 115 | + ], |
| 116 | + entry_point=graph_iterator_node |
| 117 | +) |
| 118 | + |
| 119 | +# ************************************************ |
| 120 | +# Execute the graph |
| 121 | +# ************************************************ |
| 122 | + |
| 123 | +result, execution_info = graph.execute({ |
| 124 | + "user_prompt": "List me all the Machine Learning Engineer job postings", |
| 125 | + "urls": [ |
| 126 | + "https://www.linkedin.com/jobs/machine-learning-engineer-offerte-di-lavoro/?currentJobId=3889037104&originalSubdomain=it", |
| 127 | + "https://www.glassdoor.com/Job/italy-machine-learning-engineer-jobs-SRCH_IL.0,5_IN120_KO6,31.html", |
| 128 | + "https://it.indeed.com/jobs?q=ML+engineer&vjk=3c2e6d27601ffaaa" |
| 129 | + ], |
| 130 | +}) |
| 131 | + |
| 132 | +# get the answer from the result |
| 133 | +result = result.get("answer", "No answer found.") |
| 134 | +print(json.dumps(result, indent=4)) |
0 commit comments