Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Streaming AI Answers to a Web Page (Summary)

In this video, you replayed one real practice session on the club’s page, and looked back at the steps of the course behind each part of it.

What You Can Do Now

You can now stream a model’s answer from a Node server to a web page as it’s written, through streamAnswer() in the model module. You can keep the conversation’s history within the context window and let the user stop an answer, with the key kept on the server.

One Practice Session

Here’s one real session on the finished club’s page, with one provider’s fast model. Mei sent her first message, spotted a mistake and clicked Stop, fixed her message and sent it again, then ordered a green tea. The page showed this. Your replies will differ:

Language: Program Output
Hola, me llamo Mei. Es mi primero vez aquí.

¡Hola, Mei, bienvenida a nuestro café! [Stopped]

Hola, me llamo Mei. Es mi primera vez aquí.

¡Hola, Mei, bienvenida a nuestro café! Qué bien que estés aquí por primera
vez. ¿Qué le gustaría tomar hoy?

Tip: "Es mi primero vez" should be "Es mi primera vez," because "vez" is
feminine, so the adjective must be "primera" too.

Un té verde. ¿Puede escribir mi nombre en el vaso?

¡Claro que sí, Mei! Enseguida le traigo su té verde con su nombre en el
vaso. ¿Desea algo para acompañar, como una tostada o un pastelito?

Tip: Your sentence is correct! Just note that "¿Puede escribir mi nombre en
el vaso?" is a polite request, and it works well in a café.

And the server’s terminal showed this:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 You’ve reached the end of Streaming AI Answers to a Web Page. You can now stream a model’s answer from a Node server to a web page as it’s written, keep the history within the context window, and let the user stop it, with the key on the server.

00:17 Here’s one real session with your partner, replayed. Yours will differ. Mei sends a message, spots a mistake, and presses Stop. The server logs the stop. No cost is reported, but the call isn’t free.

00:33 Her retry’s reply corrects her first try, which the server kept. Her next one doesn’t repeat her name, but the reply uses it. Each finished reply logs its cost.

00:46 Each part came from a step. You split the chunks into whole events. Then streamAnswer() hid each provider’s events behind one loop. A stream can break midway, so the try wraps the whole loop.

01:01 The model keeps nothing between calls, so you send the history. In Ravi’s thirty messages, the input grew from 162 tokens to over four thousand. A budget of a thousand held it near that line.

01:15 The key stays on the server, which streams each piece to the page. Stopping only the page let the model finish, and the club paid. So Stop ends two requests.

01:29 Things to remember. Streaming shortens the wait, not the bill. Put the try around the whole loop. Send the history every time. Trim it to a budget. Keep the key on your server.

01:42 And a Stop button ends two requests.

01:46 This course is the third of five on building AI features. The next one answers questions from your own documents, and your model folder comes along. Thanks for following along.

Become a Member to join the conversation.