Any Suggestions on How To Incorporate LLM Models into Editor Tables

Any Suggestions on How To Incorporate LLM Models into Editor Tables

Niko@1966Niko@1966 Posts: 16Questions: 7Answers: 3

I have a table that I can view through editor. I have text fields in the table that I want to have an AI model scan for key words and then characterize them into buckets. I can make it work by free typing into an HTML form. However I am having trouble integrating it with DataTables structure. Any insights would be greatly appreciated.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,434Questions: 1Answers: 10,458 Site admin

    I can make it work by free typing into an HTML form.

    How do you do that? Doing it with Editor will be very similar, once you have access to the textarea element. You can get that element using the field().input() method - e.g. perhaps something like:

    let textarea = myEditor.field('myField').input()[0]; // get the element from a jQuery instance
    
    llm(textarea);
    

    Allan

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    The important bit there is that the LLM model will "scan for key words". How does it do that scan? Do you point it towards a td element, or is there a class on the td or an element within the td that says scan this? And when you say free typing into a HTML form - is that just a direct interface into the LLM, similar to the ChatGPT typing area?

    Colin

  • Niko@1966Niko@1966 Posts: 16Questions: 7Answers: 3

    i am trying to do it through the server script. I am trying to connect the model to the table via the server script and then have it display the categorized word results on the front end html. Is this the right approach or should I try something different?

  • allanallan Posts: 63,434Questions: 1Answers: 10,458 Site admin

    That sounds okay. You just need to have an Ajax request to your server-side script, passing the server the value from the input (field().val()) and have it process the data and then display the results.

    How do you do it currently? You said you had it working with an existing input.

    Allan

  • Niko@1966Niko@1966 Posts: 16Questions: 7Answers: 3

    The existing was just a simple stand alone html form with an input box to type text into. I used that to test out the connection to the model and it worked. It would read the words, characterize them and then sort them based on type of word. Now I need to make that work for my database tables.

  • Niko@1966Niko@1966 Posts: 16Questions: 7Answers: 3

    I am able to connect to the GPT model and I can characterize the words in multiple text boxes and I can display the results in the front end table. This part works great. Where I am stuck at right now is getting the categorized results to insert into a separate table before it is displayed on the front end. When I get the data to insert I only want the PHP code to return data for id values that do not exist in the insert table. My code is below.

    ```
    <?php
    // Include the DataTables PHP library and necessary classes
    include("../lib/DataTables.php");

    // Alias Editor classes for easier use
    use DataTables\Editor;
    use DataTables\Editor\Field;

    // Function to send data to OpenAI API and categorize it
    function categorizeText($text, $fieldType) {
    if (is_null($text) || $text === '') {
    return 'Fields Empty';
    }

    $apiKey = 'xxxxxxxx'; // Replace with your OpenAI API key
    $url = 'https://api.openai.com/v1/chat/completions';
    
    $data = [
        'model' => 'gpt-3.5-turbo',
        'messages' => [
            ['role' => 'system', 'content' => 'You are a helpful assistant.'],
            ['role' => 'user', 'content' => "Categorize the following work $fieldType into Inspection, Clean, Lubricate, Repair, Calibrate, or Replace based on the content:"],
            ['role' => 'user', 'content' => 'Here are some examples to help you:'],
            ['role' => 'user', 'content' => 'Examples for Inspection: check, examine, review, audit, assess'],
            ['role' => 'user', 'content' => 'Examples for Clean: wash, scrub, wipe, sanitize, polish'],
            ['role' => 'user', 'content' => 'Examples for Lubricate: oil, grease, lube, coat, apply lubricant'],
            ['role' => 'user', 'content' => 'Examples for Repair: fix, mend, restore, rectify'],
            ['role' => 'user', 'content' => 'Examples for Calibration: calibrate, adjust, set, standardize, fine-tune'],
            ['role' => 'user', 'content' => 'Examples for Replace: change, swap, substitute, exchange, renew, new'],
            ['role' => 'user', 'content' => "Categorize this $fieldType: " . $text]
        ]
    ];
    
    $options = [
        'http' => [
            'header' => "Content-Type: application/json\r\n" . "Authorization: Bearer $apiKey\r\n",
            'method' => 'POST',
            'content' => json_encode($data),
        ],
    ];
    
    $context = stream_context_create($options);
    $response = @file_get_contents($url, false, $context); // Suppress warnings with @
    
    if ($response === FALSE) {
        $error = error_get_last();
        die('Error occurred while connecting to OpenAI API: ' . $error['message']);
    }
    
    $responseData = json_decode($response, true);
    if (!isset($responseData['choices'][0]['message']['content'])) {
        die('Error in API response: ' . json_encode($responseData));
    }
    
    $gptResponse = $responseData['choices'][0]['message']['content'];
    
    // Extract the categories from the response
    $categories = ['Inspection', 'Clean', 'Lubricate', 'Repair', 'Calibrate', 'Replace'];
    $matchedCategories = [];
    foreach ($categories as $category) {
        if (stripos($gptResponse, $category) !== false) {
            $matchedCategories[] = $category;
        }
    }
    
    return !empty($matchedCategories) ? implode(', ', $matchedCategories) : 'Uncategorized'; // Return comma-separated categories
    

    }

    // Build the Editor instance and process the data coming from _POST
    $editor = Editor::inst($db, 'equipment_wo_view_c')
    ->fields(
    Field::inst('equipment_wo_view_c.cid'),
    Field::inst('equipment_wo_view_c.id'),
    Field::inst('equipment_wo_view_c.work_description'),
    Field::inst('equipment_wo_view_c.priority_comments'),
    Field::inst('equipment_wo_view_c.closed_marker_comment'),
    Field::inst('equipment_wo_view_c.status_found_comment'),
    Field::inst('equipment_wo_view_c.status_finished_comment'),
    Field::inst('equipment_wo_view_c.work_comment')
    )
    ->where(function ($q) {
    $q->where('equipment_wo_view_c.cid', $_SESSION['update_cid']);
    })
    ->process($_POST);

    // Get the data
    $data = $editor->data();

    // Categorize each work description and work comment and add it to the data
    foreach ($data['data'] as &$row) {
    $row['equipment_wo_view_c']['description_category'] = categorizeText($row['equipment_wo_view_c']['work_description'], 'description');
    $row['equipment_wo_view_c']['priority_category'] = categorizeText($row['equipment_wo_view_c']['priority_comments'], 'priority');
    $row['equipment_wo_view_c']['closed_category'] = categorizeText($row['equipment_wo_view_c']['closed_marker_comment'], 'closed');
    $row['equipment_wo_view_c']['found_category'] = categorizeText($row['equipment_wo_view_c']['status_found_comment'], 'found');
    $row['equipment_wo_view_c']['finished_category'] = categorizeText($row['equipment_wo_view_c']['status_finished_comment'], 'finished');
    $row['equipment_wo_view_c']['comment_category'] = categorizeText($row['equipment_wo_view_c']['work_comment'], 'comment');
    }

    // Return the JSON data
    echo json_encode($data);

    <?php > ``` ?>
  • Niko@1966Niko@1966 Posts: 16Questions: 7Answers: 3
    Answer ✓

    Sorry, false alarm. I just got it to work. The issue was a mismatch between variable type I needed to insert and the variable type in the table.

Sign In or Register to comment.