New Jun 29, 2024

`window.ai` hello world

More Front-end Bloggers All from Jim Fisher’s blog View `window.ai` hello world on jameshfisher.com

If you’re running Chrome with the window.ai API, here’s a UI to prompt an LLM running locally in your browser:

Prompt

Status:

Output

 

I wrote these TypeScript type definitions for the API:

declare global {
  interface WindowOrWorkerGlobalScope {
    readonly ai?: {
      canCreateTextSession(): Promise<"readily" | "after-download" | "no">;
      createTextSession(
        options?: Partial<AITextSessionOptions>,
      ): Promise<AITextSession>;
      defaultTextSessionOptions(): Promise<AITextSessionOptions>;
    };
  }
}

type AITextSession = { prompt(input: string): Promise<string>; promptStreaming(input: string): AsyncIterable<string>; destroy(): void; clone(): AITextSession; // Not yet implemented! };

type AITextSessionOptions = { topK: number; temperature: number; };

Scroll to top