Skip to content

Commit

Permalink
feat: Move IDX template from idx github to Genkit github (#1681)
Browse files Browse the repository at this point in the history
* feat: Move IDX template from idx github to Genkit github to make it easier to maintain
  • Loading branch information
i2amsam authored Jan 29, 2025
1 parent 94ea90c commit af71fd7
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 0 deletions.
3 changes: 3 additions & 0 deletions samples/js-character-generator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test/
node_modules/
.genkit/
38 changes: 38 additions & 0 deletions samples/js-character-generator/.idx/dev.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# To learn more about how to use Nix to configure your environment
# see: https://developers.google.com/idx/guides/customize-idx-env
{ pkgs, ... }: {
# Which nixpkgs channel to use.
channel = "stable-24.05"; # or "unstable"
# Use https://search.nixos.org/packages to find packages
packages = [
pkgs.nodejs_20
pkgs.util-linux
# pkgs.go
];
# Sets environment variables in the workspace
env = {
#TODO Get a API key from https://g.co/ai/idxGetGeminiKey
GOOGLE_GENAI_API_KEY = "TODO";
};
idx = {
internal.templates-cli.enable = true;
# Search for the extensions you want on https://open-vsx.org/ and use "publisher.id"
extensions = [
# "vscodevim.vim"
# "golang.go"
];

# Workspace lifecycle hooks
workspace = {
# Runs when a workspace is first created
onCreate = {
npm-install = "npm ci --no-audit --prefer-offline --no-progress --timing";
default.openFiles = [ "README.md" "index.ts" ];
};
# Runs when the workspace is (re)started
onStart = {
run-server = "npm run dev";
};
};
};
}
20 changes: 20 additions & 0 deletions samples/js-character-generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Character Generator

<a href="https://idx.google.com/new?template=https%3A%2F%2Fgithub.com%2Ffirebase%2Fgenkit%2Ftree%2Fmain%2Fsamples%2Fjs-character-generator">
<picture>
<source
media="(prefers-color-scheme: dark)"
srcset="https://cdn.idx.dev/btn/open_dark_32.svg">
<source
media="(prefers-color-scheme: light)"
srcset="https://cdn.idx.dev/btn/open_light_32.svg">
<img
height="32"
alt="Open in IDX"
src="https://cdn.idx.dev/btn/open_purple_32.svg">
</picture>
</a>

This sample is used as the Genkit template in Project IDX, you can check it out in IDX!

To run it locally, set a [Gemini API key](https://aistudio.google.com/app/apikey) in your environment with `export GOOGLE_GENAI_API_KEY=<>` and start the app with `npm install && npm run dev`
7 changes: 7 additions & 0 deletions samples/js-character-generator/README_IDX.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#Genkit

This is a simple demonstration web app using the [Firebase Genkit Library](https://github.com/firebase/genkit) with Gemini to characters for an adventure game.

In IDX, get started by with API key at https://g.co/ai/idxGetGeminiKey and enter it in `.idx/dev.nix` and rebuild the environment.

After rebuilding the environment, open a new terminal (`Ctrl`+ `` ` ``) and follow the link that said "Genkit Developer UI" to use Genkit's built-in local developer playground.
8 changes: 8 additions & 0 deletions samples/js-character-generator/idx-template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "Genkit",
"description": "Build a new web app using Firebase Genkit",
"categories": ["Web app", "AI ML"],
"icon": "https://www.gstatic.com/monospace/240513/logo_firebase.svg",
"params": [],
"publisher": "Google LLC"
}
16 changes: 16 additions & 0 deletions samples/js-character-generator/idx-template.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{pkgs, language ? "js", tos ? "false", ... }: {
packages = [
pkgs.nodejs
];
bootstrap = ''
mkdir "$out"
mkdir "$out"/.idx
cp -r ${./.idx}/. "$out/.idx/"
cp -f ${./package.json} "$out/package.json"
cp -f ${./package-lock.json} "$out/package-lock.json"
cp -f ${./index.ts} "$out/index.ts"
cp -f ${./.gitignore} "$out/.gitignore"
cp ${./README_IDX.md} "$out"/README.md
chmod -R u+w "$out"
'';
}
53 changes: 53 additions & 0 deletions samples/js-character-generator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright 2025 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.
*/

import { gemini15Flash, googleAI } from '@genkit-ai/googleai';
import { genkit, z } from 'genkit';

const ai = genkit({
plugins: [googleAI()],
});

const prompt = ai.definePrompt(
{
name: 'Character Prompt',
model: gemini15Flash,
input: {
schema: z.object({
inspiration: z.string(),
}),
},
output: {
format: 'json',
schema: z.object({
name: z.string(),
strength: z.number(),
intelligence: z.number(),
description: z.string(),
}),
},
},
`
You're a expert DnD designer, create a new character.
Base the character on {{inspiration}} but don't make it
and exact match.
`
);

(async () => {
console.log((await prompt({ inspiration: 'Yogi Berra' })).output);
})();
21 changes: 21 additions & 0 deletions samples/js-character-generator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "Character Generator",
"version": "0.1.0",
"description": "A simple Firebase Genkit app to generate fantasy characters",
"main": "index.js",
"scripts": {
"dev": "genkit start -- npx tsx --watch index.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"genkit-cli": "^0.9",
"tsx": "^4.19.2",
"typescript": "^5.6.3"
},
"dependencies": {
"@genkit-ai/googleai": "^0.9",
"genkit": "^0.9"
}
}

0 comments on commit af71fd7

Please sign in to comment.