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

Add comments to init templates #521

Merged
merged 4 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
20 changes: 16 additions & 4 deletions genkit-tools/cli/config/main.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@ import (
"fmt"
"log"

// Import the Genkit core libraries.
"github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/genkit"
$GENKIT_MODEL_IMPORT
)

func $GENKIT_FUNC_NAME() {
ctx := context.Background()
if err := $GENKIT_MODEL_INIT; err != nil {
log.Fatal(err)
}

$GENKIT_MODEL_INIT

// Define a simple flow that prompts an LLM to generate menu suggestions.
genkit.DefineFlow("menuSuggestionFlow", func(ctx context.Context, input string) (string, error) {
m := $GENKIT_MODEL_LOOKUP
$GENKIT_MODEL_LOOKUP
if m == nil {
return "", errors.New("menuSuggestionFlow: failed to find model")
}

// Construct a request and send it to the model API.
resp, err := m.Generate(ctx,
ai.NewGenerateRequest(
&ai.GenerationCommonConfig{Temperature: 1},
Expand All @@ -30,13 +33,22 @@ func $GENKIT_FUNC_NAME() {
if err != nil {
return "", err
}

// Handle the response from the model API. In this sample, we just
// convert it to a string, but more complicated flows might coerce the
// response into structured output or chain the response into another
// LLM call, etc.
text, err := resp.Text()
if err != nil {
return "", fmt.Errorf("menuSuggestionFlow: %v", err)
}
return text, nil
})

// Initialize Genkit and start a flow server. This call must come last,
// after all of your plug-in configuration and flow definitions. When you
// pass a nil configuration to Init, Genkit starts a local flow server,
// which you can interact with using the developer UI.
if err := genkit.Init(ctx, nil); err != nil {
log.Fatal(err)
}
Expand Down
54 changes: 41 additions & 13 deletions genkit-tools/cli/src/commands/init/init-go.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,57 @@ interface ModelOption {
/** Path to Genkit sample template. */
const templatePath = '../../../config/main.go.template';

/** Ollama init call template. Keep indenting to for expected output. */
const ollamaInit = `ollama.Init(ctx, ollama.Config{
ServerAddress: "http://127.0.0.1:11434",
Models: []ollama.ModelDefinition{{Name: "gemma"}},
})`;

/** Model to plugin name. */
const modelOptions: Record<ModelProvider, ModelOption> = {
googleai: {
label: 'Google AI',
package: 'github.com/firebase/genkit/go/plugins/googleai',
init: 'googleai.Init(ctx, "")',
lookup: 'googleai.Model("gemini-1.5-flash")',
init: `// Initialize the Google AI plugin. When you pass an empty string for the
kevinthecheung marked this conversation as resolved.
Show resolved Hide resolved
\t// apiKey parameter, the Google AI plugin will use the value from the
\t// GOOGLE_GENAI_API_KEY environment variable, which is the recommended
\t// practice.
\tif err := googleai.Init(ctx, ""); err != nil {
\t\tlog.Fatal(err)
\t}`,
lookup: `// The Google AI API provides access to several generative models. Here,
\t\t// we specify gemini-1.5-flash.
\t\tm := googleai.Model("gemini-1.5-flash")`,
},
vertexai: {
label: 'Google Cloud Vertex AI',
package: 'github.com/firebase/genkit/go/plugins/vertexai',
init: 'vertexai.Init(ctx, "", "")',
lookup: 'vertexai.Model("gemini-1.5-flash")',
init: `// Initialize the Vertex AI plugin. When you pass an empty string for the
\t// projectID parameter, the Vertex AI plugin will use the value from the
\t// GCLOUD_PROJECT environment variable. When you pass an empty string for
\t// the location parameter, the plugin uses the default value, us-central1.
\tif err := vertexai.Init(ctx, "", ""); err != nil {
\t\tlog.Fatal(err)
\t}`,
lookup: `// The Vertex AI API provides access to several generative models. Here,
\t\t// we specify gemini-1.5-flash.
\t\tm := vertexai.Model("gemini-1.5-flash")`,
},
ollama: {
label: 'Ollama (e.g. Gemma)',
package: 'github.com/firebase/genkit/go/plugins/ollama',
init: ollamaInit,
lookup: 'ollama.Model("gemma")',
init: `// Initialize the Ollama plugin.
\terr := ollama.Init(ctx, ollama.Config{
\t\t// The address of your Ollama API server. This is often a different host
\t\t// from your app backend (which runs Genkit), in order to run Ollama on
\t\t// a GPU-accelerated machine.
ServerAddress: "http://127.0.0.1:11434",

\t\t// The models you want to use. These must already be downloaded and
\t\t// available to the Ollama server.
\t\tModels: []ollama.ModelDefinition{{Name: "gemma"}},
\t})
\tif err != nil {
\t\tlog.Fatal(err)
\t}`,
lookup: `// Ollama provides an interface to many open generative models. Here,
\t\t// we specify Google's Gemma model, which we configured the Ollama
\t\t// plugin to provide, above.
\t\tm := ollama.Model("gemma")`,
},
none: {
label: 'None',
Expand Down Expand Up @@ -206,7 +232,9 @@ async function generateSampleFile(model: ModelProvider) {
)
.replace(
'$GENKIT_MODEL_IMPORT',
modelOption.package ? `"${modelOption.package}"` : ''
modelOption.package
? `\n\t// Import the ${modelOption.label} plugin.\n\t"${modelOption.package}"`
: ''
)
.replace('$GENKIT_MODEL_INIT', modelOption.init)
.replace('$GENKIT_MODEL_LOOKUP', modelOption.lookup);
Expand Down
Loading