My AI calls last 15 seconds (Old title: In scenes, be able to make a random choice between a series of texts)

When I launch a Gladys scene with a button, I find it nice on some of these scenes to have an audio confirmation that the scene has been launched, or that a step has been completed. For example, I launch the scene that circulates hot water so that it rises to the upstairs bathroom, and I have a first confirmation that tells me « Ok, I’m on it », then a second one that tells me « There you go, the shower is ready ».

Except that these phrases are always the same!

I managed with node-red to bring a bit of variation (see this tutorial). But it would be great to have this directly in Gladys.

One way to achieve this would be to have a new action in the scenes, named for example « Random Text », in which we could define several texts (with the possibility of including variables), and the result would be one of these texts chosen randomly and usable as a variable in the following blocks.

For example, I would define the following texts in this action:

  • Text 1: « Ok, I’m on it »
  • Text 2: « I’m heating the water. And it’s at {{ 1.1 Water Temperature }} degrees »
  • Text 3: « Your wish is my command »
  • Text 4: « I’ll see what I can do… »

And in the next block, I could use the action « Speak on a speaker » with the text {{ 2.1 Random Text}}

@pierre-gilles With your way of relying on AI to develop, is it still useful to create a mockup or is it not necessary?

Are you using Gladys Plus @StephaneB? :slight_smile:

If so, it’s already possible with the « Ask the AI » action!

As a bonus, you’ll get a fully personalized response because it’s written by an AI, with variations each time :wink:

@pierre-gilles, I tested it with Ask the AI and technically it works, indeed.

However, since this action goes through the AI, it has a reaction time that is much too long (between 5 and 10 seconds) to be perceived as confirmation that the button press was registered.
And so when a person presses the button, they tend to think after 2-3 seconds without a reaction that the press was not registered and press again…

So I would like to maintain my request.

Hi @StephaneB and @pierre-gilles

I agree with @StephaneB, both in terms of the request (he beat me to it, I would have suggested it this weekend…) and the interactivity with the AI (response latency). I add a completely personal reflection about AI: I try as much as possible to make Gladys autonomous at home (at least for controlling connected devices), so introducing AI in the cloud will inevitably go against my approach. I’ll take advantage of this weekend to try to formulate a request in this direction :stuck_out_tongue:

Have a great day both,

Jean

I would be curious to understand what’s happening on your end, as the AI API usually responds in 500ms for relatively simple answers :slight_smile:

What is the prompt?

In that case, the right answer is probably local AI, right? :slightly_smiling_face: It’s actually a topic that is already planned and even partly developed.

To return to the initial request, I must admit that I am not convinced by the proposed approach.

We are at a time when there are extremely lightweight AI models, capable of running directly on a phone. I don’t really see the point of going back to a logic of “randomly choosing a phrase from 3 or 4”, as we did about fifteen years ago.

In my opinion, the real need is not to have predefined answers, but to obtain a natural and contextualized response. For example:

  • “Ok, I’ll take care of it.”
  • “I’m starting the hot water circulation. It is currently at 24 °C.”
  • “Here we go, the hot water will be ready in a few moments.”

All dynamically generated based on the context, rather than maintaining a list of variants manually.

I think this approach will be easier to use, more flexible and will give a much more natural result.

My prompt is as follows:
"I just turned on the hot water circulator so it can rise to the bathroom and I can take a shower. Create a short but original sentence to inform me that it will be ready in one minute, specifying that the temperature of the hot water (not the ambient air temperature of the bathroom) is at {{retrieved variable}} degrees. No need to give me the tenths of a degree.

Give me only this sentence in response, without any further explanation. Do not broadcast any message, and do not create a scene."

This gives me responses like:

  • Get ready! In 1 minute, your shower water will be at 46°C.
  • Your shower will be ready in 1 minute! The hot water has already reached 46°C.
  • Count down to 60… and dive in! Hot water at 46°C in 1 minute!

… and it’s true that it’s nice rather than defining a few sentences to choose from.

But I confirm my poor performance: the scene took between 15 and 20 seconds each time to generate these 3 sentences for me (simple display in Telegram, so no delay related to the speaker).

I specify that I did these tests at a time when my Gladys server is very responsive (so without the issue perhaps related to Enedis, described in the other topic).

Is it possible to ask the AI to suggest phrases to say on Google Cast?

With the prompt I gave earlier (or another similar one), the « ask the AI » action generates a sentence. And then if you use the « speak on a speaker » action, you can put the variable {{ ai_response }} in the ‹ phrase › field. And it works. :wink:

Good news @StephaneB :smiling_face:
Can we close the request then?

I would really like to hear @pierre-gilles’ opinion first to understand why this AI prompt takes 15-20 seconds to respond on my end, whereas it takes 0.5s on his…

