Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to properly create "messages" in JSON for OpenAI API in QT
Forum Updated to NodeBB v4.3 + New Features

How to properly create "messages" in JSON for OpenAI API in QT

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 5 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JonBJ JonB

    @Sean-Yiu said in How to properly create "messages" in JSON for OpenAI API in QT:

    QString str1 = "[{"role": "user", "content": "Hello"}]"
    obj["messages"] = str1;
    

    I don't understand why you are treating this as a string. Looking at your

     { "messages" : [{"role": "user", "content": "Hello"}], "model" : "gpt-3.5-turbo" }
    

    It (the value of messages) is a JSON array with a single JSON object element. Build your JSON structure correctly (as per whatever the API expects/specifies) and it will come out right for serializing.

    S Offline
    S Offline
    Sean Yiu
    wrote on last edited by
    #5

    @JonB

    /*
    
         * Test3
    
         */
    
        QJsonArray array;
        QString str3 =  "\"role\": \"user\"";
        QString str4 =  "\"content\": \"Hello\"";
    
        array = { str3, str4 };
    
        qDebug().noquote() << "str3= " << str3;
        qDebug().noquote() << "str4= " << str4;
    
        QJsonObject obj_new;
    
        obj_new["model"] = "gpt-3.5-turbo";
        obj_new["messages"] = array;
        postData = doc.toJson();
    
        qDebug().noquote() << "PostData= " << postData;
    }
    
    Result:
    

    str3= "role": "user" << no problem
    str4= "content": "Hello" << no problem

    PostData= {
    "messages": "[{"role": "user", "content": "Hello"}]"
    ^ ^ ^ ^ ^ ^ << problem
    "model": "gpt-3.5-turbo"
    }

    I think what's going is somehow when doc.toJson() interprets a " as " and actually adds the escape in there. Looks like a bug in QT ?

    Christian EhrlicherC JonBJ 2 Replies Last reply
    0
    • S Sean Yiu

      @JonB

      /*
      
           * Test3
      
           */
      
          QJsonArray array;
          QString str3 =  "\"role\": \"user\"";
          QString str4 =  "\"content\": \"Hello\"";
      
          array = { str3, str4 };
      
          qDebug().noquote() << "str3= " << str3;
          qDebug().noquote() << "str4= " << str4;
      
          QJsonObject obj_new;
      
          obj_new["model"] = "gpt-3.5-turbo";
          obj_new["messages"] = array;
          postData = doc.toJson();
      
          qDebug().noquote() << "PostData= " << postData;
      }
      
      Result:
      

      str3= "role": "user" << no problem
      str4= "content": "Hello" << no problem

      PostData= {
      "messages": "[{"role": "user", "content": "Hello"}]"
      ^ ^ ^ ^ ^ ^ << problem
      "model": "gpt-3.5-turbo"
      }

      I think what's going is somehow when doc.toJson() interprets a " as " and actually adds the escape in there. Looks like a bug in QT ?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @Sean-Yiu said in How to properly create "messages" in JSON for OpenAI API in QT:

      . Looks like a bug in QT ?

      No. You add two strings to an array so Qt is doing the correct thing here. If you want to add a jsonvalue then add a QJsonValue and not strings.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      S 1 Reply Last reply
      2
      • M mpergand

        @Sean-Yiu
        Try raw strings:
        https://en.cppreference.com/w/cpp/language/string_literal

        Anyway if you do:
        QByteArray bytes=str2.toLatin1():
        there is no escape \n in the array, because there are just needed for the conversion to C string.

        S Offline
        S Offline
        Sean Yiu
        wrote on last edited by
        #7

        @mpergand
        Dont think that's the problem. Please look at my Test3 example I just entered. it shows the doc.toJson adds an escape | in there

        Christian EhrlicherC 1 Reply Last reply
        0
        • S Sean Yiu

          @mpergand
          Dont think that's the problem. Please look at my Test3 example I just entered. it shows the doc.toJson adds an escape | in there

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #8

          @Sean-Yiu said in How to properly create "messages" in JSON for OpenAI API in QT:

          t shows the doc.toJson adds an escape | in there

          Which is correct as the you add strings containing " so they must be escaped.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • S Sean Yiu

            @JonB

            /*
            
                 * Test3
            
                 */
            
                QJsonArray array;
                QString str3 =  "\"role\": \"user\"";
                QString str4 =  "\"content\": \"Hello\"";
            
                array = { str3, str4 };
            
                qDebug().noquote() << "str3= " << str3;
                qDebug().noquote() << "str4= " << str4;
            
                QJsonObject obj_new;
            
                obj_new["model"] = "gpt-3.5-turbo";
                obj_new["messages"] = array;
                postData = doc.toJson();
            
                qDebug().noquote() << "PostData= " << postData;
            }
            
            Result:
            

            str3= "role": "user" << no problem
            str4= "content": "Hello" << no problem

            PostData= {
            "messages": "[{"role": "user", "content": "Hello"}]"
            ^ ^ ^ ^ ^ ^ << problem
            "model": "gpt-3.5-turbo"
            }

            I think what's going is somehow when doc.toJson() interprets a " as " and actually adds the escape in there. Looks like a bug in QT ?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #9

            @Sean-Yiu said in How to properly create "messages" in JSON for OpenAI API in QT:

            QJsonArray array;
            QString str3 =  "\"role\": \"user\"";
            QString str4 =  "\"content\": \"Hello\"";
            
            array = { str3, str4 };
            

            I really don't know what you are doing here. Again, what are you doing creating any string like "\"role\": \"user\""? In the original you have:

            [{"role": "user", "content": "Hello"}]
            

            That is an array containing a single JSON object, which has two string keys, each of which has a string value. You already have such a construct, "model" : "gpt-3.5-turbo" , these must all be handled properly and the same way.

            Seems to me @ChrisW67 already wrote the correct code for this for you, so why did you deviate from that anyway?

            1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @Sean-Yiu said in How to properly create "messages" in JSON for OpenAI API in QT:

              . Looks like a bug in QT ?

              No. You add two strings to an array so Qt is doing the correct thing here. If you want to add a jsonvalue then add a QJsonValue and not strings.

              S Offline
              S Offline
              Sean Yiu
              wrote on last edited by
              #10

              @Christian-Ehrlicher
              /*
              * Test3
              */
              QJsonArray array;
              QString str3 = ""role": "user"";
              QString str4 = ""content": "Hello"";
              array = { QJsonValue(str3), QJsonValue(str4) };

              //qDebug().noquote() << "str3= " << str3;
              //qDebug().noquote() << "str4= " << str4;
              
              QJsonObject obj_new;
              obj_new["model"] = "gpt-3.5-turbo";
              obj_new["messages"] = array;
              postData = doc.toJson();
              qDebug().noquote() << "PostData= " << postData;
              

              Same problem. in Debug mode, I look inside array and the contents are correct. But side postData, they are not.

              Christian EhrlicherC JonBJ C 3 Replies Last reply
              0
              • S Sean Yiu

                @Christian-Ehrlicher
                /*
                * Test3
                */
                QJsonArray array;
                QString str3 = ""role": "user"";
                QString str4 = ""content": "Hello"";
                array = { QJsonValue(str3), QJsonValue(str4) };

                //qDebug().noquote() << "str3= " << str3;
                //qDebug().noquote() << "str4= " << str4;
                
                QJsonObject obj_new;
                obj_new["model"] = "gpt-3.5-turbo";
                obj_new["messages"] = array;
                postData = doc.toJson();
                qDebug().noquote() << "PostData= " << postData;
                

                Same problem. in Debug mode, I look inside array and the contents are correct. But side postData, they are not.

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #11

                @Sean-Yiu said in How to properly create "messages" in JSON for OpenAI API in QT:

                ""role": "user"";

                This is a a plain string. You have to convert it to a correct JSON key value pair (aka object).
                This was told you from three different persons but you ignore it. Do you want help? Then don't ignore what we are telling you.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                2
                • S Sean Yiu

                  @Christian-Ehrlicher
                  /*
                  * Test3
                  */
                  QJsonArray array;
                  QString str3 = ""role": "user"";
                  QString str4 = ""content": "Hello"";
                  array = { QJsonValue(str3), QJsonValue(str4) };

                  //qDebug().noquote() << "str3= " << str3;
                  //qDebug().noquote() << "str4= " << str4;
                  
                  QJsonObject obj_new;
                  obj_new["model"] = "gpt-3.5-turbo";
                  obj_new["messages"] = array;
                  postData = doc.toJson();
                  qDebug().noquote() << "PostData= " << postData;
                  

                  Same problem. in Debug mode, I look inside array and the contents are correct. But side postData, they are not.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #12

                  @Sean-Yiu
                  As @Christian-Ehrlicher has said, and as we keep telling you. What about my:

                  Seems to me @ChrisW67 already wrote the correct code for this for you, so why did you deviate from that anyway?

                  ?

                      QJsonObject message;
                      message.insert("role", QJsonValue("user"));
                      message.insert("content", QJsonValue("Hello"));
                  

                  So long as you have any string(s) like

                  QString str3 = ""role": "user"";
                  QString str4 = ""content": "Hello"";
                  

                  (with/without any embedded quotes/backslashes) you will not get anywhere.

                  BTW, when the JSON you want has any unquoted/not inside a literal string:

                  • [ --- you want a QJsonArray.
                  • { --- you want a QJsonObject, that will have any number of key-value pairs for each "key" : "string-value" inside it.

                  JSON is really very simple.

                  1 Reply Last reply
                  3
                  • S Sean Yiu

                    @Christian-Ehrlicher
                    /*
                    * Test3
                    */
                    QJsonArray array;
                    QString str3 = ""role": "user"";
                    QString str4 = ""content": "Hello"";
                    array = { QJsonValue(str3), QJsonValue(str4) };

                    //qDebug().noquote() << "str3= " << str3;
                    //qDebug().noquote() << "str4= " << str4;
                    
                    QJsonObject obj_new;
                    obj_new["model"] = "gpt-3.5-turbo";
                    obj_new["messages"] = array;
                    postData = doc.toJson();
                    qDebug().noquote() << "PostData= " << postData;
                    

                    Same problem. in Debug mode, I look inside array and the contents are correct. But side postData, they are not.

                    C Offline
                    C Offline
                    ChrisW67
                    wrote on last edited by ChrisW67
                    #13

                    @Sean-Yiu There is no problem in Qt. Others have explained and I have already shown you how to construct your data structure using the Qt JSON classes.

                    The part of the data structure that you are so twisted around trying to create is a QJsonArray of message QJsonObjects. Each message object has keys role and content with values "user" and "Hello" respectively.

                    {
                      "messages" : [     <<<< this bit here
                        {
                          "role": "user", 
                          "content": "Hello"
                        }
                      ], 
                      "model" : "gpt-3.5-turbo" 
                    }
                    

                    What your code does:

                    QJsonArray array;
                    QString str3 =  R"("role": "user")";
                    QString str4 =  R"("content": "Hello")";
                    
                    array = { str3, str4 };
                    
                    qDebug().noquote() << "str3= " << str3;
                    qDebug().noquote() << "str4= " << str4;
                    qDebug().noquote() << array;
                    
                    // Ouptut is 
                    str3=  "role": "user"
                    str4=  "content": "Hello"
                    QJsonArray(["\"role\": \"user\"","\"content\": \"Hello\""])
                    

                    You can see that the input strings are what you asked for. You can also see that the result is a QJsonArray of two strings:

                    [
                      "\"role\": \"user\"",      <<<< str3 one single string
                      "\"content\": \"Hello\""   <<<< str4another single string
                    ]
                    

                    As your strings have embedded double quotes that need to be escaped in JSON output.

                    1 Reply Last reply
                    4
                    • S Offline
                      S Offline
                      Sean Yiu
                      wrote on last edited by
                      #14

                      Thanks. I understand better now.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved