kimi

package
v1.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

README

Kimi Plugin

This plugin provides Genkit support for Moonshot AI's OpenAI-compatible Kimi models.

Setup

Set a Moonshot API key:

export KIMI_API_KEY=<your-api-key>

MOONSHOT_API_KEY is also accepted. The plugin uses https://api.moonshot.ai/v1 by default; set KIMI_BASE_URL or MOONSHOT_BASE_URL, or pass option.WithBaseURL through the plugin's Opts, to use another Moonshot-compatible endpoint.

import (
    "context"

    "github.com/firebase/genkit/go/ai"
    "github.com/firebase/genkit/go/genkit"
    "github.com/firebase/genkit/go/plugins/compat_oai/kimi"
)

ctx := context.Background()
plugin := &kimi.Kimi{}
g := genkit.Init(ctx,
    genkit.WithPlugins(plugin),
    genkit.WithDefaultModel("kimi/kimi-k3"),
)

response, err := genkit.Generate(ctx, g, ai.WithPrompt("Explain mixture-of-experts models."))

Kimi's reasoning_content output is returned as Genkit reasoning parts and is available through response.Reasoning(). Reasoning parts are also preserved as reasoning_content during multi-turn and tool-call requests.

Models

kimi-k3, kimi-k2.6, kimi-k2.7-code, and kimi-k2.7-code-highspeed are registered, with kimi-k2.5 kept as deprecated for existing users during its platform sunset period. Tool-choice steering (ai.WithToolChoice) is advertised for kimi-k3 only: the K2 generation rejects a forced tool call as incompatible with thinking, which is on by default. The catalog is not a ceiling: any model ID Moonshot serves resolves on demand, and the Models field describes or corrects any model, curated or not:

plugin := &kimi.Kimi{Models: map[string]ai.ModelOptions{
    "kimi-k4": {Label: "Kimi K4", Supports: &compat_oai.Multimodal},
}}

Moonshot's API documentation, including the current model list, is at https://platform.kimi.ai/docs.

Config

Models take a typed kimi.ChatConfig: the generation fields the K-series accepts plus the Kimi-specific controls (thinking, reasoningEffort). kimi.ModelRef carries the config with the model ID. For example, Kimi K2.6 thinking can be disabled per request:

response, err := genkit.Generate(ctx, g,
    ai.WithModel(kimi.ModelRef("kimi-k2.6", &kimi.ChatConfig{
        Thinking: &kimi.ThinkingConfig{Type: kimi.ThinkingTypeDisabled},
    })),
    ai.WithPrompt("Answer concisely."),
)

reasoningEffort controls how hard a Kimi K3 generation thinks (low, high, or max, the default), and thinking.keep controls how much reasoning is preserved across turns. Moonshot documents temperature, topP, and the frequency and presence penalties for the legacy moonshot-v1 family only, so the config does not offer them, and maxOutputTokens reaches Moonshot as max_completion_tokens.

Every config also carries the settings Genkit owns: version pins the exact model version a request is served by, apiKey (settable only from Go code) serves one request with a different credential, and extra forwards request body fields the config does not declare, keyed by Moonshot's wire names.

Live tests

Live tests are skipped unless KIMI_API_KEY or MOONSHOT_API_KEY is set:

go test -v ./plugins/compat_oai/kimi

Documentation

Overview

Package kimi provides a Genkit plugin for Moonshot AI's Kimi models.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ModelRef

func ModelRef(id string, config *ChatConfig) ai.ModelRef

ModelRef names a Kimi model and carries the config to generate with, so the config is typed at the call site instead of an any the model checks at runtime. A nil config leaves the request's config unset.

ai.WithModel(kimi.ModelRef("kimi-k3", &kimi.ChatConfig{
	ReasoningEffort: "high",
}))

id is the model ID, with or without the provider prefix.

Types

type ChatConfig

type ChatConfig struct {
	compat_oai.RequestConfig

	// MaxOutputTokens is the maximum number of tokens to generate, sent as the
	// API's max_completion_tokens; Moonshot deprecated max_tokens. The default
	// and the ceiling vary by model.
	MaxOutputTokens int `` /* 202-byte string literal not displayed */
	// StopSequences stop generation when produced by the model, up to five of
	// at most 32 bytes each. The schema enforces the per-entry limit in
	// characters; Moonshot counts bytes.
	StopSequences []string `` /* 183-byte string literal not displayed */
	// LogProbs requests log probabilities for the output tokens.
	LogProbs *bool `json:"logProbs,omitempty" jsonschema_description:"Requests log probabilities for the output tokens."`
	// TopLogProbs is how many of the most likely tokens to return log
	// probabilities for at each position, from 0 to 20; it requires LogProbs.
	TopLogProbs *int `` /* 205-byte string literal not displayed */
	// Thinking controls the reasoning mode of thinking-capable Kimi models,
	// sent as the API's thinking field.
	Thinking *ThinkingConfig `` /* 142-byte string literal not displayed */
	// ReasoningEffort adjusts how hard the Kimi K3 generation thinks, from
	// [ReasoningEffortLow] to [ReasoningEffortMax], the default.
	ReasoningEffort ReasoningEffort `` /* 171-byte string literal not displayed */
}

