Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automaSetVariable() fails to add data to Automa from Javascript #35

Open
sparkls0 opened this issue May 29, 2024 · 0 comments
Open

automaSetVariable() fails to add data to Automa from Javascript #35

sparkls0 opened this issue May 29, 2024 · 0 comments

Comments

@sparkls0
Copy link

sparkls0 commented May 29, 2024

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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant