developing-genkit-go
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGenkit Go
Genkit Go
Genkit Go is an AI SDK for Go that provides generation, structured output, streaming, tool calling, prompts, and flows with a unified interface across model providers.
Genkit Go 是面向 Go 语言的 AI SDK,提供内容生成、结构化输出、流式传输、工具调用、提示词管理、工作流能力,对不同模型提供商提供统一的调用接口。
Hello World
Hello World
go
package main
import (
"context"
"fmt"
"log"
"net/http"
"github.com/genkit-ai/genkit/go/ai"
"github.com/genkit-ai/genkit/go/genkit"
"github.com/genkit-ai/genkit/go/plugins/googlegenai"
"github.com/genkit-ai/genkit/go/plugins/server"
)
func main() {
ctx := context.Background()
g := genkit.Init(ctx, genkit.WithPlugins(&googlegenai.GoogleAI{}))
genkit.DefineFlow(g, "jokeFlow", func(ctx context.Context, topic string) (string, error) {
return genkit.GenerateText(ctx, g,
ai.WithModelName("googleai/gemini-flash-latest"),
ai.WithPrompt("Tell me a joke about %s", topic),
)
})
mux := http.NewServeMux()
for _, f := range genkit.ListFlows(g) {
mux.HandleFunc("POST /"+f.Name(), genkit.Handler(f))
}
log.Fatal(server.Start(ctx, "127.0.0.1:8080", mux))
}go
package main
import (
"context"
"fmt"
"log"
"net/http"
"github.com/genkit-ai/genkit/go/ai"
"github.com/genkit-ai/genkit/go/genkit"
"github.com/genkit-ai/genkit/go/plugins/googlegenai"
"github.com/genkit-ai/genkit/go/plugins/server"
)
func main() {
ctx := context.Background()
g := genkit.Init(ctx, genkit.WithPlugins(&googlegenai.GoogleAI{}))
genkit.DefineFlow(g, "jokeFlow", func(ctx context.Context, topic string) (string, error) {
return genkit.GenerateText(ctx, g,
ai.WithModelName("googleai/gemini-flash-latest"),
ai.WithPrompt("Tell me a joke about %s", topic),
)
})
mux := http.NewServeMux()
for _, f := range genkit.ListFlows(g) {
mux.HandleFunc("POST /"+f.Name(), genkit.Handler(f))
}
log.Fatal(server.Start(ctx, "127.0.0.1:8080", mux))
}Core Features
核心功能
Load the appropriate reference based on what you need:
| Feature | Reference | When to load |
|---|---|---|
| Initialization | references/getting-started.md | Setting up |
| Generation | references/generation.md | |
| Prompts | references/prompts.md | |
| Tools | references/tools.md | |
| Flows & HTTP | references/flows-and-http.md | |
| Model Providers | references/providers.md | Google AI, Vertex AI, Anthropic, OpenAI-compatible, Ollama setup |
根据你的需求加载对应的参考文档:
| 功能 | 参考文档 | 适用场景 |
|---|---|---|
| 初始化 | references/getting-started.md | 配置 |
| 内容生成 | references/generation.md | |
| 提示词 | references/prompts.md | |
| 工具 | references/tools.md | |
| 工作流与HTTP | references/flows-and-http.md | |
| 模型提供商 | references/providers.md | Google AI、Vertex AI、Anthropic、OpenAI兼容模型、Ollama配置 |
Genkit CLI
Genkit CLI
Check if installed:
genkit --versionInstallation:
bash
curl -sL cli.genkit.dev | bashKey commands:
bash
undefined检查是否安装:
genkit --version安装方式:
bash
curl -sL cli.genkit.dev | bash核心命令:
bash
undefinedStart app with Developer UI (tracing, flow testing) at http://localhost:4000
Start app with Developer UI (tracing, flow testing) at http://localhost:4000
genkit start -- go run .
genkit start -o -- go run . # also opens browser
genkit start -- go run .
genkit start -o -- go run . # also opens browser
Run a flow directly from the CLI
Run a flow directly from the CLI
genkit flow:run myFlow '{"data": "input"}'
genkit flow:run myFlow '{"data": "input"}' --stream # with streaming
genkit flow:run myFlow '{"data": "input"}' --wait # wait for completion
genkit flow:run myFlow '{"data": "input"}'
genkit flow:run myFlow '{"data": "input"}' --stream # with streaming
genkit flow:run myFlow '{"data": "input"}' --wait # wait for completion
Look up Genkit documentation
Look up Genkit documentation
genkit docs:search "streaming" go
genkit docs:list go
genkit docs:read go/flows.md
See [references/getting-started.md](references/getting-started.md) for full CLI and Developer UI details.genkit docs:search "streaming" go
genkit docs:list go
genkit docs:read go/flows.md
完整的CLI和开发者UI详情请查看 [references/getting-started.md](references/getting-started.md)。Key Guidance
核心使用指引
- Pass explicitly. The
ginstance returned by*Genkitis the central registry. Pass it to all Genkit functions rather than storing it as a global. This is a core pattern throughout the SDK.genkit.Init - Wrap AI logic in flows. Flows give you tracing, observability, HTTP deployment via , and the ability to test from the Developer UI and CLI. Any generation call worth keeping should live in a flow.
genkit.Handler - Use struct tags on output types. The model uses these descriptions to understand what each field should contain. Without them, structured output quality drops significantly.
jsonschema:"description=..." - Write good tool descriptions. The model decides which tools to call based on their description string. Vague descriptions lead to missed or incorrect tool calls.
- Use files for complex prompts. They separate prompt content from Go code, support Handlebars templating, and can be iterated on without recompilation. Code-defined prompts are better for simple, single-line cases.
.prompt - Look up the latest model IDs. Model names change frequently. Check provider documentation for current model IDs rather than relying on hardcoded names. See references/providers.md.
- 显式传递 参数。
g返回的genkit.Init实例是核心注册中心,请将其传递给所有 Genkit 函数,而不是将其存储为全局变量,这是整个SDK的核心设计模式。*Genkit - 将AI逻辑封装在工作流中。 工作流为你提供链路追踪、可观测性、通过 快速部署HTTP服务的能力,同时支持从开发者UI和CLI进行测试,所有需要长期维护的生成调用都应该放在工作流中。
genkit.Handler - 在输出类型上使用 结构体标签。 模型会通过这些描述理解每个字段应该包含的内容,缺少这些标签会大幅降低结构化输出的质量。
jsonschema:"description=..." - 编写清晰的工具描述。 模型会根据工具的描述字符串决定调用哪个工具,模糊的描述会导致漏调用或错调用工具。
- 复杂提示词使用 文件存储。 这样可以将提示词内容与Go代码分离,支持Handlebars模板,而且迭代提示词无需重新编译。代码定义的提示词更适合简单的单行场景。
.prompt - 查询最新的模型ID。 模型名称会频繁更新,请参考模型提供商的文档获取当前可用的模型ID,不要依赖硬编码的名称,详情见 references/providers.md。