I have an assistant configured with openai api v2 using gpt-4o. The assistant is linked to a vector store containing pdf documents. When testing the assistant in the openai playground it replies based on the content in the pdf documents. However, when setting up communication with the assistant through the OpenAI API the assistant is unaware of the vector store in the replies. After reading forums and documentation for days I’m still not able to figure out why this is happening. Below is my php code for this setup
$api_key = 'MY-API-KEY';
$assistant_id = 'MY-ASSISTANT-ID';
// Get the POST data
$data = json_decode(file_get_contents('php://input'), true);
$user_message = isset($data['message']) ? $data['message'] : '';
// Debugging: Log incoming message
error_log("User message: " . $user_message);
// Prepare the payload for OpenAI API request
$payload = json_encode([
'model' => 'gpt-4o',
'messages' => [
['role' => 'system', 'content' => 'You are connected to a vector store for enhanced responses.'],
['role' => 'user', 'content' => $user_message]
]
]);
// Debugging: Log payload
error_log("Payload: " . $payload);
// Initialize cURL session
$ch = curl_init();
// Set the URL and other options
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key,
]);
// Execute cURL session and get the response
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error_message = 'Error:' . curl_error($ch);
error_log($error_message);
echo json_encode(['reply' => $error_message]);
exit;
}
curl_close($ch);
// Debugging: Log raw response
error_log("Raw response: " . $response);
// Decode the response
$response_data = json_decode($response, true);
// Debugging: Log decoded response
error_log("Decoded response: " . print_r($response_data, true));
// Check for errors in the response
if (isset($response_data['error'])) {
error_log("API Error: " . $response_data['error']['message']);
echo json_encode(['reply' => 'API Error: ' . $response_data['error']['message']]);
exit;
}
// Extract the assistant's reply
$assistant_reply = isset($response_data['choices'][0]['message']['content']) ?
$response_data['choices'][0]['message']['content'] : 'No response from assistant.';
// Debugging: Log assistant reply
error_log("Assistant reply: " . $assistant_reply);
// Return the reply as JSON
echo json_encode(['reply' => $assistant_reply]);