Documentation
ΒΆ
Index ΒΆ
- Constants
- func Bool(v bool) *bool
- func Float64(v float64) *float64
- func Int(v int) *int
- func String(v string) *string
- type APIError
- type AudioResponse
- type AudioTranscriptionRequest
- type ChatCompletionRequest
- type ChatCompletionResponse
- type ChatCompletionStream
- type ChatMessage
- type Choice
- type Client
- func (c *Client) CreateChatCompletion(ctx context.Context, req ChatCompletionRequest) (*ChatCompletionResponse, error)
- func (c *Client) CreateChatCompletionStream(ctx context.Context, req ChatCompletionRequest) (*ChatCompletionStream, error)
- func (c *Client) CreateSpeech(ctx context.Context, req CreateSpeechRequest) ([]byte, error)
- func (c *Client) CreateTranscription(ctx context.Context, req AudioTranscriptionRequest) (*AudioResponse, error)
- func (c *Client) CreateTranslation(ctx context.Context, req AudioTranscriptionRequest) (*AudioResponse, error)
- func (c *Client) ListModels(ctx context.Context) (*ModelListResponse, error)
- type CreateSpeechRequest
- type ErrorResponse
- type Model
- type ModelListResponse
- type Option
- type ResponseFormat
- type StreamChoice
- type StreamOptions
- type StreamResponseChunk
- type Tool
- type ToolCall
- type ToolCallFunction
- type ToolFunction
- type Usage
- type XGroq
Constants ΒΆ
const ( RoleSystem = "system" RoleUser = "user" RoleAssistant = "assistant" RoleTool = "tool" )
Constants for Roles
Variables ΒΆ
This section is empty.
Functions ΒΆ
Types ΒΆ
type AudioResponse ΒΆ
type AudioResponse struct {
Text string `json:"text"`
}
type ChatCompletionRequest ΒΆ
type ChatCompletionRequest struct {
Model string `json:"model"`
Messages []ChatMessage `json:"messages"`
Temperature *float64 `json:"temperature,omitempty"`
MaxCompletionTokens int `json:"max_completion_tokens,omitempty"` // Replaces MaxTokens
MaxTokens int `json:"max_tokens,omitempty"` // Deprecated but supported
TopP *float64 `json:"top_p,omitempty"`
Stream bool `json:"stream,omitempty"`
StreamOptions *StreamOptions `json:"stream_options,omitempty"` // Added for usage stats
Stop interface{} `json:"stop,omitempty"` // string or []string
Seed *int `json:"seed,omitempty"`
Format *ResponseFormat `json:"response_format,omitempty"`
Tools []Tool `json:"tools,omitempty"`
ToolChoice interface{} `json:"tool_choice,omitempty"` // "auto", "none", "required", or specific object
User string `json:"user,omitempty"`
ServiceTier string `json:"service_tier,omitempty"` // "auto", "on_demand", "flex"
ParallelToolCalls *bool `json:"parallel_tool_calls,omitempty"`
}
ChatCompletionRequest is the payload for /chat/completions.
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"`
Choices []Choice `json:"choices"`
Usage Usage `json:"usage"`
}
ChatCompletionResponse is the standard response object.
type ChatCompletionStream ΒΆ
type ChatCompletionStream struct {
// contains filtered or unexported fields
}
ChatCompletionStream manages the stream connection.
func (*ChatCompletionStream) Close ΒΆ
func (s *ChatCompletionStream) Close() error
func (*ChatCompletionStream) Recv ΒΆ
func (s *ChatCompletionStream) Recv() (*StreamResponseChunk, error)
Recv receives the next chunk from the stream.
type ChatMessage ΒΆ
type ChatMessage struct {
Role string `json:"role"`
Content string `json:"content"` // Text content
Name string `json:"name,omitempty"` // Author name (optional)
ToolCalls []ToolCall `json:"tool_calls,omitempty"` // For assistant to request tools
ToolCallID string `json:"tool_call_id,omitempty"` // For tool role to reference the call
}
ChatMessage represents a message in the conversation.
type Choice ΒΆ
type Choice struct {
Index int `json:"index"`
Message ChatMessage `json:"message"`
FinishReason string `json:"finish_reason"` // "stop", "length", "tool_calls"
}
type Client ΒΆ
type Client struct {
// contains filtered or unexported fields
}
Client is the main entry point for the Groq API.
func (*Client) CreateChatCompletion ΒΆ
func (c *Client) CreateChatCompletion(ctx context.Context, req ChatCompletionRequest) (*ChatCompletionResponse, error)
CreateChatCompletion makes a blocking (non-streaming) chat request.
func (*Client) CreateChatCompletionStream ΒΆ
func (c *Client) CreateChatCompletionStream(ctx context.Context, req ChatCompletionRequest) (*ChatCompletionStream, error)
CreateChatCompletionStream initiates a streaming request.
func (*Client) CreateSpeech ΒΆ
CreateSpeech generates audio from text. Returns the raw audio bytes (e.g. MP3/WAV data).
func (*Client) CreateTranscription ΒΆ
func (c *Client) CreateTranscription(ctx context.Context, req AudioTranscriptionRequest) (*AudioResponse, error)
CreateTranscription transcribes audio to the input language.
func (*Client) CreateTranslation ΒΆ
func (c *Client) CreateTranslation(ctx context.Context, req AudioTranscriptionRequest) (*AudioResponse, error)
CreateTranslation translates audio into English.
func (*Client) ListModels ΒΆ
func (c *Client) ListModels(ctx context.Context) (*ModelListResponse, error)
type CreateSpeechRequest ΒΆ
type CreateSpeechRequest struct {
Model string `json:"model"` // e.g., "playai-tts" or "whisper-v3" depending on availability
Input string `json:"input"`
Voice string `json:"voice"`
ResponseFormat string `json:"response_format,omitempty"` // mp3, wav, flac, etc.
Speed float64 `json:"speed,omitempty"`
}
type ErrorResponse ΒΆ
type ErrorResponse struct {
// We rename the field to 'GroqError' to avoid conflict with the Error() method.
// The json tag remains "error" to match the API response.
GroqError APIError `json:"error"`
}
ErrorResponse represents an error returned by the Groq API.
func (*ErrorResponse) Error ΒΆ
func (e *ErrorResponse) Error() string
Error satisfies the Go error interface.
type ModelListResponse ΒΆ
type Option ΒΆ
type Option func(*Client)
Option allows configuring the client.
func WithHTTPClient ΒΆ
WithHTTPClient allows passing a custom *http.Client.
type ResponseFormat ΒΆ
type ResponseFormat struct {
Type string `json:"type"` // "json_object", "json_schema", or "text"
JSONSchema interface{} `json:"json_schema,omitempty"` // Structure for Strict Structured Outputs
}
ResponseFormat allows forcing JSON mode or JSON Schema.
type StreamChoice ΒΆ
type StreamChoice struct {
Index int `json:"index"`
Delta ChatMessage `json:"delta"` // Contains partial content or tool calls
FinishReason *string `json:"finish_reason"`
}
type StreamOptions ΒΆ
type StreamOptions struct {
IncludeUsage bool `json:"include_usage,omitempty"`
}
StreamOptions configuration for streaming responses.
type StreamResponseChunk ΒΆ
type StreamResponseChunk struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
SystemFingerprint string `json:"system_fingerprint"`
Choices []StreamChoice `json:"choices"`
Usage *Usage `json:"usage,omitempty"` // Groq sends usage in the last chunk
XGroq *XGroq `json:"x_groq,omitempty"` // Internal metadata
}
StreamResponseChunk represents a single chunk of data from the stream.
type Tool ΒΆ
type Tool struct {
Type string `json:"type"` // Currently only "function"
Function ToolFunction `json:"function"`
}
Tool represents a tool that the model can call.
type ToolCall ΒΆ
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function ToolCallFunction `json:"function"`
}
ToolCall represents a request from the model to call a tool.
type ToolCallFunction ΒΆ
type ToolFunction ΒΆ
type Usage ΒΆ
type Usage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
QueueTime float64 `json:"queue_time,omitempty"`
PromptTime float64 `json:"prompt_time,omitempty"`
CompletionTime float64 `json:"completion_time,omitempty"`
TotalTime float64 `json:"total_time,omitempty"`
}
Usage contains token usage statistics.