This document will guide you through the steps to set up a Typescript server that exposes two endpoints - the first one leverages Firebase Genkit to first expose an endpoint that does generation based on user prompt in the voice of the Terminator, and next exposes several endpoints that show off Genkit's capabilities around schema-based generation, streaming, and more.
Requirements:
- A computer with Node.js (v20+) installed (visit https://nodejs.org/ to download and install Node.js).
- A code editor (e.g., Visual Studio Code preferred)
- Access to the terminal (any of cmd, powershell, bash, zsh, etc.)
- Access to the internet
- Create a new directory and navigate to it.
- Run
npm init -y
to create a new package.json file. - Install typescript, nodemon and ts-node as development dependencies, and express as a production dependency:
npm install express
npm install --save-dev typescript ts-node nodemon @types/express
- Run
npx tsc --init
to create a tsconfig.json file. - Create a new directory named
src
and navigate to it. - Create a new file named
server.ts
and add the following code (no genkit yet, we will create a hello world server first):
import express from 'express';
const app = express();
const port = 3000;
app.get('/api/v1/hello_world', (req, res) => {
res.send('Hello World!');
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`, 'visit http://localhost:3000/api/v1/hello_world to see the response');
});
- Navigate back to the root directory and run
npx nodemon src/server.ts
to start the server. - Open your browser and navigate to
http://localhost:3000/api/v1/hello_world
to see the response. - You have successfully set up a Typescript server! To check-in changes to git, remember to add a gitignore. You can simply run
npx gitignore node
to create a node gitignore file.
- Sign up on https://aistudio.google.com and visit https://aistudio.google.com/app/apikey to get an API key. You may be asked to create a Google Cloud project with billing enabled, first, to get the API key.
- Place the key in a
.env
file in the root directory of this project asGOOGLE_GENAI_API_KEY=<your_api_key_here>
. - Install the Firebase Genkit package and its Google AI plugin:
npm install genkit @genkit-ai/googleai
- Now we can define a new endpoint that uses Firebase Genkit to respond to user prompts:
File: src/generateGemini15FlashResponse.ts
// import the Genkit and Google AI plugin libraries
import { gemini15Flash, googleAI } from '@genkit-ai/googleai';
import { genkit } from 'genkit';
// configure a Genkit instance
const ai = genkit({
plugins: [googleAI()],
model: gemini15Flash, // set default model
});
export async function generateGemini15FlashResponse(prompt: string = 'Hello, Gemini!'): Promise<string> {
// make a generation request
const { text } = await ai.generate({
prompt,
system: 'talk like the terminator',
model: gemini15Flash,
config: {
maxOutputTokens: 400,
stopSequences: ['<end>', '<fin>'],
temperature: 1.2,
topP: 0.4,
topK: 50,
}
});
}
File: src/server.ts
import express from 'express';
import { generateGemini15FlashResponse } from './generateGemini15FlashResponse';
const app = express();
const port = 3000;
app.get('/api/v1/generate_gemini15_flash', async (req, res) => {
res.send(await generateGemini15FlashResponse(req.query.prompt as string));
});
- Create a new file
src/recipeWriterFlow.ts
and add the following code:
// import the Genkit and Google AI plugin libraries
import { gemini15Flash, googleAI } from '@genkit-ai/googleai';
import { genkit } from 'genkit';
// configure a Genkit instance
const ai = genkit({
plugins: [googleAI()],
model: gemini15Flash, // set default model
});
export const recipeWriterFlow = ai.defineFlow(
{
name: 'recipeWriterFlow',
},
async (dishName) => {
const { text } = await ai.generate({
system: 'You are a top chef at a michein star restaurant, think Gordon Ramsay (do not replicate his foul language, though). You are providing an elegant recipe for the dish the user wants to know about.',
model: gemini15Flash,
prompt: `Write me a recipe for ${dishName}.`,
});
return text;
}
);
Update file src/server.ts
as follows:
import express from 'express';
import { generateGemini15FlashResponse } from './generateGemini15FlashResponse';
import { recipeWriterFlow } from './recipeWriterFlow';
...
app.get('/api/v1/recipe_writer', async (req, res) => {
res.send(await recipeWriterFlow(req.query.dishName as string));
});
...
- Now try the flow out first using genkit-cli:
npm install genkit-cli --save-dev
npx genkit-cli start npm run serve:dev