I call the agent through the API and it does not search the uploaded vector for a list of products

I leave you the code that I use to invoke an agent that I have created, it has a json file uploaded in a vector and is assigned correctly, testing it in PlayGround it works well, but if I call it through the API that I use for WhatsApp it does not consult said products, it simply ignores them and responds as if it had nothing. Thank you in advance.

<?php

class OpenAI {
    private $apiKey;
    private $assistantId;

    public function __construct($apiKey, $assistantId) {
        $this->apiKey = $apiKey;
        $this->assistantId = $assistantId;
    }

    public function generateAssistantResponse($userInput) {
        // Paso 1: Crear un hilo
        $threadId = $this->createThread();

        // Paso 2: Añadir un mensaje al hilo
        $this->addMessageToThread($threadId, $userInput);

        // Paso 3: Ejecutar el asistente
        $runId = $this->runAssistant($threadId);

        // Paso 4: Esperar a que la ejecución se complete
        $this->waitForRunCompletion($threadId, $runId);

        // Paso 5: Recuperar la respuesta del asistente
        return $this->getAssistantResponse($threadId);
    }

    private function createThread() {
        $url = "https://api.openai.com/v1/threads";
        $response = $this->sendRequest($url, [], 'POST');
        return $response['id'];
    }

    private function addMessageToThread($threadId, $content) {
        $url = "https://api.openai.com/v1/threads/{$threadId}/messages";
        $data = [
            'role' => 'user',
            'content' => $content
        ];
        $this->sendRequest($url, $data, 'POST');
    }

    private function runAssistant($threadId) {
        $url = "https://api.openai.com/v1/threads/{$threadId}/runs";
        $data = [
            'assistant_id' => $this->assistantId
        ];
        $response = $this->sendRequest($url, $data, 'POST');
        return $response['id'];
    }

    private function waitForRunCompletion($threadId, $runId) {
        $url = "https://api.openai.com/v1/threads/{$threadId}/runs/{$runId}";
        $status = '';
        do {
            $response = $this->sendRequest($url, null, 'GET');
            $status = $response['status'];
            if ($status === 'completed') {
                break;
            }
            sleep(1);
        } while ($status !== 'failed' && $status !== 'expired');

        if ($status !== 'completed') {
            throw new Exception("La ejecución falló o expiró con el estado: " . $status);
        }
    }

    private function getAssistantResponse($threadId) {
        $url = "https://api.openai.com/v1/threads/{$threadId}/messages";
        $response = $this->sendRequest($url, null, 'GET');
        $messages = $response['data'];
        $assistantMessages = array_filter($messages, function($msg) {
            return $msg['role'] === 'assistant';
        });
        $latestMessage = reset($assistantMessages);
        return $latestMessage['content'][0]['text']['value'];
    }

    private function sendRequest($url, $data = null, $method = 'POST') {
        $headers = [
            "Authorization: Bearer " . $this->apiKey,
            "Content-Type: application/json",
            "OpenAI-Beta: assistants=v1"
        ];

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

        if ($method === 'POST') {
            curl_setopt($ch, CURLOPT_POST, true);
            if ($data !== null) {
                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);

        if (curl_errno($ch)) {
            throw new Exception('Error en la solicitud cURL: ' . curl_error($ch));
        }

        curl_close($ch);

        $decodedResponse = json_decode($response, true);
        if ($httpCode >= 400) {
            throw new Exception("La solicitud a la API falló con el estado {$httpCode}: " . json_encode($decodedResponse));
        }

        return $decodedResponse;
    }
}


222

Hi, would you mind putting the code in a code box like so:

I can’t right now, my friend, please copy and paste the code and look at it. I would appreciate it if you have any suggestions. Regards.

Very sorry, can’t copy the code on my phone… because it is not in a code box

Now I was able to put the code in a box