Help

Error in code for sending prompts to ChatGPT in a conversation and returning separate results.

Topic Labels: Automations Integrations
607 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Milliemoohippo
4 - Data Explorer
4 - Data Explorer

I'm trying to send data with a series of prompts to chatgpt and get a response of each item back into Airtable. I want each subsequent result to be based on the whole conversation so far. 

When I try and run this as an automation I keep getting a time out error.

Can anyone help?

Here is the code I have:

let table = base.getTable("Brand Guidelines");
let view = table.getView("Grid view");
let records = await view.selectRecordsAsync({ fields: table.fields });
const apiKey = 'OPENAI-API-KEY';
const url = 'https://api.openai.com/v1/chat/completions';

// Set the batch size (adjust as needed)
const batchSize = 10;

for (let i = 0; i < records.records.length; i += batchSize) {
const batch = records.records.slice(i, i + batchSize);

for (let record of batch) {
// Initialize the conversation context
let conversationContext = [];

// Start the conversation with clientDescription
conversationContext.push({
role: "system",
content: "You are assisting a client with the following request:\n" + record.getCellValue("clientDescription"),
});

// Define the prompts and corresponding output fields
const prompts = [
{ prompt: "you are a marketing research assistant use the clientDescription to provide a definition of the target audience", field: "Target_Audience_Definition" },
{ prompt: "now use the previous data in the conversation to create their catalyst moments", field: "Catalyst_Moments" },
{ prompt: "now use the previous data in the conversation to expand on their main problem", field: "Expanded_Main_Problem" },
{ prompt: "now use the previous data in this conversation to define the primary objective of the customer", field: "The_Primary_Objective" },
{ prompt: "now use the conversation data to define their Emotional and Social Desires", field: "Emotional_and_Social_Desires" },
];

// Iterate through prompts
for (let { prompt, field } of prompts) {
// Add user message to the conversation context
conversationContext.push({ role: "user", content: prompt });

// Send conversation context to GPT-3 and get a response
let data = {
messages: conversationContext,
model: 'gpt-3.5-turbo',
temperature: 0.3,
};

let options = {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
},
method: 'POST',
body: JSON.stringify(data),
};

let response = await fetch(url, options);
let json = await response.json();

if (json.choices && json.choices.length > 0) {
// Extract AI response and update the corresponding Airtable field
let AIResponse = json.choices[0].message.content;
await table.updateRecordAsync(record.id, { [field]: AIResponse });

// Add AI response to the conversation context for the next round
conversationContext.push({ role: "assistant", content: AIResponse });
} else {
console.error("Empty or invalid response from the AI model.");
}
}
}

// Sleep for a short period to avoid exceeding Airtable's execution time limit
await new Promise(resolve => setTimeout(resolve, 1000));
}

0 Replies 0