openaicompat

package
v0.38.1 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: Apache-2.0 Imports: 10 Imported by: 6

Documentation

Overview

Package openaicompat provides an implementation of the fantasy AI SDK for OpenAI-compatible APIs.

Package openaicompat provides an implementation of the fantasy AI SDK for OpenAI-compatible APIs.

Index

Constants

View Source
const (
	TypeProviderOptions    = Name + ".options"
	TypeContentExtraFields = Name + ".content_extra_fields"
)

Global type identifiers for OpenAI-compatible provider data.

View Source
const (
	// Name is the name of the OpenAI-compatible provider.
	Name = "openai-compat"
)

Variables

This section is empty.

Functions

func ExtraContentFunc

func ExtraContentFunc(choice openaisdk.ChatCompletionChoice) []fantasy.Content

ExtraContentFunc adds extra content to the response.

func New

func New(opts ...Option) (fantasy.Provider, error)

New creates a new OpenAI-compatible provider with the given options.

func NewProviderOptions

func NewProviderOptions(opts *ProviderOptions) fantasy.ProviderOptions

NewProviderOptions creates new provider options for the OpenAI-compatible provider.

func PrepareCallFunc

func PrepareCallFunc(model fantasy.LanguageModel, params *openaisdk.ChatCompletionNewParams, call fantasy.Call) ([]fantasy.CallWarning, error)

PrepareCallFunc prepares the call for the language model.

func StreamExtraFunc

func StreamExtraFunc(chunk openaisdk.ChatCompletionChunk, yield func(fantasy.StreamPart) bool, ctx map[string]any) (map[string]any, bool)

StreamExtraFunc handles extra functionality for streaming responses.

func ToPromptFunc added in v0.2.1

ToPromptFunc converts a fantasy prompt to OpenAI format with reasoning support. It handles fantasy.ContentTypeReasoning in assistant messages by adding the reasoning_content field to the message JSON.

Types

type ContentExtraFields added in v0.35.0

type ContentExtraFields struct {
	Fields map[string]any `json:"fields"`
}

ContentExtraFields injects non-standard JSON fields onto an individual message content block. It is the content-block analogue of ProviderOptions.ExtraBody (which injects fields onto the request root), and exists to support provider extensions that the OpenAI API format does not model.

When a content part carries this option, its content is automatically serialized in array form (rather than as a plain string), and the given fields are merged onto the text block.

The primary use case is explicit prompt caching on Alibaba Cloud Qwen 3.6 models, which expect a non-standard cache_control field on cached content blocks:

textPart := fantasy.TextPart{
    Text: "Large system prompt or document (min 1024 tokens)...",
    ProviderOptions: fantasy.ProviderOptions{
        "openai-compat": &openaicompat.ContentExtraFields{
            Fields: map[string]any{
                "cache_control": map[string]string{"type": "ephemeral"},
            },
        },
    },
}

Providers that do not recognize the injected fields will ignore them.

Note: at the message level this shares a provider-options key with ProviderOptions, so a single message cannot carry both message-level ProviderOptions and message-level ContentExtraFields. Set ContentExtraFields at the content-part level to avoid that conflict.

func (ContentExtraFields) MarshalJSON added in v0.35.0

func (o ContentExtraFields) MarshalJSON() ([]byte, error)

MarshalJSON implements custom JSON marshaling with type info for ContentExtraFields.

func (*ContentExtraFields) Options added in v0.35.0

func (*ContentExtraFields) Options()

Options implements the ProviderOptions interface.

func (*ContentExtraFields) UnmarshalJSON added in v0.35.0

func (o *ContentExtraFields) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling with type info for ContentExtraFields.

type Option

type Option = func(*options)

Option defines a function that configures OpenAI-compatible provider options.

func WithAPIKey

func WithAPIKey(apiKey string) Option

WithAPIKey sets the API key for the OpenAI-compatible provider.

func WithBaseURL

func WithBaseURL(url string) Option

WithBaseURL sets the base URL for the OpenAI-compatible provider.

func WithHTTPClient

func WithHTTPClient(client option.HTTPClient) Option

WithHTTPClient sets the HTTP client for the OpenAI-compatible provider.

func WithHeaders

func WithHeaders(headers map[string]string) Option

WithHeaders sets the headers for the OpenAI-compatible provider.

func WithLanguageModelOptions added in v0.33.0

func WithLanguageModelOptions(opts ...openai.LanguageModelOption) Option

WithLanguageModelOptions appends language model options to the provider. This allows callers to customize usage extraction, stream handling, and other language model behaviors.

func WithName

func WithName(name string) Option

WithName sets the name for the OpenAI-compatible provider.

func WithObjectMode added in v0.3.0

func WithObjectMode(om fantasy.ObjectMode) Option

WithObjectMode sets the object generation mode for the OpenAI-compatible provider. Supported modes: ObjectModeTool, ObjectModeText. ObjectModeAuto and ObjectModeJSON are automatically converted to ObjectModeTool since OpenAI-compatible endpoints typically don't support native JSON mode.

func WithResponsesAPIFunc added in v0.24.0

func WithResponsesAPIFunc(fn func(modelID string) bool) Option

WithResponsesAPIFunc sets a custom filter for which models use the Responses API.

func WithSDKOptions

func WithSDKOptions(opts ...option.RequestOption) Option

WithSDKOptions sets the SDK options for the OpenAI-compatible provider.

func WithUseResponsesAPI added in v0.6.0

func WithUseResponsesAPI() Option

WithUseResponsesAPI configures the provider to use the responses API for models that support it.

func WithUserAgent added in v0.12.0

func WithUserAgent(ua string) Option

WithUserAgent sets an explicit User-Agent header, overriding the default and any value set via WithHeaders.

type ProviderOptions

type ProviderOptions struct {
	User            *string                 `json:"user"`
	ReasoningEffort *openai.ReasoningEffort `json:"reasoning_effort"`
	ExtraBody       map[string]any          `json:"extra_body,omitempty"`
}

ProviderOptions represents additional options for the OpenAI-compatible provider.

func ParseOptions

func ParseOptions(data map[string]any) (*ProviderOptions, error)

ParseOptions parses provider options from a map for OpenAI-compatible provider.

func (ProviderOptions) MarshalJSON added in v0.3.0

func (o ProviderOptions) MarshalJSON() ([]byte, error)

MarshalJSON implements custom JSON marshaling with type info for ProviderOptions.

func (*ProviderOptions) Options

func (*ProviderOptions) Options()

Options implements the ProviderOptions interface.

func (*ProviderOptions) UnmarshalJSON added in v0.3.0

func (o *ProviderOptions) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling with type info for ProviderOptions.

type ReasoningData

type ReasoningData struct {
	ReasoningContent string `json:"reasoning_content"`
	Reasoning        string `json:"reasoning"`
}

ReasoningData represents reasoning data for OpenAI-compatible provider. Some providers use "reasoning_content" (e.g. Avian), others use "reasoning" (e.g. Moonshot AI/Kimi).

func (ReasoningData) GetReasoningContent added in v0.21.0

func (r ReasoningData) GetReasoningContent() string

GetReasoningContent returns the reasoning text from whichever field is populated.

Jump to

Keyboard shortcuts

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