documentation icon indicating copy to clipboard operation
documentation copied to clipboard

automaSetVariable() fails to add data to Automa from Javascript

Open sparkls0 opened this issue 9 months ago • 0 comments

I tried doing an API call to GPT using only Javascript, I know that in itself it works, I can see in console the result, but automaSetVariable fails to give the data to Automa

Yet, in the data, there is nothing special

console shows me ID and Response extracted from GPT's initial JSON's response :

ID: chatcmpl-9UHmQkKsB4BPkPNbqfLjPnmTDYWrK VM616:110 Response: Hello! How can I help you today?

here is the code

let model = automaRefData('variables', 'model');       
let message = automaRefData('variables', 'message'); 

console.log(api_key);
console.log(model);
console.log(message);
let gpt_id, gpt_response;

async function sendMessageToGPT(api_key, model, message) {
    let url = "https://api.openai.com/v1/chat/completions";
    
    let data = {
        model: model,
        messages: [{ role: "user", content: message }],
    };
    
    let headers = {
        "Content-Type": "application/json",
        "Authorization": `Bearer ${api_key}`
    };
    
    try {
        let response = await fetch(url, {
            method: "POST",
            headers: headers,
            body: JSON.stringify(data)
        });
        
        if (!response.ok) {
            throw new Error(`Error: ${response.statusText}`);
        }
        
        let responseData = await response.json();
        
        gpt_id = responseData.id;
        gpt_response = responseData.choices[0].message.content;
        
        console.log("ID:", gpt_id);
        console.log("Response:", gpt_response);
        
    } catch (error) {
        console.error("Error:", error);
    }
}
automaSetVariable('gpt_id', gpt_id);
automaSetVariable('gpt_id', gpt_id);
automaSetVariable('gpt_response', gpt_response);
sendMessageToGPT(api_key, model, message);
automaNextBlock();

sparkls0 avatar May 29 '24 17:05 sparkls0