How are you guys saving response text generated by openai on your website (in WordPress)?

I am using Wordpress, and I’ve been playing around with different methods to save the generated response text, that comes back from openai via API, and I was wondering what other methods there are to save the text so it doesn’t get lost, and the users can refer to it later. Since I use Wordpress I am trying to save each response as a post, and there probably is an easier way that I am not seeing. Thank you

Hi Jayroo - naturally there are many ways to save response text, the big question though is not “saving” but how it’s used. example - you say “… the users can refer to it later…” do you mean later in the session or later in “hours/days/months” etc? If it was/is just a simple session then you could save as a localSession or localStorage variable - session is literally that “the session” where as localStorage is “forever” unless the user deletes it. That’s just a simple bit of javascript - no need to post to the WP database, as ot’s WP based you could also create/write and save a JSON file for that user … Just a couple of ideas.

3 Likes

Since it seems like you’re still experimenting, if you have access to create tables in the database, then I would create a new table with a JSON column.

The JSON column allows you to make alterations to the structure of the data without worrying much about the DB schema. This is good when you’re still experimenting with the possibilities and are unsure what exactly you need to persist into the future.

2 Likes

Hi @Jayroo

I save all messages in a DB text column as a serialized array of the same messages format sent to the API. Works fine as serializing JSON data is a very old and mature way of doing things like this.

Storage is the “least” challenfing part of session and dialog management.

The challenging part is not storage but in managing the tokens to stay under the 4096 token limit.

There is no need to manipulate the stored messages in the DB, you just read them, prune them if required, use them (send them to the API along with the new messages) and update the DB.

HTH

:slight_smile:

3 Likes

Thanks for your reply. My site has a members area where they get access to a openai script to generate articles, I’m using woocommerce, and on another site I am using Ultimate Membership Pro, not that it matters. So, I guess I just want them to save each generated articles, with a download button that I will have next to the response text. so I just want it to save in their dashboard under ‘my-content’, for the life of their membership. I will try the javascript, thanks.

Hi there, thanks for that, I was using My SQL, but it was a bit of a mess. So, that’s why I started experimenting with posts, as it was just the one area “my-content”, that I want for each member, to have the option to download and save the response text.

Thanks for your reply, Ruby, I will keep that in mind, for sure. Cheers

If you’re working with WP, there should be a way to save the results of your plugin…

I would search Google and Stackoverflow for things like “WP plugin” and “WP plugin save output” and “WP plugin save file”

HTH.

1 Like

Hi Paul,

Yes, there are a couple of ways, though I keep getting conflicts with the script tags (which I now put in a code snippet) that loads the Jquery library that helps the client choose options via menus & sliders, that are related to the text field that forms the initial prompt box, which gets sent to openai via a submit button, and the javascript that functions the download button on the same page, along with other forms that do not work on other pages., like woo-commerce etc, as soon as disable the code snippet the other buttons work again.
So been trying to figure out a way to move the Jquery library elsewhere, or use another method.

And thanks for those references, I will take a look at those today, thanks heaps.

Sounds like you’re trying to load Jquery twice or something maybe?

For jquery, too, if you’re loading dynamic content, it won’t be attached to the original DOM when the page was first loaded, so you have to attach to the body first then find the DOM element you’re wanting…

You’ll get there. I have faith in you!

I use SQL and it’s not a mess at all.

Let me show you:

Here is my SQL (working) scheme I currently use:

 create_table "chat_conversations", force: :cascade do |t|
    t.string "description"
    t.integer "message_count"
    t.string "user"
    t.string "messages"
    t.integer "total_tokens"
    t.integer "prompt_tokens"
    t.integer "completion_tokens"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

I store all the messages in a single string , which is the serialized messages array. This is the “standard” way of storing JSON and other data which requires serialization. This method is “as old as the hills” as they say, going back 15 years at least and is still used today.

In Rails, we set this up easily in the model as follows:

class ChatConversation < ApplicationRecord
    serialize :messages, Array
end

Let’s create a chat, for example and look at the DB:

First we create a new chat and strip all prior chat message which were stored in the DB:

Set Up Chat

Completion

Let’s look at the DB now:

You can see that the chat messages are all nicely serialized in the DB.

This is the current “state” of the chat.

Now we can run another chat, of course. I pull all the messages from the associated chatid from the DB and add the new message, for example to illustrate:

Second Chat, Use Prior Chat Data from DB

Chat works as expected, system role is correct:

Lets go directly to the DB:

You can clearly see that all is working fine. The system message works fine as well (debunking the community and discord misinformation that the system role does not work at the beginning, BTW. It works just fine if the session management code is written properly).

The DB now has serialized 5 messages, 3 from the first chat API call, and 2 from the second.

Of course, as the number of chat messages increase, we start bumping up against the very annoying 4096 OpenAI token limitation, so we have to prune.

In my lab, I current test a variety of “pruning messages” as you can see in the menu below:

We can use the TikTok tokenizer to estimate token amount and add this to the running token count in the DB to prune the messages before sending them to the API and then update the DB with the new messages array. This is only one of many possible pruning strategies. Currently, I disabled that feature and enjoy pruning manually because that is how I develop full situational knowledge of what is going on with the new API method.

What I am trying to mention is that it is pretty easy for a web developer to use a DB to manage the chat API dialog, store the messages array in the DB and to prune the messages array as required (based on your favorite pruning strategy), update the DB, rinse and repeat. This is all “very basic” webdev 101 stuff, to be candid.

Storage is easy. Pruning is a bit more tricky, but not so difficult with a bit of practice and testing.

Also, note, as mentioned before, there is some misinformation in our community about the “system” role not working as the chat progresses. This is only true if you drop the system role from the messages array (which is not a good idea if you want to keep the role active).

However, if developers manage the messages array properly, the system message work fine because the system role remains in the messages array.

Hope this helps.

:slight_smile:

2 Likes

Hi Ruby, Wow thanks heaps for your time and effort detailing your method, I really appreciate it. I will be studying this tonight, and will give SQL another crack. Thanks heaps.

Kind regards

Jay

You are welcome @Jayroo

There is a lot of misinformation floating around here and elsewhere.

I understand that it can be confusing!

:slight_smile:

1 Like

I still think it would be easiest to just save as a WP post as you’re generating inside of WP…

As long as you get it saved, though… that’s the point.

1 Like

@ruby_coder, is that UI you are using an open source that you can share? It looks like a useful interface.

RECUWEB INC. recently released OpenAI Form Completion plugin for WordPress to simplify the AI integration process by allowing you to hold down the Shift key while selecting text from any input field or textarea.

OpenAI_Text_Completion

And here is an additional list of things the plugin does:

DIALOG BOX – Simply hold down the Shift key on your keyboard while selecting a piece of text to open a convenient dialog box.

EMBEDDEDNESS – Seamlessly works from any input text field or textarea located in your backend.

CUSTOM PROMPTS – Tailor your own prompts to manipulate the selected text as per your specific task, whether it’s paraphrasing, translating, completing, or formatting.

MULTIPLE MODELS – Choose from a variety of available AI models for text completion, including GPT-3, GPT-4…

EFFORTLESS REPLACEMENT – Easily update the old text with the new one in just one click, streamlining your content management process.

Ditto:

I probably need to clean it up some, but it does the job so far. Users can also download their query history to a local csv file.

1 Like

Hi Jayroo,

I’m also developing a plugin for WordPress that enable users to use the API service.

There are “personas” that users can interact with, and I find it easier to have a “history post” for each user - persona interaction.

Inside that history post, there’s an ACF (Advanced Custom Field) field with repeater type that stores the conversation.

Just like what ruby_coder mentioned, though. The problem I’m having is to keep users stays under the token limit. Since with that approach, the repeater field that stores conversation will just grow larger per interaction. That also means that users will use larger and larger token count as the conversation goes.

1 Like

A “chat” on OpenAI maps perfectly to the built in WordPress comment system! If each side of the conversation is a WordPress user, you can map the entire chat to a post, or a custom post type. Then the “system message”, “functions”, are mapped to WordPress custom post meta. There is a new discussion about “threading” AI conversations. That is, complex processes that involves moving the conversation to some kind of decision node, then branching it. Like a conversation about “pick a door”, then another conversation about what is actually behind the door. Well, if WordPress CPTs map URLs to AI conversations, then AI THREADS CAN GO ACROSS DOMAINS.