Index: /gs3-installations/thewillow/trunk/sites/thewillow/dlcol-chatgpt/create-assistant/main.js
===================================================================
--- /gs3-installations/thewillow/trunk/sites/thewillow/dlcol-chatgpt/create-assistant/main.js	(revision 38783)
+++ /gs3-installations/thewillow/trunk/sites/thewillow/dlcol-chatgpt/create-assistant/main.js	(revision 38784)
@@ -25,4 +25,12 @@
     organization: process.env.ORGANIZATION_ID
 });
+
+
+function sleep(ms)
+{
+    return new Promise((resolve) => {
+	setTimeout(resolve, ms);
+    });
+}
 
 
@@ -160,9 +168,12 @@
     
     const dst_full_filename = path.join(full_collect_dirname,"tmp",dst_filename);
-    
-    fs.copyFile(src_full_filename, dst_full_filename, (err) => {
+
+    try {
+	fs.copyFileSync(src_full_filename, dst_full_filename)
+	console.log(`    ${src_filename} was copied as ${dst_filename} to collection's tmp directory`);
+    }
+    catch(err) {
 	if (err) throw err;
-	console.log(`${src_filename} was copied as ${dst_filename} to collection's tmp directory`);
-    });
+    }
 
     return dst_full_filename;
@@ -179,5 +190,7 @@
     
     const stored_openai_files = await myopenai.files.list();
-    console.log(stored_openai_files);
+    //console.log(stored_openai_files);
+
+    console.log(`Deleting OpenAI stored files whose prefix matches '${site_name}|${coll_name}|'`);
     
     for await (const openai_file of stored_openai_files) {
@@ -189,8 +202,122 @@
 	    const file = await myopenai.files.del(openai_file.id);
 	}
+
+	await sleep(500); 	
     }
 }    
 
