Skip to main content

Generating content

Firebase Genkit provides an easy interface for generating content with LLMs.

Models

Models in Firebase Genkit are libraries and abstractions that provide access to various Google and non-Google LLMs.

Models are fully instrumented for observability and come with tooling integrations provided by the Genkit Developer UI -- you can try any model using the model runner.

When working with models in Genkit, you first need to configure the model you want to work with. Model configuration is performed by the plugin system. In this example you are configuring the Vertex AI plugin, which provides Gemini models.

import {
{% includecode github_path="firebase/genkit/go/internal/doc-snippets/models.go" region_tag="import" %}
}
{% includecode github_path="firebase/genkit/go/internal/doc-snippets/models.go" region_tag="init" adjust_indentation="auto" %}

Note: Different plugins and models use different methods of authentication. For example, Vertex API uses the Google Auth Library so it can pull required credentials using Application Default Credentials.

To use models provided by the plugin, you need a reference to the specific model and version:

{% includecode github_path="firebase/genkit/go/internal/doc-snippets/models.go" region_tag="model" adjust_indentation="auto" %}

Supported models

Genkit provides model support through its plugin system. The following plugins are officially supported:

PluginModels
Google Generative AIGemini Pro, Gemini Pro Vision
Google Vertex AIGemini Pro, Gemini Pro Vision, Gemini 1.5 Flash, Gemini 1.5 Pro, Imagen2
OllamaMany local models, including Gemma, Llama 2, Mistral, and more

See the docs for each plugin for setup and usage information.

How to generate content

Genkit provides a simple helper function for generating content with models.

To just call the model:

{% includecode github_path="firebase/genkit/go/internal/doc-snippets/models.go" region_tag="call" adjust_indentation="auto" %}

You can pass options along with the model call. The options that are supported depend on the model and its API.

{% includecode github_path="firebase/genkit/go/internal/doc-snippets/models.go" region_tag="options" adjust_indentation="auto" %}

Streaming responses

Genkit supports chunked streaming of model responses. To use chunked streaming, pass a callback function to Generate():

{% includecode github_path="firebase/genkit/go/internal/doc-snippets/models.go" region_tag="streaming" adjust_indentation="auto" %}

Multimodal input

If the model supports multimodal input, you can pass image prompts:

{% includecode github_path="firebase/genkit/go/internal/doc-snippets/models.go" region_tag="multimodal" adjust_indentation="auto" %}

The exact format of the image prompt (https URL, gs URL, data URI) is model-dependent.

Function calling (tools)

Genkit models provide an interface for function calling, for models that support it.

{% includecode github_path="firebase/genkit/go/internal/doc-snippets/models.go" region_tag="tools" adjust_indentation="auto" %}

This will automatically call the tools in order to fulfill the user prompt.

Recording message history

Genkit models support maintaining a history of the messages sent to the model and its responses, which you can use to build interactive experiences, such as chatbots.

In the first prompt of a session, the "history" is simply the user prompt:

{% includecode github_path="firebase/genkit/go/internal/doc-snippets/models.go" region_tag="hist1" adjust_indentation="auto" %}

When you get a response, add it to the history:

{% includecode github_path="firebase/genkit/go/internal/doc-snippets/models.go" region_tag="hist2" adjust_indentation="auto" %}

You can serialize this history and persist it in a database or session storage. For subsequent user prompts, add them to the history before calling Generate():

{% includecode github_path="firebase/genkit/go/internal/doc-snippets/models.go" region_tag="hist3" adjust_indentation="auto" %}

If the model you're using supports the system role, you can use the initial history to set the system message:

{% includecode github_path="firebase/genkit/go/internal/doc-snippets/models.go" region_tag="hist4" adjust_indentation="auto" %}