Documentation
¶
Overview ¶
Package cerebras provides a Go client library for the Cerebras Inference API.
The Cerebras Inference API offers high-performance language model inference with industry-leading speed. This SDK provides a simple and idiomatic Go interface to all Cerebras API endpoints.
Basic Usage ¶
Create a client and make a chat completion request:
client := cerebras.NewClient("your-api-key")
// or
client, err := cerebras.NewClientFromEnv()
resp, err := client.Chat.Create(context.Background(), &cerebras.ChatCompletionRequest{
Model: cerebras.ModelLlama31_8B,
Messages: []cerebras.Message{
cerebras.NewUserMessage("Hello, how are you?"),
},
})
Streaming ¶
The SDK supports streaming responses for real-time output:
stream, err := client.Chat.CreateStream(context.Background(), &cerebras.ChatCompletionRequest{
Model: cerebras.ModelLlama31_8B,
Messages: []cerebras.Message{
cerebras.NewUserMessage("Tell me a story"),
},
})
defer stream.Close()
for {
chunk, err := stream.Next()
if err == io.EOF {
break
}
// Process chunk
}
Error Handling ¶
The SDK provides typed errors for different failure scenarios:
resp, err := client.Chat.Create(ctx, request)
if err != nil {
switch e := err.(type) {
case *cerebras.RateLimitError:
// Handle rate limit
case *cerebras.AuthenticationError:
// Handle auth error
default:
// Handle other errors
}
}
Tool Use ¶
Define and use tools for function calling:
tools := []cerebras.Tool{
cerebras.CreateWeatherTool(),
}
request := &cerebras.ChatCompletionRequest{
Model: cerebras.ModelLlama31_8B,
Messages: messages,
Tools: tools,
ToolChoice: cerebras.ToolChoiceAuto,
}
Structured Outputs ¶
Use JSON schemas for structured responses:
schema := cerebras.CreatePersonSchema()
request.WithJSONSchema("person", schema)
Index ¶
- Constants
- Variables
- func Bool(v bool) *bool
- func CreatePersonSchema() map[string]interface{}
- func CreateResponseSchema() map[string]interface{}
- func Float32(v float32) *float32
- func GenerateSchema(v interface{}) (map[string]interface{}, error)
- func Int(v int) *int
- func ParseToolCall(toolCall ToolCall, v interface{}) error
- func String(v string) *string
- func ValidateJSONSchema(schema map[string]interface{}) error
- type APIConnectionError
- type APIError
- type APIStatusError
- type APITimeoutError
- type AuthenticationError
- type BadRequestError
- type ChatCompletionChunk
- type ChatCompletionRequest
- func (r *ChatCompletionRequest) WithJSONMode() *ChatCompletionRequest
- func (r *ChatCompletionRequest) WithJSONSchema(name string, schema map[string]interface{}) *ChatCompletionRequest
- func (r *ChatCompletionRequest) WithResponseFormat(format *ResponseFormat) *ChatCompletionRequest
- func (r *ChatCompletionRequest) WithTools(tools []Tool) *ChatCompletionRequest
- type ChatCompletionResponse
- type ChatCompletionStream
- type ChatService
- func (s *ChatService) Create(ctx context.Context, request *ChatCompletionRequest) (*ChatCompletionResponse, error)
- func (s *ChatService) CreateStream(ctx context.Context, request *ChatCompletionRequest) (*ChatCompletionStream, error)
- func (s *ChatService) CreateWithOptions(ctx context.Context, request *ChatCompletionRequest) (interface{}, error)
- type Choice
- type Client
- type CompletionChoice
- type CompletionChunk
- type CompletionRequest
- func (r *CompletionRequest) WithEcho(echo bool) *CompletionRequest
- func (r *CompletionRequest) WithGrammarRoot(grammar string) *CompletionRequest
- func (r *CompletionRequest) WithMaxTokens(tokens int) *CompletionRequest
- func (r *CompletionRequest) WithMinTokens(tokens int) *CompletionRequest
- func (r *CompletionRequest) WithReturnRawTokens(returnTokens bool) *CompletionRequest
- func (r *CompletionRequest) WithSeed(seed int) *CompletionRequest
- func (r *CompletionRequest) WithStop(sequences ...string) *CompletionRequest
- func (r *CompletionRequest) WithTemperature(temp float32) *CompletionRequest
- func (r *CompletionRequest) WithTopP(topP float32) *CompletionRequest
- type CompletionResponse
- type CompletionStream
- type CompletionsService
- func (s *CompletionsService) Create(ctx context.Context, request *CompletionRequest) (*CompletionResponse, error)
- func (s *CompletionsService) CreateStream(ctx context.Context, request *CompletionRequest) (*CompletionStream, error)
- func (s *CompletionsService) CreateWithOptions(ctx context.Context, request *CompletionRequest) (interface{}, error)
- type Config
- type Delta
- type Function
- type FunctionCall
- type InternalServerError
- type JSONSchema
- type LogProbContent
- type LogProbs
- type Message
- func CreateToolResponse(toolCallID string, response interface{}) (Message, error)
- func NewAssistantMessage(content string) Message
- func NewChatMessage(role, content string) Message
- func NewSystemMessage(content string) Message
- func NewToolMessage(content, toolCallID string) Message
- func NewUserMessage(content string) Message
- type Model
- type ModelsResponse
- type ModelsService
- type NotFoundError
- type PermissionDeniedError
- type PropertyBuilder
- func AnyOf(description string, types ...PropertyBuilder) PropertyBuilder
- func Array(description string, items PropertyBuilder) PropertyBuilder
- func Boolean(description string) PropertyBuilder
- func Enum(description string, values ...string) PropertyBuilder
- func Integer(description string) PropertyBuilder
- func Number(description string) PropertyBuilder
- func Object(description string) PropertyBuilder
- func StringProp(description string) PropertyBuilder
- func (pb PropertyBuilder) Build() map[string]interface{}
- func (pb PropertyBuilder) WithMaxLength(max int) PropertyBuilder
- func (pb PropertyBuilder) WithMaximum(max float64) PropertyBuilder
- func (pb PropertyBuilder) WithMinLength(min int) PropertyBuilder
- func (pb PropertyBuilder) WithMinimum(min float64) PropertyBuilder
- func (pb PropertyBuilder) WithPattern(pattern string) PropertyBuilder
- func (pb PropertyBuilder) WithProperty(name string, prop PropertyBuilder, required bool) PropertyBuilder
- type RateLimitError
- type ResponseFormat
- type SchemaBuilder
- func (sb *SchemaBuilder) Build() map[string]interface{}
- func (sb *SchemaBuilder) WithDefinition(name string, def map[string]interface{}) *SchemaBuilder
- func (sb *SchemaBuilder) WithProperty(name string, prop PropertyBuilder, required bool) *SchemaBuilder
- func (sb *SchemaBuilder) WithRef(name, ref string, required bool) *SchemaBuilder
- type TimeInfo
- type Tool
- type ToolBuilder
- func (tb *ToolBuilder) Build() Tool
- func (tb *ToolBuilder) Strict() *ToolBuilder
- func (tb *ToolBuilder) WithArrayParameter(name, description, itemType string, required bool) *ToolBuilder
- func (tb *ToolBuilder) WithEnumParameter(name, description string, values []string, required bool) *ToolBuilder
- func (tb *ToolBuilder) WithObjectParameter(name, description string, properties map[string]interface{}, required bool) *ToolBuilder
- func (tb *ToolBuilder) WithParameter(name, paramType, description string, required bool) *ToolBuilder
- type ToolCall
- type ToolChoice
- type TopLogProb
- type UnprocessableEntityError
- type Usage
Constants ¶
const ( ModelLlama4Scout17B = "llama-4-scout-17b-16e-instruct" ModelLlama31_8B = "llama3.1-8b" ModelLlama33_70B = "llama-3.3-70b" ModelQwen3_32B = "qwen-3-32b" ModelDeepSeekR1 = "deepseek-r1-distill-llama-70b" )
Helper constants for available models
Variables ¶
var ( ToolChoiceNone = ToolChoice{Type: "none"} ToolChoiceAuto = ToolChoice{Type: "auto"} ToolChoiceRequired = ToolChoice{Type: "required"} )
Tool choice constants
Functions ¶
func CreatePersonSchema ¶
func CreatePersonSchema() map[string]interface{}
CreatePersonSchema creates a schema for a person object
func CreateResponseSchema ¶
func CreateResponseSchema() map[string]interface{}
CreateResponseSchema creates a schema for API responses
func GenerateSchema ¶
GenerateSchema attempts to generate a JSON schema from a Go type
func ParseToolCall ¶
ParseToolCall parses the arguments of a tool call into the provided interface
func ValidateJSONSchema ¶
ValidateJSONSchema validates that a JSON schema is well-formed
Types ¶
type APIConnectionError ¶
type APIConnectionError struct{ APIError }
Error types matching SDK error hierarchy
type APIStatusError ¶
type APIStatusError struct{ APIError }
Error types matching SDK error hierarchy
type APITimeoutError ¶
type APITimeoutError struct{ APIConnectionError }
Error types matching SDK error hierarchy
type AuthenticationError ¶
type AuthenticationError struct{ APIStatusError }
Error types matching SDK error hierarchy
type BadRequestError ¶
type BadRequestError struct{ APIStatusError }
Error types matching SDK error hierarchy
type ChatCompletionChunk ¶
type ChatCompletionChunk 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"`
}
Streaming types
type ChatCompletionRequest ¶
type ChatCompletionRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
MaxCompletionTokens *int `json:"max_completion_tokens,omitempty"`
Temperature *float32 `json:"temperature,omitempty"`
TopP *float32 `json:"top_p,omitempty"`
Stream bool `json:"stream,omitempty"`
Seed *int `json:"seed,omitempty"`
Stop []string `json:"stop,omitempty"`
User string `json:"user,omitempty"`
LogProbs *bool `json:"logprobs,omitempty"`
TopLogProbs *int `json:"top_logprobs,omitempty"`
ResponseFormat *ResponseFormat `json:"response_format,omitempty"`
Tools []Tool `json:"tools,omitempty"`
ToolChoice interface{} `json:"tool_choice,omitempty"`
ParallelToolCalls *bool `json:"parallel_tool_calls,omitempty"`
}
Chat types
func (*ChatCompletionRequest) WithJSONMode ¶
func (r *ChatCompletionRequest) WithJSONMode() *ChatCompletionRequest
WithJSONMode enables JSON mode
func (*ChatCompletionRequest) WithJSONSchema ¶
func (r *ChatCompletionRequest) WithJSONSchema(name string, schema map[string]interface{}) *ChatCompletionRequest
WithJSONSchema enables structured output with a JSON schema
func (*ChatCompletionRequest) WithResponseFormat ¶
func (r *ChatCompletionRequest) WithResponseFormat(format *ResponseFormat) *ChatCompletionRequest
WithResponseFormat sets the response format for structured outputs
func (*ChatCompletionRequest) WithTools ¶
func (r *ChatCompletionRequest) WithTools(tools []Tool) *ChatCompletionRequest
WithTools adds tool definitions to a chat 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"`
Choices []Choice `json:"choices"`
Usage Usage `json:"usage"`
TimeInfo TimeInfo `json:"time_info"`
}
func CollectChatStream ¶
func CollectChatStream(stream *ChatCompletionStream) (*ChatCompletionResponse, error)
Helper function to collect all chunks from a chat stream
type ChatCompletionStream ¶
type ChatCompletionStream struct {
// contains filtered or unexported fields
}
ChatCompletionStream handles streaming chat completions
func (*ChatCompletionStream) Close ¶
func (s *ChatCompletionStream) Close() error
func (*ChatCompletionStream) Next ¶
func (s *ChatCompletionStream) Next() (*ChatCompletionChunk, error)
type ChatService ¶
type ChatService struct {
// contains filtered or unexported fields
}
ChatService handles chat completion operations
func (*ChatService) Create ¶
func (s *ChatService) Create(ctx context.Context, request *ChatCompletionRequest) (*ChatCompletionResponse, error)
Create sends a chat completion request
func (*ChatService) CreateStream ¶
func (s *ChatService) CreateStream(ctx context.Context, request *ChatCompletionRequest) (*ChatCompletionStream, error)
CreateStream sends a streaming chat completion request
func (*ChatService) CreateWithOptions ¶
func (s *ChatService) CreateWithOptions(ctx context.Context, request *ChatCompletionRequest) (interface{}, error)
CreateWithOptions is a convenience method that handles both streaming and non-streaming
type Client ¶
type Client struct {
Chat *ChatService
Completions *CompletionsService
Models *ModelsService
// contains filtered or unexported fields
}
func NewClientFromEnv ¶
func NewClientWithConfig ¶
type CompletionChoice ¶
type CompletionChunk ¶
type CompletionRequest ¶
type CompletionRequest struct {
Model string `json:"model"`
Prompt string `json:"prompt"`
MaxTokens *int `json:"max_tokens,omitempty"`
MinTokens *int `json:"min_tokens,omitempty"`
Temperature *float32 `json:"temperature,omitempty"`
TopP *float32 `json:"top_p,omitempty"`
Stream bool `json:"stream,omitempty"`
Seed *int `json:"seed,omitempty"`
Stop []string `json:"stop,omitempty"`
Echo bool `json:"echo,omitempty"`
ReturnRawTokens bool `json:"return_raw_tokens,omitempty"`
GrammarRoot *string `json:"grammar_root,omitempty"`
User string `json:"user,omitempty"`
}
Completion types
func NewCompletionRequest ¶
func NewCompletionRequest(model, prompt string) *CompletionRequest
NewCompletionRequest creates a new completion request with defaults
func (*CompletionRequest) WithEcho ¶
func (r *CompletionRequest) WithEcho(echo bool) *CompletionRequest
WithEcho enables echoing the prompt in the response
func (*CompletionRequest) WithGrammarRoot ¶
func (r *CompletionRequest) WithGrammarRoot(grammar string) *CompletionRequest
WithGrammarRoot sets the grammar root for structured generation
func (*CompletionRequest) WithMaxTokens ¶
func (r *CompletionRequest) WithMaxTokens(tokens int) *CompletionRequest
WithMaxTokens sets the maximum tokens to generate
func (*CompletionRequest) WithMinTokens ¶
func (r *CompletionRequest) WithMinTokens(tokens int) *CompletionRequest
WithMinTokens sets the minimum tokens to generate
func (*CompletionRequest) WithReturnRawTokens ¶
func (r *CompletionRequest) WithReturnRawTokens(returnTokens bool) *CompletionRequest
WithReturnRawTokens enables returning raw tokens instead of text
func (*CompletionRequest) WithSeed ¶
func (r *CompletionRequest) WithSeed(seed int) *CompletionRequest
WithSeed sets the random seed for deterministic generation
func (*CompletionRequest) WithStop ¶
func (r *CompletionRequest) WithStop(sequences ...string) *CompletionRequest
WithStop adds stop sequences
func (*CompletionRequest) WithTemperature ¶
func (r *CompletionRequest) WithTemperature(temp float32) *CompletionRequest
WithTemperature sets the sampling temperature
func (*CompletionRequest) WithTopP ¶
func (r *CompletionRequest) WithTopP(topP float32) *CompletionRequest
WithTopP sets the nucleus sampling parameter
type CompletionResponse ¶
type CompletionResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
SystemFingerprint string `json:"system_fingerprint"`
Choices []CompletionChoice `json:"choices"`
Usage Usage `json:"usage"`
TimeInfo TimeInfo `json:"time_info"`
}
func CollectCompletionStream ¶
func CollectCompletionStream(stream *CompletionStream) (*CompletionResponse, error)
Helper function to collect all chunks from a completion stream
type CompletionStream ¶
type CompletionStream struct {
// contains filtered or unexported fields
}
CompletionStream handles streaming completions
func (*CompletionStream) Close ¶
func (s *CompletionStream) Close() error
func (*CompletionStream) Next ¶
func (s *CompletionStream) Next() (*CompletionChunk, error)
type CompletionsService ¶
type CompletionsService struct {
// contains filtered or unexported fields
}
CompletionsService handles text completion operations
func (*CompletionsService) Create ¶
func (s *CompletionsService) Create(ctx context.Context, request *CompletionRequest) (*CompletionResponse, error)
Create sends a completion request
func (*CompletionsService) CreateStream ¶
func (s *CompletionsService) CreateStream(ctx context.Context, request *CompletionRequest) (*CompletionStream, error)
CreateStream sends a streaming completion request
func (*CompletionsService) CreateWithOptions ¶
func (s *CompletionsService) CreateWithOptions(ctx context.Context, request *CompletionRequest) (interface{}, error)
CreateWithOptions is a convenience method that handles both streaming and non-streaming
type FunctionCall ¶
type InternalServerError ¶
type InternalServerError struct{ APIStatusError }
Error types matching SDK error hierarchy
type JSONSchema ¶
type LogProbContent ¶
type LogProbContent struct {
Token string `json:"token"`
LogProb float64 `json:"logprob"`
Bytes []int `json:"bytes"`
TopLogProbs []TopLogProb `json:"top_logprobs"`
}
type LogProbs ¶
type LogProbs struct {
Content []LogProbContent `json:"content"`
}
type Message ¶
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}
func CreateToolResponse ¶
CreateToolResponse creates a tool response message
func NewAssistantMessage ¶
NewAssistantMessage creates an assistant message
func NewChatMessage ¶
NewChatMessage creates a new chat message
func NewSystemMessage ¶
NewSystemMessage creates a system message
func NewToolMessage ¶
NewToolMessage creates a tool response message
func NewUserMessage ¶
NewUserMessage creates a user message
type Model ¶
type Model struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
OwnedBy string `json:"owned_by"`
}
Model types
type ModelsResponse ¶
type ModelsService ¶
type ModelsService struct {
// contains filtered or unexported fields
}
ModelsService handles operations related to models
func (*ModelsService) List ¶
func (s *ModelsService) List(ctx context.Context) (*ModelsResponse, error)
List retrieves all available models
type NotFoundError ¶
type NotFoundError struct{ APIStatusError }
Error types matching SDK error hierarchy
type PermissionDeniedError ¶
type PermissionDeniedError struct{ APIStatusError }
Error types matching SDK error hierarchy
type PropertyBuilder ¶
type PropertyBuilder struct {
// contains filtered or unexported fields
}
PropertyBuilder helps construct individual properties
func AnyOf ¶
func AnyOf(description string, types ...PropertyBuilder) PropertyBuilder
AnyOf creates a union type property
func Array ¶
func Array(description string, items PropertyBuilder) PropertyBuilder
Array creates an array property
func Enum ¶
func Enum(description string, values ...string) PropertyBuilder
Enum creates an enum property
func StringProp ¶
func StringProp(description string) PropertyBuilder
StringProp creates a string property
func (PropertyBuilder) Build ¶
func (pb PropertyBuilder) Build() map[string]interface{}
Build returns the constructed property
func (PropertyBuilder) WithMaxLength ¶
func (pb PropertyBuilder) WithMaxLength(max int) PropertyBuilder
WithMaxLength adds a maximum length constraint
func (PropertyBuilder) WithMaximum ¶
func (pb PropertyBuilder) WithMaximum(max float64) PropertyBuilder
WithMaximum adds a maximum value constraint
func (PropertyBuilder) WithMinLength ¶
func (pb PropertyBuilder) WithMinLength(min int) PropertyBuilder
WithMinLength adds a minimum length constraint
func (PropertyBuilder) WithMinimum ¶
func (pb PropertyBuilder) WithMinimum(min float64) PropertyBuilder
WithMinimum adds a minimum value constraint
func (PropertyBuilder) WithPattern ¶
func (pb PropertyBuilder) WithPattern(pattern string) PropertyBuilder
WithPattern adds a regex pattern constraint
func (PropertyBuilder) WithProperty ¶
func (pb PropertyBuilder) WithProperty(name string, prop PropertyBuilder, required bool) PropertyBuilder
WithProperty adds a property to an object type
type RateLimitError ¶
type RateLimitError struct{ APIStatusError }
Error types matching SDK error hierarchy
type ResponseFormat ¶
type ResponseFormat struct {
Type string `json:"type"`
JSONSchema *JSONSchema `json:"json_schema,omitempty"`
}
type SchemaBuilder ¶
type SchemaBuilder struct {
// contains filtered or unexported fields
}
SchemaBuilder helps construct JSON schemas for structured outputs
func (*SchemaBuilder) Build ¶
func (sb *SchemaBuilder) Build() map[string]interface{}
Build returns the constructed schema
func (*SchemaBuilder) WithDefinition ¶
func (sb *SchemaBuilder) WithDefinition(name string, def map[string]interface{}) *SchemaBuilder
WithDefinition adds a reusable definition
func (*SchemaBuilder) WithProperty ¶
func (sb *SchemaBuilder) WithProperty(name string, prop PropertyBuilder, required bool) *SchemaBuilder
WithProperty adds a property to the schema
func (*SchemaBuilder) WithRef ¶
func (sb *SchemaBuilder) WithRef(name, ref string, required bool) *SchemaBuilder
WithRef adds a reference property
type Tool ¶
func CreateCalculatorTool ¶
func CreateCalculatorTool() Tool
CreateCalculatorTool creates a calculator tool
func CreateWeatherTool ¶
func CreateWeatherTool() Tool
CreateWeatherTool creates a weather lookup tool
type ToolBuilder ¶
type ToolBuilder struct {
// contains filtered or unexported fields
}
ToolBuilder helps construct tool definitions
func NewTool ¶
func NewTool(name, description string) *ToolBuilder
NewTool creates a new tool builder
func (*ToolBuilder) Strict ¶
func (tb *ToolBuilder) Strict() *ToolBuilder
Strict enables strict mode for the tool
func (*ToolBuilder) WithArrayParameter ¶
func (tb *ToolBuilder) WithArrayParameter(name, description, itemType string, required bool) *ToolBuilder
WithArrayParameter adds an array parameter
func (*ToolBuilder) WithEnumParameter ¶
func (tb *ToolBuilder) WithEnumParameter(name, description string, values []string, required bool) *ToolBuilder
WithEnumParameter adds an enum parameter to the tool
func (*ToolBuilder) WithObjectParameter ¶
func (tb *ToolBuilder) WithObjectParameter(name, description string, properties map[string]interface{}, required bool) *ToolBuilder
WithObjectParameter adds a nested object parameter
func (*ToolBuilder) WithParameter ¶
func (tb *ToolBuilder) WithParameter(name, paramType, description string, required bool) *ToolBuilder
WithParameter adds a parameter to the tool
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function FunctionCall `json:"function"`
}
type ToolChoice ¶
ToolChoice represents the tool choice parameter
func SpecificTool ¶
func SpecificTool(name string) ToolChoice
SpecificTool creates a tool choice for a specific function
func (ToolChoice) MarshalJSON ¶
func (tc ToolChoice) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for ToolChoice
type TopLogProb ¶
type UnprocessableEntityError ¶
type UnprocessableEntityError struct{ APIStatusError }
Error types matching SDK error hierarchy