You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constfs=require('fs');constopenai=require('openai');// Import the OpenAI API packageconstopenaiApiKey='YOUR_API_KEY';// Replace this with your actual API keyconstmodel='text-davinci-002';// Replace this with the desired GPT-3 modelconstinputFilePath='/path/to/your/color-names.csv';// Replace this with the path to your actual CSV fileconstoutputFilePath='/path/to/your/descriptions.csv';// Replace this with the desired output file pathconstcsvData=fs.readFileSync(inputFilePath,'utf-8');// Read the CSV fileconstcolorNames=csvData.split('\n');// Split the CSV data into an array of color names// Initialize the OpenAI API with your API keyopenai.api_key=openaiApiKey;constdescriptions=[];// Iterate through each color name and generate a description using the OpenAI APIasyncfunctiongenerateDescriptions(){for(leti=0;i<colorNames.length;i++){constcolorName=colorNames[i].trim();// Generate a description for the color name using the OpenAI API and the GPT-3 modelconstresponse=awaitopenai.complete({engine: model,prompt: `Describe the color "${colorName}" in a single sentence.`,max_tokens: 50,temperature: 0.5,n: 1,stop: '\n',});constdescription=response.data.choices[0].text.trim();descriptions.push(description);}// Write the descriptions to a new CSV fileconstoutputData=descriptions.join('\n');fs.writeFileSync(outputFilePath,outputData,'utf-8');}generateDescriptions();
The text was updated successfully, but these errors were encountered:
constopenai=require('openai')('your-api-key-here');constcsv=require('csv-parser');constfs=require('fs');constinputFilePath='input.csv';constoutputFilePath='output.csv';constcategories=['nature','technology','popculture','gaming','music','fantasy','sci-fi','history','horror','joy','feminine','masculine'];constresults=[];constprocessRow=(row)=>{constname=row.name.trim();openai.complete({engine: 'text-davinci-002',prompt: `Generate tags for the color name "${name}". Categories: ${categories.join(', ')}`,maxTokens: 50,n: 1,stop: '\n',temperature: 0.7,presencePenalty: 0.3,frequencyPenalty: 0.3,}).then((res)=>{consttags=res.data.choices[0].text.trim().split(',').map((t)=>t.trim());results.push({ name, tags });if(results.length===count){saveResults();}}).catch((err)=>{console.error(err);});};constsaveResults=()=>{conststream=fs.createWriteStream(outputFilePath,{flags: 'a'});stream.write(`name,tags\n`);results.forEach((r)=>{consttagsString=r.tags.join(';');stream.write(`${r.name},${tagsString}\n`);});stream.end();};fs.createReadStream(inputFilePath).pipe(csv()).on('data',processRow).on('end',()=>{console.log('Finished processing all rows.');});
meodai
changed the title
Use OpenAI to write a description for every single color
Use AI to write a description for every single color
Dec 19, 2023
The text was updated successfully, but these errors were encountered: