openai

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package openai provides OpenAI provider adapter for the OmniLLM unified interface

Package openai provides OpenAI API client implementation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewProvider

func NewProvider(apiKey, baseURL string, httpClient *http.Client) provider.Provider

NewProvider creates a new OpenAI provider adapter

Types

type Choice

type Choice struct {
	Index        int     `json:"index"`
	Message      Message `json:"message"`
	FinishReason *string `json:"finish_reason"`
}

Choice represents a choice in the response

type Client

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

Client implements OpenAI API client

func New

func New(apiKey, baseURL string, httpClient *http.Client) *Client

New creates a new OpenAI client

func (*Client) Close

func (c *Client) Close() error

Close closes the client

func (*Client) CreateCompletion

func (c *Client) CreateCompletion(ctx context.Context, req *Request) (*Response, error)

CreateCompletion creates a chat completion

func (*Client) CreateCompletionStream

func (c *Client) CreateCompletionStream(ctx context.Context, req *Request) (*Stream, error)

CreateCompletionStream creates a streaming chat completion

func (*Client) Name

func (c *Client) Name() string

Name returns the provider name

type Message

type Message struct {
	Role       string     `json:"role"`
	Content    string     `json:"content"`
	Name       *string    `json:"name,omitempty"`
	ToolCallID *string    `json:"tool_call_id,omitempty"`
	ToolCalls  []ToolCall `json:"tool_calls,omitempty"`
}

Message represents a chat message

type Provider

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

Provider represents the OpenAI provider adapter

func (*Provider) Close

func (p *Provider) Close() error

Close closes the provider

func (*Provider) CreateChatCompletion

CreateChatCompletion creates a chat completion

func (*Provider) CreateChatCompletionStream

func (p *Provider) CreateChatCompletionStream(ctx context.Context, req *provider.ChatCompletionRequest) (provider.ChatCompletionStream, error)

CreateChatCompletionStream creates a streaming chat completion

func (*Provider) Name

func (p *Provider) Name() string

Name returns the provider name

type Request

type Request struct {
	Model            string          `json:"model"`
	Messages         []Message       `json:"messages"`
	MaxTokens        *int            `json:"max_tokens,omitempty"`
	Temperature      *float64        `json:"temperature,omitempty"`
	TopP             *float64        `json:"top_p,omitempty"`
	Stream           *bool           `json:"stream,omitempty"`
	Stop             []string        `json:"stop,omitempty"`
	PresencePenalty  *float64        `json:"presence_penalty,omitempty"`
	FrequencyPenalty *float64        `json:"frequency_penalty,omitempty"`
	LogitBias        map[string]int  `json:"logit_bias,omitempty"`
	User             *string         `json:"user,omitempty"`
	Tools            []Tool          `json:"tools,omitempty"`
	ToolChoice       any             `json:"tool_choice,omitempty"`
	Seed             *int            `json:"seed,omitempty"`
	N                *int            `json:"n,omitempty"`
	ResponseFormat   *ResponseFormat `json:"response_format,omitempty"`
	Logprobs         *bool           `json:"logprobs,omitempty"`
	TopLogprobs      *int            `json:"top_logprobs,omitempty"`
}

Request represents an OpenAI chat completion request

type Response

type Response struct {
	ID      string   `json:"id"`
	Object  string   `json:"object"`
	Created int64    `json:"created"`
	Model   string   `json:"model"`
	Choices []Choice `json:"choices"`
	Usage   Usage    `json:"usage"`
}

Response represents an OpenAI chat completion response

type ResponseFormat added in v0.11.0

type ResponseFormat struct {
	Type string `json:"type"` // "text" or "json_object"
}

ResponseFormat specifies the format of the response

type Stream

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

Stream implements streaming for OpenAI

func (*Stream) Close

func (s *Stream) Close() error

Close closes the stream

func (*Stream) Recv

func (s *Stream) Recv() (*StreamChunk, error)

Recv receives the next chunk from the stream

type StreamAdapter

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

StreamAdapter adapts OpenAI stream to unified interface

func (*StreamAdapter) Close

func (s *StreamAdapter) Close() error

Close closes the stream

func (*StreamAdapter) Recv

Recv receives the next chunk from the stream

type StreamChoice

type StreamChoice struct {
	Index        int      `json:"index"`
	Delta        *Message `json:"delta,omitempty"`
	FinishReason *string  `json:"finish_reason"`
}

StreamChoice represents a choice in streaming response

type StreamChunk

type StreamChunk struct {
	ID      string         `json:"id"`
	Object  string         `json:"object"`
	Created int64          `json:"created"`
	Model   string         `json:"model"`
	Choices []StreamChoice `json:"choices"`
	Usage   *Usage         `json:"usage,omitempty"`
}

StreamChunk represents a chunk in streaming response

type Tool added in v0.12.0

type Tool struct {
	Type     string   `json:"type"`
	Function ToolSpec `json:"function"`
}

Tool represents a tool that can be called

type ToolCall added in v0.12.0

type ToolCall struct {
	ID       string       `json:"id"`
	Type     string       `json:"type"`
	Function ToolFunction `json:"function"`
}

ToolCall represents a tool function call

type ToolFunction added in v0.12.0

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

ToolFunction represents the function being called

type ToolSpec added in v0.12.0

type ToolSpec struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	Parameters  any    `json:"parameters"`
}

ToolSpec defines a tool specification

type Usage

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

Usage represents token usage information

Jump to

Keyboard shortcuts

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