I hadn’t specified my setup, in case it helps to understand: mini-PC Intel NUC5PPYB, N3700 processor 4 cores 1.6GHz, 8GB of RAM, running Ubuntu 22.04, connected to my fiber network via Ethernet.

Thanks, it works.

Hi @StephaneB, sorry for the delay.

On the subject, I won’t go for a list of pre-written phrases drawn at random. The direction of Gladys is AI: a generated and contextual response, not a fixed dictionary. So the issue to address here isn’t « how to bypass the AI, » it’s « why the AI takes 15 seconds on your end. »

And 15-20 seconds is not normal. To be transparent about the architecture: the « Ask the AI » action doesn’t make a single call. First, there’s a quick intention classification call, then the main call, and then one or more additional round trips if the model decides to call a tool (e.g., to read a temperature). But even with 3 round trips, we should be around 1.5-2 seconds, not 15. So something specific to your installation is happening.

I don’t have time to investigate myself, but the good news is that you have everything you need to do it without waiting for me — without a command line, and Gladys already records everything necessary. Here are the keys.


1. See exactly what the AI did, tool call by tool call

This is the core of the diagnosis. Trigger your scene, wait for the voice response, then go to Integrations → Artificial Intelligence, section « AI Debug » → « Download JSON context for debugging. »

Open the obtained file (a text editor is sufficient) and look at the _debugconversationHistory section. It contains two things:

  • toolCalls: the exact list of all the tools the AI called, with for each its name (tool_name), its result (tool_status: success or error), the arguments passed to it, and the exact time (created_at).
  • messages: the complete conversation in chronological order — your message, each tool call, the final response — each entry timestamped.

By subtracting the created_at, you see precisely how many tools were called during your scene execution, which ones, and which one took the time. Two signals to watch for:

  • multiple tool calls for a simple confirmation phrase → each call is a complete round trip to the model, that’s probably where your seconds are going;
  • a tool_status: "error" → a tool that fails makes the model restart for an additional round, which can double or triple the total time. This is the kind of thing you don’t suspect and that jumps out in this file.

The calls triggered by a scene are well recorded here: the history is that of the user chosen in the « Ask the AI » action. So make sure to trigger the scene before downloading the file.

Incidentally, the tools section of the same file lists all the tools sent to the model with each call. If it is very large (many devices), it also weighs down each request.

2. Supplement with logs if needed

If the file above is not enough to decide, Settings → System → Download logs, then search for [AI_CHAT] in the file. You get the server-side trace, timestamped line by line:

[AI_CHAT] New request userId=... message=...
[AI_CHAT] Forcing tool_choice=required for categories=device_query
[AI_CHAT] Assistant turn iteration=1 tool_calls=1 tool_choice=required tools=[...]
[AI_CHAT] Running tool=...
[AI_CHAT] Tool finished tool=... status=success resultLength=...
[AI_CHAT] Assistant turn iteration=2 tool_calls=0 ...
[AI_CHAT] Completed answerLength=...

The reading grid:

Where time is spent What it means
Between New request and the 1st Assistant turn Local preparation (Gladys rebuilds the list of tools from your database) + classification call
Many lines Assistant turn iteration= The number of round trips to the model explodes
Between Running tool= and Tool finished tool= The execution of the tool on your instance that is slow
A single Assistant turn, but 10 s to reach it The model or your network connection

3. An immediate comparative test

In the chat with Gladys, successively ask your complete prompt, then a short version without any reference to a sensor: « Give me a short phrase to confirm that you are handling the request. » Time both with a stopwatch.

If the short one responds in 2 seconds and the long one in 15 seconds, it’s confirmed: it’s your prompt that triggers tool calls. Mentioning the water temperature makes the model understand that it needs to go read a sensor before responding. In this case, you have an immediate gain by rewriting your prompt so that it doesn’t need any data — and the file from step 1 will show it to you in black and white.


Let an AI help you analyze all this

This is exactly the kind of task where Claude (or your favorite assistant) is very effective, and it saves you from waiting for me. Give them the content of _debug.conversationHistory with an instruction like:

« Here is the timestamped history of an AI call in Gladys Assistant, in JSON format. List the tools called in order, calculate the duration between each step, and tell me where the majority of the total time is spent. Flag any tool in error. »

Gladys is open source, so you can also give them the file that handles all this flow so they can reason on the actual code: server/lib/gateway/gateway.forwardMessageToAiChat.js in the project’s GitHub repository.

Small precaution: this file contains your messages, your room and device names. Remove what you don’t want to share before pasting it anywhere, here or elsewhere.

Thanks for the analysis tips, Pierre-Gilles. I’ll look into that this weekend.