Hello! I’m developing a chatbot based on an API assistant previously created via the OpenAI platform. I only use PHP, Javascript, html and css. No Python. I can communicate with an instance of chatGPT, but not with “my” API Assistant. Note that my assistant uses gpt-4-turbo. How do I target it?
Here’s my PHP code:
<?php
require 'config.php';
header('Content-Type: application/json');
function callAPI($model, $messages) {
$url = 'https://api.openai.com/v1/chat/completions';
$data = [
'model' => $model,
'messages' => $messages
];
$headers = [
"Content-Type: application/json",
"Authorization: Bearer " . API_KEY
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (!$response || $httpcode != 200) {
http_response_code($httpcode ?: 500);
echo json_encode(['error' => 'API Error: ' . curl_error($ch)]);
curl_close($ch);
exit;
}
curl_close($ch);
return $response;
}
$data = json_decode(file_get_contents('php://input'), true);
if (!isset($data['prompt'])) {
http_response_code(400);
echo json_encode(['error' => 'Missing prompt']);
exit;
}
$prompt = htmlspecialchars($data['prompt'], ENT_QUOTES, 'UTF-8');
$response = callAPI('gpt-4-turbo', [
["role" => "system", "content" => "Vous êtes un professeur d'histoire."],
["role" => "user", "content" => $prompt]
]);
echo $response;
?>
Here’s an overview of Assistants…
https://platform.openai.com/docs/assistants/overview
You won’t be able to directly connect with a ChatGPT Custom GPT via the Assistants API, though. You need to recreate it via Assistants.
Potential Issues
- Calling the chat API here:
-
'https://api.openai.com/v1/chat/completions'
-
Instead point this to assistant API https://api.openai.com/v1/assistants
-
Start Here Assistant Creation or Skip to Thread : Link
- Remember to include the assistant ID, not present in your current call :
- GET
https://api.openai.com/v1/assistants/{assistant_id}
- Reference Only Code by GPT:
<?php
require 'config.php';
header('Content-Type: application/json');
function callAPI($endpoint, $data) {
$url = 'https://api.openai.com/v1/' . $endpoint;
$headers = [
"Content-Type: application/json",
"Authorization: Bearer " . API_KEY
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (!$response || $httpcode != 200) {
http_response_code($httpcode ?: 500);
echo json_encode(['error' => 'API Error: ' . curl_error($ch)]);
curl_close($ch);
exit;
}
curl_close($ch);
return json_decode($response, true);
}
$data = json_decode(file_get_contents('php://input'), true);
if (!isset($data['prompt'])) {
http_response_code(400);
echo json_encode(['error' => 'Missing prompt']);
exit;
}
$prompt = htmlspecialchars($data['prompt'], ENT_QUOTES, 'UTF-8');
// Create a thread
$threadData = ['model' => 'gpt-4-turbo', 'assistant_id' => 'your-assistant-id'];
$threadResponse = callAPI('threads', $threadData);
$threadId = $threadResponse['data']['id'];
// Add message to the thread
$messageData = [
'thread_id' => $threadId,
'messages' => [
["role" => "system", "content" => "Vous êtes un professeur d'histoire."],
["role" => "user", "content" => $prompt]
]
];
$messageResponse = callAPI('threads/' . $threadId . '/messages', $messageData);
// Run the thread
$runData = ['thread_id' => $threadId];
$runResponse = callAPI('threads/' . $threadId . '/run', $runData);
// Retrieve messages from the thread
$retrieveData = ['thread_id' => $threadId];
$retrieveResponse = callAPI('threads/' . $threadId . '/messages', $retrieveData);
echo json_encode($retrieveResponse);
?>
1 Like
I knew this. Maybe I did not express myself correctly…
Thank you anyway !
Mmm, a step forward. But I still have an error in my console :
Failed to load resource: the server responded with a status of 400 ()
This is certainly because I don’t know where should I include the assistant ID ? I mean where to add the following line of code
GET https://api.openai.com/v1/assistants/asst_b2bZI9WQu1l6c5sDCdvye7z0
I put the assistant ID in this bloc of code too :
// Create a thread
$threadData = ['model' => 'gpt-4-turbo', 'assistant_id' => 'asst_b2bZI9WQu1l6c5sDCdvye7z0'];
$threadResponse = callAPI('threads', $threadData);
$threadId = $threadResponse['data']['id'];
Is this correct ?
I really appreciate a lot you help !
Thank you so much.
Thank you ‘Engagepy’ !
I’ll try to do my best, but as I’m not developper, I need some luck
1 Like
Your articulation is good, just put GPT to the task. Best of luck !