is-string-ai
Version:
An AI-powered package to determine if an input is a string.
44 lines (37 loc) • 1.43 kB
JavaScript
// index.js
require('dotenv').config();
const axios = require('axios');
// Main function that uses OpenAI to "detect if input is a string"
async function isStringAI(input) {
try {
// Check for missing API key
if (!process.env.OPENAI_API_KEY) {
throw new Error("Missing OpenAI API key in environment variables.");
}
// Use OpenAI API with a prompt that humorously checks if input is a string
const response = await axios.post(
'https://api.openai.com/v1/completions',
{
model: "gpt-3.5-turbo",
prompt: `Is the following input a string? Answer with only "true" or "false". Input: ${JSON.stringify(input)}`,
max_tokens: 5,
temperature: 0.3,
},
{
headers: {
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
'Content-Type': 'application/json',
},
}
);
// OpenAI's response text
const aiResponse = response.data.choices[0].text.trim();
// Parse the response as a Boolean and return it
return aiResponse.toLowerCase() === 'true';
} catch (error) {
console.error("Error with isStringAI:", error);
return false;
}
}
// Export the function
module.exports = isStringAI;