Last active
February 26, 2023 04:22
-
-
Save SourceCode/7fa03900cfb3b363f5b379b3d54779fc to your computer and use it in GitHub Desktop.
ChatGPT prompt to JSON output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
YOUR_OPENAI_API_KEY_HERE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
"Silphium terebinthinaceum", | |
"Rumex sagittatus", | |
"Rumex conglomeratus", | |
"Rumex dentatus", | |
"Rosa dumalis" | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Importing the "axios" and "fs" modules | |
const axios = require('axios'); | |
const fs = require('fs'); | |
// Creating an instance of Axios with a "Bearer" token for authorization | |
const client = axios.create({ | |
headers: { | |
Authorization: `Bearer ${fs.readFileSync('apikey.txt', 'utf-8').trim()}` | |
} | |
}); | |
// Setting the URL for the OpenAI API and loading a list of plant names from a JSON file | |
const ModelURL = 'https://api.openai.com/v1/completions'; | |
// Load plants.json - a JSON array of strings of binomials | |
const plantNames = JSON.parse(fs.readFileSync('plants.json', 'utf-8')); | |
// Iterating through the list of plant names with a forEach loop | |
plantNames.forEach(async (name) => { | |
try { | |
// Creating a filename for the current plant's data file, and a prompt for the API request | |
const filename = `./data/${name.toLowerCase().replace(' ', '_')}.json`; | |
// The prompt that uses the binomial and the model we wish to have results returned in JSON | |
const prompt = `provide me the data for the plant ${name}, using this schema below and respond with only JSON | |
type ImageMedia | |
{ | |
image: String! | |
altText: String! | |
order: Int! | |
} | |
type ReferenceLink | |
{ | |
url: String! | |
title: String! | |
order: Int! | |
} | |
type PlantSize | |
{ | |
minCM: Int! | |
maxCM: Int! | |
} | |
type Plant | |
{ | |
id: ID! | |
primaryCommonName: String! | |
commonNames: [String] | |
scientificName: String | |
description: String | |
habit: String | |
plantType: String | |
kingdom: Kingdom! | |
phylum: String | |
class: String | |
order: String | |
family: String | |
genus: String | |
species: String | |
cultivar: Cultivar | |
description: String | |
images: [ImageMedia!] | |
growthHabit: String | |
toxicity: String | |
cultivation: String | |
distribution: String | |
habitat: String | |
physicalManagement: String | |
biologicalManagement: String | |
cultivationOptions: String | |
edible: Boolean | |
heightCM: PlantSize | |
widthCM: PlantSize | |
referenceLinks: [ReferenceLink] | |
conservationStatus: ConservationStatus! | |
growthRate: String | |
lifespan: String | |
temperatureRange: String | |
lightRange: String | |
phRange: String | |
hardnessRange: String | |
placement: String | |
size: String | |
depth: String | |
substrate: String | |
} | |
`; | |
// Checking if the data file already exists | |
if (fs.existsSync(filename)) { | |
console.log(`File exists: ${name}`); | |
} else { | |
// If the data file does not exist, sending a request to the OpenAI API with the prompt and some parameters | |
console.log(`Processing: ${name} - writing: ${filename}`); | |
const params = { | |
prompt, | |
model: 'text-davinci-003', // Use GPT3 | |
max_tokens: 1200, // Use a lot of tokens :-x | |
temperature: 0 | |
}; | |
// Sending a POST request to the API with the parameters, and writing the result to a file | |
await client | |
.post(ModelURL, params) | |
.then((result) => { | |
console.log(`- Processed: ${name}`); | |
fs.writeFileSync(filename, result.data.choices[0].text); | |
}) | |
.catch((err) => { | |
console.log(err); | |
}); | |
} | |
} catch (error) { | |
// Handling errors that may occur during the process | |
console.error(`Error getting data for ${name}`); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To run:
node QueryChatGPT.js