Chargement du document dans l'assistant ChatGPT

Informations sur le document

Nom du fichier:

exemple.pdf

Type:

application/pdf

Taille:

1 MB

Date d'upload:

2025-03-06 12:00:00

Processus de chargement

Le chargement du document dans l'assistant ChatGPT implique les étapes suivantes:

  1. Utilisation de l'assistant existant avec l'ID spécifique
  2. Création d'un vector store pour stocker les embeddings du document
  3. Upload du fichier et ajout au vector store
  4. Mise à jour de l'assistant pour utiliser le vector store
  5. Mise à jour du statut du document dans la base de données

Une fois le document chargé, vous pourrez poser des questions sur son contenu à l'assistant ChatGPT.

Cette opération peut prendre quelques instants en fonction de la taille du document.

Annuler

Implémentation technique

// Exemple de code pour l'intégration avec l'API OpenAI via cURL

// Configuration de base pour les requêtes cURL
$headers = [
    'Authorization: Bearer ' . $api_key,
    'Content-Type: application/json',
    'OpenAI-Beta: assistants=v2'
];

// 1. Utiliser l'assistant existant avec l'ID spécifique
$assistant_id = 'asst_wgsoAkX7Lzo4jKgq1B2xVUSm';

// 2. Créer un vector store
$ch = curl_init('https://api.openai.com/v1/vector_stores');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    'name' => 'Documents Club Value - ' . $document['original_name']
]));
$response = curl_exec($ch);
$vector_store_data = json_decode($response, true);
$vector_store_id = $vector_store_data['id'];

// 3. Uploader le fichier et l'ajouter au vector store
// Upload du fichier
$boundary = uniqid();
$delimiter = '-------------' . $boundary;
$post_data = '--' . $delimiter . "\r\n";
$post_data .= 'Content-Disposition: form-data; name="purpose"' . "\r\n\r\n";
$post_data .= 'assistants' . "\r\n";
$post_data .= '--' . $delimiter . "\r\n";
$post_data .= 'Content-Disposition: form-data; name="file"; filename="' . $file_name . '"' . "\r\n";
$post_data .= 'Content-Type: ' . $file_mime . "\r\n\r\n";
$post_data .= file_get_contents($file_path) . "\r\n";
$post_data .= '--' . $delimiter . '--' . "\r\n";

$ch = curl_init('https://api.openai.com/v1/files');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer ' . $api_key,
    'Content-Type: multipart/form-data; boundary=' . $delimiter
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$response = curl_exec($ch);
$file_data = json_decode($response, true);
$file_id = $file_data['id'];

// Ajouter le fichier au vector store
$ch = curl_init('https://api.openai.com/v1/vector_stores/' . $vector_store_id . '/files');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    'file_id' => $file_id
]));
$response = curl_exec($ch);

// 4. Mettre à jour l'assistant pour utiliser le vector store
$ch = curl_init('https://api.openai.com/v1/assistants/' . $assistant_id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    'tool_resources' => [
        'file_search' => [
            'vector_store_ids' => [$vector_store_id]
        ]
    ]
]));
$response = curl_exec($ch);