Hi,
ChatGPT crafted this code but I am getting an error trying to receive response from within AppsScript function:
function getImprovedTitle(text) {
var apiKey = "MY API KEY";
var assistantId = "MY-ASSITANT-ID";
var data = {
"messages": [
{"role": "system", "content": "This is a system message to start the conversation."},
{"role": "user", "content": text}
]
};
var options = {
"method" : "post",
"contentType": "application/json",
"headers": {
"Authorization": "Bearer " + apiKey
},
"payload": JSON.stringify(data),
"muteHttpExceptions": true
};
try {
var url = "(hypertext)api.openai(DOTCOM)/v1/assistants/" + assistantId + "/messages";
var response = UrlFetchApp.fetch(url, options);
var responseData = JSON.parse(response.getContentText());
if (response.getResponseCode() == 200) {
var messages = responseData.data.messages;
var lastMessage = messages[messages.length - 1];
return lastMessage.content; // Assuming the last message is the response from the assistant.
} else {
return "API request failed with response: " + response.getContentText(); // Show full error message
}
} catch (e) {
Logger.log(e.toString());
return "Error: Could not retrieve an improved title due to an exception: " + e.toString();
}
}
The error I receive in response is the following:
“API request failed with response: {
““error””: {
““message””: ““Invalid URL (POST /v1/assistants/345345/messages)””,
““type””: ““invalid_request_error””,
““param””: null,
““code””: null
}
}”
Please note that var url = "(hypertext) /(DOTCOM) is deliberately changed because no links are allowed in chat.
Can you please tell me what is wrong with the call - ideally rewrite it ?
Thanks!