dashscope

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: 7 Imported by: 0

README

DashScope Plugin

This plugin provides Genkit support for Qwen models through Alibaba Cloud Model Studio's OpenAI-compatible DashScope endpoint.

Setup

Set a Model Studio API key:

export DASHSCOPE_API_KEY=<your-api-key>

By default the plugin points at the international endpoint, https://dashscope-intl.aliyuncs.com/compatible-mode/v1. Mainland-China accounts use https://dashscope.aliyuncs.com/compatible-mode/v1 instead, and Alibaba recommends a workspace-dedicated domain for production; set either through DASHSCOPE_BASE_URL, or pass option.WithBaseURL through the plugin's Opts. See https://help.aliyun.com/en/model-studio/base-url for the endpoint reference.

import (
    "context"

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

ctx := context.Background()
plugin := &dashscope.DashScope{}
g := genkit.Init(ctx,
    genkit.WithPlugins(plugin),
    genkit.WithDefaultModel("dashscope/qwen-plus"),
)

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

Qwen's reasoning_content output is returned as Genkit reasoning parts and is available through response.Reasoning().

Models

The registered catalog spans the commercial Qwen line (qwen-flash, qwen-plus, the qwen3.5 through qwen3.7 series, qwen3-max), vision (qwen3-vl-plus), and coding (qwen3-coder-plus) models. The catalog is not a ceiling: any model ID Model Studio serves resolves on demand, and the Models field describes or corrects any model, curated or not:

plugin := &dashscope.DashScope{Models: map[string]ai.ModelOptions{
    "qwen4-max": {Label: "Qwen4 Max", Supports: &compat_oai.Multimodal},
}}

The current model list is at https://www.alibabacloud.com/help/en/model-studio/models.

Config

Models take a typed dashscope.ChatConfig: the generation fields the compatible mode accepts plus the DashScope-specific controls (seed, enableThinking, thinkingBudget, enableSearch). dashscope.ModelRef carries the config with the model ID:

response, err := genkit.Generate(ctx, g,
    ai.WithModel(dashscope.ModelRef("qwen-plus", &dashscope.ChatConfig{
        EnableThinking: openai.Ptr(true),
        ThinkingBudget: openai.Ptr(2048),
    })),
    ai.WithPrompt("Explain mixture-of-experts models."),
)

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 DashScope's wire names (for example search_options).

Tool choice

Qwen models support tool calling, but forced tool-choice modes (required/none) carry model- and thinking-mode-specific restrictions. This plugin does not advertise ToolChoice support and always uses automatic tool selection.

Live tests

Live tests are skipped unless DASHSCOPE_API_KEY is set:

go test -race ./plugins/compat_oai/dashscope -run '^TestPluginLive$' -v -count=1

Documentation

Overview

Package dashscope provides a Genkit plugin for Alibaba Cloud's Qwen models, served through DashScope's OpenAI-compatible mode.

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 Qwen 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(dashscope.ModelRef("qwen-plus", &dashscope.ChatConfig{
	EnableThinking: openai.Ptr(true),
}))

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

Types

type ChatConfig

type ChatConfig struct {
	compat_oai.RequestConfig

	// Temperature controls the degree of randomness in token selection, from
	// 0 inclusive up to but not including 2.
	Temperature *float64 `` /* 193-byte string literal not displayed */
	// TopP is the nucleus sampling threshold, above 0 and up to 1 inclusive.
	TopP *float64 `` /* 147-byte string literal not displayed */
	// MaxOutputTokens is the maximum number of tokens to generate, sent as the
	// API's max_tokens; the default and the ceiling are both the model's
	// maximum output length.
	MaxOutputTokens int `` /* 220-byte string literal not displayed */
	// StopSequences stop generation when produced by the model.
	StopSequences []string `json:"stopSequences,omitempty" jsonschema_description:"Stop generation when produced by the model."`
	// PresencePenalty penalizes tokens that have appeared at all, from -2.0 to
	// 2.0. DashScope's compatible mode documents no frequency penalty.
	PresencePenalty *float64 `` /* 153-byte string literal not displayed */
	// Seed makes generation reproducible across calls when set, from 0 to
	// 2^31-1.
	Seed *int `` /* 159-byte string literal not displayed */
	// EnableThinking turns the thinking mode of hybrid Qwen models on or off,
	// sent as the API's enable_thinking.
	EnableThinking *bool `` /* 148-byte string literal not displayed */
	// ThinkingBudget is the maximum number of tokens the model may think
	// with, sent as the API's thinking_budget; it requires EnableThinking.
	// The default is the model's maximum chain-of-thought length.
	ThinkingBudget *int `` /* 247-byte string literal not displayed */
	// EnableSearch lets the model consult web search, sent as the API's
	// enable_search.
	EnableSearch *bool `json:"enableSearch,omitempty" jsonschema_description:"Lets the model consult web search, sent as the API's enable_search."`
}

ChatConfig is the per-request config for Qwen models served through DashScope's OpenAI-compatible mode: the common generation fields plus the DashScope-specific controls the mode accepts as extra request fields. See https://www.alibabacloud.com/help/en/model-studio/use-qwen-by-calling-api.

func (ChatConfig) ApplyToChatCompletion

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

ApplyToChatCompletion implements compat_oai.ChatConfig: the generation fields land on their chat completion counterparts and the DashScope controls ride as the mode's extra request fields.

type DashScope

type DashScope struct {
	// APIKey is the DashScope API key. If empty, DASHSCOPE_API_KEY is consulted.
	APIKey string
	// Opts contains additional OpenAI client request options, such as
	// [option.WithBaseURL] for a different endpoint (DASHSCOPE_BASE_URL works
	// 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 Qwen model, keyed by
	// model ID, bare or provider-prefixed. Every Qwen model already works
	// without an entry: known IDs carry curated capabilities and the rest take
	// the Qwen 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.
	//
	//	&dashscope.DashScope{Models: map[string]ai.ModelOptions{
	//		"qwen-plus": {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 [DashScope.ListActions] advertises and [DashScope.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
}

DashScope configures the Alibaba Cloud DashScope (Qwen) plugin.

func (*DashScope) Init

func (d *DashScope) Init(ctx context.Context) []api.Action

Init implements genkit.Plugin.

func (*DashScope) ListActions

func (d *DashScope) ListActions(ctx context.Context) []api.ActionDesc

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

func (*DashScope) Name

func (d *DashScope) Name() string

Name implements genkit.Plugin.

func (*DashScope) ResolveAction

func (d *DashScope) ResolveAction(atype api.ActionType, id string) api.Action

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

Jump to

Keyboard shortcuts

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