GithubHelp home page GithubHelp logo

c-o-l-i-n / ng-generative-ai-demo Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 3.0 119 KB

๐Ÿค– Stream realtime generative AI to your Angular app using Google Gemini, HttpClient, RxJS, and Signals

TypeScript 78.84% HTML 4.47% SCSS 16.69%
ai angular gemini gemini-api generative-ai rxjs signals

ng-generative-ai-demo's Introduction

๐Ÿค– Angular Generative AI Demo

demo.webm

๐Ÿค” Why does this matter?

Generative AI is changing the way we interact with technology. As AI chatbots become more commonplace, users expect certain behaviors from our apps, such as realtime text updates. Using LLM APIs, Signals, and some RxJS magic, we can create modern AI-driven user experiences.

๐Ÿƒ Getting started

Note

The Gemini API is free, so long as you're willing to share all of your usage data with Google.

  1. Run git clone https://github.com/c-o-l-i-n/ng-generative-ai-demo.git to clone this repo.

  2. Visit the Google AI Studio to generate an API key.

  3. Create a .env file in the project root with your API key:

    GOOGLE_AI_STUDIO_API_KEY=paste-api-key-here
    
  4. Run npm install to install dependencies.

  5. Run npm run server to start the backend server (server.ts) on port 3000.

  6. In another terminal, run npm start to start the Angular dev server on port 4200.

  7. Navigate to http://localhost:4200/

๐Ÿ”‘ Key takeaways

  • Manage State with Signals: Keep track of the chat state (list of messages and whether the LLM is generating a new message) with Angular Signals.

  • Realtime Text Streaming with RxJS Observables: Utilize RxJS to react to realtime updates from the LLM API.

  • HTTP Client Configuration: Configure the Angular HTTP client to handle realtime text streams:

    1. Provide the HTTP client "with fetch" in app.config.ts:
    provideHttpClient(withFetch());
    1. Tell the HTTP client to observe text events and report progress:
    this.http.post('http://localhost:3000/message', prompt, {
      responseType: 'text',
      observe: 'events',
      reportProgress: true,
    });
  • Blinking Cursor: Create a blinking cursor effect using the CSS ::after pseudo-element and CSS @keyframes:

    .message {
      &.generating {
        &::after {
          content: 'โ–‹';
          animation: fade-cursor ease-in-out 500ms infinite alternate;
        }
      }
    }
    
    @keyframes fade-cursor {
      from {
        opacity: 25%;
      }
      to {
        opacity: 100%;
      }
    }

๐Ÿ”ญ Files to explore

ng-generative-ai-demo's People

Contributors

c-o-l-i-n avatar

Stargazers

Ernesto  Bonet avatar Tomas Rimkus avatar Alvaro Camillo Neto avatar  avatar Solat Ali avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.