-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_data_llm.py
365 lines (289 loc) · 13.5 KB
/
extract_data_llm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import os
import requests
from openai import OpenAI
from markdownify import markdownify as md
from typing import Literal, Optional
from pydantic import BaseModel, Field
from load_file import *
from process_general_info_date_type import *
# The max/typical power that the router can draw
class Power(BaseModel):
value: float = Field(description="The maximum or the typical power that the router can draw")
unit: str = Field(description="The unit of the value mentioned above, usually in W but others could also exist")
description: Optional[str] = Field(description="The power description that may be associated with the power value (such as the temperature, throughput, ...)")
# Basically the throughput
class Throughput(BaseModel):
value: float = Field(description="The value of the throughput of a router")
unit: str = Field(description="The unit of the value mentioned above, usually in Tbps or Gbps")
# Class to represent the PSU details
class PSU(BaseModel):
efficiency_rating: Optional[Literal["Bronze", "Silver", "Gold", "Platinum"]] = Field(description="The 80 Plus certification level indicating the power supply unit's efficiency, with ratings from Bronze to Platinum, where higher levels signify greater energy efficiency.")
power_rating: Optional[Power] = Field(description="How much power a PSU can deliver")
number_of_modules: int = Field(description="The number of Power Supply Units (PSUs) used for this router")
part_number: Optional[str] = Field(description="The part number of this PSU")
# The overall infomation of a router
class RouterInfo(BaseModel):
datasheet_pdf: str = Field(description="The pdf file storing the information on this router device.")
max_throughput: Optional[Throughput] = Field(description="The maximum throughput or the bandwidth used in the url, usually in the unit of Tbps or Gbps")
typical_power_draw: Optional[Power] = Field(description="This is the usual amount of power comsumed by the device under normal operating conditions. It reflects the average power usage when the device is functioning with a typical load")
max_power_draw: Optional[Power] = Field(description="This is the maximum amount of power the router can consume, usually measured under peak load or stressful conditions. It represents the highest power demand the device will require, typically when all resources are being utilized to their fullest capacity")
is_poe_capable: bool = Field(description="A boolean value. If this router is poe_capable, then return true, otherwise return false")
max_poe_draw: Optional[Power] = Field(description="This is the maximum Power over Ethernet consumption. PoE enables network cables to supply both data and electrical power to devices")
psu: Optional[PSU] = Field(description="The Power Supplier Unit(PSU) related data")
class ValueReference(BaseModel):
value: str = Field(description="A value based on a specific class. In our current case, it can be either date of router_type")
reference: str = Field(description="A reference link or source indicating where this date is found online")
class RouterDate(BaseModel):
release_date: Optional[ValueReference] = Field(description="The date when the product was first made available to the public in the format of YYYY-MM-DD. It marks the start of the product's lifecycle on the market")
end_of_sale: Optional[ValueReference] = Field(description="The last date on which the product could be purchased in the format of YYYY-MM-DD. After this date, the product is no longer available for sale from the manufacturer")
end_of_support: Optional[ValueReference] = Field(description="The final date which the manufacturer will provide support in the format of YYYY-MM-DD. After this date, official support ends, and the product is considered 'end of life'")
class RouterType(BaseModel):
router_type: Optional[ValueReference] = Field(description="The category of the router, such as 'edge', 'core', 'distribution', among others.")
class RouterURL(BaseModel):
router_url: Optional[str] = Field(description="The official URL for the router, providing detailed product information and specifications.")
class RouterSeries(BaseModel):
router_series: Optional[str] = Field(description="The router series which this router model belong to.")
def extract_datasheet_with_url_llm(router_name, url):
"""
Use the OpenAI API to help us extract the information based on provided URL
Parameters:
router_name: The router name
url: The URL of this model
Returns:
The extracted information of the given router based on its URL datasheet
"""
prompt_file = "../llm_prompt/process_datasheet_with_url_prompt.txt"
components = load_prompt_components(prompt_file, router_name)
# Define the system_prompt
system_prompt = "\n".join([
components["PERSONA"],
components["HIGH_LEVEL_TASK"],
components["LOW_LEVEL_TASK"]
])
client = OpenAI()
html_content = requests.get(url).text
# Transfer the HTML to MD to reduce the number of tokens
# encoding = tiktoken.encoding_for_model("gpt-4o-mini")
os.makedirs("../markdown", exist_ok=True)
if url.endswith("pdf"):
markdown_content = pdf_to_markdown(url, router_name)
else:
markdown_content = md(html_content)
with open(f"../markdown/{router_name}.md", "w") as f:
f.write(markdown_content)
if markdown_content == None:
return None
# Call the OpenAI API
try:
completion = client.beta.chat.completions.parse(
temperature = 0,
model = "gpt-4o",
messages=[
{
"role": "system",
"content": system_prompt
},
{
"role": "user",
"content": markdown_content
}
],
response_format=RouterInfo,
)
output = completion.choices[0].message.parsed
parsed_router_url_llm = output.model_dump(mode="yaml")
return parsed_router_url_llm
except Exception as e:
print("Error: ", e)
pass
def find_router_url_llm(router_name):
"""
Find a router URL if it does not reveal in the netbox dataset.
Parameters:
router_name: The name of this router.
Returns:
The URL of this router.
"""
client = OpenAI()
system_prompt = f"Find the URL datasheet of the {router_name}. \
The URL datasheet can be a html or a pdf. \
For example, Cisco ME 3600X Series Ethernet Access Switches's URL datasheet is https://andovercg.com/datasheets/cisco-me-3600x-series-ethernet-access-switches-data_sheet.pdf \
For example, Cisco MS350 Series' URL datasheet is https://documentation.meraki.com/MS/MS_Overview_and_Specifications/MS350_Overview_and_Specifications."
completion = client.beta.chat.completions.parse(
temperature = 0,
model = "gpt-4o",
messages=[
{
"role": "system",
"content": system_prompt
},
{
"role": "user",
"content": "Let's find the router url mentioned in the prompt."
}
],
response_format=RouterURL,
)
output_url = completion.choices[0].message.parsed.router_url
return output_url
def extract_datasheet_without_url_llm(router_name):
"""
Extract the information based on the LLM's own searching result of a given router. This router does not contain URL datasheet
Parameters:
router_name: The name of this router
Returns:
The extracted information of the given router based on LLM's own knowledge
"""
prompt_file = "../llm_prompt/process_datasheet_without_url_prompt.txt"
components = load_prompt_components(prompt_file, router_name)
# Define the system_prompt
system_prompt = "\n".join([
components["PERSONA"],
components["HIGH_LEVEL_TASK"],
components["LOW_LEVEL_TASK"]
])
client = OpenAI()
try:
completion = client.beta.chat.completions.parse(
temperature = 0,
model = "gpt-4o",
messages=[
{
"role": "system",
"content": system_prompt
},
{
"role": "user",
"content": "Let's find the router information based on the prompt"
}
],
response_format=RouterInfo,
)
output = completion.choices[0].message.parsed
parsed_router_url_llm = output.model_dump(mode="yaml")
# Extract units
return parsed_router_url_llm
except Exception as e:
print("Error: ", e)
pass
def process_router_date_llm(router_series, url = None):
"""
Use the OpenAI API to help us extract the date information
Parameters:
router_series: The router series
Returns:
The three key dates (release date, end-of-sale date and end-of-support date) of this router
"""
print("router series url: ", url)
if url:
system_prompt = f"Please extract the dates from the this given url: {url}."
html_content = requests.get(url).text
content = md(html_content)
else:
prompt_file = "../llm_prompt/process_router_date_prompt.txt"
components = load_prompt_components(prompt_file, router_series)
system_prompt = "\n".join([
components["PERSONA"],
components["HIGH_LEVEL_TASK"],
components["LOW_LEVEL_TASK"]
])
content = "Let's find the date!"
client = OpenAI()
try:
completion = client.beta.chat.completions.parse(
temperature = 0,
model = "gpt-4o",
messages=[
{
"role": "system",
"content": system_prompt
},
{
"role": "user",
"content": content
}
],
response_format=RouterDate,
)
output = completion.choices[0].message.parsed
print("output: ", output)
parsed_router_date_llm = output.model_dump(mode="yaml")
return parsed_router_date_llm
except Exception as e:
print("Error: ", e)
pass
def process_router_type_llm(router_series):
"""
Use the OpenAI API to help us extract the type information
Parameters:
router_name: The router name
Returns:
The type of this router and the source which LLM refers to
"""
prompt_file = "../llm_prompt/process_router_type_prompt.txt"
components = load_prompt_components(prompt_file, router_series)
# Define the system_prompt
system_prompt = "\n".join([
components["PERSONA"],
components["HIGH_LEVEL_TASK"],
components["LOW_LEVEL_TASK"]
])
client = OpenAI()
try:
completion = client.beta.chat.completions.parse(
temperature = 0,
model = "gpt-4o",
messages=[
{
"role": "system",
"content": system_prompt
},
{
"role": "user",
"content": "Let's find the date mentioned in the prompt."
}
],
response_format=RouterType,
)
output = completion.choices[0].message.parsed
parsed_router_type_llm = output.model_dump(mode="yaml")
return parsed_router_type_llm
except Exception as e:
print("Error: ", e)
pass
def find_router_series_llm(cisco_router_series_file_path, router_name, manufacturer=None):
"""
Find the corresponding router series with the help of LLM.
Parameters:
cisco_router_series_file_path: The file path indicating the Cisco router series information.
router_name: The name of the router.
manufacturer: The manufacturer of a router, e.g., Cisco.
Returns:
The series of the router.
"""
client = OpenAI()
system_prompt = f"Given the router model '{router_name}', its associated manufacturer '{manufacturer}' and the '{cisco_router_series_file_path}' file\
please return the router series for this specific model. \
It will be perfect if the router series belongs to one of the those in '{cisco_router_series_file_path}'. \
If you can't find the corresponding series in '{cisco_router_series_file_path}', then use your own knowledge to search for the series. \
If you still can't find it, leave it empty. \
For example, router 'Catalyst 3650-48FQM-L' belongs to the series 'Catalyst 3650 Series Switches'"
cisco_router_series = load_json(cisco_router_series_file_path)
cisco_router_series_str = json.dumps(cisco_router_series)
completion = client.beta.chat.completions.parse(
temperature = 0,
model = "gpt-4o",
messages=[
{
"role": "system",
"content": system_prompt
},
{
"role": "user",
"content": cisco_router_series_str
}
],
response_format=RouterSeries,
)
output_series = completion.choices[0].message.parsed.router_series
return output_series