Problem connecting Assistant API to website

Hi !
I want to connect my assistant created on assisstant api to my website.
My code is :

ChatGPT Interface #submitButton { padding: 10px 15px; background-color: #4CAF50; color: white; border: none; cursor: pointer; }
    #inputText, #outputText {
        width: 100%;
        padding: 10px;
        margin: 10px 0;
        display: inline-block;
        border: 1px solid #ccc;
        box-sizing: border-box;
    }
</style>
Envoyer
<script>
document.addEventListener("DOMContentLoaded", function() {
    const inputBox = document.getElementById("inputText");
    const submitButton = document.getElementById("submitButton");
    const outputBox = document.getElementById("outputText");

    submitButton.addEventListener("click", function() {
        const userInput = inputBox.value;
        sendToChatGPT(userInput).then(response => {
            outputBox.value = response;
        });
    });
});

function sendToChatGPT(userInput) {
const openAIKey = 'apikey';


const url = `https://api.openai.com/v1/assistants/asst_id/`;

const headers = {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${openAIKey}`,
    'OpenAI-Beta': 'assistants=v1'
};

   const body = JSON.stringify({
    "model": "gpt-3.5-turbo",
    "messages": [
        {"role": "user", "content": userInput}
    ]
});





return fetch(url, { method: 'POST', headers: headers, body: body })
    .then(response => response.json())
    .then(data => {
        
        if (data.choices && data.choices.length > 0) {
            return data.choices[0].message.content;
        } else {
            console.error('Réponse inattendue ou absence de choix dans les données:', data);
            return "La réponse de l'API ne contient pas les données attendues.";
        }
    })
    .catch(err => {
        console.error('Erreur lors de la communication avec l\'API:', err);
    });

}

But, I get this error : 1 validation error for Request↵body -> messages↵ extra fields not permitted (type=value_error.extra Do you have any leads ? Thanks a lot !

I haven’t messed with Assistants API but,
looking at the URL you’re calling,
its the endpoint for Retrieve Assistant,
which looks like getting information about the assistant with that id,
and if you look at the docs for it,
there is no parameter for anything called “messages”

API Reference - OpenAI API

that’s also incredibly insecure to leave your api keys on web server. you need some kind of proxy.

Please don’t hardcode your openAi key and store it in your code. You should use an environment variable instead.

Thanks a lot ! Of course I have a backend server for the API key but it was just more simple for this demonstration. The problem is : when I remove the section “messages”: [
{“role”: “user”, “content”: userInput} : my website connect successfully with the assistant because I get in the error area the name and description of the assistant. I also tried "prompt’ : userInput instead of “messages”. I can’t find anything in the documentation. Please let me know if you have something. Thank you for your time !

Are you swapping out asst_id with some other value? Or is asst_id literally the id of your assistant?
Anyway, to rephrase what I said earlier, the endpoint at the url you are calling

https://api.openai.com/v1/assistants/{assistant_id}

seems designed to return data about an assistant. Not an endpoint where you can pass messages. I think, if you just want a prompt/completion, then the Assistant prodcut is overkill, maybe start with just the completion endpoint (although it will be shut off in a few days, but it is much simpler to learn).

But, I think that is why you’re getting an error, and the error goes away when you delete the messages part of your request. The endpoint is not expecting anything called Messages.

Hi, ia.thiollier !
were you able to solve your connection problem with the API wizard?
because I’m having a similar problem?

Thank you for your reply