This demo showcases a simple chatbot that utilizes Genkit's tool calling feature. The chatbot is capable of responding to user prompts by using two defined tools: getWeather, which simulates fetching weather data, and rollDice, which simulates rolling a six-sided die.

Things to Try

  • Ask the bot for the weather in a specific location.
  • Ask the bot to roll a die.
  • Combine requests, for example "What's the weather like, and then roll a die".
  • Change the system message to influence the bot's behavior, for example "Roll a die each time before responding. If it's 1-3, talk like a pirate. If it's 4-6, talk like a 1950's scifi robot.".

Source Code

// api/route.ts

import { z } from "genkit";
// this example requires beta features
import { genkit } from "genkit/beta";

import { googleAI, gemini20Flash } from "@genkit-ai/googleai";

const ai = genkit({
  plugins: [googleAI()], // set the GOOGLE_API_KEY env variable
  model: gemini20Flash,
});

import genkitEndpoint from "@/lib/genkit-endpoint";

const getWeather = ai.defineTool(
  {
    name: "getWeather",
    description: "Gets the current weather in a given location",
    inputSchema: z.object({
      location: z
        .string()
        .describe("The location to get the current weather for"),
    }),
    outputSchema: z.object({
      temperature: z
        .number()
        .describe("The current temperature in degrees Fahrenheit"),
      condition: z
        .enum(["sunny", "cloudy", "rainy", "snowy"])
        .describe("The current weather condition"),
    }),
  },
  async ({ location }) => {
    // Fake weather data
    const randomTemp = Math.floor(Math.random() * 30) + 50; // Random temp between 50 and 80
    const conditions = ["sunny", "cloudy", "rainy", "snowy"] as any;
    const randomCondition =
      conditions[Math.floor(Math.random() * conditions.length)];

    return { temperature: randomTemp, condition: randomCondition };
  }
);

const rollDice = ai.defineTool(
  {
    name: "rollDice",
    description: "Rolls a six-sided die",
    outputSchema: z.number().int().min(1).max(6),
  },
  async () => {
    return Math.floor(Math.random() * 6) + 1;
  }
);

export const POST = genkitEndpoint(async ({ system, messages, prompt }) => {
  const chat = ai.chat({ system, messages, tools: [getWeather, rollDice] });
  return chat.sendStream({ prompt });
});
Genkit by Example uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.