Documentation
¶
Index ¶
- Constants
- func WithToolHandler(ctx context.Context, h ToolHandler) context.Context
- type ChatCompletionRequest
- type ChatCompletionResponse
- type ChatStream
- type Choice
- type Client
- type CompletionAccumulator
- type Config
- type ContentPart
- type Delta
- type DeltaFunction
- type DeltaToolCall
- type MCPServer
- type Message
- type Model
- type ModelsResponse
- type Service
- type Tool
- type ToolCall
- type ToolCallFunction
- type ToolFilter
- type ToolFunction
- type ToolHandler
- type Usage
Constants ¶
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 (*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 ContentPart ¶
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 ¶
GetContentAsString returns the content as a string, handling both string and array formats
func (*Message) SetContentAsString ¶
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 ¶
ModelsResponse represents the response from the models endpoint
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
func NewService ¶
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 ToolFunction ¶
type ToolFunction struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]any `json:"parameters"`
}
ToolFunction represents a function definition