Documentation
¶
Overview ¶
Package alibaba implements a client for the Alibaba Cloud DashScope API (Model Studio).
It uses the OpenAI-compatible endpoint documented at https://www.alibabacloud.com/help/en/model-studio/compatibility-of-openai-with-dashscope
Regional endpoints:
Index ¶
- func ProcessStream(chunks iter.Seq[ChatStreamChunkResponse]) (iter.Seq[genai.Reply], func() (genai.Usage, [][]genai.Logprob, error))
- func ScoreboardForBackend(b ProviderOptionBackend) scoreboard.Score
- type ChatRequest
- type ChatResponse
- type ChatStreamChunkResponse
- type Client
- func (c *Client) GenStream(ctx context.Context, msgs genai.Messages, opts ...genai.GenOption) (iter.Seq[genai.Reply], func() (genai.Result, error))
- func (c *Client) GenStreamRaw(ctx context.Context, in *ChatRequest) (iter.Seq[ChatStreamChunkResponse], func() error)
- func (c *Client) GenSync(ctx context.Context, msgs genai.Messages, opts ...genai.GenOption) (genai.Result, error)
- func (c *Client) GenSyncRaw(ctx context.Context, in *ChatRequest, out *ChatResponse) error
- func (c *Client) HTTPClient() *http.Client
- func (c *Client) ListModels(ctx context.Context) ([]genai.Model, error)
- func (c *Client) ModelID() string
- func (c *Client) Name() string
- func (c *Client) OutputModalities() genai.Modalities
- func (c *Client) Scoreboard() scoreboard.Score
- func (c *Client) ScoreboardVariants() []genai.ScoreboardVariant
- type Content
- type ContentType
- type Contents
- type ErrorResponse
- type FinishReason
- type GenOption
- type Message
- type Model
- type ModelsResponse
- type ProviderOptionBackend
- type Tool
- type ToolCall
- type Usage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ProcessStream ¶
func ProcessStream(chunks iter.Seq[ChatStreamChunkResponse]) (iter.Seq[genai.Reply], func() (genai.Usage, [][]genai.Logprob, error))
ProcessStream converts raw stream chunks to genai.Reply fragments.
func ScoreboardForBackend ¶
func ScoreboardForBackend(b ProviderOptionBackend) scoreboard.Score
ScoreboardForBackend returns the scoreboard for a specific backend.
CN falls back to Intl since the model catalog is shared.
Types ¶
type ChatRequest ¶
type ChatRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Stream bool `json:"stream"`
Temperature float64 `json:"temperature,omitzero"` // [0, 2]
TopP float64 `json:"top_p,omitzero"` // [0, 1]
TopK int64 `json:"top_k,omitzero"` // DashScope extension
MaxToks int64 `json:"max_tokens,omitzero"` // Max output tokens
Stop []string `json:"stop,omitzero"` // Stop sequences
ResponseFormat struct {
Type string `json:"type,omitzero"` // "text", "json_object"
} `json:"response_format,omitzero"`
StreamOptions struct {
IncludeUsage bool `json:"include_usage,omitzero"`
} `json:"stream_options,omitzero"`
ToolChoice string `json:"tool_choice,omitzero"` // "none", "auto", "required"
Tools []Tool `json:"tools,omitzero"`
Seed int64 `json:"seed,omitzero"`
// DashScope extension: enable web search.
EnableSearch bool `json:"enable_search,omitzero"`
// DashScope extension: enable thinking mode. Not omitzero so false is sent explicitly,
// overriding the default (enabled) on qwen3.5 models.
EnableThinking bool `json:"enable_thinking"`
// DashScope extension: maximum number of reasoning tokens. 0 means no limit.
ThinkingBudget int64 `json:"thinking_budget,omitzero"`
}
ChatRequest is the OpenAI-compatible chat completion request with DashScope extensions.
func (*ChatRequest) SetStream ¶
func (c *ChatRequest) SetStream(stream bool)
SetStream sets the streaming mode.
type ChatResponse ¶
type ChatResponse struct {
ID string `json:"id"`
SystemFingerprint string `json:"system_fingerprint"`
RequestID string `json:"request_id"` // DashScope-specific
Choices []struct {
FinishReason FinishReason `json:"finish_reason"`
Index int64 `json:"index"`
Message Message `json:"message"`
Logprobs json.RawMessage `json:"logprobs"`
} `json:"choices"`
Created int64 `json:"created"`
Model string `json:"model"`
Object string `json:"object"` // "chat.completion"
Usage Usage `json:"usage"`
}
ChatResponse is the chat completion response.
type ChatStreamChunkResponse ¶
type ChatStreamChunkResponse struct {
ID string `json:"id"`
SystemFingerprint string `json:"system_fingerprint"`
RequestID string `json:"request_id"` // DashScope-specific
Object string `json:"object"` // "chat.completion.chunk"
Created int64 `json:"created"` // Unix timestamp
Model string `json:"model"`
Choices []struct {
Index int64 `json:"index"`
Delta Message `json:"delta"`
Logprobs json.RawMessage `json:"logprobs"`
FinishReason FinishReason `json:"finish_reason"`
} `json:"choices"`
Usage Usage `json:"usage"`
}
ChatStreamChunkResponse is a streaming chat chunk.
type Client ¶
type Client struct {
base.NotImplemented
// contains filtered or unexported fields
}
Client implements genai.Provider for Alibaba Cloud DashScope.
func New ¶
New creates a new client for the Alibaba Cloud DashScope API.
If ProviderOptionAPIKey is not provided, it tries DASHSCOPE_API_KEY_INTL, DASHSCOPE_API_KEY_US, DASHSCOPE_API_KEY_CN (auto-selecting the backend), then DASHSCOPE_API_KEY.
ProviderOptionBackend selects a named regional endpoint (e.g. BackendUS). When set, the matching DASHSCOPE_API_KEY_<region> is tried first. ProviderOptionRemote overrides all other endpoint selection with a full URL.
func (*Client) GenStream ¶
func (c *Client) GenStream(ctx context.Context, msgs genai.Messages, opts ...genai.GenOption) (iter.Seq[genai.Reply], func() (genai.Result, error))
GenStream implements genai.Provider.
func (*Client) GenStreamRaw ¶
func (c *Client) GenStreamRaw(ctx context.Context, in *ChatRequest) (iter.Seq[ChatStreamChunkResponse], func() error)
GenStreamRaw provides access to the raw API.
func (*Client) GenSync ¶
func (c *Client) GenSync(ctx context.Context, msgs genai.Messages, opts ...genai.GenOption) (genai.Result, error)
GenSync implements genai.Provider.
func (*Client) GenSyncRaw ¶
func (c *Client) GenSyncRaw(ctx context.Context, in *ChatRequest, out *ChatResponse) error
GenSyncRaw provides access to the raw API.
func (*Client) HTTPClient ¶
HTTPClient returns the HTTP client.
func (*Client) ListModels ¶
ListModels implements genai.Provider.
func (*Client) OutputModalities ¶
func (c *Client) OutputModalities() genai.Modalities
OutputModalities implements genai.Provider.
func (*Client) Scoreboard ¶
func (c *Client) Scoreboard() scoreboard.Score
Scoreboard implements genai.Provider.
func (*Client) ScoreboardVariants ¶
func (c *Client) ScoreboardVariants() []genai.ScoreboardVariant
ScoreboardVariants implements genai.ProviderScoreboardVariants.
type Content ¶
type Content struct {
Type ContentType `json:"type,omitzero"`
// Type == "text"
Text string `json:"text,omitzero"`
// Type == "image_url"
ImageURL struct {
URL string `json:"url,omitzero"`
Detail string `json:"detail,omitzero"` // "auto", "low", "high"
} `json:"image_url,omitzero"`
}
Content is a content block supporting text and image_url types.
type ContentType ¶
type ContentType string
ContentType is a content type discriminator.
const ( ContentText ContentType = "text" ContentImageURL ContentType = "image_url" )
Content type values.
type Contents ¶
type Contents []Content
Contents marshals single text blocks as a string for compatibility.
func (*Contents) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*Contents) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type ErrorResponse ¶
type ErrorResponse struct {
ID string `json:"id"` // DashScope includes id in error responses
RequestID string `json:"request_id"` // DashScope-specific
ErrorVal struct {
Message string `json:"message"`
Type string `json:"type"`
Param string `json:"param"`
Code string `json:"code"`
} `json:"error"`
}
ErrorResponse is the DashScope error response.
func (*ErrorResponse) Error ¶
func (er *ErrorResponse) Error() string
func (*ErrorResponse) IsAPIError ¶
func (er *ErrorResponse) IsAPIError() bool
IsAPIError implements base.ErrAPI.
type FinishReason ¶
type FinishReason string
FinishReason is a provider-specific finish reason.
const ( FinishStop FinishReason = "stop" FinishToolCalls FinishReason = "tool_calls" FinishLength FinishReason = "length" FinishContentFilter FinishReason = "content_filter" )
Finish reason values.
func (FinishReason) ToFinishReason ¶
func (f FinishReason) ToFinishReason() genai.FinishReason
ToFinishReason converts to a genai.FinishReason.
type GenOption ¶
type GenOption struct {
// Thinking controls the thinking mode. Qwen3.5 models default to thinking enabled.
Thinking bool
// ThinkingBudget limits the maximum number of reasoning tokens. 0 means no limit.
ThinkingBudget int64
}
GenOption defines Alibaba DashScope specific generation options.
type Message ¶
type Message struct {
Role string `json:"role,omitzero"` // "system", "assistant", "user"
Name string `json:"name,omitzero"`
Content Contents `json:"content,omitzero"`
ReasoningContent string `json:"reasoning_content,omitzero"` // Qwen3 thinking mode
ToolCalls []ToolCall `json:"tool_calls,omitzero"`
ToolCallID string `json:"tool_call_id,omitzero"`
}
Message is an OpenAI-compatible message with DashScope extensions.
type Model ¶
type Model struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
OwnedBy string `json:"owned_by"`
}
Model is a model returned by the DashScope models API.
type ModelsResponse ¶
type ModelsResponse struct {
Object string `json:"object"` // "list"
Data []Model `json:"data"`
FirstID string `json:"first_id"`
LastID string `json:"last_id"`
HasMore bool `json:"has_more"`
}
ModelsResponse is the response from the DashScope models listing endpoint.
func (*ModelsResponse) ToModels ¶
func (r *ModelsResponse) ToModels() []genai.Model
ToModels converts to genai.Model interfaces.
type ProviderOptionBackend ¶
type ProviderOptionBackend string
ProviderOptionBackend selects a DashScope regional endpoint.
const ( // BackendIntl is the international (Singapore) endpoint (default). BackendIntl ProviderOptionBackend = "dashscope-intl" // BackendUS is the US (Virginia) endpoint. BackendUS ProviderOptionBackend = "dashscope-us" // BackendCN is the China (Beijing) endpoint. BackendCN ProviderOptionBackend = "dashscope" )
func (ProviderOptionBackend) Validate ¶
func (p ProviderOptionBackend) Validate() error
Validate implements genai.ProviderOption.
type Tool ¶
type Tool struct {
Type string `json:"type"` // "function"
Function struct {
Name string `json:"name,omitzero"`
Description string `json:"description,omitzero"`
Parameters genai.JSONSchema `json:"parameters,omitzero"`
} `json:"function"`
}
Tool is an OpenAI-compatible tool definition.
type ToolCall ¶
type ToolCall struct {
Index int64 `json:"index,omitzero"`
ID string `json:"id,omitzero"`
Type string `json:"type,omitzero"` // "function"
Function struct {
Name string `json:"name,omitzero"`
Arguments string `json:"arguments,omitzero"`
} `json:"function,omitzero"`
}
ToolCall is a tool call in a response.
type Usage ¶
type Usage struct {
CompletionTokens int64 `json:"completion_tokens"`
PromptTokens int64 `json:"prompt_tokens"`
TotalTokens int64 `json:"total_tokens"`
PromptTokensDetails struct {
CachedTokens int64 `json:"cached_tokens"`
TextTokens int64 `json:"text_tokens"` // VL models
ImageTokens int64 `json:"image_tokens"` // VL models
} `json:"prompt_tokens_details"`
CompletionTokensDetails struct {
ReasoningTokens int64 `json:"reasoning_tokens"`
TextTokens int64 `json:"text_tokens"` // VL models
} `json:"completion_tokens_details"`
}
Usage is the token usage in a response.