DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a Voice-Powered Smart Kitchen App Using LLaMA 3.1, Firebase, and Node.js
  • AI-Powered Flashcard Application With Next.js, Clerk, Firebase, Material UI, and LLaMA 3.1
  • AI-Driven Development: Maximizing Efficiency With GitHub Copilot and Seamless Chat Collaboration in Visual Studio IDE
  • How Can Developers Drive Innovation by Combining IoT and AI?

Trending

  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  • How to Configure and Customize the Go SDK for Azure Cosmos DB
  • A Modern Stack for Building Scalable Systems
  • Kubeflow: Driving Scalable and Intelligent Machine Learning Systems
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Building NLU Flows With Firebase GenKit and GitHub Models Powered by LLMs

Building NLU Flows With Firebase GenKit and GitHub Models Powered by LLMs

Learn how to set up an NLU flow using Firebase Genkit AI and Firebase Functions to detect intents and entities from text using GitHub Models.

By 
Xavier Portilla Edo user avatar
Xavier Portilla Edo
DZone Core CORE ·
Nov. 13, 24 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
544 Views

Join the DZone community and get the full member experience.

Join For Free

In this project, we’ll set up a Natural Language Understanding (NLU) flow using Firebase’s Genkit AI and Firebase Functions. For reference, the NLU flow detects intents and extracts entities from a given text input.

We'll use GitHub Models using the Genkit GitHub models plugin, along with the following tools:

  • Firebase Functions
  • Firebase Genkit
  • GitHub Models

We'll also use the following Node.js packages:

  1. @genkit-ai/firebase: Genkit Firebase SDK, to be able to use Genkit in Firebase Functions
  2. genkitx-github: Genkit GitHub plugin, to be able to use GitHub Models in Genkit
  3. @genkit-ai/ai, @genkit-ai/core and @genkit-ai/flow: Genkit AI Core SDK
  4. @genkit-ai/dotprompt: Plugin to use DotPrompt in Genkit

Primary Setup

  1. Clone this repository: GitHub repository.
  2. Run npm install to install the dependencies in the functions folder.
  3. Run firebase login to log in to your Firebase account.
  4. Install genkit-cli by running npm install -g genkit.

This repository is supposed to be used with NodeJS version 20.

Step-by-Step Guide

1. Run the Firebase Emulator

To run the function locally, run GENKIT_ENV=dev firebase emulators:start --inspect-functions.

The emulator will be available at http://localhost:4000. 

2. Open Genkit UI

Go to the functions folder and run genkit start --attach http://localhost:3100 --port 4001 to open the Genkit UI. The UI will be available at http://localhost:4001.

Screenshot of Firebase Genkit

Screenshot of Firebase Genkit

3. Set Up Configuration

  1. Ensure you have the necessary Firebase configuration files (firebase.json, .firebaserc).
  2. Update the nlu/intents.yml and nlu/entities.yml files with your intents and entities.

4. Run Development Tasks

Linting

Run ESLint to check for code quality issues:

Shell
 
npm run lint


Building

Compile the TypeScript code:

Shell
 
npm run build


5. Review the Code Explanation

Configuration

The configureGenkit function is called to set up the Genkit environment with plugins for Firebase, GitHub, and Dotprompt. It also sets the log level to “debug” and enables tracing and metrics.

TypeScript
 
configureGenkit({
  plugins: [firebase(), github({}), dotprompt()],
  logLevel: "debug",
  enableTracingAndMetrics: true,
});


Flow Definition

The nluFlow is defined using the onFlow function.

  • Configuration: The flow is named nluFlow and has input and output schemas defined using Zod. The input schema expects an object with a text string, and the output schema is a string. The flow does not require authentication (noAuth).
  • nluFlow: The flow processes the input.
    • Schema definition: Defines an nluOutput schema with intent and entities.
    • Prompt reference: Gets a reference to the “nlu” dotprompt file.
    • File reading: Reads intents.yml and entities.yml files.
    • Prompt generation: Uses the nluPrompt to generate output based on the input text and the read intents and entities.
    • Return output: Returns the generated output with type nluOutput.
TypeScript
 
export const nluFlow = onFlow(
  {
    name: "nluFlow",
    inputSchema: z.object({text: z.string()}),
    outputSchema: z.string(),
    authPolicy: noAuth(), // Not requiring authentication.
  },
  async (toDetect) => {
    const nluOutput = defineSchema(
      "nluOutput",
      z.object({
        intent: z.string(),
        entities: z.map(z.string(), z.string()),
      }),
    );

    const nluPrompt = promptRef("nlu");

    const intents = readFileSync('nlu/intents.yml','utf8');
    const entities = readFileSync('nlu/entities.yml','utf8');

    const result = await nluPrompt.generate<typeof nluOutput>({
      input: {
        intents: intents,
        entities: entities,
        user_input: toDetect.text,
      },
    });

    return result.output();
  },
);


6. Define the Prompt

This nlu.prompt file defines a prompt for an NLU model. Here’s a breakdown of its components:

Model Specification

This specifies the LLM model to be used; in this case, github/gpt-4o.

YAML
 
model: github/gpt-4o


Input Schema

This defines the input schema for the prompt. It expects three string inputs:

  • intents: A string representing the intents.
  • entities: A string representing the entities.
  • user_input: A string representing the user’s input text.
YAML
 
input:
  schema:
    intents: string
    entities: string
    user_input: string


Output Specification

This defines the output format and schema. The output will be in JSON format and should conform to the nluOutput schema.

YAML
 
output:
  format: json
  schema: nluOutput


Prompt Text

This is the actual prompt text that will be used by the model. It provides context and instructions to the model:

  • It describes the role of the model as an NLU system.
  • It includes placeholders ({{intents}}, {{entities}}, {{user_input}}) that will be replaced with the actual input values.
  • It asks the model to specify the detected intent and entity based on the provided user input.
YAML
 
  ---
  model: github/gpt-4o
  input:
 schema:
   intents: string
   entities: string
   user_input: string
  output:
 format: json
 schema: nluOutput
  ---
You are a NLU that detects intents and extract entities from a given text.

you have these intents and utterances:
{{intents}}
You also have these entities:
{{entities}}

The user says: {{user_input}}
Please specify the intent detected and the entity detected


7. Use the NLU Flow

The main NLU flow is defined in index.ts. It reads intents and entities from YAML files and uses a prompt defined in nlu.prompt to generate responses.

Intents

The intents are defined in the nlu/intents.yml file. Each intent has a name and a list of training phrases.

As an example, the following intent is defined in the nlu/intents.yml file:

YAML
 
order_shoes:
  utterances: 
    - I want a pair of shoes from {shoes_brand}
    - a shoes from {shoes_brand}


The format is as follows:

YAML
 
intent-name:
  utterances:
    - training phrase 1
    - training phrase 2
    - ...


Entities

The entities are defined in the nlu/entities.yml file. Each entity has a name and a list of synonyms.

As an example, the following entity is defined in the nlu/entities.yml file:

YAML
 
shoes_brand:
  examples:
    - Puma
    - Nike


The format is as follows:

YAML
 
entity-name:
  examples:
    - synonym 1
    - synonym 2
    - ...


Example

To trigger the NLU flow, send a request with the following structure:

JSON
 
{
  "text": "Your input text here"
}


The response will be a JSON object with the following structure:

JSON
 
{
  "intent": "intent-name",
  "entities": {
    "entity-name": "entity-value"
  }
}


8. Deploy the Function

To deploy the function, run firebase deploy --only functions.

Conclusion

Using Genkit and GitHub Models in Firebase Functions provides an easy way to set up NLU flows. You can use this example as a foundation to create customized functions using Genkit and GitHub Models. You can find the full code of this project in the GitHub repository.

AI Firebase GitHub

Published at DZone with permission of Xavier Portilla Edo, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a Voice-Powered Smart Kitchen App Using LLaMA 3.1, Firebase, and Node.js
  • AI-Powered Flashcard Application With Next.js, Clerk, Firebase, Material UI, and LLaMA 3.1
  • AI-Driven Development: Maximizing Efficiency With GitHub Copilot and Seamless Chat Collaboration in Visual Studio IDE
  • How Can Developers Drive Innovation by Combining IoT and AI?

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: