ark

package module
v0.1.69 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: Apache-2.0 Imports: 20 Imported by: 48

README

Volcengine Ark Model

A Volcengine Ark model implementation for Eino that implements the ToolCallingChatModel interface. This enables seamless integration with Eino's LLM capabilities for enhanced natural language processing and generation.

This package provides two distinct models:

  • ChatModel: For text-based and multi-modal chat completions.
  • ImageGenerationModel: For generating images from text prompts or image.
  • ResponsesAPIChatModel: Contains methods and other services that help with interacting with ResponsesAPI.

Features

  • Implements github.com/cloudwego/eino/components/model.Model
  • Easy integration with Eino's model system
  • Configurable model parameters
  • Support for both chat completion, image generation and response api
  • Support for streaming responses
  • Custom response parsing support
  • Flexible model configuration

Installation

go get github.com/cloudwego/eino-ext/components/model/ark@latest

Chat Completion

This model is used for standard chat and text generation tasks.

Quick Start

Here's a quick example of how to use the ChatModel:

package main

import (
	"context"
	"encoding/json"
	"errors"
	"io"
	"log"
	"os"

	"github.com/cloudwego/eino/schema"

	"github.com/cloudwego/eino-ext/components/model/ark"
)

func main() {
	ctx := context.Background()

	chatModel, err := ark.NewChatModel(ctx, &ark.ChatModelConfig{
		APIKey: os.Getenv("ARK_API_KEY"),
		Model:  os.Getenv("ARK_MODEL_ID"),
	})

	if err != nil {
		log.Fatalf("NewChatModel failed, err=%v", err)
	}

	inMsgs := []*schema.Message{
		{
			Role:    schema.User,
			Content: "how do you generate answer for user question as a machine, please answer in short?",
		},
	}

	msg, err := chatModel.Generate(ctx, inMsgs)
	if err != nil {
		log.Fatalf("Generate failed, err=%v", err)
	}

	log.Printf("\ngenerate output: \n")
	log.Printf("  request_id: %s\n", ark.GetArkRequestID(msg))
	respBody, _ := json.MarshalIndent(msg, "  ", "  ")
	log.Printf("  body: %s\n", string(respBody))

	sr, err := chatModel.Stream(ctx, inMsgs)
	if err != nil {
		log.Fatalf("Stream failed, err=%v", err)
	}

	chunks := make([]*schema.Message, 0, 1024)
	for {
		msgChunk, err := sr.Recv()
		if errors.Is(err, io.EOF) {
			break
		}
		if err != nil {
			log.Fatalf("Stream Recv failed, err=%v", err)
		}

		chunks = append(chunks, msgChunk)
	}

	msg, err = schema.ConcatMessages(chunks)
	if err != nil {
		log.Fatalf("ConcatMessages failed, err=%v", err)
	}

	log.Printf("stream final output: \n")
	log.Printf("  request_id: %s\n", ark.GetArkRequestID(msg))
	respBody, _ = json.MarshalIndent(msg, "  ", "  ")
	log.Printf("  body: %s\n", string(respBody))
}
Configuration

The ChatModel can be configured using the ark.ChatModelConfig struct:

type ChatModelConfig struct {
    // Timeout specifies the maximum duration to wait for API responses
    // If HTTPClient is set, Timeout will not be used.
    // Optional. Default: 10 minutes
    Timeout *time.Duration `json:"timeout"`
    
    // HTTPClient specifies the client to send HTTP requests.
    // If HTTPClient is set, Timeout will not be used.
    // Optional. Default &http.Client{Timeout: Timeout}
    HTTPClient *http.Client `json:"http_client"`
    
    // RetryTimes specifies the number of retry attempts for failed API calls
    // Optional. Default: 2
    RetryTimes *int `json:"retry_times"`
    
    // BaseURL specifies the base URL for Ark service
    // Optional. Default: "https://ark.cn-beijing.volces.com/api/v3"
    BaseURL string `json:"base_url"`
    
    // Region specifies the region where Ark service is located
    // Optional. Default: "cn-beijing"
    Region string `json:"region"`
    
    // The following three fields are about authentication - either APIKey or AccessKey/SecretKey pair is required
    // For authentication details, see: https://www.volcengine.com/docs/82379/1298459
    // APIKey takes precedence if both are provided
    APIKey string `json:"api_key"`
    
    AccessKey string `json:"access_key"`
    
    SecretKey string `json:"secret_key"`
    
    // The following fields correspond to Ark's chat completion API parameters
    // Ref: https://www.volcengine.com/docs/82379/1298454
    
    // Model specifies the ID of endpoint on ark platform
    // Required
    Model string `json:"model"`
    
    // MaxTokens limits the maximum number of output tokens in the Chat Completion API.
    // In Responses API, corresponds to `max_output_tokens`, representing both the model output and reasoning output.
    // Optional. In chat completion, default: 4096, in Responses API, no default.
    MaxTokens *int `json:"max_tokens,omitempty"`
    
    // MaxCompletionTokens specifies the maximum tokens in the Chat Completion API,
    // representing both the model output and reasoning output.
    // Range: 0 to 65,536 tokens. Exceeding the maximum threshold will result in an error.
    // Note: In chat completion, MaxCompletionTokens and MaxTokens cannot both be set, in Responses API, this field is ignored; use MaxTokens.
    // Optional.
    MaxCompletionTokens *int `json:"max_completion_tokens,omitempty"`
    
    // Temperature specifies what sampling temperature to use
    // Generally recommend altering this or TopP but not both
    // Range: 0.0 to 1.0. Higher values make output more random
    // Optional. Default: 1.0
    Temperature *float32 `json:"temperature,omitempty"`
    
    // TopP controls diversity via nucleus sampling
    // Generally recommend altering this or Temperature but not both
    // Range: 0.0 to 1.0. Lower values make output more focused
    // Optional. Default: 0.7
    TopP *float32 `json:"top_p,omitempty"`
    
    // Stop sequences where the API will stop generating further tokens
    // Optional. Example: []string{"\n", "User:"}
    Stop []string `json:"stop,omitempty"`
    
    // FrequencyPenalty prevents repetition by penalizing tokens based on frequency
    // Range: -2.0 to 2.0. Positive values decrease likelihood of repetition
    // Optional. Default: 0
    FrequencyPenalty *float32 `json:"frequency_penalty,omitempty"`
    
    // LogitBias modifies likelihood of specific tokens appearing in completion
    // Optional. Map token IDs to bias values from -100 to 100
    LogitBias map[string]int `json:"logit_bias,omitempty"`
    
    // PresencePenalty prevents repetition by penalizing tokens based on presence
    // Range: -2.0 to 2.0. Positive values increase likelihood of new topics
    // Optional. Default: 0
    PresencePenalty *float32 `json:"presence_penalty,omitempty"`
    
    // CustomHeader the http header passed to model when requesting model
    CustomHeader map[string]string `json:"custom_header"`
    
    // LogProbs specifies whether to return log probabilities of the output tokens.
    LogProbs bool `json:"log_probs"`
    
    // TopLogProbs specifies the number of most likely tokens to return at each token position, each with an associated log probability.
    TopLogProbs int `json:"top_log_probs"`
    
    // ResponseFormat specifies the format that the model must output.
    ResponseFormat *ResponseFormat `json:"response_format,omitempty"`
    
    // Thinking controls whether the model is set to activate the deep thinking mode.
    // It is set to be enabled by default.
    Thinking *model.Thinking `json:"thinking,omitempty"`
    
    // ServiceTier specifies whether to use the TPM guarantee package. The effective target has purchased the inference access point for the guarantee package.
    ServiceTier *string `json:"service_tier"`
    
    // ReasoningEffort specifies the reasoning effort of the model.
    // Optional.
    ReasoningEffort *model.ReasoningEffort `json:"reasoning_effort,omitempty"`
    
    // BatchChat ark batch chat config
    // Optional.
    BatchChat *BatchChatConfig `json:"batch_chat,omitempty"`
    
    Cache *CacheConfig `json:"cache,omitempty"`
}
Request Options

The ChatModel supports various request options to customize the behavior of API calls. Here are the available options:

// WithCustomHeader sets custom headers for a single request
// the headers will override all the headers given in ChatModelConfig.CustomHeader
func WithCustomHeader(m map[string]string) model.Option {}

Image Generation

This model is used specifically for generating images from text prompts.

Quick Start

Here's a quick example of how to use the ImageGenerationModel:

package main

import (
	"context"
	"encoding/json"
	"log"
	"os"

	"github.com/cloudwego/eino/schema"
	"github.com/cloudwego/eino-ext/components/model/ark"
)

func main() {
	ctx := context.Background()

	// Get ARK_API_KEY and an image generation model ID
	imageGenerationModel, err := ark.NewImageGenerationModel(ctx, &ark.ImageGenerationConfig{
		APIKey: os.Getenv("ARK_API_KEY"),
		Model:  os.Getenv("ARK_IMAGE_MODEL_ID"), // Use an appropriate image model ID
	})

	if err != nil {
		log.Fatalf("NewImageGenerationModel failed, err=%v", err)
	}

	inMsgs := []*schema.Message{
		{
			Role:    schema.User,
			Content: "a photo of a cat sitting on a table",
		},
	}

	msg, err := imageGenerationModel.Generate(ctx, inMsgs)
	if err != nil {
		log.Fatalf("Generate failed, err=%v", err)
	}

	log.Printf("\ngenerate output: \n")
	respBody, _ := json.MarshalIndent(msg, "  ", "  ")
	log.Printf("  body: %s\n", string(respBody))

	sr, err := imageGenerationModel.Stream(ctx, inMsgs)
	if err != nil {
		log.Fatalf("Stream failed, err=%v", err)
	}

	log.Printf("stream output: \n")
	index := 0
	for {
		msgChunk, err := sr.Recv()
		if errors.Is(err, io.EOF) {
			break
		}
		if err != nil {
			log.Fatalf("Stream Recv failed, err=%v", err)
		}

		respBody, _ = json.MarshalIndent(msgChunk, "  ", "  ")
		log.Printf("stream chunk %d: body: %s\n", index, string(respBody))
		index++
	}
}
Configuration

The ImageGenerationModel can be configured using the ark.ImageGenerationConfig struct:


type ImageGenerationConfig struct {
    // For authentication, APIKey is required as the image generation API only supports API Key authentication.
    // For authentication details, see: https://www.volcengine.com/docs/82379/1298459
    // Required
    APIKey string `json:"api_key"`
    
    // Model specifies the ID of endpoint on ark platform
    // Required
    Model string `json:"model"`
    
    // Timeout specifies the maximum duration to wait for API responses
    // If HTTPClient is set, Timeout will not be used.
    // Optional. Default: 10 minutes
    Timeout *time.Duration `json:"timeout"`
    
    // HTTPClient specifies the client to send HTTP requests.
    // If HTTPClient is set, Timeout will not be used.
    // Optional. Default &http.Client{Timeout: Timeout}
    HTTPClient *http.Client `json:"http_client"`
    
    // RetryTimes specifies the number of retry attempts for failed API calls
    // Optional. Default: 2
    RetryTimes *int `json:"retry_times"`
    
    // BaseURL specifies the base URL for Ark service
    // Optional. Default: "https://ark.cn-beijing.volces.com/api/v3"
    BaseURL string `json:"base_url"`
    
    // Region specifies the region where Ark service is located
    // Optional. Default: "cn-beijing"
    Region string `json:"region"`
    
    // The following fields correspond to Ark's image generation API parameters
    // Ref: https://www.volcengine.com/docs/82379/1541523
    
    // Size specifies the dimensions of the generated image.
    // It can be a resolution keyword (e.g., "1K", "2K", "4K") or a custom resolution
    // in "{width}x{height}" format (e.g., "1920x1080").
    // When using custom resolutions, the total pixels must be between 1280x720 and 4096x4096,
    // and the aspect ratio (width/height) must be between 1/16 and 16.
    // Optional. Defaults to "2048x2048".
    Size string `json:"size"`
    
    // SequentialImageGeneration determines if the model should generate a sequence of images.
    // Possible values:
    //  - "auto": The model decides whether to generate multiple images based on the prompt.
    //  - "disabled": Only a single image is generated.
    // Optional. Defaults to "disabled".
    SequentialImageGeneration SequentialImageGeneration `json:"sequential_image_generation"`
    
    // SequentialImageGenerationOption sets the maximum number of images to generate when
    // SequentialImageGeneration is set to "auto".
    // The value must be between 1 and 15.
    // Optional. Defaults to 15.
    SequentialImageGenerationOption *model.SequentialImageGenerationOptions `json:"sequential_image_generation_option"`
    
    // ResponseFormat specifies how the generated image data is returned.
    // Possible values:
    //  - "url": A temporary URL to download the image (valid for 24 hours).
    //  - "b64_json": The image data encoded as a Base64 string in the response.
    // Optional. Defaults to "url".
    ResponseFormat ImageResponseFormat `json:"response_format"`
    
    // DisableWatermark, if set to true, removes the "AI Generated" watermark
    // from the bottom-right corner of the image.
    // Optional. Defaults to false.
    DisableWatermark bool `json:"disable_watermark"`

    // 	BatchMaxParallel specifies the maximum number of parallel requests to send to the chat completion API.
    //	Optional. Default: 3000.
    BatchMaxParallel *int `json:"batch_max_parallel,omitempty"`

    // BatchChat ark batch chat config
    // Optional.
    BatchChat *BatchChatConfig `json:"batch_chat,omitempty"`

    Cache *CacheConfig `json:"cache,omitempty"`
}

Examples

See the following examples for more usage:

Chat Completion Examples
  • generate - Basic text generation with both Generate() and Stream() methods

    • Shows how to use the chat model for standard text generation
    • Demonstrates both non-streaming and streaming responses
    • Includes request ID tracking
  • generate_with_image - Multi-modal chat with image input

    • Demonstrates processing images alongside text
    • Shows how to encode and send images in base64 format
    • Example of using ChatTemplate with images
  • stream - Streaming response with reasoning effort

    • Shows typewriter-style streaming output
    • Demonstrates using reasoning effort options
    • Example of proper stream handling and cleanup
  • intent_tool - Tool calling and function execution

    • Demonstrates function/tool calling capabilities
    • Shows how to define and register tools
    • Example of handling tool calls and responses
  • generate_batch_chat - Batch chat completions

    • Shows how to process multiple chat conversations in batch
    • Demonstrates batch configuration options
    • Example of handling batch results
Caching Examples
  • prefixcache/contextapi - Prefix caching with Context API

    • Demonstrates prefix caching to improve performance
    • Shows cache hit/miss tracking
    • Context API integration example
  • prefixcache/responsesapi - Prefix caching with Responses API

    • Same as above but using Responses API
    • Shows different API integration approach
  • sessioncache/contextapi - Session caching with Context API

    • Demonstrates session-level caching
    • Shows multi-turn conversation caching
  • sessioncache/responsesapi - Session caching with Responses API

    • Same as above but using Responses API
Image Generation Example
  • image_generate - Text-to-image generation
    • Demonstrates using the ImageGenerationModel
    • Shows how to configure image generation parameters
    • Example of saving generated images

Quick Example

Here's a minimal example to get started (for complete examples, see above):

package main

import (
	"context"
	"encoding/json"
	"errors"
	"io"
	"log"
	"os"

	"github.com/cloudwego/eino/schema"

	"github.com/cloudwego/eino-ext/components/model/ark"
)

func main() {
	ctx := context.Background()

	// Get ARK_API_KEY and ARK_MODEL_ID: https://www.volcengine.com/docs/82379/1399008
	chatModel, err := ark.NewChatModel(ctx, &ark.ChatModelConfig{
		APIKey: os.Getenv("ARK_API_KEY"),
		Model:  os.Getenv("ARK_MODEL_ID"),
	})

	if err != nil {
		log.Fatalf("NewChatModel failed, err=%v", err)
	}

	inMsgs := []*schema.Message{
		{
			Role:    schema.User,
			Content: "how do you generate answer for user question as a machine, please answer in short?",
		},
	}

	msg, err := chatModel.Generate(ctx, inMsgs)
	if err != nil {
		log.Fatalf("Generate failed, err=%v", err)
	}

	log.Printf("\ngenerate output: \n")
	log.Printf("  request_id: %s\n", ark.GetArkRequestID(msg))
	respBody, _ := json.MarshalIndent(msg, "  ", "  ")
	log.Printf("  body: %s\n", string(respBody))

	sr, err := chatModel.Stream(ctx, inMsgs)
	if err != nil {
		log.Fatalf("Stream failed, err=%v", err)
	}

	chunks := make([]*schema.Message, 0, 1024)
	for {
		msgChunk, err := sr.Recv()
		if errors.Is(err, io.EOF) {
			break
		}
		if err != nil {
			log.Fatalf("Stream Recv failed, err=%v", err)
		}

		chunks = append(chunks, msgChunk)
	}

	msg, err = schema.ConcatMessages(chunks)
	if err != nil {
		log.Fatalf("ConcatMessages failed, err=%v", err)
	}

	log.Printf("stream final output: \n")
	log.Printf("  request_id: %s\n", ark.GetArkRequestID(msg))
	respBody, _ = json.MarshalIndent(msg, "  ", "  ")
	log.Printf("  body: %s\n", string(respBody))
}

Documentation

Overview

Package ark implements chat model for ark runtime.

Index

Constants

View Source
const (
	SourceOfToutiao = "toutiao"
	SourceOfDouyin  = "douyin"
	SourceOfMoji    = "moji"
)
View Source
const (
	ImageSizeKey = "seedream-image-size"
)

Variables

View Source
var (
	ErrEmptyResponse = errors.New("empty response received from model")
)

Functions

func GetArkRequestID

func GetArkRequestID(msg *schema.Message) string

func GetCacheExpiration

func GetCacheExpiration(msg *schema.Message) (expireAtSec int64, ok bool)

GetCacheExpiration returns the cache expiration time in seconds. Only available for ResponsesAPI responses.

func GetContextID deprecated

func GetContextID(msg *schema.Message) (string, bool)

Deprecated: Use GetResponseID instead. GetContextID returns the conversation context ID from the message. Available only for ResponsesAPI responses.

func GetFPS

func GetFPS(part *schema.ChatMessageVideoURL) *float64

func GetImageSize

func GetImageSize(part *schema.ChatMessageImageURL) (string, bool)

func GetInputImageSize

func GetInputImageSize(part *schema.MessageInputImage) (string, bool)

func GetInputVideoFPS

func GetInputVideoFPS(part *schema.MessageInputVideo) *float64

func GetModelName

func GetModelName(msg *schema.Message) (string, bool)

func GetOutputImageSize

func GetOutputImageSize(part *schema.MessageOutputImage) (string, bool)

func GetOutputVideoFPS

func GetOutputVideoFPS(part *schema.MessageOutputVideo) *float64

func GetReasoningContent

func GetReasoningContent(msg *schema.Message) (string, bool)

func GetResponseID

func GetResponseID(msg *schema.Message) (string, bool)

GetResponseID returns the response ID from the message. Available only for ResponsesAPI responses.

func GetServiceTier

func GetServiceTier(msg *schema.Message) (string, bool)

func InvalidateMessageCaches

func InvalidateMessageCaches(messages []*schema.Message) error

InvalidateMessageCaches disables caching for the specified messages. When a message is modified, ARK invalidates caches for that message and all subsequent ones. Call this to mark those message caches as invalid.

func SetFPS

func SetFPS(part *schema.ChatMessageVideoURL, fps float64)

func SetImageSize

func SetImageSize(part *schema.ChatMessageImageURL, size string)

func SetInputVideoFPS

func SetInputVideoFPS(part *schema.MessageInputVideo, fps float64)

func SetPartial

func SetPartial(msg *schema.Message)

SetPartial marks the message as a partial message to enable continuation (prefill) mode. By pre-filling part of the assistant role's content, it guides and controls the model to continue generating from existing text fragments and maintain consistency in role-play scenarios. To use this, set the role of the last message in the input list to assistant and call SetPartial on it. The model will then continue writing based on the message's content. Only available for ResponsesAPI.

func WithCache

func WithCache(cache *CacheOption) model.Option

WithCache is an option to configure model caching.

func WithCustomHeader

func WithCustomHeader(m map[string]string) model.Option

WithCustomHeader sets custom headers for a single request the headers will override all the headers given in ChatModelConfig.CustomHeader

func WithEnableReasoningContentPassback

func WithEnableReasoningContentPassback(enable bool) model.Option

WithEnableReasoningContentPassback controls whether reasoning content is passed back to the model in multi-turn conversations. This option is only supported for the ResponsesAPIChatModel. See ResponsesAPIConfig.EnableReasoningContentPassback.

func WithEnableToolWebSearch

func WithEnableToolWebSearch(toolWebSearch *ToolWebSearch) model.Option

WithEnableToolWebSearch enables the web search tool. This option is only supported for the ResponsesAPIChatModel. Web Search is a basic internet search tool that can obtain real-time public network information (such as news, products, weather, etc.) for your large model through the Responses API. This tool can solve core issues such as data timeliness, knowledge gaps, and information synchronization, and you do not need to develop your own search engine or maintain data resources. Note: This option is only effective for the Responses API. For more details, see https://www.volcengine.com/docs/82379/1756990?lang=zh

func WithMaxCompletionTokens

func WithMaxCompletionTokens(maxCompletionTokens int) model.Option

WithMaxCompletionTokens is used to set the max completion tokens for the request.

func WithMaxToolCalls

func WithMaxToolCalls(n int64) model.Option

WithMaxToolCalls sets the maximum number of tool-calling rounds. This option is only supported for the ResponsesAPIChatModel. The value must be in the range [1, 10]. After this limit is reached, the model is prompted to stop making further tool calls and generate a response. Note: This is a best-effort parameter, and the actual number of calls may be affected by model performance and tool results. The default value for the Web Search tool is 3. For more details, see https://www.volcengine.com/docs/82379/1569618?lang=zh

func WithPrefixCache deprecated

func WithPrefixCache(contextID string) model.Option

Deprecated: use WithCache instead. WithPrefixCache creates an option to specify a context ID for the request. The context ID is typically obtained from a previous call to CreatePrefixCache.

When this option is provided, the model will use the cached prefix context associated with this ID, allowing you to avoid resending the same context messages in each request, which improves efficiency and reduces token usage.

Note: it is unavailable for doubao models of version 1.6 and above.

func WithReasoningEffort

func WithReasoningEffort(effort arkModel.ReasoningEffort) model.Option

func WithThinking

func WithThinking(thinking *arkModel.Thinking) model.Option

WithThinking sets the thinking process configuration for the ark.

Types

type APIType

type APIType string
const (
	// ContextAPI is defined from  https://www.volcengine.com/docs/82379/1528789
	ContextAPI APIType = "context_api"
	// ResponsesAPI is defined from https://www.volcengine.com/docs/82379/1569618
	// Deprecated: Use NewResponsesAPIChatModel to create a model for the ResponsesAPIChatModel.
	ResponsesAPI APIType = "responses_api"
)

type BatchChatConfig

type BatchChatConfig struct {
	// EnableBatchChat specifies whether to use the batch chat completion API. Only applies to non-streaming scenarios.
	// For authentication details, see: https://www.volcengine.com/docs/82379/1399517?lang=en#01826852
	EnableBatchChat bool `json:"enable_batch_chat,omitempty"`

	// BatchChatTimeout specifies the timeout for the batch chat completion API. When using batch chat model must set a timeout period.
	// Model will keep retrying until the timeout or the execution succeeds. It is using context timeout to implement the retry time limit.
	// Attention: BatchChatAsyncRetryTimeout is different from the http client timeout which controls the timeout for a single HTTP request.
	// Required. Recommend to set a longer timeout period.
	BatchChatAsyncRetryTimeout time.Duration `json:"batch_chat_async_retry_timeout,omitempty"`

	// BatchMaxParallel specifies the maximum number of parallel requests to send to the chat completion API.
	// Optional. Default: 3000.
	BatchMaxParallel *int `json:"batch_max_parallel,omitempty"`
}

type CacheConfig

type CacheConfig struct {
	// APIType controls which API the cache uses to make calls.
	// Note that if the type is ResponsesAPI,
	// the following configuration will not be available (ARK may support it in the future):
	// `Region`, `AccessKey`, `SecretKey`, `Stop`, `FrequencyPenalty`, `LogitBias`, `PresencePenalty`,
	// `LogProbs`, `TopLogProbs`.
	// It can be overridden by [WithCache].
	// Optional. Default: ContextAPI.
	//
	// Deprecated: This field defaults to ContextAPI. To use the ResponsesAPI, use NewResponsesAPIChatModel to create a ResponsesAPIChatModel instead of setting APIType to ResponsesAPI.
	APIType *APIType `json:"api_type,omitempty"`

	// SessionCache is the configuration of ResponsesAPI session cache.
	// It can be overridden by [WithCache].
	// Optional.
	SessionCache *SessionCacheConfig `json:"session_cache,omitempty"`
}

type CacheInfo

type CacheInfo struct {
	// ContextID return by ContextAPI, it's specifies the id of prefix that can be used with [WithCache.ContextID] option.
	ContextID string
	// ResponseID return by ResponsesAPI, it's specifies the id of prefix that can be used with [WithCache.HeadPreviousResponseID] option.
	ResponseID string
	// Usage specifies the token usage of prefix
	Usage schema.TokenUsage
}

type CacheOption

type CacheOption struct {
	// APIType specifies the API type for caching.
	// Deprecated: This field defaults to ContextAPI and will be removed in a future version.
	// To use the ResponsesAPI, please use NewResponsesAPIChatModel to create a ResponsesAPIChatModel.
	APIType APIType

	// ContextID is the unique identifier returned by ContextAPI.
	// Note: This field is only applicable when using ContextAPI.
	// Important: ContextID will not be compatible with response ID from ResponsesAPI in future releases.
	// For prefix caching with ResponsesAPI, use HeadPreviousResponseID instead.
	// For session caching with ResponsesAPI, use SessionCache instead.
	// Optional.
	ContextID *string

	// HeadPreviousResponseID is a response ID from a previous ResponsesAPI call.
	// This ID links the current request to a previous conversation context, enabling
	// features like conversation continuation and prefix caching.
	// The referenced response must be cached before use.
	// Only applicable for ResponsesAPI.
	// Optional.
	HeadPreviousResponseID *string

	// SessionCache is the configuration of ResponsesAPI session cache.
	// Optional.
	SessionCache *SessionCacheConfig
}

type ChatModel

type ChatModel struct {
	// contains filtered or unexported fields
}

func NewChatModel

func NewChatModel(_ context.Context, config *ChatModelConfig) (*ChatModel, error)

func (*ChatModel) BindForcedTools

func (cm *ChatModel) BindForcedTools(tools []*schema.ToolInfo) (err error)

func (*ChatModel) BindTools

func (cm *ChatModel) BindTools(tools []*schema.ToolInfo) (err error)

func (*ChatModel) CreatePrefixCache

func (cm *ChatModel) CreatePrefixCache(ctx context.Context, prefix []*schema.Message, ttl int, opts ...fmodel.Option) (info *CacheInfo, err error)

CreatePrefixCache creates a prefix context on the server side. In each subsequent turn of conversation, use WithCache to pass in the ContextID. The server will input the prefix cached context and this turn of input into the model for processing. This improves efficiency by reducing token usage and request size.

Parameters:

  • ctx: The context for the request
  • prefix: Initial messages to be cached as prefix context
  • ttl: Time-to-live in seconds for the cached prefix, default: 86400

Returns:

  • info: Information about the created prefix cache, including the context ID and token usage
  • err: Any error encountered during the operation

ref: https://www.volcengine.com/docs/82379/1396490#_1-%E5%88%9B%E5%BB%BA%E5%89%8D%E7%BC%80%E7%BC%93%E5%AD%98

Note:

  • It is unavailable for doubao models of version 1.6 and above.

func (*ChatModel) CreateSessionCache

func (cm *ChatModel) CreateSessionCache(ctx context.Context, prefix []*schema.Message, ttl int, truncation *model.TruncationStrategy) (info *CacheInfo, err error)

CreateSessionCache creates an initial session context on the server side. It returns an initial context ID. In each subsequent turn of conversation, use WithCache to pass in the ContextID. The server will input all cached context and this turn of input into the model for processing. This turn of conversation will also be automatically cached. Suitable for use in multi-turn conversation scenarios. Note that it does not apply to concurrent requests.

Parameters:

  • ctx: The context for the request
  • prefix: Initial messages to be cached as prefix context
  • ttl: Time-to-live in seconds for the cached prefix, default: 86400
  • truncation: Truncation strategy, default: nil

Returns:

  • info: Information about the created session cache, including the context ID and token usage
  • err: Any error encountered during the operation

ref: https://www.volcengine.com/docs/82379/1396491?redirect=1#%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8B

Note:

  • It is unavailable for doubao models of version 1.6 and above.
  • Only supports calling by ContextAPI.

func (*ChatModel) Generate

func (cm *ChatModel) Generate(ctx context.Context, in []*schema.Message, opts ...fmodel.Option) (
	outMsg *schema.Message, err error)

func (*ChatModel) GetType

func (cm *ChatModel) GetType() string

func (*ChatModel) IsCallbacksEnabled

func (cm *ChatModel) IsCallbacksEnabled() bool

func (*ChatModel) Stream

func (cm *ChatModel) Stream(ctx context.Context, in []*schema.Message, opts ...fmodel.Option) (
	outStream *schema.StreamReader[*schema.Message], err error)

func (*ChatModel) WithTools

func (cm *ChatModel) WithTools(tools []*schema.ToolInfo) (fmodel.ToolCallingChatModel, error)

type ChatModelConfig

type ChatModelConfig struct {
	// Timeout specifies the maximum duration to wait for API responses
	// If HTTPClient is set, Timeout will not be used.
	// Optional. Default: 10 minutes
	Timeout *time.Duration `json:"timeout"`

	// HTTPClient specifies the client to send HTTP requests.
	// If HTTPClient is set, Timeout will not be used.
	// Optional. Default &http.Client{Timeout: Timeout}
	HTTPClient *http.Client `json:"http_client"`

	// RetryTimes specifies the number of retry attempts for failed API calls
	// Optional. Default: 2
	RetryTimes *int `json:"retry_times"`

	// BaseURL specifies the base URL for Ark service
	// Optional. Default: "https://ark.cn-beijing.volces.com/api/v3"
	BaseURL string `json:"base_url"`

	// Region specifies the region where Ark service is located
	// Optional. Default: "cn-beijing"
	Region string `json:"region"`

	// The following three fields are about authentication - either APIKey or AccessKey/SecretKey pair is required
	// For authentication details, see: https://www.volcengine.com/docs/82379/1298459
	// APIKey takes precedence if both are provided
	APIKey string `json:"api_key"`

	AccessKey string `json:"access_key"`

	SecretKey string `json:"secret_key"`

	// Model specifies the ID of endpoint on ark platform
	// Required
	Model string `json:"model"`

	// MaxTokens limits the maximum number of output tokens in the Chat Completion API. See https://www.volcengine.com/docs/82379/1494384.
	// In Responses API, corresponds to `max_output_tokens`, representing both the model output and reasoning output. See https://www.volcengine.com/docs/82379/1569618.
	// Optional. In chat completion, default: 4096, in Responses API, no default.
	MaxTokens *int `json:"max_tokens,omitempty"`

	// MaxCompletionTokens specifies the maximum tokens in the Chat Completion API,
	// representing both the model output and reasoning output. See https://www.volcengine.com/docs/82379/1569618.
	// Range: 0 to 65,536 tokens. Exceeding the maximum threshold will result in an error.
	// Note: In chat completion, MaxCompletionTokens and MaxTokens cannot both be set, in Responses API, this field is ignored; use MaxTokens.
	// Optional.
	MaxCompletionTokens *int `json:"max_completion_tokens,omitempty"`

	// Temperature specifies what sampling temperature to use
	// Generally recommend altering this or TopP but not both
	// Range: 0.0 to 1.0. Higher values make output more random
	// Optional. Default: 1.0
	Temperature *float32 `json:"temperature,omitempty"`

	// TopP controls diversity via nucleus sampling
	// Generally recommend altering this or Temperature but not both
	// Range: 0.0 to 1.0. Lower values make output more focused
	// Optional. Default: 0.7
	TopP *float32 `json:"top_p,omitempty"`

	// Stop sequences where the API will stop generating further tokens
	// Optional. Example: []string{"\n", "User:"}
	Stop []string `json:"stop,omitempty"`

	// FrequencyPenalty prevents repetition by penalizing tokens based on frequency
	// Range: -2.0 to 2.0. Positive values decrease likelihood of repetition
	// Optional. Default: 0
	FrequencyPenalty *float32 `json:"frequency_penalty,omitempty"`

	// LogitBias modifies likelihood of specific tokens appearing in completion
	// Optional. Map token IDs to bias values from -100 to 100
	LogitBias map[string]int `json:"logit_bias,omitempty"`

	// PresencePenalty prevents repetition by penalizing tokens based on presence
	// Range: -2.0 to 2.0. Positive values increase likelihood of new topics
	// Optional. Default: 0
	PresencePenalty *float32 `json:"presence_penalty,omitempty"`

	// CustomHeader the http header passed to model when requesting model
	CustomHeader map[string]string `json:"custom_header"`

	// LogProbs specifies whether to return log probabilities of the output tokens.
	LogProbs bool `json:"log_probs"`

	// TopLogProbs specifies the number of most likely tokens to return at each token position, each with an associated log probability.
	TopLogProbs int `json:"top_log_probs"`

	// ResponseFormat specifies the format that the model must output.
	ResponseFormat *ResponseFormat `json:"response_format,omitempty"`

	// Thinking controls whether the model is set to activate the deep thinking mode.
	// It is set to be enabled by default.
	Thinking *model.Thinking `json:"thinking,omitempty"`

	// ServiceTier specifies whether to use the TPM guarantee package. The effective target has purchased the inference access point for the guarantee package.
	ServiceTier *string `json:"service_tier"`

	// ReasoningEffort specifies the reasoning effort of the model.
	// Optional.
	ReasoningEffort *model.ReasoningEffort `json:"reasoning_effort,omitempty"`

	// BatchChat ark batch chat config
	// Optional.
	BatchChat *BatchChatConfig `json:"batch_chat,omitempty"`

	Cache *CacheConfig `json:"cache,omitempty"`
}

type ImageGenerationConfig

type ImageGenerationConfig struct {
	// For authentication, APIKey is required as the image generation API only supports API Key authentication.
	// For authentication details, see: https://www.volcengine.com/docs/82379/1298459
	// Required
	APIKey string `json:"api_key"`

	// Model specifies the ID of endpoint on ark platform
	// Required
	Model string `json:"model"`

	// Timeout specifies the maximum duration to wait for API responses
	// If HTTPClient is set, Timeout will not be used.
	// Optional. Default: 10 minutes
	Timeout *time.Duration `json:"timeout"`

	// HTTPClient specifies the client to send HTTP requests.
	// If HTTPClient is set, Timeout will not be used.
	// Optional. Default &http.Client{Timeout: Timeout}
	HTTPClient *http.Client `json:"http_client"`

	// RetryTimes specifies the number of retry attempts for failed API calls
	// Optional. Default: 2
	RetryTimes *int `json:"retry_times"`

	// BaseURL specifies the base URL for Ark service
	// Optional. Default: "https://ark.cn-beijing.volces.com/api/v3"
	BaseURL string `json:"base_url"`

	// Region specifies the region where Ark service is located
	// Optional. Default: "cn-beijing"
	Region string `json:"region"`

	// Size specifies the dimensions of the generated image.
	// It can be a resolution keyword (e.g., "1K", "2K", "4K") or a custom resolution
	// in "{width}x{height}" format (e.g., "1920x1080").
	// When using custom resolutions, the total pixels must be between 1280x720 and 4096x4096,
	// and the aspect ratio (width/height) must be between 1/16 and 16.
	// Optional. Defaults to "2048x2048".
	Size string `json:"size"`

	// SequentialImageGeneration determines if the model should generate a sequence of images.
	// Possible values:
	//  - "auto": The model decides whether to generate multiple images based on the prompt.
	//  - "disabled": Only a single image is generated.
	// Optional. Defaults to "disabled".
	SequentialImageGeneration SequentialImageGeneration `json:"sequential_image_generation"`

	// SequentialImageGenerationOption sets the maximum number of images to generate when
	// SequentialImageGeneration is set to "auto".
	// The value must be between 1 and 15.
	// Optional. Defaults to 15.
	SequentialImageGenerationOption *model.SequentialImageGenerationOptions `json:"sequential_image_generation_option"`

	// ResponseFormat specifies how the generated image data is returned.
	// Possible values:
	//  - "url": A temporary URL to download the image (valid for 24 hours).
	//  - "b64_json": The image data encoded as a Base64 string in the response.
	// Optional. Defaults to "url".
	ResponseFormat ImageResponseFormat `json:"response_format"`

	// DisableWatermark, if set to true, removes the "AI Generated" watermark
	// from the bottom-right corner of the image.
	// Optional. Defaults to false.
	DisableWatermark bool `json:"disable_watermark"`
}

