Last active
August 22, 2024 14:31
-
-
Save stevermeister/00587c8641facd5f78d387a6e1789216 to your computer and use it in GitHub Desktop.
code provided for this video - https://youtu.be/XZJ_C3j8_WI. (How to Integrate ChatGPT with Google Sheets Using Custom Functions)
This file contains hidden or 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
function AI(prompt, cellData) { | |
// Add a system prompt to ensure output is specific and concise | |
var systemPrompt = "You are an AI assistant that provides very specific responses. Your outputs are meant to be placed directly into a spreadsheet cell. If the request is for a number, output just the number with no additional text or explanation. Be as concise as possible."; | |
// Combine the system prompt with the user prompt and cell data | |
var fullPrompt = systemPrompt + "\n\n" + prompt; | |
if (cellData !== undefined && cellData !== null && cellData !== "") { | |
fullPrompt += ": " + cellData; | |
} | |
// Set up the request to the OpenAI API | |
var response = UrlFetchApp.fetch("https://api.openai.com/v1/completions", { | |
method: "post", | |
contentType: "application/json", | |
headers: { | |
"Authorization": "Bearer " + OPENAI_API_KEY | |
}, | |
payload: JSON.stringify({ | |
model: "gpt-3.5-turbo-instruct", // Updated model | |
prompt: fullPrompt, | |
max_tokens: 100, // Adjust the number of tokens based on your needs | |
temperature: 0.7 // Controls creativity; adjust as needed | |
}) | |
}); | |
// Parse the API response | |
var json = response.getContentText(); | |
var data = JSON.parse(json); | |
var result = data.choices[0].text.trim(); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment