@@ -2,16 +2,7 @@ import OpenAI from "openai";
22import fs from "fs" ;
33import dotenv from "dotenv" ;
44import { questions } from "./refined-prompts.data" ;
5- import {
6- ai_paper ,
7- ai_part ,
8- ai_author ,
9- FullDataType ,
10- PartData ,
11- AuthorData ,
12- PreliminaryTestData ,
13- } from "./types" ;
14- import { Author } from "./models" ;
5+ import { ai_paper , ai_part , PreliminaryTestData } from "./types" ;
156
167dotenv . config ( ) ; // Load environment variables
178
@@ -121,6 +112,7 @@ async function runThread(threadId: string, assistantId: string): Promise<any> {
121112 console . log ( `⏳ Running thread... (Run ID: ${ run . id } )` ) ;
122113
123114 // Poll for completion
115+ process . stdout . write ( "Waiting for completion..." ) ;
124116 while ( true ) {
125117 const runStatus = await openai . beta . threads . runs . retrieve (
126118 threadId ,
@@ -132,8 +124,8 @@ async function runThread(threadId: string, assistantId: string): Promise<any> {
132124 return null ;
133125 }
134126
135- console . log ( "🔄 Waiting for completion.. .") ;
136- await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) ) ; // Wait 2 seconds
127+ process . stdout . write ( " .") ;
128+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ; // Wait 2 seconds
137129 }
138130
139131 // Retrieve the assistant’s response
@@ -181,11 +173,15 @@ async function runThread(threadId: string, assistantId: string): Promise<any> {
181173 * @param threadId
182174 * @returns
183175 */
184- async function getPaperData (
185- assistantId : string ,
186- fileId : string ,
187- threadId : string ,
188- ) : Promise < ai_paper > {
176+ async function getPaperData ( {
177+ assistantId,
178+ fileId,
179+ threadId,
180+ } : {
181+ assistantId : string ;
182+ fileId : string ;
183+ threadId : string ;
184+ } ) : Promise < ai_paper > {
189185 await addMessage ( threadId , fileId , questions . paperMetaData . prompt ) ;
190186 const papermetdata_json = await runThread ( threadId , assistantId ) ;
191187 const paperObject = papermetdata_json as ai_paper ;
@@ -199,15 +195,19 @@ async function getPaperData(
199195 * @param threadId
200196 * @returns
201197 */
202- async function getPartData (
203- assistantId : string ,
204- fileId : string ,
205- threadId : string ,
206- ) : Promise < ai_part [ ] > {
198+ async function getPartData ( {
199+ assistantId,
200+ fileId,
201+ threadId,
202+ } : {
203+ assistantId : string ;
204+ fileId : string ;
205+ threadId : string ;
206+ } ) : Promise < Partial < ai_part > [ ] > {
207207 await addMessage ( threadId , fileId , questions . componentDetails . prompt ) ;
208208 const partdata_json = await runThread ( threadId , assistantId ) ;
209209 const partObjects = partdata_json . map ( ( part : any ) => {
210- return part as ai_part ;
210+ return part as Partial < ai_part > ;
211211 } ) ;
212212 return partObjects ;
213213}
@@ -220,74 +220,92 @@ async function getPartData(
220220 * @param parts
221221 * @returns
222222 */
223- async function getPartTests (
224- assistantId : string ,
225- fileId : string ,
226- threadId : string ,
227- parts : ai_part [ ] ,
228- ) : Promise < PreliminaryTestData [ ] > {
229- const partTests : PreliminaryTestData [ ] = [ ] ;
223+ async function getPartTests ( {
224+ assistantId,
225+ fileId,
226+ threadId,
227+ parts,
228+ } : {
229+ assistantId : string ;
230+ fileId : string ;
231+ threadId : string ;
232+ parts : Partial < ai_part > [ ] ;
233+ } ) : Promise < ai_part [ ] > {
234+ const partTests : ai_part [ ] = [ ] ;
230235 for ( const part of parts ) {
231236 await addMessage (
232237 threadId ,
233238 fileId ,
234- `You are to only look for data related to this part ${ part . device_name } , ${ questions . testingConditions . prompt } ` ,
239+ `You are to only look for data related to this part ${
240+ part ! . device_name
241+ } , ${ questions . testingConditions . prompt } `,
235242 ) ;
236243 const partRun = await runThread ( threadId , assistantId ) ;
237- partTests . push ( partRun as PreliminaryTestData ) ;
244+ partTests . push ( {
245+ ...part ,
246+ preliminary_test_data : partRun . map ( ( data : PreliminaryTestData ) => ( {
247+ ...data ,
248+ seeData : data . seeData || [ ] ,
249+ tidData : data . tidData || [ ] ,
250+ ddData : data . ddData || [ ] ,
251+ } ) ) ,
252+ } as ai_part ) ;
238253 }
254+ console . log ( partTests ) ;
239255 return partTests ;
240256}
241257
242- async function getSpecificTest (
243- assistantId : string ,
244- fileId : string ,
245- threadId : string ,
246- partTests : PreliminaryTestData [ ] ,
247- ) {
248- let partSpecific : {
249- SEE : any [ ] ;
250- TID : any [ ] ;
251- DD : any [ ] ;
252- } = {
253- SEE : [ ] ,
254- TID : [ ] ,
255- DD : [ ] ,
256- } ;
257- for ( const test of partTests ) {
258- if ( test . testing_type === "SEE" ) {
259- await addMessage (
260- threadId ,
261- fileId ,
262- `You are to only look for data related to this part ${ test . device_name } , ${ questions . seeData . prompt } ` ,
263- ) ;
264- partSpecific . SEE . push ( await runThread ( threadId , assistantId ) ) ;
265- } else if ( test . testing_type === "TID" ) {
266- await addMessage (
267- threadId ,
268- fileId ,
269- `You are to only look for data related to this part ${ test . device_name } , ${ questions . tidData . prompt } ` ,
270- ) ;
271- partSpecific . TID . push ( await runThread ( threadId , assistantId ) ) ;
272- } else if ( test . testing_type === "DD" ) {
273- await addMessage (
274- threadId ,
275- fileId ,
276- `You are to only look for data related to this part ${ test . device_name } , ${ questions . ddData . prompt } ` ,
277- ) ;
278- partSpecific . DD . push ( await runThread ( threadId , assistantId ) ) ;
279- } else {
280- console . log ( "Passing" , test . testing_type ) ;
258+ async function getSpecificTest ( {
259+ assistantId,
260+ fileId,
261+ threadId,
262+ parts,
263+ } : {
264+ assistantId : string ;
265+ fileId : string ;
266+ threadId : string ;
267+ parts : ai_part [ ] ;
268+ } ) : Promise < ai_part [ ] > {
269+ for ( const part of parts ) {
270+ for ( const test of part . preliminary_test_data ) {
271+ if ( test . testing_type === "SEE" ) {
272+ await addMessage (
273+ threadId ,
274+ fileId ,
275+ `You are to only look for data related to this part ${ part . device_name } , ${ questions . seeData . prompt } ` ,
276+ ) ;
277+ test . seeData . push ( await runThread ( threadId , assistantId ) ) ;
278+ } else if ( test . testing_type === "TID" ) {
279+ await addMessage (
280+ threadId ,
281+ fileId ,
282+ `You are to only look for data related to this part ${ part . device_name } , ${ questions . tidData . prompt } ` ,
283+ ) ;
284+ test . tidData . push ( await runThread ( threadId , assistantId ) ) ;
285+ } else if ( test . testing_type === "DD" ) {
286+ await addMessage (
287+ threadId ,
288+ fileId ,
289+ `You are to only look for data related to this part ${ part . device_name } , ${ questions . ddData . prompt } ` ,
290+ ) ;
291+ test . ddData . push ( await runThread ( threadId , assistantId ) ) ;
292+ } else {
293+ console . log ( "Passing" , test . testing_type ) ;
294+ }
281295 }
282296 }
283- return partSpecific ;
297+ return parts ;
284298}
285299
286- async function getTablesAndFigures (
287- assistantId : string ,
288- fileId : string ,
289- threadId : string ,
290- ) {
300+ async function getTablesAndFigures ( {
301+ assistantId,
302+ fileId,
303+ threadId,
304+ } : {
305+ assistantId : string ;
306+ fileId : string ;
307+ threadId : string ;
308+ } ) {
291309 await addMessage (
292310 threadId ,
293311 fileId ,
@@ -315,73 +333,42 @@ export async function processRadiationPaper(pdfPath: string) {
315333
316334 const threadId = await createThread ( ) ;
317335 if ( ! threadId ) return ;
336+ const ids = { assistantId, fileId, threadId } ;
318337
319338 console . log ( "Getting Paper Data\n" ) ;
320339 //await addMessage(threadId, fileId, questions.paperMetaData.prompt);
321340 //const papermetdata_json = await runThread(threadId, assistantId);
322- const papersData = await getPaperData ( assistantId , fileId , threadId ) ;
341+ const papersData = await getPaperData ( { ... ids } ) ;
323342 console . log ( "Getting Part Data\n" ) ;
324- const partData = await getPartData ( assistantId , fileId , threadId ) ;
325-
326- console . log ( "Getting Table and Figure Data" ) ;
327- const figureTableData = await getTablesAndFigures (
328- assistantId ,
329- fileId ,
330- threadId ,
331- ) ;
332-
333- console . log ( "Getting Part Test" ) ;
334- const partTestData = await getPartTests (
335- assistantId ,
336- fileId ,
337- threadId ,
338- partData ,
339- ) ;
340-
341- console . log ( "Getting specific part tests" ) ;
342- const specificTestData = await getSpecificTest (
343- assistantId ,
344- fileId ,
345- threadId ,
346- partTestData ,
347- ) ;
348-
349- const fullData : FullDataType = {
350- name : papersData . paper_name ,
351- year : papersData . year ,
352- authors : papersData . authors . map ( ( author ) => author as AuthorData ) ,
353- parts : partData . map ( ( part ) => part as PartData ) ,
354- testingData : partTestData . map ( ( test ) => ( {
355- id : test . id ,
356- testing_type : test . testing_type ,
357- max_fluence : test . max_fluence ,
358- energy : test . energy ,
359- facility : test . facility ,
360- environment : test . environment ,
361- terrestrial : test . terrestrial ,
362- flight : test . flight ,
363- // Adding specific test data
364- tidData : test . tidData ,
365- seeData : test . seeData ,
366- ddData : test . ddData ,
367- } ) ) ,
368- } ;
343+ const partData = await getSpecificTest ( {
344+ ...ids ,
345+ parts : await getPartTests ( {
346+ ...ids ,
347+ parts : await getPartData ( { ...ids } ) ,
348+ } ) ,
349+ } ) ;
369350
370351 await deleteThread ( threadId ) ;
371352
372- console . log ( "🔍 Extracted Data:" , JSON . stringify ( fullData , null , 2 ) ) ;
353+ console . log (
354+ "🔍 Extracted Data:" ,
355+ JSON . stringify ( { paper : papersData , parts : partData } , null , 2 ) ,
356+ ) ;
373357
374358 console . log ( "🚀 Running thread to process all queries..." ) ;
375359
376360 // ✅ Correctly write structured JSON data
377361 const outputFile = pdfPath . replace ( ".pdf" , "_extracted.json" ) ;
378- fs . writeFileSync ( outputFile , JSON . stringify ( fullData , null , 4 ) ) ;
362+ fs . writeFileSync (
363+ outputFile ,
364+ JSON . stringify ( { paper : papersData , parts : partData } , null , 4 ) ,
365+ ) ;
379366
380367 console . log ( `✅ Extraction complete! Data saved to ${ outputFile } ` ) ;
381368}
382369
383- /*
384- async function processRadiationPapers(pdfPaths: string[], assistant_id?: string) {\
370+
371+ async function processRadiationPapers ( pdfPaths : string [ ] , assistant_id ?: string ) {
385372 // Create a new assistant if one is not given
386373 let assistantId = assistant_id ?? await createAssistant ( ) ;
387374 const fileIDs = await Promise . all (
@@ -393,4 +380,4 @@ async function processRadiationPapers(pdfPaths: string[], assistant_id?: string)
393380 } ) )
394381
395382}
396- */
383+
0 commit comments