openai

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CompletionModelDefault = openai.ChatModelGPT5_4
	CompletionPath         = "/chat/completions"
)
View Source
const (
	DefaultBaseUrl = "https://api.openai.com/v1"
)
View Source
const (
	DefaultRealTimeModel = "gpt-4o-realtime-preview"
)
View Source
const (
	EmbeddingPath = "/embeddings"
)

Variables

This section is empty.

Functions

func NewOpenaiProvider

func NewOpenaiProvider(opts ...types.ProviderOption) (types.Provider, error)

Types

type AudioFormatType

type AudioFormatType string
const (
	AudioFormatTypePcm16    AudioFormatType = "audio/pcm"
	AudioFormatTypeG711Ulaw AudioFormatType = "audio/pcmu"
	AudioFormatTypeG711Alaw AudioFormatType = "audio/pcma"
)

type ClientEventConversationItemCreate

type ClientEventConversationItemCreate struct {
	EventBase
	PreviousItemId string       `json:"previous_item_id,omitempty"`
	Item           RealTimeItem `json:"item,omitempty"`
}

func NewClientEventConversationItemCreate

func NewClientEventConversationItemCreate() *ClientEventConversationItemCreate

type ClientEventInputAudioBufferAppend

type ClientEventInputAudioBufferAppend struct {
	EventBase
	Audio string `json:"audio,omitempty"`
}

func NewClientEventInputAudioBufferAppend

func NewClientEventInputAudioBufferAppend() *ClientEventInputAudioBufferAppend

type ClientEventResponseCreate

type ClientEventResponseCreate struct {
	EventBase
}

func NewClientEventResponseCreate

func NewClientEventResponseCreate() *ClientEventResponseCreate

type ClientEventSessionUpdate

type ClientEventSessionUpdate struct {
	EventBase
	Session ClientEventSessionUpdateSession `json:"session"`
}

func CreateSessionUpdateEvent

func CreateSessionUpdateEvent(model string, opts *types.RealTimeOptions) (*ClientEventSessionUpdate, error)

func NewClientEventSessionUpdate

func NewClientEventSessionUpdate() *ClientEventSessionUpdate

type ClientEventSessionUpdateSession

type ClientEventSessionUpdateSession struct {
	Type             string                                          `json:"type"` //realtime,transcription
	Model            string                                          `json:"model,omitempty"`
	Instructions     string                                          `json:"instructions,omitempty"`
	OutputModalities []Modality                                      `json:"output_modalities,omitempty"`
	Tools            []*ClientEventSessionUpdateSessionToolFunctions `json:"tools,omitempty"`
	Audio            *ClientEventSessionUpdateSessionAudio           `json:"audio,omitempty"`
}

type ClientEventSessionUpdateSessionAudio

type ClientEventSessionUpdateSessionAudio struct {
	Input  *ClientEventSessionUpdateSessionAudioInput  `json:"input,omitempty"`
	Output *ClientEventSessionUpdateSessionAudioOutput `json:"output,omitempty"`
}

type ClientEventSessionUpdateSessionAudioInput

type ClientEventSessionUpdateSessionAudioInput struct {
	TurnDetection *ClientEventSessionUpdateSessionAudioInputTurnDetection `json:"turn_detection,omitempty"`
}

type ClientEventSessionUpdateSessionAudioInputTurnDetection

type ClientEventSessionUpdateSessionAudioInputTurnDetection struct {
	Type              ClientTurnDetectionType `json:"type"` //server_vad or semantic_vad
	CreateResponse    bool                    `json:"create_response"`
	InterruptResponse bool                    `json:"interrupt_response"`
}

type ClientEventSessionUpdateSessionAudioOutput

type ClientEventSessionUpdateSessionAudioOutput struct {
	Voice Voice `json:"voice"`
}

type ClientEventSessionUpdateSessionToolFunctions

type ClientEventSessionUpdateSessionToolFunctions struct {
	Type        string `json:"type"` // must be function
	Name        string `json:"name"`
	Description string `json:"description"`
	Parameters  any    `json:"parameters"`
}

type ClientSession

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

func (*ClientSession) Close

func (r *ClientSession) Close()

func (*ClientSession) Recv

func (*ClientSession) Send

func (r *ClientSession) Send(ctx context.Context, msg *types.Message) error

type ClientTurnDetectionType

type ClientTurnDetectionType string
const (
	ClientTurnDetectionTypeServerVad   ClientTurnDetectionType = "server_vad"
	ClientTurnDetectionTypeSemanticVad ClientTurnDetectionType = "semantic_vad"
)

type CompletionChoise

type CompletionChoise struct {
	Index        int                `json:"index"`
	FinishReason string             `json:"finish_reason"`
	Delta        *CompletionMessage `json:"delta"`
	Message      *CompletionMessage `json:"message"`
}

type CompletionMessage

type CompletionMessage struct {
	ReasoningContent string               `json:"reasoning_content"`
	Content          string               `json:"content"`
	Refusal          string               `json:"refusal"`
	ToolCalls        []CompletionToolCall `json:"tool_calls"`
}

type CompletionMessageParam

type CompletionMessageParam struct {
	Role             string                `json:"role"`
	Content          string                `json:"content,omitempty"`
	ReasoningContent string                `json:"reasoning_content,omitempty"`
	Refusal          string                `json:"refusal,omitempty"`
	ToolCalls        []*CompletionToolCall `json:"tool_calls,omitempty"`
	ToolCallID       string                `json:"tool_call_id,omitempty"`
}

type CompletionReq

type CompletionReq struct {
	Model               string                      `json:"model,omitempty"`
	Messages            []*CompletionMessageParam   `json:"messages,omitempty"`
	Stream              bool                        `json:"stream,omitempty"`
	StreamOptions       *CompletionReqStreamOptions `json:"stream_options,omitempty"`
	Tools               []*Tool                     `json:"tools,omitempty"`
	Temperature         *float32                    `json:"temperature,omitempty"`
	TopP                *float32                    `json:"top_p,omitempty"`
	MaxTokens           *int64                      `json:"max_tokens,omitempty"`
	MaxCompletionTokens *int64                      `json:"max_completion_tokens,omitempty"`
	Stop                []string                    `json:"stop,omitempty"`
	ReasoningEffort     *ReasoningEffort            `json:"reasoning_effort,omitempty"`
	ExtraBody           map[string]any              `json:"-"`
}

func (*CompletionReq) MarshalJSON

func (c *CompletionReq) MarshalJSON() ([]byte, error)

type CompletionReqExtra

type CompletionReqExtra struct {
	Thinking           any `json:"thinking,omitempty"`
	ChatTemplateKwargs any `json:"chat_template_kwargs,omitempty"`
}

type CompletionReqStreamOptions

type CompletionReqStreamOptions struct {
	IncludeUsage bool `json:"include_usage"`
}

type CompletionRsp

type CompletionRsp struct {
	ID                string              `json:"id,omitempty"`
	Object            string              `json:"object,omitempty"`
	Created           int64               `json:"created,omitempty"`
	Model             string              `json:"model,omitempty"`
	SystemFingerprint string              `json:"system_fingerprint,omitempty"`
	Choices           []*CompletionChoise `json:"choices,omitempty"`
	Usage             CompletionUsage     `json:"usage,omitempty"`
}

type CompletionTokenDetail

type CompletionTokenDetail struct {
	ReasoningTokens          int64 `json:"reasoning_tokens"`
	AcceptedPredictionTokens int64 `json:"accepted_prediction_tokens"`
	RejectedPredictionTokens int64 `json:"rejected_prediction_tokens"`
	AudioTokens              int64 `json:"audio_tokens"`
}

type CompletionToolCall

type CompletionToolCall struct {
	Index    int                         `json:"index"`
	ID       string                      `json:"id"`
	Type     ToolType                    `json:"type"` // function, custom
	Function *CompletionToolCallFunction `json:"function"`
}

type CompletionToolCallFunction

type CompletionToolCallFunction struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

type CompletionUsage

type CompletionUsage struct {
	CompletionTokens int64                 `json:"completion_tokens"`
	PromptTokens     int64                 `json:"prompt_tokens"`
	TotalTokens      int64                 `json:"total_tokens"`
	TokenDetail      CompletionTokenDetail `json:"completion_tokens_details"`
}

type EmbeddingReq

type EmbeddingReq struct {
	Model          string   `json:"model"`
	Input          []string `json:"input"`
	Dimensions     int      `json:"dimensions,omitempty"`
	EncodingFormat string   `json:"encoding_format"` //float base64
}

type EmbeddingRsp

type EmbeddingRsp struct {
	Model   string              `json:"model"`
	Data    []*EmbeddingRspData `json:"data"`
	Usage   CompletionUsage     `json:"usage"`
	Object  string              `json:"object"`
	Message string              `json:"message"`
}

type EmbeddingRspData

type EmbeddingRspData struct {
	Index     int       `json:"index"`
	Embedding []float64 `json:"embedding"`
}

type Event

type Event struct {
	EventBase
	EventPayload
}

func NewEvent

func NewEvent(t EventType, id string) *Event

func (Event) MarshalJSON

func (e Event) MarshalJSON() ([]byte, error)

func (*Event) UnmarshalJSON

func (e *Event) UnmarshalJSON(data []byte) error

type EventBase

type EventBase struct {
	Type    EventType `json:"type,omitempty"`
	EventId string    `json:"event_id,omitempty"`
}

type EventPayload

type EventPayload map[string]any

type EventType

type EventType string
var (
	ClientEventTypeSessionUpdate          EventType = "session.update"
	ClientEventTypeConversationItemCreate EventType = "conversation.item.create"
	ClientEventTypeInputAudioBufferAppend EventType = "input_audio_buffer.append"
	ClientEventTypeResponseCreate         EventType = "response.create"

	ServerEventTypeResponseOutputAudioDelta          EventType = "response.output_audio.delta"
	ServerEventTypeResponseFunctionCallArgumentsDone EventType = "response.function_call_arguments.done"
	ServerEventTypeResponseDone                      EventType = "response.done"
)