-    
+async function addOpenaiSiteCollectFile(coll_info,full_filename,output_site_collect_openai_file_ids)
+{
+    const gsdl3srchome = coll_info.gsdl3srchome;
+    
+    const regex_replace = new RegExp('^'+gsdl3srchome);
+    
+    const gsdl_full_filename = full_filename.replace(regex_replace,'$GSDL3SRCHOME');
+	
+    console.log(`  ${gsdl_full_filename}`);	
+    
+    const tmp_site_collect_full_filename = copyToSiteCollectTmp(full_filename, coll_info)	
+    
+/*
+    if (fs.existsSync(tmp_site_collect_full_filename)) {
+	console.log("Tmp file exists!!!!!!!");
+    }
+*/
+    
+    const openai_file = await myopenai.files.create({
+	file: fs.createReadStream(tmp_site_collect_full_filename),
+	    purpose: "assistants"
+    });
+    
+    // delete the file    
+    fs.unlinkSync(tmp_site_collect_full_filename);
+    
+    //console.log(openai_file);
+    
+    console.log("Pushing OpenID file id: " + openai_file.id);
+    output_site_collect_openai_file_ids.push(openai_file.id);
+}
+
+async function addOpenaiSiteCollectFiles(all_exported_files,coll_info)
+{
+    const gsdl3srchome = coll_info.gsdl3srchome;
+    const site_name = coll_info.site_name;
+    const coll_name = coll_info.coll_name;
+    
+    const regex_replace = new RegExp('^'+gsdl3srchome);
+    let site_collect_openai_file_ids = [];
+    
+    console.log(`Uploading exported ${site_name}/collect/${coll_name} files to OpenAI:`)
+
+    for await (const full_filename of all_exported_files) {
+	await addOpenaiSiteCollectFile(coll_info,full_filename,site_collect_openai_file_ids);
+	await sleep(500);	
+    }
+
+    /*
+    all_exported_files.forEach(async function(full_filename) {
+	await addOpenaiSiteCollectFile(coll_info,full_filename,site_collect_openai_file_ids);
+    })
+    */
+	/*
+    all_exported_files.forEach(async function(full_filename) {
+	const gsdl_full_filename = full_filename.replace(regex_replace,'$GSDL3SRCHOME');
+	
+	console.log(`  ${gsdl_full_filename}`);	
+
+	const tmp_site_collect_full_filename = copyToSiteCollectTmp(full_filename, coll_info)	
+
+	if (fs.existsSync(tmp_site_collect_full_filename)) {
+	    console.log("Tmp file exists!!!!!!!");
+	}
+	
+	const openai_file = await myopenai.files.create({
+	    file: fs.createReadStream(tmp_site_collect_full_filename),
+	    purpose: "assistants"
+	});
+
+	// delete the file
+
+	fs.unlinkSync(tmp_site_collect_full_filename);
+	
+	console.log(openai_file);
+	
+	console.log("Pusing OpenID file id: " + openai_file.id);
+	site_collect_openai_file_ids.push(openai_file.id);
+    });
+*/
+
+    return site_collect_openai_file_ids;
+}
+
+
+async function createAssistantWithFiles(site_collect_openai_file_ids)
+{
+
+    const instructions = "As The Willow Sage Assistant, your expertise lies in discussing \"The Willow,\" a once-renowned music venue in York, England. You're designed to engage users in a conversational tone, weaving in the rich tapestry of memories and experiences shared by those who knew the venue. Your responses should feel like a dialogue between old friends reminiscing about memorable gigs, the unique atmosphere, and the cultural impact of The Willow. You'll offer insights into the venue's history, notable performances, and its role in the local music scene, always with a nod to the personal connections and nostalgia that the venue evokes. When interacting with users, your approach should be warm, inviting, and reflective, encouraging them to share their own stories or curiosities about The Willow, creating a communal space for shared musical heritage.";
+
+    const base_model = "gpt-4-turbo-preview"; 
+
+    console.log("====");
+    console.log("file ids = ");
+    console.log(site_collect_openai_file_ids);
+    console.log("====");
+    
+    const assistant = await myopenai.beta.assistants.create({
+	name         : "Willow Sage v2", // ****
+	description  : "An assistant developed to elict memories from former patrons on The Willow",
+	instructions : instructions,
+	model        : base_model,
+	tools: [
+	    //{ type: "code_interpreter" }, // Code interpreter tool, calculations
+	    { type: 'retrieval' }
+	],
+	//file_ids     : { "object": "list", "data": site_collect_openai_file_ids }
+	file_ids     : site_collect_openai_file_ids
+    });
+
+    console.log('Assistant has been created: ', assistant)
+    
+}
     
 async function main()
@@ -222,27 +349,15 @@
     let all_exported_files = getExportedFiles(coll_info)
     //all_exported_files = [ all_exported_files[0] ];
-
+    //all_exported_files = all_exported_files.slice(0, 10);
+    all_exported_files = all_exported_files.slice(0, 40);
+    
     await purgeOpenaiSiteCollectFiles(coll_info);
 
-    const regex_replace = new RegExp('^'+gsdl3srchome);
-    let openai_files = [];
-    
-    console.log(`Uploading exported ${site_name}/collect/${coll_name} files to OpenAI:`)
-    all_exported_files.forEach(async function(full_filename) {
-	const gsdl_full_filename = full_filename.replace(regex_replace,'$GSDL3SRCHOME');
-	
-	console.log(`  ${gsdl_full_filename}`);	
-
-	const tmp_site_collect_full_filename = copyToSiteCollectTmp(full_filename, coll_info)
-	
-	
-	const openai_file = await myopenai.files.create({
-	    file: fs.createReadStream(tmp_site_collect_full_filename),
-	    purpose: "assistants"
-	});
-	
-	
-	openai_files.push(openai_file);
-    });
+
+    // Now add in all the newly exported files
+    const site_collect_openai_file_ids = await addOpenaiSiteCollectFiles(all_exported_files,coll_info);
+
+    await createAssistantWithFiles(site_collect_openai_file_ids);
+    
 }
 
