You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

70 lines
1.7 KiB

import { readDir, BaseDirectory } from '@tauri-apps/plugin-fs';
import { resourceDir } from '@tauri-apps/api/path';
export async function processRawFiles(folder, outputFolder){
// const resourceDirPath = await resourceDir();
// console.log("Resource Directory Path:", resourceDirPath);
// return;
const files = await readDir(folder, { dir: BaseDirectory.Resource });
console.log("Files in raw folder:", files);
for(const file of files){
const res=await threadJsonFileAgent(file.path);
// save to filesystem
console.log("Agent response for", file, ":", res);
}
}
export async function threadJsonFileAgent(filePath){
let system_prompt;
const res=await fetch('agent.txt');
if(res.ok){
system_prompt=await res.text();
} else {
console.error("Failed to load agent.txt");
return;
}
let content;
const fileRes=await fetch(filePath);
if(fileRes.ok){
const fileText=await fileRes.text();
content=fileText;
} else {
console.error("Failed to load JSON file");
return;
}
// use openai agent to process json file
const response=await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${import.meta.env.VITE_OPENAI_API_KEY}`
},
body: JSON.stringify({
model: "gpt-4-0613",
messages: [
{role: "system", content: system_prompt},
{role: "user", content: content}
],
})
});
const data = await response.json();
console.log(data);
return data;
}