func UnmarshalServerEvent

func UnmarshalServerEvent(data []byte) (EventType, any, error)

type Modality

type Modality string
const (
	ModalityText  Modality = "text"
	ModalityAudio Modality = "audio"
)

func ToModality

func ToModality(in types.Modality) (Modality, error)

type OpenaiProvider

type OpenaiProvider struct {
	types.ProviderNop
	// contains filtered or unexported fields
}

func (*OpenaiProvider) Completion

func (p *OpenaiProvider) Completion(ctx context.Context, model string, messages []*types.Message, options ...types.CompletionOption) (*types.Completion, error)

func (*OpenaiProvider) Embedding

func (p *OpenaiProvider) Embedding(ctx context.Context, model string, input []string, options ...types.EmbeddingOption) (*types.Embedding, error)

func (*OpenaiProvider) Realtime

func (p *OpenaiProvider) Realtime(ctx context.Context, model string, messages []*types.Message, options ...types.RealTimeOption) (types.RealTimeSession, error)

type RealTimeContent

type RealTimeContent struct {
	Type string `json:"type,omitempty"` //input_text, input_audio, input_image, output_text, output_audio
	//text
	Text string `json:"text,omitempty"`
	//audio
	Audio      string `json:"audio,omitempty"`
	Transcript string `json:"transcript,omitempty"`
	//image
	ImageUrl string `json:"image_url,omitempty"`
	Detail   string `json:"detail,omitempty"`
}

type RealTimeItem

type RealTimeItem struct {
	Role string `json:"role,omitempty"`
	Type string `json:"type,omitempty"` //message,function_call,function_call_output
	ID   string `json:"id,omitempty"`
	//message
	Content []*RealTimeContent `json:"content,omitempty"`
	//function_call
	CallID    string `json:"call_id,omitempty"`
	Name      string `json:"name,omitempty"`
	Arguments string `json:"arguments,omitempty"`
	//function_call_output
	Output string `json:"output,omitempty"`
}

type ReasoningEffort

type ReasoningEffort string
const (
	ReasoningEffortNone    ReasoningEffort = "none"
	ReasoningEffortMinimal ReasoningEffort = "minimal"
	ReasoningEffortLow     ReasoningEffort = "low"
	ReasoningEffortMedium  ReasoningEffort = "medium"
	ReasoningEffortHigh    ReasoningEffort = "high"
	ReasoningEffortXhigh   ReasoningEffort = "xhigh"
)

type Role

type Role string
const (
	RoleDeveloper Role = "developer"
	RoleSystem    Role = "system"
	RoleUser      Role = "user"
	RoleAssistant Role = "assistant"
	RoleTool      Role = "tool"
)

type ServerEventResponseDone

type ServerEventResponseDone struct {
	EventBase
	ID       string                          `json:"id"`
	Response ServerEventResponseDoneResponse `json:"response"`
}

type ServerEventResponseDoneResponse

type ServerEventResponseDoneResponse struct {
	ConversationId string          `json:"conversation_id"`
	Status         string          `json:"status"`
	Output         []*RealTimeItem `json:"ouput"`
}

type ServerEventResponseFunctionCallArgumentsDone

type ServerEventResponseFunctionCallArgumentsDone struct {
	EventBase
	CallId    string `json:"call_id"`
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

type ServerEventResponseOutputAudioDelta

type ServerEventResponseOutputAudioDelta struct {
	EventBase
	Delta string `json:"delta"`
}

type ServerTurnDetectionType

type ServerTurnDetectionType string
const (
	ServerTurnDetectionTypeNone        ServerTurnDetectionType = "none"
	ServerTurnDetectionTypeServerVad   ServerTurnDetectionType = "server_vad"
	ServerTurnDetectionTypeSemanticVad ServerTurnDetectionType = "semantic_vad"
)

type Tool

type Tool struct {
	Type     ToolType      `json:"type"`
	Function *ToolFunction `json:"function"`
}

type ToolFunction

type ToolFunction struct {
	Name        string         `json:"name"`
	Description string         `json:"description"`
	Parameters  map[string]any `json:"parameters"`
	Strict      bool           `json:"strict"`
}

type ToolType

type ToolType string
const (
	ToolTypeFucntion ToolType = "function"
	ToolTypeCustom   ToolType = "custom"
)

type Voice

type Voice string
const (
	VoiceNone    Voice = ""
	VoiceAlloy   Voice = "alloy"
	VoiceAsh     Voice = "ash"
	VoiceBallad  Voice = "ballad"
	VoiceCoral   Voice = "coral"
	VoiceEcho    Voice = "echo"
	VoiceFable   Voice = "fable"
	VoiceOnyx    Voice = "onyx"
	VoiceNova    Voice = "nova"
	VoiceSage    Voice = "sage"
	VoiceShimmer Voice = "shimmer"
	VoiceVerse   Voice = "verse"
)

func ToVoice

func ToVoice(in types.AudioVoiceType) (Voice, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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