Hi, I am using the below code to point to a custom assistant that I created.
I can’t seem to get the code to actually give me results based on the assistant, it just connects me to a normal chatbot
I tried multiple variations of the code, but for the life of me, I can’t seem to get it to work. Please can someone assist
if (!$api_key) {
echo json_encode(['error' => 'API key is missing.']);
exit;
}
// User input from the front end
$user_input = trim($_POST['message'] ?? '');
// Ensure the user input is not empty
if (empty($user_input)) {
echo json_encode(['error' => 'No message provided.']);
exit;
}
// OpenAI API endpoint
$url = 'https://api.openai.com/v1/chat/completions';
// Set up the request headers
$headers = [
'Content-Type: application/json',
'Authorization: ' . 'Bearer ' . $api_key,
];
// Set up the request body
$data = [
'model' => 'gpt-4o-mini', // Adjust the model if necessary
'messages' => [
['role' => 'system', 'content' => 'You are Assistant ID: {assistant_id}. Behave accordingly. Restrict all answers to the Assistant and do not answer any other questions'],
['role' => 'user', 'content' => $user_input]
],
];
// Initialize cURL
$ch = curl_init($url);
// Set cURL options
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
// Execute the API request
$response = curl_exec($ch);
// Check for cURL errors
if ($response === false) {
echo json_encode(['error' => 'Request Error: ' . curl_error($ch)]);
curl_close($ch);
exit;
}
// Close the cURL session
curl_close($ch);
// Decode the API response
$response_data = json_decode($response, true);
// Check for API errors in the response
if (isset($response_data['error'])) {
echo json_encode(['error' => 'API Error: ' . $response_data['error']['message']]);
exit;
}
// Validate the response structure
if (isset($response_data['choices']) && isset($response_data['choices'][0]['message']['content'])) {
$bot_reply = $response_data['choices'][0]['message']['content'];
$formatted_reply = nl2br(htmlspecialchars($bot_reply)); // Sanitize and format the response
echo json_encode(['reply' => $formatted_reply]);
} else {
echo json_encode(['error' => 'Invalid response structure from API.']);
}
?>
Problem is, I have the assistant ID in the $data section, but it doesn’t seem to like that. I also tried adding the assistant ID to the $headers but that failed.
I am now trying to pass it in the $url as you can see, but that still doesnt work
I’m just reading the manual here and trying to push this forward , I will sit down later and have another look in a few hours if you still don’t have a fix.
Still struggling with this thing. Been staring at the API docs for a full day and I can’t seem to find any reference to the assistant_id being required in this process.
Updated the function and even asked chatgpt but it still throws out: ‘missing required parameters: assistant_id’
Its definitely getting the right assistant ID back and storing it in the session, so not sure
Ok so just an update here. I can now get my messages to attach to the sessions thread, but Its not sending me back a response, which is extremely weird.
It displays the User input on the thread, but n0o assistant responses