1
+ """
2
+ Module for making the graph building
3
+ """
4
+ import graphviz
1
5
from langchain_core .prompts import ChatPromptTemplate
2
6
from langchain .chains import create_extraction_chain
3
7
from ..models import OpenAI
4
- from ..utils import nodes_metadata , graph_schema
8
+ from ..helpers import nodes_metadata , graph_schema
9
+
5
10
6
11
class GraphBuilder :
7
12
"""
@@ -11,19 +16,24 @@ class GraphBuilder:
11
16
12
17
Attributes:
13
18
prompt (str): The user's natural language prompt for the scraping task.
14
- llm (ChatOpenAI): An instance of the ChatOpenAI class configured with the specified llm_config.
19
+ llm (ChatOpenAI): An instance of the ChatOpenAI class configured
20
+ with the specified llm_config.
15
21
nodes_description (str): A string description of all available nodes and their arguments.
16
- chain (LLMChain): The extraction chain responsible for processing the prompt and creating the graph.
22
+ chain (LLMChain): The extraction chain responsible for
23
+ processing the prompt and creating the graph.
17
24
18
25
Methods:
19
- build_graph(): Executes the graph creation process based on the user prompt and returns the graph configuration.
20
- convert_json_to_graphviz(json_data): Converts a JSON graph configuration to a Graphviz object for visualization.
26
+ build_graph(): Executes the graph creation process based on the user prompt
27
+ and returns the graph configuration.
28
+ convert_json_to_graphviz(json_data): Converts a JSON graph configuration
29
+ to a Graphviz object for visualization.
21
30
22
31
Args:
23
32
prompt (str): The user's natural language prompt describing the desired scraping operation.
24
33
url (str): The target URL from which data is to be scraped.
25
- llm_config (dict): Configuration parameters for the language model, where 'api_key' is mandatory,
26
- and 'model_name', 'temperature', and 'streaming' can be optionally included.
34
+ llm_config (dict): Configuration parameters for the
35
+ language model, where 'api_key' is mandatory,
36
+ and 'model_name', 'temperature', and 'streaming' can be optionally included.
27
37
28
38
Raises:
29
39
ValueError: If 'api_key' is not included in llm_config.
@@ -38,7 +48,7 @@ def __init__(self, user_prompt: str, llm_config: dict):
38
48
self .llm = self ._create_llm ()
39
49
self .nodes_description = self ._generate_nodes_description ()
40
50
self .chain = self ._create_extraction_chain ()
41
-
51
+
42
52
def _create_llm (self ):
43
53
"""
44
54
Creates an instance of the OpenAI class with the provided language model configuration.
@@ -77,7 +87,8 @@ def _generate_nodes_description(self):
77
87
78
88
def _create_extraction_chain (self ):
79
89
"""
80
- Creates an extraction chain for processing the user prompt and generating the graph configuration.
90
+ Creates an extraction chain for processing the user prompt and
91
+ generating the graph configuration.
81
92
82
93
Returns:
83
94
LLMChain: An instance of the LLMChain class.
@@ -90,20 +101,22 @@ def _create_extraction_chain(self):
90
101
91
102
Based on the user's input: "{input}", identify the essential nodes required for the task and suggest a graph configuration that outlines the flow between the chosen nodes.
92
103
""" .format (nodes_description = self .nodes_description , input = "{input}" )
93
- extraction_prompt = ChatPromptTemplate .from_template (create_graph_prompt_template )
104
+ extraction_prompt = ChatPromptTemplate .from_template (
105
+ create_graph_prompt_template )
94
106
return create_extraction_chain (prompt = extraction_prompt , schema = graph_schema , llm = self .llm )
95
107
96
108
def build_graph (self ):
97
109
"""
98
- Executes the graph creation process based on the user prompt and returns the graph configuration.
110
+ Executes the graph creation process based on the user prompt and
111
+ returns the graph configuration.
99
112
100
113
Returns:
101
114
dict: A JSON representation of the graph configuration.
102
115
"""
103
116
return self .chain .invoke (self .user_prompt )
104
-
117
+
105
118
@staticmethod
106
- def convert_json_to_graphviz (json_data , format = 'pdf' ):
119
+ def convert_json_to_graphviz (json_data , format : str = 'pdf' ):
107
120
"""
108
121
Converts a JSON graph configuration to a Graphviz object for visualization.
109
122
@@ -113,11 +126,10 @@ def convert_json_to_graphviz(json_data, format='pdf'):
113
126
Returns:
114
127
graphviz.Digraph: A Graphviz object representing the graph configuration.
115
128
"""
116
- import graphviz
117
129
118
130
graph = graphviz .Digraph (comment = 'ScrapeGraphAI Generated Graph' , format = format ,
119
- node_attr = {'color' : 'lightblue2' , 'style' : 'filled' })
120
-
131
+ node_attr = {'color' : 'lightblue2' , 'style' : 'filled' })
132
+
121
133
graph_config = json_data ["text" ][0 ]
122
134
123
135
# Retrieve nodes, edges, and the entry point from the JSON data
@@ -142,4 +154,4 @@ def convert_json_to_graphviz(json_data, format='pdf'):
142
154
else :
143
155
graph .edge (edge ['from' ], edge ['to' ])
144
156
145
- return graph
157
+ return graph
0 commit comments