openai

package
v0.19.2 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const MAX_TOOL_CALL_ITERATIONS = 20

Variables

This section is empty.

Functions

func WithToolHandler

func WithToolHandler(ctx context.Context, h ToolHandler) context.Context

WithToolHandler attaches a per-request ToolHandler to the context.

Types

type ChatCompletionRequest

type ChatCompletionRequest struct {
	Model           string    `json:"model"`
	Messages        []Message `json:"messages"`
	Tools           []Tool    `json:"tools,omitempty"`
	MaxTokens       int       `json:"max_tokens,omitempty"`
	Temperature     float32   `json:"temperature,omitempty"`
	ReasoningEffort string    `json:"reasoning_effort,omitempty"`
	Stream          bool      `json:"stream"`
}

ChatCompletionRequest represents a request to the chat completions endpoint

type ChatCompletionResponse

type ChatCompletionResponse 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,omitempty"`
}

ChatCompletionResponse represents a response from the chat completions endpoint

type ChatStream

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

func (*ChatStream) Current

func (s *ChatStream) Current() ChatCompletionResponse

func (*ChatStream) Err

func (s *ChatStream) Err() error

func (*ChatStream) Next

func (s *ChatStream) Next() bool

type Choice

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

Choice represents a single choice in the response

type Client

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

Client represents an OpenAI API client

func New

func New(config Config, mcpServer MCPServer) (*Client, error)

New creates a new OpenAI client

func (*Client) ChatCompletion

func (c *Client) ChatCompletion(ctx context.Context, req ChatCompletionRequest) (*ChatCompletionResponse, error)

ChatCompletion performs a non-streaming chat completion with automatic tool processing

func (*Client) GetModels

func (c *Client) GetModels(ctx context.Context) (*ModelsResponse, error)

GetModels retrieves the list of available models from OpenAI

func (*Client) StreamChatCompletion

func (c *Client) StreamChatCompletion(ctx context.Context, req ChatCompletionRequest) *ChatStream

StreamChatCompletion performs a streaming chat completion with automatic tool processing Returns a channel of pure OpenAI ChatCompletionResponse chunks

type CompletionAccumulator

type CompletionAccumulator struct {
	Choices []Choice `json:"choices"`
}

CompletionAccumulator accumulates streaming chunks into a complete ChatCompletionResponse

func (*CompletionAccumulator) AddChunk

func (acc *CompletionAccumulator) AddChunk(chunk ChatCompletionResponse)

AddChunk adds a streaming chunk to the accumulator

func (*CompletionAccumulator) FinishedContent

func (acc *CompletionAccumulator) FinishedContent() (string, bool)

FinishedContent returns the content if it was completed

func (*CompletionAccumulator) FinishedRefusal

func (acc *CompletionAccumulator) FinishedRefusal() (string, bool)

FinishedRefusal returns the refusal if it was completed

func (*CompletionAccumulator) FinishedToolCall

func (acc *CompletionAccumulator) FinishedToolCall() (*ToolCall, bool)

FinishedToolCall returns a tool call if it was completed

type Config

type Config struct {
	APIKey  string
	BaseURL string
	Timeout time.Duration
}

Config holds configuration for the OpenAI client

type ContentPart

type ContentPart struct {
	Type string `json:"type"`
	Text string `json:"text,omitempty"`
}

ContentPart represents a part of message content (for multimodal messages)

type Delta

type Delta struct {
	Role             string          `json:"role,omitempty"`
	Content          string          `json:"content,omitempty"`
	ReasoningContent string          `json:"reasoning_content"`
	ToolCalls        []DeltaToolCall `json:"tool_calls,omitempty"`
}

Delta represents incremental changes in streaming responses

type DeltaFunction

type DeltaFunction struct {
	Name      string `json:"name,omitempty"`
	Arguments string `json:"arguments,omitempty"`
}

DeltaFunction represents incremental function call data

type DeltaToolCall

type DeltaToolCall struct {
	Index    int           `json:"index"`
	ID       string        `json:"id,omitempty"`
	Type     string        `json:"type,omitempty"`
	Function DeltaFunction `json:"function,omitempty"`
}

DeltaToolCall represents incremental tool call data in streaming

type MCPServer

type MCPServer interface {
	ListTools() []mcp.MCPTool
	CallTool(ctx context.Context, name string, args map[string]any) (*mcp.ToolResponse, error)
}

MCPServer interface for MCP server operations

type Message

type Message struct {
	Role       string     `json:"role,omitempty"`
	Content    any        `json:"content,omitempty"` // Can be string or []ContentPart
	ToolCalls  []ToolCall `json:"tool_calls,omitempty"`
	ToolCallID string     `json:"tool_call_id,omitempty"`
}

Message represents a message in the conversation

func (*Message) GetContentAsString

func (m *Message) GetContentAsString() string

GetContentAsString returns the content as a string, handling both string and array formats

func (*Message) SetContentAsString

func (m *Message) SetContentAsString(content string)

SetContentAsString sets the content as a simple string

type Model

type Model struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Created int64  `json:"created"`
	OwnedBy string `json:"owned_by"`
}

Model represents a single model from the OpenAI API

type ModelsResponse

type ModelsResponse struct {
	Data   []Model `json:"data"`
	Object string  `json:"object"`
}

ModelsResponse represents the response from the models endpoint

type Service

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

func NewService

func NewService(client *Client, systemPrompt string) *Service

func (*Service) HandleChatCompletions

func (s *Service) HandleChatCompletions(w http.ResponseWriter, r *http.Request)

Handles POST /v1/chat/completions

func (*Service) HandleGetModels

func (s *Service) HandleGetModels(w http.ResponseWriter, r *http.Request)

Handles GET /v1/models

type Tool

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

Tool represents a tool definition

type ToolCall

type ToolCall struct {
	Index    int              `json:"index,omitempty"`
	ID       string           `json:"id"`
	Type     string           `json:"type"`
	Function ToolCallFunction `json:"function"`
}

ToolCall represents a function call request

type ToolCallFunction

type ToolCallFunction struct {
	Name      string         `json:"name"`
	Arguments map[string]any `json:"-"` // Custom marshaling handles this
}

ToolCallFunction represents the function part of a tool call

func (ToolCallFunction) MarshalJSON

func (tcf ToolCallFunction) MarshalJSON() ([]byte, error)

Custom JSON marshaling for ToolCallFunction

func (*ToolCallFunction) UnmarshalJSON

func (tcf *ToolCallFunction) UnmarshalJSON(data []byte) error

Custom JSON unmarshaling for ToolCallFunction

type ToolFilter

type ToolFilter func(toolName string) bool

type ToolFunction

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

ToolFunction represents a function definition

type ToolHandler

type ToolHandler interface {
	OnToolCall(toolCall ToolCall) error
	OnToolResult(toolCallID, toolName, result string) error
}

ToolHandler receives tool-related events during streaming

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