Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(go): basic 0.9 updates #1308

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions docs-go/cloud-run.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Firebase Genkit with Cloud Run

You can deploy Firebase Genkit flows as web services using Cloud Run. This page,
as an example, walks you through the process of deploying the default sample
flow.
You can deploy Firebase Genkit flows as web services using Cloud Run.

1. Install the [Google Cloud CLI](https://cloud.google.com/sdk/docs/install) if
you haven't already.
Expand Down Expand Up @@ -36,23 +34,26 @@ flow.
go mod init example/cloudrun
```

1. Initialize Genkit in your project:
1. Install the Genkit package and the model plugin you want to use:

```posix-terminal
genkit init
go get "github.com/firebase/genkit/go"
```

Select the model provider you want to use.
One of the following:

Accept the defaults for the remaining prompts. The `genkit` tool will create
a sample source file to get you started developing your own AI flows.
For the rest of this tutorial, however, you'll just deploy the sample flow.
```posix-terminal
go get "github.com/firebase/genkit/go/plugins/googleai"
```

```posix-terminal
go get "github.com/firebase/genkit/go/plugins/vertexai"
```

1. Edit the sample file (`main.go` or `genkit.go`) to explicitly specify the
port the flow server should listen on:
1. Create a file (`main.go`) for your flows:

```golang
{% includecode github_path="firebase/genkit/go/internal/doc-snippets/flows.go" region_tag="init" adjust_indentation="auto" %}
{% includecode github_path="firebase/genkit/go/internal/doc-snippets/deploy/main.go" region_tag="main" adjust_indentation="auto" %}
```

1. Make API credentials available to your deployed function. Do one of the
Expand Down Expand Up @@ -118,7 +119,7 @@ flow.
1. Start the UI:

```posix-terminal
genkit start
genkit start -- go run main.go
kevinthecheung marked this conversation as resolved.
Show resolved Hide resolved
```

1. In the developer UI (http://localhost:4000/), run the flow:
Expand All @@ -139,14 +140,14 @@ flow.
- {Gemini (Google AI)}

```posix-terminal
gcloud run deploy --port 3400 \
gcloud run deploy \
apascal07 marked this conversation as resolved.
Show resolved Hide resolved
--update-secrets=GOOGLE_GENAI_API_KEY=<your-secret-name>:latest
```

- {Gemini (Vertex AI)}

```posix-terminal
gcloud run deploy --port 3400 \
gcloud run deploy \
--set-env-vars GCLOUD_PROJECT=<your-gcloud-project> \
--set-env-vars GCLOUD_LOCATION=us-central1
```
Expand Down
28 changes: 14 additions & 14 deletions docs-go/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
You can deploy Firebase Genkit flows as web services using any service that can
host a Go binary.
This page, as an example, walks you through the general process of deploying the
default sample flow, and points out where you must take provider-specific
a sample flow, and points out where you must take provider-specific
actions.

1. Create a directory for the Genkit sample project:
Expand All @@ -22,28 +22,28 @@ actions.
go mod init example/cloudrun
```

1. Initialize Genkit in your project:
1. Install the Genkit package and the model plugin you want to use:

```posix-terminal
genkit init
go get "github.com/firebase/genkit/go"
```

Select the model provider you want to use.
One of the following:

Accept the defaults for the remaining prompts. The `genkit` tool will create
a sample source file to get you started developing your own AI flows.
For the rest of this tutorial, however, you'll just deploy the sample flow.
```posix-terminal
go get "github.com/firebase/genkit/go/plugins/googleai"
```

```posix-terminal
go get "github.com/firebase/genkit/go/plugins/vertexai"
```

1. Edit the sample file (`main.go` or `genkit.go`) to explicitly specify the
port the flow server should listen on:
1. Create a file (`main.go`) for your flows:

```golang
{% includecode github_path="firebase/genkit/go/internal/doc-snippets/flows.go" region_tag="init" adjust_indentation="auto" %}
{% includecode github_path="firebase/genkit/go/internal/doc-snippets/deploy/main.go" region_tag="main" adjust_indentation="auto" %}
```

If your provider requires you to listen on a specific port, be sure to
configure Genkit accordingly.

1. Implement some form of authentication and authorization to gate access to
the flows you plan to deploy.

Expand Down Expand Up @@ -120,7 +120,7 @@ actions.
1. Start the UI:

```posix-terminal
genkit start
genkit start -- go run main.go
kevinthecheung marked this conversation as resolved.
Show resolved Hide resolved
```

1. In the developer UI (http://localhost:4000/), run the flow:
Expand Down
191 changes: 74 additions & 117 deletions docs-go/get-started-go.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,71 @@ exploration.
If you discover issues with the libraries or this documentation please report
them in our [GitHub repository](https://github.com/firebase/genkit/).

To get started with Genkit, install the Genkit CLI and run
`genkit init` in a Go project. The rest of this page shows you how.
This page shows you how to get started with Genkit in a Go project.

## Requirements

- Go 1.22 or later. See [Download and install](https://go.dev/doc/install) in
the official Go docs.
Go 1.22 or later. See [Download and install](https://go.dev/doc/install) in
the official Go docs.

- Node.js 20 or later (for the Genkit CLI and UI). See the next section for a
brief guide on installing Node.
## Install Genkit dependencies {:#install}

## Install Genkit {:#install}
1. If you don't already have a Go project that you want to add AI features to,
create a new module in an empty directory:

1. If you don't already have Node 20 or newer on your system, install it now.
```posix-terminal
go mod init example/genkit-getting-started
```

1. Install the Genkit package and the `googleai` model plugin:

```posix-terminal
go get "github.com/firebase/genkit/go"

go get "github.com/firebase/genkit/go/plugins/googleai"
```

## Configure your model API key

For this guide, we’ll show you how to use the Gemini API which provides a
generous free tier and does not require a credit card to get started. To use the
Gemini API, you'll need an API key. If you don't already have one, create a key
in Google AI Studio.

[Get an API key from Google AI Studio](https://makersuite.google.com/app/apikey)

After you’ve created an API key, set the `GOOGLE_GENAI_API_KEY` environment
variable to your key with the following command:

```posix-terminal
export GOOGLE_GENAI_API_KEY=<your API key>
```

Note: While this tutorial uses the Gemini API from AI Studio, Genkit supports a
wide variety of model providers including
[Gemini from Vertex AI](/docs/genkit/plugins/vertex-ai#generative_ai_models),
Anthropic’s Claude 3 models and Llama 3.1 through the
[Vertex AI Model Garden](/docs/genkit/plugins/vertex-ai#anthropic_claude_3_on_vertex_ai_model_garden),
open source models through
[Ollama](/docs/genkit/plugins/ollama), and several other
[community-supported providers](/docs/genkit/models#models-supported) like
OpenAI and Cohere.

## Make your first request

Get started with Genkit in just a few lines of simple code.

Recommendation: The [`nvm`](https://github.com/nvm-sh/nvm) and
```go
{% includecode github_path="firebase/genkit/go/internal/doc-snippets/init/main.go" region_tag="main" adjust_indentation="auto" %}
```

## Optional: Install the Genkit CLI

Genkit has a CLI and developer UI that helps you locally test and debug your
app. To install these tools:

1. If you don't already have Node 20 or newer on your system,
the [`nvm`](https://github.com/nvm-sh/nvm) and
[`nvm-windows`](https://github.com/coreybutler/nvm-windows) tools are a
convenient way to install specific versions of Node if it's not already
installed on your system. These tools install Node on a per-user basis, so you
Expand Down Expand Up @@ -53,116 +102,24 @@ To get started with Genkit, install the Genkit CLI and run
1. Install the Genkit CLI by running the following command:

```posix-terminal
npm i -g genkit
npm i -g genkit-cli
```

This command installs the Genkit CLI into your Node installation directory
so that it can be used outside of a Node project.

## Create and explore a sample project {:#explore}

1. Create a new project directory:

```posix-terminal
mkdir genkit-intro && cd genkit-intro
```

1. Initialize a Genkit project:

```posix-terminal
genkit init
```

1. Select `Go` as the runtime environment.

1. Select your model:

- {Gemini (Google AI)}

The simplest way to get started is with Google AI Gemini API. Make sure
it's
[available in your region](https://ai.google.dev/available_regions).

[Generate an API key](https://aistudio.google.com/app/apikey) for the
Gemini API using Google AI Studio. Then, set the `GOOGLE_GENAI_API_KEY`
environment variable to your key:

```posix-terminal
export GOOGLE_GENAI_API_KEY=<your API key>
```

- {Gemini (Vertex AI)}

If the Google AI Gemini API is not available in your region, consider
using the Vertex AI API which also offers Gemini and other models. You
will need to have a billing-enabled Google Cloud project, enable AI
Platform API, and set some additional environment variables:

```posix-terminal
gcloud services enable aiplatform.googleapis.com

export GCLOUD_PROJECT=<your project ID>

export GCLOUD_LOCATION=us-central1
```

See [Vertex AI pricing](https://cloud.google.com/vertex-ai/generative-ai/pricing).

1. Specify anything for the module name. For example: `example/genkit-intro`

1. Choose default answers to the rest of the questions, which will
initialize your project folder with some sample code.

The `genkit init` command creates a sample Go module and installs the
required dependencies. The file `main.go` contains a single flow,
`menuSuggestionFlow`, that prompts an LLM to suggest an item for a
restaurant with a given theme.

This file looks something like the following (the plugin configuration steps
might look different if you selected Vertex AI):

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

As you build out your app's AI features with Genkit, you will likely create
flows with multiple steps such as input preprocessing, more sophisticated
prompt construction, integrating external information sources for
retrieval-augmented generation (RAG), and more.

1. Now you can run and explore Genkit features and the sample project locally
on your machine. Download and start the Genkit Developer UI:

```posix-terminal
genkit start
```

<img src="resources/welcome_to_genkit_developer_ui.png" alt="Welcome to
Genkit Developer UI" class="screenshot attempt-right">

The Genkit Developer UI is now running on your machine. When you run models
or flows in the next step, your machine will perform the orchestration tasks
needed to get the steps of your flow working together; calls to external
services such as the Gemini API will continue to be made against live
servers.

Also, because you are in a dev environment, Genkit will store traces and
flow state in local files.

1. The Genkit Developer UI downloads and opens automatically when you run the
`genkit start` command.

The Developer UI lets you see which flows you have defined and models you
configured, run them, and examine traces of previous runs. Try out some of
these features:

- On the **Run** tab, you will see a list of all of the flows that you have
defined and any models that have been configured by plugins.

Click **menuSuggestionFlow** and try running it with some input text (for
example, `"cat"`). If all goes well, you'll be rewarded with a menu
suggestion for a cat themed restaurant.

- On the **Inspect** tab, you'll see a history of flow executions. For each
flow, you can see the parameters that were passed to the flow and a trace
of each step as they ran.
## Next steps

Now that you’re set up to make model requests with Genkit, learn how to use more
Genkit capabilities to build your AI-powered apps and workflows. To get started
with additional Genkit capabilities, see the following guides:

* [Generating content](/docs/genkit-go/models): Learn how to use Genkit’s unified
generation API to generate text and structured data from any supported
model.
* [Creating flows](/docs/genkit-go/flows): Learn how to use special Genkit
functions, called flows, that provide end-to-end observability for workflows
and rich debugging from Genkit tooling.
* [Prompting models](/docs/genkit-go/prompts): Learn how Genkit lets you treat
prompt templates as functions, encapsulating model configurations and
input/output schema.
Loading
Loading