Hi, I am trying to call openAI's API for an Assistant I build, but constantly get this redirect error:
O: Error: Response status was 307 'Temporary Redirect' but redirect mode was set to 'error'
at queryAssistant on line 11 at processAndUpdateRecords on line 51 at async main on line 60
The code is as follows
(line 11 is: let response = await remoteFetchAsync('https://api.openai.com/v1/assistants/asst_My_ID/messages', {
51: const aiResponse = await queryAssistant(domain);
60: await processAndUpdateRecords();)
// Define the API key and model
let apiKey = 'API_Key'; // Replace with your actual API key
let model = 'gpt-4-1106-preview'; // Replace with your chosen model
// Function to query the Assistant AI
async function queryAssistant(domain) {
let response = await remoteFetchAsync('https://api.openai.com/v1/assistants/asst_My_ID/messages', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'OpenAI-Beta': 'assistants=v1',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: model,
prompt: domain,
max_tokens: 200 // Adjust as needed
})
});
if (!response.ok) {
console.error("API request failed:", response.status, response.statusText);
return "API request failed";
}
let data = await response.json();
if (data.choices && data.choices.length > 0) {
return data.choices[0].text;
} else {
console.error("No response from AI:", data);
return "No response from AI";
}
}
// Function to process records and update Airtable
async function processAndUpdateRecords() {
const table = base.getTable("AccountsStaging"); // Replace with your table name
const queryResult = await table.selectRecordsAsync({fields: ["Domain", "Process", "GPT Crypto Exposure"]});
const records = queryResult.records;
for (let record of records) {
if (record.getCellValue("Process") && record.getCellValue("Domain")) {
const domain = record.getCellValue("Domain");
const aiResponse = await queryAssistant(domain);
await table.updateRecordAsync(record.id, {
"GPT Response": aiResponse
});
}
}
}
await processAndUpdateRecords();
XXXXXXXXXXX
Tried different code versions (like leaving out "messages" in the response URL), but never got beyond this error. Any help would be highly appreciated. Thanks.