ChatConfig is the per-request config for Kimi models: the generation fields the K-series accepts plus the Moonshot-specific controls. See https://platform.kimi.ai/docs/api/chat.

Moonshot documents temperature, topP, and the frequency and presence penalties for the legacy moonshot-v1 family only, so the K-series models this plugin serves do not take them and they are deliberately absent.

func (ChatConfig) ApplyToChatCompletion

func (c ChatConfig) ApplyToChatCompletion(params *openai.ChatCompletionNewParams)

ApplyToChatCompletion implements compat_oai.ChatConfig: the generation fields land on their chat completion counterparts, reasoning effort on the SDK's reasoning_effort, and thinking rides as Moonshot's extra request field.

type Kimi

type Kimi struct {
	// APIKey is the Moonshot API key. If empty, KIMI_API_KEY and then
	// MOONSHOT_API_KEY are consulted.
	APIKey string
	// Opts contains additional OpenAI client request options, such as
	// [option.WithBaseURL] for a different endpoint (KIMI_BASE_URL and
	// MOONSHOT_BASE_URL work too). Options supplied here are applied after
	// the plugin defaults, so they win on overlap.
	Opts []option.RequestOption

	// Models overrides what the plugin knows about a Kimi model, keyed by
	// model ID, bare or provider-prefixed. Every Kimi model already works
	// without an entry: known IDs carry curated capabilities and the rest take
	// the Kimi defaults. Supply an entry only to correct or extend what the
	// plugin resolves, most often for a model released after this version of
	// the plugin.
	//
	//	&kimi.Kimi{Models: map[string]ai.ModelOptions{
	//		"kimi-k3": {Supports: &ai.ModelSupports{Multiturn: true, Tools: true}},
	//	}}
	//
	// Fields left at their zero value keep what the plugin resolves, so an
	// entry can pin one capability without restating the label or the
	// versions. Entries apply to the models Init registers as well as the
	// ones [Kimi.ListActions] advertises and [Kimi.ResolveAction] builds,
	// which is the way to describe a curated model differently: Init has
	// already registered those and nothing can re-register them.
	Models map[string]ai.ModelOptions
	// contains filtered or unexported fields
}

Kimi configures the Moonshot AI Kimi plugin.

func (*Kimi) Init

func (k *Kimi) Init(ctx context.Context) []api.Action

Init implements genkit.Plugin.

func (*Kimi) ListActions

func (k *Kimi) ListActions(ctx context.Context) []api.ActionDesc

ListActions lists the models the configured Kimi endpoint exposes, described by the plugin's config schema and capabilities.

func (*Kimi) Name

func (k *Kimi) Name() string

Name implements genkit.Plugin.

func (*Kimi) ResolveAction

func (k *Kimi) ResolveAction(atype api.ActionType, id string) api.Action

ResolveAction dynamically builds a model exposed by the Kimi endpoint, described by the plugin's config schema and capabilities.

type ReasoningEffort

type ReasoningEffort string

ReasoningEffort is how hard the Kimi K3 generation thinks before it answers. Moonshot documents three levels, with ReasoningEffortMax the default.

const (
	// ReasoningEffortLow is the fastest, shallowest reasoning.
	ReasoningEffortLow ReasoningEffort = "low"
	// ReasoningEffortHigh is deeper reasoning, below the default.
	ReasoningEffortHigh ReasoningEffort = "high"
	// ReasoningEffortMax is the deepest reasoning, and the default.
	ReasoningEffortMax ReasoningEffort = "max"
)

type ThinkingConfig

type ThinkingConfig struct {
	// Type turns thinking [ThinkingTypeEnabled] or [ThinkingTypeDisabled].
	Type ThinkingType `json:"type,omitempty" jsonschema:"enum=enabled,enum=disabled" jsonschema_description:"Turns thinking enabled or disabled."`
	// Keep controls how much reasoning is preserved across turns, "all" or
	// unset. It is not an enum in the schema: Moonshot documents one value
	// today, and a list of one would reject whatever it adds next.
	Keep string `json:"keep,omitempty" jsonschema_description:"How much reasoning is preserved across turns: all, or unset for the default."`
}

ThinkingConfig configures the reasoning of thinking-capable Kimi models.

type ThinkingType

type ThinkingType string

ThinkingType turns the reasoning of thinking-capable Kimi models on or off.

const (
	// ThinkingTypeEnabled turns thinking on.
	ThinkingTypeEnabled ThinkingType = "enabled"
	// ThinkingTypeDisabled turns thinking off.
	ThinkingTypeDisabled ThinkingType = "disabled"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL