KEPLIN Docs

Live answers on screen

Showing an AI model's answer on screen while the model writes it, with an AI step in an API or with a script, and a Markdown widget.

A long answer from a model can take tens of seconds. With a live answer, the text appears on screen as the model writes it, like in a chat, instead of appearing all at once at the end.

You need two pieces with the same name:

  • What asks for the answer: the AI step of an API, with the name in Live answer, or a script, with stream="name".
  • What shows it: a Markdown widget with the source Live AI answer and the same name in Answer name.

Before you start

  • Real-time notifications must be turned on in the Platform settings. The text reaches the screen through the same channel as the notifications.
  • Whoever uses the screen must be signed in to the app. The answer goes only to the person who made the request: other users don't see it.
  • The app needs a text AI model (see App AI models).

Show the answer of an API

  1. Create an API of type mutation with an AI step (see The API builder). Write the prompt and, in Live answer, the name resumo.

  2. On the screen, add a Markdown widget. In Source, choose Live AI answer and, in Answer name, write resumo.

  3. Optional: write in the widget's content the text that appears before the first answer, for example Click Summarize.

  4. Add a Summarize button and, in the onClick event, call the API:

    await keplin.api.mutate("resumirPedido", { id: keplin.nav.params.id });
    

When the person clicks the button, the widget shows the text as it arrives, with a blinking cursor until the end. The API returns the whole answer at the end, as always.

Show the answer of a script

In a script, pass the name in stream=:

from api_manager import ai


def main(input):
    r = ai.chat(
        "Escreve uma proposta comercial para: " + input["args"]["pedido"],
        stream="resumo",
    )
    return {"texto": r["text"]}

ai.json and ai.run also accept stream=. With ai.run, the text starts over with each request to the model, and the screen keeps the text of the final answer.

What the person sees

Moment The widget shows
Before the first request The content written in the widget.
While the model writes The text that has arrived so far, with a blinking cursor.
At the end The whole answer.
If the call fails The text that had already arrived and the error message.
On a new request The text starts over.

Frequently asked questions

Do two open windows receive the same answer? Yes, if they belong to the same person and have a widget with the same name. The answer goes to every open window of the person who made the request.

Does it work in KeplinGo? Yes. The mobile app uses the same real-time channel.

What about a schedule, or an API called with an API key? Nobody with a session is waiting, so the text does not go to any screen. The call works as always.

Does the workflow AI step also show the answer live? No. A process runs with nobody waiting: the step stores the answer in a variable.