Assistant API and getting result using PHP & CURL

Hi,
I’m trying to get a result from an Assistant API via PHP using CURL
I’ve manged to add a message and create a run. The last response from a run command has ’ [status] => completed’ in it but the response is not appended to the end of it.
The end of the response is has [first_id] => run_ktDR3igEM42KeoxPZCqo1jCM [last_id] => run_ktDR3igEM42KeoxPZCqo1jCM [has_more] => ) at the end.
How do I get the result back from the API?
I’ve tried:
$apiResponseURL = ‘https://api.openai.com/v1/threads/{$threadId}/messages’;
$apiResponse = makeCurlRequest($pollUrl, ‘GET’, , $apiKey);

But again I only get up to [has_more] => , no results.

<?php

function makeCurlRequest($url, $method, $apiKey, $data = null) {
    $ch = curl_init();

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

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    if ($method === 'POST') {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    } elseif ($method === 'GET') {
        curl_setopt($ch, CURLOPT_HTTPGET, true);
    }

    $response = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    if ($httpcode !== 200) {
        // Handle error here
        return ['error' => 'Request failed with status code ' . $httpcode, 'response' => $response];
    }

    return json_decode($response, true);
}

$apiKey = 'your_api_key';
$threadId = 'your_thread_id';
$runId = 'your_run_id';

// Poll the messages endpoint to get the results
$apiResponseURL = "https://api.openai.com/v1/threads/{$threadId}/messages";
$apiResponse = makeCurlRequest($apiResponseURL, 'GET', $apiKey);

if (isset($apiResponse['error'])) {
    echo "Error: " . $apiResponse['error'];
    var_dump($apiResponse['response']);
} else {
    // Check if the response contains the desired information
    if (isset($apiResponse['messages']) && !empty($apiResponse['messages'])) {
        foreach ($apiResponse['messages'] as $message) {
            // Check for the completed run message
            if ($message['id'] === $runId && isset($message['status']) && $message['status'] === 'completed') {
                echo "Response: " . json_encode($message['content'], JSON_PRETTY_PRINT);
                break;
            }
        }
    } else {
        echo "No messages found or run is not completed.";
    }
}

From GPt-4o…

1 Like

Hi,

Thanks dude, almost correct got me most of the way.

Reported ‘No messages found or run is not completed.’ but the result (SELECT * FROM products;') is in the response.

‘[role] => assistant [content] => Array ( [0] => Array ( [type] => text [text] => Array ( [value] => SELECT * FROM products;’

Thanks.

I believe you can still check threads in Playground? Might take a look and see if you’re getting back what you’re expecting? Good luck…

2 Likes

Hi,
Yes all is good.
Plenty python examples but PHP seems much more rare. There is a GitHub repository to help but I like knowing how the nuts and bolts work. Your CURL function was very similar to mine so didn’t need much adjusting.
Thanks again.

1 Like

Cool deal. Hope you stick around. We could use more PHP devs. :wink:

We’ve got a great dev community garden growing here…

Hi,

Indeed I fully agree.

AI has already been used extensively in the creative arts side to create text/marketing but now I can ask my database to create more customer, product and order information and statistics than was ever possible unless you live and breath SQL.

Sales guys can now ask for anything to help locate an order, product or customer.

Our E-commerce site is all PHP as are most i would have thought.

I can now ask for individual product sales data with respect to customer location, date etc.

I’m an old assembler and C++ programmer but even I got out my pit to learn ‘new’ tech like API calls and JSON formatting.

I’m so old I still own an ZX80- and can read morse code at 30 words a minute but even I can see the impending revolution AI is about to create…

ChatGPT not too reliable for code suggestions on the assistants api yet it seems as the tech is all so new.

Peace, love and bananas.

2 Likes