type ImageGenerationModel

type ImageGenerationModel struct {
	// contains filtered or unexported fields
}

func NewImageGenerationModel

func NewImageGenerationModel(_ context.Context, config *ImageGenerationConfig) (*ImageGenerationModel, error)

func (*ImageGenerationModel) Generate

func (im *ImageGenerationModel) Generate(ctx context.Context, in []*schema.Message, opts ...einoModel.Option) (outMsg *schema.Message, err error)

func (*ImageGenerationModel) Stream

func (im *ImageGenerationModel) Stream(ctx context.Context, in []*schema.Message, opts ...einoModel.Option) (outStream *schema.StreamReader[*schema.Message], err error)

type ImageResponseFormat

type ImageResponseFormat string
const (
	ImageResponseFormatURL ImageResponseFormat = "url"
	ImageResponseFormatB64 ImageResponseFormat = "b64_json"
)

type ReasoningEffort

type ReasoningEffort = arkModel.ReasoningEffort

type ResponseFormat

type ResponseFormat struct {
	Type       model.ResponseFormatType                       `json:"type"`
	JSONSchema *model.ResponseFormatJSONSchemaJSONSchemaParam `json:"json_schema,omitempty"`
}

type ResponsesAPIChatModel

type ResponsesAPIChatModel struct {
	// contains filtered or unexported fields
}

func NewResponsesAPIChatModel

func NewResponsesAPIChatModel(_ context.Context, config *ResponsesAPIConfig) (*ResponsesAPIChatModel, error)

func (*ResponsesAPIChatModel) CreatePrefixCache

func (cm *ResponsesAPIChatModel) CreatePrefixCache(ctx context.Context, prefix []*schema.Message, ttl int, opts ...model.Option) (info *CacheInfo, err error)

CreatePrefixCache establishes a server-side cache for a prefix context, which is ideal for storing initial information like system prompts, user roles, or background details.

Once the cache is created, you can reuse it in subsequent conversational turns by passing the returned ResponseID with the WithCache option. The server will then automatically combine the cached prefix context with the new input before processing it with the model.

This approach is particularly beneficial for applications with repetitive or standardized opening prompts, as it reduces token consumption, minimizes redundant computations, and lowers overall usage costs.

Note:

  • The number of input tokens needs to be greater than or equal to 1024; otherwise, an error will be reported.
  • The stream parameter cannot be set to true.
  • When creating a prefix cache, in the returned usage, total_tokens=input_tokens, and output_tokens is always 0.
  • It is unavailable for doubao models of version 1.6 and above.

Parameters:

  • ctx: The context for the request.
  • prefix: The initial messages to be cached, such as roles and backgrounds.
  • ttl: Time-to-live in seconds for the cached prefix, default: 86400.

Returns:

  • info: Information about the created prefix cache, including the response id and token usage.
  • err: Any error encountered during the operation.

ref: https://www.volcengine.com/docs/82379/1602228?lang=zh

func (*ResponsesAPIChatModel) Generate

func (cm *ResponsesAPIChatModel) Generate(ctx context.Context, input []*schema.Message,
	opts ...model.Option) (outMsg *schema.Message, err error)

func (*ResponsesAPIChatModel) GetType

func (cm *ResponsesAPIChatModel) GetType() string

func (*ResponsesAPIChatModel) IsCallbacksEnabled

func (cm *ResponsesAPIChatModel) IsCallbacksEnabled() bool

func (*ResponsesAPIChatModel) Stream

func (cm *ResponsesAPIChatModel) Stream(ctx context.Context, input []*schema.Message,
	opts ...model.Option) (outStream *schema.StreamReader[*schema.Message], err error)

func (*ResponsesAPIChatModel) WithTools

type ResponsesAPIConfig

type ResponsesAPIConfig struct {
	// Timeout specifies the timeout for the HTTP client making requests to the ResponsesAPI.
	// If HTTPClient is set, Timeout will not be used.
	// Optional. Default: 10 minutes
	Timeout *time.Duration `json:"timeout"`

	// HTTPClient specifies the client to send HTTP requests.
	// If HTTPClient is set, Timeout will not be used.
	// Optional. Default &http.Client{Timeout: Timeout}
	HTTPClient *http.Client `json:"http_client"`

	// RetryTimes specifies the number of retry attempts for failed API calls
	// Optional. Default: 2
	RetryTimes *int `json:"retry_times"`

	// BaseURL specifies the base URL for Ark service
	// Optional. Default: "https://ark.cn-beijing.volces.com/api/v3"
	BaseURL string `json:"base_url"`

	// Region specifies the region where Ark service is located
	// Optional. Default: "cn-beijing"
	Region string `json:"region"`

	// The following three fields are about authentication - either APIKey or AccessKey/SecretKey pair is required
	// For authentication details, see: https://www.volcengine.com/docs/82379/1298459
	// APIKey takes precedence if both are provided
	APIKey string `json:"api_key"`

	AccessKey string `json:"access_key"`

	SecretKey string `json:"secret_key"`

	// Model specifies the ID of endpoint on ark platform
	// Required
	Model string `json:"model"`

	// MaxOutputTokens specifies the maximum number of tokens for model output, including both model responses and thought chain content.
	// Optional.
	MaxOutputTokens *int `json:"max_output_tokens,omitempty"`

	// Temperature specifies what sampling temperature to use
	// Generally recommend altering this or TopP but not both
	// Range: 0.0 to 2.0. Higher values make output more random
	// Optional. Default: 1.0
	Temperature *float32 `json:"temperature,omitempty"`

	// TopP controls diversity via nucleus sampling
	// Generally recommend altering this or Temperature but not both
	// Range: 0.0 to 1.0. Lower values make output more focused
	// Optional. Default: 0.7
	TopP *float32 `json:"top_p,omitempty"`

	// CustomHeader the http header passed to model when requesting model
	// Optional.
	CustomHeader map[string]string `json:"custom_header"`

	// ResponseFormat specifies the format that the model must output.
	// Optional.
	ResponseFormat *ResponseFormat `json:"response_format,omitempty"`

	// Thinking controls whether the model is set to activate the deep thinking mode.
	// It is set to be enabled by default.
	// Optional.
	Thinking *Thinking `json:"thinking,omitempty"`

	// ServiceTier specifies whether to use the TPM guarantee package. The effective target has purchased the inference access point for the guarantee package.
	// Optional.
	ServiceTier *string `json:"service_tier"`

	// ReasoningEffort specifies the reasoning effort of the model.
	// Optional.
	ReasoningEffort *ReasoningEffort `json:"reasoning_effort,omitempty"`

	// SessionCache is the configuration of ResponsesAPI session cache.
	// It can be overridden by [WithCache].
	// Optional.
	SessionCache *SessionCacheConfig `json:"session_cache,omitempty"`

	// EnableToolWebSearch enables the web search tool.
	// Web Search is a basic internet search tool that can obtain real-time public network information
	// (such as news, products, weather, etc.) for your large model through the Responses API.
	// This tool can solve core issues such as data timeliness, knowledge gaps, and information synchronization,
	// and you do not need to develop your own search engine or maintain data resources.
	// Note: This option is only effective for the Responses API.
	// For more details, see https://www.volcengine.com/docs/82379/1756990?lang=zh
	// Optional.
	EnableToolWebSearch *ToolWebSearch `json:"enable_tool_web_search,omitempty"`

	// MaxToolCalls specifies the maximum number of tool-calling rounds.
	// The value must be in the range [1, 10].
	// After this limit is reached, the model is prompted to stop making further tool calls and generate a response.
	// Note: This is a best-effort parameter, and the actual number of calls may be affected by model performance and tool results.
	// The default value for the Web Search tool is 3.
	// For more details, see https://www.volcengine.com/docs/82379/1569618?lang=zh
	// Optional.
	MaxToolCalls *int64 `json:"max_tool_calls,omitempty"`

	// EnableReasoningContentPassback controls whether reasoning content
	// from assistant messages is passed back to the model in multi-turn conversations.
	// When enabled, reasoning content is included as reasoning summary items in the input,
	// allowing the model to be aware of its prior chain-of-thought.
	// However, if a valid previous_response_id is set (via session cache), the passback is
	// automatically skipped because previous_response_id already preserves the full conversation
	// context including the chain-of-thought on the server side.
	// Note: This feature requires doubao models v1.8+. Earlier versions (e.g., v1.6) do not
	// support reasoning items in the input and will return an error.
	// For more details, see https://www.volcengine.com/docs/82379/1449737
	// Default: false.
	// Optional.
	EnableReasoningContentPassback bool `json:"enable_reasoning_content_passback,omitempty"`
}

type SequentialImageGeneration

type SequentialImageGeneration string
const (
	SequentialImageGenerationDisabled SequentialImageGeneration = "disabled"
	SequentialImageGenerationAuto     SequentialImageGeneration = "auto"
)

type SessionCacheConfig

type SessionCacheConfig struct {
	// EnableCache controls whether session caching is active.
	// When enabled, the model stores both inputs and responses for each conversation turn,
	// allowing them to be retrieved later via API.
	// Response IDs are saved in output messages and can be accessed using GetResponseID.
	// For multi-turn conversations, the ARK ChatModel automatically identifies the most recent
	// cached message from all inputs and passes its response ID to model to maintain context continuity.
	// This message and all previous ones are trimmed before being sent to the model.
	// When both HeadPreviousResponseID and cached message exist, the message's response ID takes precedence.
	// Use InvalidateMessageCaches to disables caching for the specified messages.
	EnableCache bool `json:"enable_cache"`

	// TTL specifies the survival time of cached data in seconds, with a maximum of 3 * 86400(3 days).
	TTL int `json:"ttl"`
}

type Source

type Source string

Source specifies the additional content source for web searches. Optional sources are Toutiao, Douyin , and Moji Weather.

  • toutiao: Additional content source from Toutiao for web searches.
  • douyin: Additional content source from Douyin for web searches.
  • moji: Additional content source from Moji Weather for web searches.

type Thinking

type Thinking = arkModel.Thinking

type ThinkingType

type ThinkingType = arkModel.ThinkingType

type ToolWebSearch

type ToolWebSearch struct {
	// Limit is the maximum number of results to retrieve per search in a single round.
	// It affects input size and performance. The value must be in the range [1, 50].
	// Optional. Default 10
	Limit *int64 `json:"limit,omitempty"`

	// UserLocation is the user's geographical location, used for scenarios like weather queries.
	// It includes `type`, `country`, `city`, and `region` fields.
	UserLocation *UserLocation `json:"user_location,omitempty"`

	// Sources is a list of additional content sources for web searches. See the Source type for available options.
	Sources []Source `json:"sources,omitempty"`

	// MaxKeyword is the maximum number of keywords to search in parallel within a single tool-use round.
	// For example, if the model identifies multiple keywords to search (e.g., "A", "B", "C")
	// and max_keyword is 1, only the first keyword ("A") will be searched.
	// The value must be in the range [1, 50].
	// Optional.
	MaxKeyword *int32 `json:"max_keyword,omitempty"`
}

ToolWebSearch holds the configuration for the web search tool.

type UserLocation

type UserLocation struct {
	City     *string `json:"city,omitempty"`
	Country  *string `json:"country,omitempty"`
	Region   *string `json:"region,omitempty"`
	Timezone *string `json:"timezone,omitempty"`
}

UserLocation is the user's geographical location, used for scenarios like weather queries. It includes `country`, `city`, `region` and `timezone` fields.

Directories

Path Synopsis
examples
generate command
image_generate command
intent_tool command
stream command

Jump to

Keyboard shortcuts

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