// Example script to show how you can connect to the Open AI GPT API and use the API to help you with your PPC tasks // // (c) Nils Rooijmans - https://www.linkedin.com/in/nilsrooijmans/ // // // WANT TO LEARN HOW TO USE CHATGPT TO CREATE SCRIPTS FROM SCRATCH? // SIGN UP FOR MY WORKSHKOP: https://nilsrooijmans.com/5-day-chatgpt-and-google-ads-scripts-challenge/ function main() { var systemPrompt = "You are a senior Google Ads consultant with excellent skills in creating and optimizing Google Ads campaigns."; var userPrompt = "Please provide keyword suggestions for a high end yoga resort that targets lawyers and doctors in the US."; var gptResponse = getGPTResponse(systemPrompt, userPrompt); console.log(gptResponse); } // START OF GPT CODE // 1. Set API endpoint const ENDPOINT_URL = 'https://api.openai.com/v1/chat/completions'; // 2. Insert API key, get your key at https://platform.openai.com/account/api-keys const CHAT_GPT_API_KEY = ''; // 3. Choose your model, see details https://platform.openai.com/docs/models/overview // Possible models: gpt-4, gpt-4-0613, gpt-4-32k, gpt-4-32k-0613, gpt-3.5-turbo, gpt-3.5-turbo-0613, gpt-3.5-turbo-16k, gpt-3.5-turbo-16k-0613 const GPT_MODEL = 'gpt-4'; // 4. Set temperature, What sampling temperature to use, between 0 and 2. Default is 1. // Higher values like 1.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. const TEMPERATURE = 1; // 5. Set nucleus sampling. The model considers the results of the tokens with top_p probability mass. // So 0.1 means only the tokens comprising the top 10% probability mass are considered. Default is 1. const TOP_P = 1; // For more details on the parameter options, visit https://platform.openai.com/docs/api-reference/chat/create function getGPTResponse(systemPrompt, userPrompt) { try { const messages= [ {"role": "system", "content": systemPrompt}, {"role": "user", "content": userPrompt} ]; const headers = { "Content-Type": "application/json", "Authorization": `Bearer ${CHAT_GPT_API_KEY}` }; const payload = { "model": GPT_MODEL, "messages": messages, "temperature": TEMPERATURE, "top_p": TOP_P }; const httpOptions = { "method" : "POST", "muteHttpExceptions": true, "headers" : headers, 'payload': JSON.stringify(payload) }; const response = JSON.parse(UrlFetchApp.fetch(ENDPOINT_URL, httpOptions)); const responseContent = response.choices[0].message.content; //console.log("responseContent = "+responseContent); return responseContent; } catch (e) { console.log("### ERROR: some error occured. Please check and try again.\n "+e); } }