Assistant on php backend access

Hey there, I got that code in my backend and conncetion to gpt-3 is working fine. But here is the issue to connect it with the assistant. Has anyone experience in that? What is missing?

$url = 'https://api.openai.com/v1/assistants/asst_dxxxxxxxxxxxxxxxxxxxxxxxxx/messages';
$postData = [
    'model' => "gpt-4", // Verwendung von GPT-4
    'messages' => [
        [
            // Setzen eines Kontexts, der den Assistenten "Sena" einleitet
            "role" => "system",
            "content" => "Du sprichst jetzt mit Sena, deinem virtuellen Assistenten.",
        ],
        [
            "role" => "user",
            "content" => $message,
        ]
    ]
];


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer ' . $apiKey;

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

return json_decode($response, true);

}

Thanks in advance

You are a very long way away from being able to interact with an assistant with that code. You have a chat completion request there. The only thing that would indicate differently is that you added an unused line at the start with a non-existent URL.

Are you actually trying to use the new “assistants” framework on the API? That takes much more complex code.

Or is it just that the upgrade to gpt-4 in code is not working for you because you haven’t funded your API account with prepaid credits to unlock gpt-4 models yet?

thanks for your aswer. Yes I will try the new assistants framework and the money isnt the problem here. I made some modifications but i got that error notification:

{“error”:{“message”:“Invalid URL (POST /v1/assistants/asst_dZfQzgz7oVcKJcDXDg0kXRTn/messages)”,“type”:“invalid_request_error”,“param”:null,“code”:null}}

i will check that URL. Thank you

No, using “assistants” is FAR FAR beyond just changing some URL.

You create the assistant, create threads, create messages with threads, create runs, monitor run steps, monitor for completion or an AI request for an external function, track IDs of all parts used…All different API calls that must be coordinated.

Documentation: https://platform.openai.com/docs/assistants/overview

2 Likes

Just adding on here what others said, you can’t just swap urls, the assistant api is quite a bit different. Also, (yet to be totally understood) could be a lot more expensive. Also, i’m all for baking your own, but if you’re using php you might want to check out GitHub - openai-php/client: ⚡️ OpenAI PHP is a supercharged community-maintained PHP API client that allows you to interact with OpenAI API. so you don’t have to bother with all the extra stuff and concentrate on learning/working with the api structure.

1 Like

Thank you Mr. Watson.
That github link might be interessting indeed. I will check that out.

Thank you! I see its far more than that and I changed the code and got no that notification, that status is qeued,…

I guess it will work now.

As I see, there are two ways to create an assistant: dynamically or calling an assistant with a given ID. What would you suggest to use?

Unless you need lots of assistants it is easiest to create one in the playground and grab its id and use that in your code.