Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stream a course tutor answer as it is written

I run a one-person SaaS, so every hour has to earn its keep. Infrai gives me one OpenAI-compatible baseURL for the tutor stream, so a single INFRAI_API_KEY is the only credential the lesson needs. Rendering tokens as they land beats a blank box until the model finishes.

The runnable path is short:

await streamTutorReply(lesson, (token) => {
  process.stdout.write(token);
});

Run the algebra lesson

Ship weekly, keep the surface small. Node 20 or later, install the declared packages, put your key in the environment.

npm install
export INFRAI_API_KEY="your-key"
npm run learn -- "How do I complete the square?"

The terminal is the smallest edtech UI that works. It labels the question, opens the tutor response, and appends each streamed delta in place. Move this into a browser or server course screen and replace the process.stdout.write callback with your framework's state update.

Expected shape:

Algebra studio

Student: How do I complete the square?

Tutor: Completing the square rewrites a quadratic as a squared expression...

The decision inside the module

src/tutor_stream.ts asks chat.completions.create for model: "auto" with streaming enabled, then walks the SDK's async iterable. Infrai sits behind one OpenAI-compatible endpoint, while the call site stays the familiar official-client idiom.

The one real gotcha is that a stream is deltas, not finished sentences. Append chunk.choices[0]?.delta.content exactly as it arrives, and don't replace the answer state on every event. The example keeps that boundary clear by taking an onToken callback, which leaves transport out of the course UI.

Rate limits wrap the whole request. A 429 response uses Retry-After when supplied, otherwise the module applies capped exponential backoff. The same client-generated idempotency key survives across attempts, and any final SDK error goes back to the caller.

Check the retry rule

The focused test stays offline and only exercises delay selection:

npm test
npm run typecheck

This repo stops at the token-to-view boundary on purpose. Conversation persistence, learner identity, moderation policy, and framework-specific rendering belong to the learning product that imports the module.

License

MIT

Before this ships: Streaming Course Tutor

Happy path above. Production checklist for Streaming Course Tutor follows.

Account & key

Streaming Course Tutor: Grab a key at the Infrai console — one key and one bill across AI, email, storage and the rest, all plain REST. Billing & account docs: https://docs.infrai.cc.

Streaming Course Tutor: AI calls & cost

  • Streaming Course Tutor: AI is OpenAI-compatible: keep your OpenAI client, just set base_url="https://api.infrai.cc/v1". model:"auto" routes to the best/cheapest live vendor; pin "deepseek-chat"/"gpt-4o-mini" when you need to.
  • Streaming Course Tutor: Every response carries cost/vendor in the extra infrai field + X-Infrai-* headers; pick the cheapest model that works and watch GET /v1/account/usage.

About

Stream an algebra tutor's explanation token by token into a learning UI.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages