search
HomeBackend DevelopmentPHP TutorialHow to Optimize Large JSON Files for Use with ChatGPT API?

How to Optimize Large JSON Files for Use with ChatGPT API?

I am trying to use ChatGPT as the chatbot for my Magento 2 website, and I want to pass product data to it. To do this, I collected all the products and stored them in a JSON file, which I then read to embed the data in the systemRoleContent of the system role. However, the issue I am facing is that the JSON file is quite large.

{
    "bot_response": "Error: ChatBot Error: Unexpected API response structure: {\n    \"error\": {\n        \"message\": \"Request too large for gpt-4o on tokens per min (TPM): Limit 30000, Requested 501140. The input or output tokens must be reduced in order to run successfully. Visit https://platform.openai.com/account/rate-limits to learn more.\",\n        \"type\": \"tokens\",\n        \"param\": null,\n        \"code\": \"rate_limit_exceeded\"\n    }\n}\n"
}

I noticed that there is a function that needs to be added to the API configuration, which allows you to run a query to select products based on keywords found in their names or descriptions that match keywords in the user’s message. The challenge is that users initially may not know the names of the products; they come to the chatbot to discover them. How can I address this issue?

This is the code that I am working with right now:

<?php namespace MetaCares\Chatbot\Model;

use Magento\Framework\App\ObjectManager;

class ChatBot
{
    private $authorization;
    private $endpoint;
    private $conversationHistory = [];
    private $productsFile;
    private $fetchingDateFile;
    private $didFetchProducts = false;

    public function __construct()
    {
        $this->authorization = 'sk-proj-';
        $this->endpoint = 'https://api.openai.com/v1/chat/completions';

        $this->productsFile = __DIR__ . '/products.json';
        $this->fetchingDateFile = __DIR__ . '/fetching_date.json';

        $currentTime = time();
        $timeDifferenceSeconds = 24 * 3600;

        if (!file_exists($this->fetchingDateFile)) {
            file_put_contents($this->fetchingDateFile, json_encode(['last_fetch_time' => 0]));
        }

        $fetchingData = json_decode(file_get_contents($this->fetchingDateFile), true);
        $lastFetchTime = $fetchingData['last_fetch_time'] ?? 0;

        if ($currentTime - $lastFetchTime > $timeDifferenceSeconds) {
            $products = $this->fetchProductsUsingModel();
            $productsJson = json_encode($products);
            file_put_contents($this->productsFile, $productsJson);
            $fetchingData['last_fetch_time'] = $currentTime;
            file_put_contents($this->fetchingDateFile, json_encode($fetchingData));
            $this->didFetchProducts = true;
        }

        $jsonSampleData = file_get_contents($this->productsFile);

        $systemRoleContent = conversationHistory[] = [
            'role' => 'system',
            'content' => $systemRoleContent
        ];

        if (session_status() == PHP_SESSION_NONE) {
            session_start();
        }
        if (isset($_SESSION['chat_history'])) {
            $this->conversationHistory = $_SESSION['chat_history'];
        }
    }

    public function fetchProductsUsingModel(): array
    {
        return $products;
    }

    private function getCategoryNames(array $categoryIds): array
    {
        return $categoryNames;
    }

    public function sendMessage(string $message): array
    {
        try {
            $this->conversationHistory[] = [
                'role' => 'user',
                'content' => $message
            ];

            $data = [
                'model' => 'gpt-4o',
                'messages' => array_map(function ($msg) {
                    return [
                        'role' => $msg['role'] === 'bot' ? 'assistant' : $msg['role'],
                        'content' => $msg['content']
                    ];
                }, $this->conversationHistory)
            ];

            $response = $this->makeApiRequest($data);
            $arrResult = json_decode($response, true);

            if (json_last_error() !== JSON_ERROR_NONE) {
                throw new \Exception('Invalid API response format');
            }

            if (!isset($arrResult['choices']) || !isset($arrResult['choices'][0]['message']['content'])) {
                throw new \Exception('Unexpected API response structure: ' . $response);
            }

            $assistantResponse = $arrResult['choices'][0]['message']['content'];
            $this->conversationHistory[] = [
                'role' => 'bot',
                'content' => $assistantResponse
            ];

            $_SESSION['chat_history'] = $this->conversationHistory;
            return [
                "conversationHistory" => $_SESSION['chat_history'],
                'didFetchProducts' => $this->didFetchProducts,
                'response' => $assistantResponse,
            ];
        } catch (\Exception $e) {
            throw new \Exception('ChatBot Error: ' . $e->getMessage());
        }
    }

    private function makeApiRequest(array $data): string
    {
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL => $this->endpoint,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => json_encode($data),
            CURLOPT_HTTPHEADER => [
                'Content-Type: application/json',
                'Authorization: Bearer ' . $this->authorization,
            ],
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => 0
        ]);

        $response = curl_exec($ch);

        if (curl_errno($ch)) {
            $error = curl_error($ch);
            curl_close($ch);
            throw new \Exception('API request failed: ' . $error);
        }

        curl_close($ch);
        return $response;
    }
}

The above is the detailed content of How to Optimize Large JSON Files for Use with ChatGPT API?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor