Skip to content

Commit

Permalink
chore: added some samples for Angular and streaming (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelgj authored Jun 25, 2024
1 parent f72d762 commit 9fb985a
Show file tree
Hide file tree
Showing 41 changed files with 1,823 additions and 1 deletion.
44 changes: 43 additions & 1 deletion js/testapps/flow-simple-ai/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ import { defineFirestoreRetriever, firebase } from '@genkit-ai/firebase';
import { defineFlow, run } from '@genkit-ai/flow';
import { googleCloud } from '@genkit-ai/google-cloud';
import { googleAI, geminiPro as googleGeminiPro } from '@genkit-ai/googleai';
import { geminiPro, textEmbeddingGecko, vertexAI } from '@genkit-ai/vertexai';
import {
gemini15ProPreview,
geminiPro,
textEmbeddingGecko,
vertexAI,
} from '@genkit-ai/vertexai';
import { AlwaysOnSampler } from '@opentelemetry/sdk-trace-base';
import { initializeApp } from 'firebase-admin/app';
import { getFirestore } from 'firebase-admin/firestore';
Expand Down Expand Up @@ -331,3 +336,40 @@ export const dotpromptContext = defineFlow(
return result.output() as any;
}
);

const jokeSubjectGenerator = defineTool(
{
name: 'jokeSubjectGenerator',
description: 'can be called to generate a subject for a joke',
},
async () => {
return 'banana';
}
);

export const toolCaller = defineFlow(
{
name: 'toolCaller',
outputSchema: z.string(),
},
async (_, streamingCallback) => {
if (!streamingCallback) {
throw new Error('this flow only works in streaming mode');
}

const { response, stream } = await generateStream({
model: gemini15ProPreview,
config: {
temperature: 1,
},
tools: [jokeSubjectGenerator],
prompt: `tell me a joke`,
});

for await (const chunk of stream()) {
streamingCallback(chunk);
}

return (await response()).text();
}
);
1 change: 1 addition & 0 deletions samples/js-angular/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib
24 changes: 24 additions & 0 deletions samples/js-angular/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Angular and Genkit streaming sample

This is a simple UI for streaming RPG character generator.

To build:

```bash
npm i
npm run build
```

The sample is using Vertex AI, so you'll need to auth:

```bash
gcloud auth application-default login
```

To run the sample:

```bash
npm start
```

Point your browser to http://localhost:4200/
16 changes: 16 additions & 0 deletions samples/js-angular/genkit-app/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Editor configuration, see https://editorconfig.org
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

[*.ts]
quote_type = single

[*.md]
max_line_length = off
trim_trailing_whitespace = false
42 changes: 42 additions & 0 deletions samples/js-angular/genkit-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# See https://docs.github.com/get-started/getting-started-with-git/ignoring-files for more about ignoring files.

# Compiled output
/dist
/tmp
/out-tsc
/bazel-out

# Node
/node_modules
npm-debug.log
yarn-error.log

# IDEs and editors
.idea/
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# Visual Studio Code
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history/*

# Miscellaneous
/.angular/cache
.sass-cache/
/connect.lock
/coverage
/libpeerconnection.log
testem.log
/typings

# System files
.DS_Store
Thumbs.db
27 changes: 27 additions & 0 deletions samples/js-angular/genkit-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# GenkitApp

This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 18.0.2.

## Development server

Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The application will automatically reload if you change any of the source files.

## Code scaffolding

Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`.

## Build

Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory.

## Running unit tests

Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io).

## Running end-to-end tests

Run `ng e2e` to execute the end-to-end tests via a platform of your choice. To use this command, you need to first add a package that implements end-to-end testing capabilities.

## Further help

To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI Overview and Command Reference](https://angular.dev/tools/cli) page.
99 changes: 99 additions & 0 deletions samples/js-angular/genkit-app/angular.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"genkit-app": {
"projectType": "application",
"schematics": {
"@schematics/angular:component": {
"style": "scss"
}
},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:application",
"options": {
"outputPath": "dist/genkit-app",
"index": "src/index.html",
"browser": "src/main.ts",
"polyfills": ["zone.js"],
"tsConfig": "tsconfig.app.json",
"inlineStyleLanguage": "scss",
"assets": [
{
"glob": "**/*",
"input": "public"
}
],
"styles": [
"@angular/material/prebuilt-themes/azure-blue.css",
"src/styles.scss"
],
"scripts": []
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kB",
"maximumError": "1MB"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kB",
"maximumError": "4kB"
}
],
"outputHashing": "all"
},
"development": {
"optimization": false,
"extractLicenses": false,
"sourceMap": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"buildTarget": "genkit-app:build:production"
},
"development": {
"buildTarget": "genkit-app:build:development"
}
},
"defaultConfiguration": "development"
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n"
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"polyfills": ["zone.js", "zone.js/testing"],
"tsConfig": "tsconfig.spec.json",
"inlineStyleLanguage": "scss",
"assets": [
{
"glob": "**/*",
"input": "public"
}
],
"styles": [
"@angular/material/prebuilt-themes/azure-blue.css",
"src/styles.scss"
],
"scripts": []
}
}
}
}
}
}
40 changes: 40 additions & 0 deletions samples/js-angular/genkit-app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "genkit-app",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test"
},
"private": true,
"dependencies": {
"@angular/animations": "^18.0.0",
"@angular/cdk": "^18.0.1",
"@angular/common": "^18.0.0",
"@angular/compiler": "^18.0.0",
"@angular/core": "^18.0.0",
"@angular/forms": "^18.0.0",
"@angular/material": "^18.0.1",
"@angular/platform-browser": "^18.0.0",
"@angular/platform-browser-dynamic": "^18.0.0",
"@angular/router": "^18.0.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
},
"devDependencies": {
"@angular-devkit/build-angular": "^18.0.2",
"@angular/cli": "^18.0.2",
"@angular/compiler-cli": "^18.0.0",
"@types/jasmine": "~5.1.0",
"jasmine-core": "~5.1.0",
"karma": "~6.4.0",
"karma-chrome-launcher": "~3.2.0",
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "~2.1.0",
"typescript": "~5.4.2"
}
}
Binary file added samples/js-angular/genkit-app/public/favicon.ico
Binary file not shown.
36 changes: 36 additions & 0 deletions samples/js-angular/genkit-app/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!--
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<div class="wrapper">
<mat-toolbar color="secondary">
<a routerLink="/" class="home-link"> Firebase Genkit Samples </a>

<mat-tab-nav-panel #tabPanel></mat-tab-nav-panel>

<div class="flex-spacer"></div>
<a
mat-icon-button
href="https://firebase.google.com/docs/genkit"
matTooltip="Docs"
target="_blank">
<mat-icon>description</mat-icon>
</a>
</mat-toolbar>

<article>
<router-outlet></router-outlet>
</article>
</div>
Loading

0 comments on commit 9fb985a

Please sign in to comment.