Skip to content

Commit a9b11e4

Browse files
committed
fix: bug for calculate costs
1 parent 9cd5165 commit a9b11e4

File tree

2 files changed

+22
-45
lines changed

2 files changed

+22
-45
lines changed

scrapegraphai/graphs/base_graph.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def _create_edges(self, edges: list) -> dict:
5656
edge_dict[from_node.node_name] = to_node.node_name
5757
return edge_dict
5858

59-
def execute(self, initial_state: dict) -> dict:
59+
def execute(self, initial_state: dict) -> (dict, list):
6060
"""
6161
Executes the graph by traversing nodes starting from the entry point. The execution
6262
follows the edges based on the result of each node's execution and continues until
@@ -68,13 +68,12 @@ def execute(self, initial_state: dict) -> dict:
6868
Returns:
6969
dict: The state after execution has completed, which may have been altered by the nodes.
7070
"""
71-
print(self.nodes)
7271
current_node_name = self.nodes[0]
7372
state = initial_state
7473

7574
# variables for tracking execution info
7675
total_exec_time = 0.0
77-
exec_info = {}
76+
exec_info = []
7877
cb_total = {
7978
"total_tokens": 0,
8079
"prompt_tokens": 0,
@@ -94,18 +93,19 @@ def execute(self, initial_state: dict) -> dict:
9493
total_exec_time += node_exec_time
9594

9695
cb = {
96+
"node_name": index.node_name,
9797
"total_tokens": cb.total_tokens,
9898
"prompt_tokens": cb.prompt_tokens,
9999
"completion_tokens": cb.completion_tokens,
100100
"successful_requests": cb.successful_requests,
101101
"total_cost_USD": cb.total_cost,
102-
}
103-
104-
exec_info[current_node_name] = {
105102
"exec_time": node_exec_time,
106-
"model_info": cb
107103
}
108104

105+
exec_info.append(
106+
cb
107+
)
108+
109109
cb_total["total_tokens"] += cb["total_tokens"]
110110
cb_total["prompt_tokens"] += cb["prompt_tokens"]
111111
cb_total["completion_tokens"] += cb["completion_tokens"]
@@ -119,10 +119,14 @@ def execute(self, initial_state: dict) -> dict:
119119
else:
120120
current_node_name = None
121121

122-
execution_info = {
123-
"total_exec_time": total_exec_time,
124-
"total_model_info": cb_total,
125-
"nodes_info": exec_info
126-
}
127-
128-
return state, execution_info
122+
exec_info.append({
123+
"node_name": "TOTAL RESULT",
124+
"total_tokens": cb_total["total_tokens"],
125+
"prompt_tokens": cb_total["prompt_tokens"],
126+
"completion_tokens": cb_total["completion_tokens"],
127+
"successful_requests": cb_total["successful_requests"],
128+
"total_cost_USD": cb_total["total_cost_USD"],
129+
"exec_time": total_exec_time,
130+
})
131+
132+
return state, exec_info

scrapegraphai/utils/prettify_exec_info.py

+4-31
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,17 @@
55
import pandas as pd
66

77

8-
def prettify_exec_info(complete_result: dict) -> pd.DataFrame:
8+
def prettify_exec_info(complete_result: list[dict]) -> pd.DataFrame:
99
"""
1010
Transform the execution information of the graph into a DataFrame for better visualization.
1111
1212
Args:
13-
- complete_result (dict): The complete execution information of the graph.
13+
- complete_result (list[dict]): The complete execution information of the graph.
1414
1515
Returns:
1616
- pd.DataFrame: The execution information of the graph in a DataFrame.
1717
"""
1818

19-
nodes_info = complete_result['nodes_info']
20-
total_info = {
21-
'total_exec_time': complete_result['total_exec_time'],
22-
'total_model_info': complete_result['total_model_info']
23-
}
19+
df_nodes = pd.DataFrame(complete_result)
2420

25-
# Convert node-specific information to DataFrame
26-
flat_data = []
27-
for node_name, node_info in nodes_info.items():
28-
flat_data.append({
29-
'Node': node_name,
30-
'Execution Time': node_info['exec_time'],
31-
# Unpack the model_info dict into the row
32-
**node_info['model_info']
33-
})
34-
35-
df_nodes = pd.DataFrame(flat_data)
36-
37-
# Add a row for the total execution time and total model info
38-
total_row = {
39-
'Node': 'Total',
40-
'Execution Time': total_info['total_exec_time'],
41-
# Unpack the total_model_info dict into the row
42-
**total_info['total_model_info']
43-
}
44-
df_total = pd.DataFrame([total_row])
45-
46-
# Combine the nodes DataFrame with the total info DataFrame
47-
df_combined_with_total = pd.concat([df_nodes, df_total], ignore_index=True)
48-
return df_combined_with_total
21+
return df_nodes

0 commit comments

Comments
 (0)