10-llm-stream.agi 842 B

12345678910111213141516171819202122232425
  1. /*---
  2. title: Stream a reply
  3. group: Services
  4. summary: llm.streamRequest delivers the reply in chunks; set their size in the LLM window.
  5. setup: {"mocks":{"llm":{"mode":"fixed","reply":"Streaming sends the answer piece by piece, so a WebApp can show it while it is written.","reasoning":"The user wants a demo of streaming.","chunkSize":10,"chunkDelayMs":60}}}
  6. ---*/
  7. requirelib("llm");
  8. var pieces = 0;
  9. var thinking = "";
  10. var reply = llm.streamRequest(
  11. [{ role: "user", content: "Explain streaming in one sentence." }],
  12. {},
  13. function (delta) {
  14. if (delta.reasoning) {
  15. thinking += delta.reasoning;
  16. }
  17. if (delta.content) {
  18. pieces++;
  19. console.log("chunk " + pieces + ": " + delta.content);
  20. }
  21. }
  22. );
  23. sendJSONResp({ chunks: pieces, reasoning: thinking, result: reply });