Claude3 API help requested (javascript)
Hey Team
I really need some coding help.
Specifically does anyone know how to use fetchURL to call the Anthropic API ??
I have this code (below) that I use in a Google Apps/Ads Script for OpenAI & would love to be able to offer users the option of using Claude3 (& Gemini) instead of just OpenAI
The 'app' such that it is, is a Google Sheet that has a tab for settings & the output
(the script runs inside a google ads account - but doesn't actually pull data from that account - it pulls it from another sheet in the settings)
I'm really just using google ads as my environment to run the code!
I wanted to show marketers how easy it can be to put these AI building blocks together!
actually maybe that's a YT idea for you??? To create API calls from inside a google apps script, so people can call these models using spreadsheets (which they're more familiar with)??
Any help very happily received!
Thanks
Mike
PS - posting screenshot of settings tab as it's the closest thing to an 'app' I have!
-----
Code: (existing OpenAI API call inside a function)
function generateTextOpenAI(prompt, data, apiKey, model) {
Logger.log('Generating report with OpenAI');
let messages = [
{ "role": "user", "content": prompt }
];
let payload = {
"model": model,
"messages": messages
};
let httpOptions = {
"method": "POST",
"muteHttpExceptions": true,
"contentType": "application/json",
"headers": {
"Authorization": 'Bearer ' + apiKey
},
'payload': JSON.stringify(payload)
};
let response = UrlFetchApp.fetch(url, httpOptions);
let startTime = Date.now();
while (response.getResponseCode() !== 200 && Date.now() - startTime < 30000) {
Utilities.sleep(5000);
response = UrlFetchApp.fetch(url, httpOptions);
Logger.log('Time elapsed: ' + (Date.now() - startTime) / 1000 + ' seconds');
}
if (response.getResponseCode() !== 200) {
Logger.log('Error: OpenAI API request timed out after 30 seconds. Please try again.');
return;
}
let responseJson = JSON.parse(response.getContentText());
let choices = responseJson.choices;
let text = choices[0].message.content;
return (text);
}
1
5 comments
Mike Rhodes
2
Claude3 API help requested (javascript)
AI Developer Accelerator
skool.com/ai-developer-accelerator
Master AI & software development to build apps and unlock new income streams. Transform ideas into profits. 💡➕🤖➕👨‍💻🟰💰
Leaderboard (30-day)
Powered by