Documentation
¶
Index ¶
- func ExtractAllTextContent(response *mcp.ToolResponse) string
- func ExtractToolResult(response *mcp.ToolResponse) (string, error)
- func GenerateToolCallID(index int) string
- func WithToolHandler(ctx context.Context, h ToolHandler) context.Context
- type ChatCompletionRequest
- type ChatCompletionResponse
- type ChatStream
- type Choice
- type CompletionAccumulator
- func (acc *CompletionAccumulator) AddChunk(chunk ChatCompletionResponse)
- func (acc *CompletionAccumulator) Content() string
- func (acc *CompletionAccumulator) FinishReason() string
- func (acc *CompletionAccumulator) FinishedContent() (string, bool)
- func (acc *CompletionAccumulator) FinishedRefusal() (string, bool)
- func (acc *CompletionAccumulator) FinishedToolCall() (*ToolCall, bool)
- func (acc *CompletionAccumulator) FinishedToolCalls() ([]ToolCall, bool)
- func (acc *CompletionAccumulator) IsComplete() bool
- func (acc *CompletionAccumulator) Reset()
- type CompletionTokensDetails
- type ContentPart
- type Delta
- type DeltaFunction
- type DeltaToolCall
- type ImageURL
- type Message
- type Model
- type ModelsResponse
- type NoOpToolHandler
- type PromptTokensDetails
- type Tool
- type ToolCall
- type ToolCallFunction
- type ToolFilter
- type ToolFunction
- type ToolHandler
- type Usage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractAllTextContent ¶
func ExtractAllTextContent(response *mcp.ToolResponse) string
ExtractAllTextContent extracts all text content from an MCP ToolResponse, concatenating multiple text parts with newlines.
func ExtractToolResult ¶
func ExtractToolResult(response *mcp.ToolResponse) (string, error)
ExtractToolResult extracts a string result from an MCP ToolResponse for use in OpenAI tool result messages.
Priority order:
- StructuredContent - serialized to JSON
- First text content in Content array
- Default success message
func GenerateToolCallID ¶
GenerateToolCallID creates a unique ID for tool calls. This is useful when LLMs don't provide an ID in streaming responses. The format matches OpenAI's tool call ID format: "call_" followed by random characters.
func WithToolHandler ¶
func WithToolHandler(ctx context.Context, h ToolHandler) context.Context
WithToolHandler attaches a ToolHandler to the context. The handler will receive events during tool processing.
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 an OpenAI chat completion request
type ChatCompletionResponse ¶
type ChatCompletionResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
SystemFingerprint string `json:"system_fingerprint,omitempty"`
Choices []Choice `json:"choices"`
Usage *Usage `json:"usage,omitempty"`
}
ChatCompletionResponse represents an OpenAI chat completion response
type ChatStream ¶
type ChatStream struct {
// contains filtered or unexported fields
}
ChatStream provides an iterator interface for streaming chat completion responses. It is designed to be used in a for loop pattern:
stream := openai.NewChatStream(ctx, responseChan, errorChan)
for stream.Next() {
chunk := stream.Current()
// process chunk
}
if err := stream.Err(); err != nil {
// handle error
}
func NewChatStream ¶
func NewChatStream(ctx context.Context, responseChan <-chan ChatCompletionResponse, errorChan <-chan error) *ChatStream
NewChatStream creates a new ChatStream from response and error channels.
func (*ChatStream) Current ¶
func (s *ChatStream) Current() ChatCompletionResponse
Current returns the current response chunk. Must be called after Next returns true.
func (*ChatStream) Done ¶
func (s *ChatStream) Done() bool
Done returns true if the stream has completed.
func (*ChatStream) Err ¶
func (s *ChatStream) Err() error
Err returns any error that occurred during streaming. Should be checked after Next returns false.
func (*ChatStream) Next ¶
func (s *ChatStream) Next() bool
Next advances to the next response chunk. Returns true if a chunk is available, false if the stream is done or an error occurred.
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 completion choice
type CompletionAccumulator ¶
type CompletionAccumulator struct {
Choices []accumulatorChoice
}
CompletionAccumulator accumulates streaming chat completion chunks into complete responses. It handles the incremental building of content, tool calls, and refusals.
func (*CompletionAccumulator) AddChunk ¶
func (acc *CompletionAccumulator) AddChunk(chunk ChatCompletionResponse)
AddChunk processes a streaming chunk and accumulates its content.
func (*CompletionAccumulator) Content ¶
func (acc *CompletionAccumulator) Content() string
Content returns the current accumulated content for the first choice.
func (*CompletionAccumulator) FinishReason ¶
func (acc *CompletionAccumulator) FinishReason() string
FinishReason returns the finish reason for the first choice.
func (*CompletionAccumulator) FinishedContent ¶
func (acc *CompletionAccumulator) FinishedContent() (string, bool)
FinishedContent returns the accumulated content for the first choice if complete. Returns the content and true if finish_reason is "stop", otherwise empty string and false.
func (*CompletionAccumulator) FinishedRefusal ¶
func (acc *CompletionAccumulator) FinishedRefusal() (string, bool)
FinishedRefusal returns the accumulated refusal for the first choice if present. Returns the refusal and true if there is refusal content, otherwise empty string and false.
func (*CompletionAccumulator) FinishedToolCall ¶
func (acc *CompletionAccumulator) FinishedToolCall() (*ToolCall, bool)
FinishedToolCall returns the first accumulated tool call for the first choice if complete. Returns the tool call and true if finish_reason is "tool_calls", otherwise nil and false.
func (*CompletionAccumulator) FinishedToolCalls ¶
func (acc *CompletionAccumulator) FinishedToolCalls() ([]ToolCall, bool)
FinishedToolCalls returns all accumulated tool calls for the first choice if complete. Returns the tool calls and true if finish_reason is "tool_calls", otherwise nil and false.
func (*CompletionAccumulator) IsComplete ¶
func (acc *CompletionAccumulator) IsComplete() bool
IsComplete returns true if the first choice has a finish reason.
func (*CompletionAccumulator) Reset ¶
func (acc *CompletionAccumulator) Reset()
Reset clears the accumulator for reuse.
type CompletionTokensDetails ¶
type CompletionTokensDetails struct {
ReasoningTokens int `json:"reasoning_tokens"`
AudioTokens int `json:"audio_tokens"`
AcceptedPredictionTokens int `json:"accepted_prediction_tokens"`
RejectedPredictionTokens int `json:"rejected_prediction_tokens"`
}
CompletionTokensDetails represents detailed completion token usage
type ContentPart ¶
type ContentPart struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
ImageURL *ImageURL `json:"image_url,omitempty"`
}
ContentPart represents a multi-modal content part
type Delta ¶
type Delta struct {
Role string `json:"role,omitempty"`
Content string `json:"content,omitempty"`
Refusal string `json:"refusal,omitempty"`
ToolCalls []DeltaToolCall `json:"tool_calls,omitempty"`
}
Delta represents a streaming delta
type DeltaFunction ¶
type DeltaFunction struct {
Name string `json:"name,omitempty"`
Arguments string `json:"arguments,omitempty"`
}
DeltaFunction represents a streaming function delta
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 a streaming tool call delta
type Message ¶
type Message struct {
Role string `json:"role,omitempty"`
Content any `json:"content,omitempty"`
Refusal string `json:"refusal,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}
Message represents a chat message
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 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 an individual model
type ModelsResponse ¶
ModelsResponse represents the response from the /models endpoint
type NoOpToolHandler ¶
type NoOpToolHandler struct{}
NoOpToolHandler is a ToolHandler that does nothing. Useful as a default or for testing.
func (NoOpToolHandler) OnToolCall ¶
func (NoOpToolHandler) OnToolCall(toolCall ToolCall) error
func (NoOpToolHandler) OnToolResult ¶
func (NoOpToolHandler) OnToolResult(toolCallID, toolName, result string) error
type PromptTokensDetails ¶
type PromptTokensDetails struct {
CachedTokens int `json:"cached_tokens"`
AudioTokens int `json:"audio_tokens"`
}
PromptTokensDetails represents detailed prompt token usage
type Tool ¶
type Tool struct {
Type string `json:"type"`
Function ToolFunction `json:"function"`
}
Tool represents an OpenAI tool definition
func MCPToolsToOpenAI ¶
MCPToolsToOpenAI converts MCP tools to OpenAI function calling format
func MCPToolsToOpenAIFiltered ¶
MCPToolsToOpenAIFiltered converts MCP tools to OpenAI format with optional filtering. If filter is nil, all tools are included. Otherwise, only tools where filter(name) returns true are included.
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function ToolCallFunction `json:"function"`
}
ToolCall represents a tool call from the assistant
type ToolCallFunction ¶
type ToolCallFunction struct {
Name string `json:"name"`
Arguments map[string]any `json:"arguments"`
}
ToolCallFunction represents the function details of a tool call
func (ToolCallFunction) MarshalJSON ¶
func (tcf ToolCallFunction) MarshalJSON() ([]byte, error)
MarshalJSON implements custom JSON marshaling for ToolCallFunction. OpenAI expects arguments as a JSON string, not an object.
func (*ToolCallFunction) UnmarshalJSON ¶
func (tcf *ToolCallFunction) UnmarshalJSON(data []byte) error
UnmarshalJSON implements custom JSON unmarshaling for ToolCallFunction. OpenAI sends arguments as a JSON string, not an object.
type ToolFilter ¶
ToolFilter is a function type for filtering tools by name
func ExcludeTools ¶
func ExcludeTools(names ...string) ToolFilter
ExcludeTools returns a filter that excludes tools with the specified names
func ToolsByName ¶
func ToolsByName(names ...string) ToolFilter
ToolsByName returns a filter that includes only tools with the specified names
type ToolFunction ¶
type ToolFunction struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters map[string]any `json:"parameters,omitempty"`
}
ToolFunction represents a function definition for a tool
type ToolHandler ¶
type ToolHandler interface {
// OnToolCall is called when a tool call is about to be executed.
OnToolCall(toolCall ToolCall) error
// OnToolResult is called when a tool call has completed.
OnToolResult(toolCallID, toolName, result string) error
}
ToolHandler receives events during tool processing. Implement this interface to receive notifications when tools are called and when results are received.
func ToolHandlerFromContext ¶
func ToolHandlerFromContext(ctx context.Context) ToolHandler
ToolHandlerFromContext retrieves a ToolHandler from the context. Returns nil if no handler is attached.
type Usage ¶
type Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
PromptTokensDetails *PromptTokensDetails `json:"prompt_tokens_details,omitempty"`
CompletionTokensDetails *CompletionTokensDetails `json:"completion_tokens_details,omitempty"`
}
Usage represents token usage