xai

package
v0.10.0 Latest Latest
Warning

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

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

Documentation

Overview

Package xai provides the main client for interacting with xAI services.

Package xai provides the main client for interacting with xAI services.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client represents the main xAI SDK client.

func NewClient

func NewClient(config *Config) (*Client, error)

NewClient creates a new xAI client with the given configuration.

func NewClientFromEnvironment

func NewClientFromEnvironment() (*Client, error)

NewClientFromEnvironment creates a new client loading configuration from environment variables.

func NewClientWithAPIKey

func NewClientWithAPIKey(apiKey string) (*Client, error)

NewClientWithAPIKey creates a new client with the given API key and default configuration.

func (*Client) Auth

func (c *Client) Auth() *auth.Client

Auth returns the auth service client.

func (*Client) Chat

func (c *Client) Chat() xaiv1.ChatClient

Chat returns the chat service client.

func (*Client) Close

func (c *Client) Close() error

Close closes the client and its underlying connections.

func (*Client) CloseWithContext

func (c *Client) CloseWithContext(ctx context.Context) error

CloseWithContext closes the client with a context for cancellation.

func (*Client) Collections

func (c *Client) Collections() *collections.Client

Collections returns the collections service client.

func (*Client) Config

func (c *Client) Config() *Config

Config returns the client's configuration.

func (*Client) CreatedAt

func (c *Client) CreatedAt() time.Time

CreatedAt returns the time when the client was created.

func (*Client) Deferred added in v0.3.0

func (c *Client) Deferred() *deferred.Client

Deferred returns the deferred completions service client.

func (*Client) Documents added in v0.3.0

func (c *Client) Documents() *documents.Client

Documents returns the document search service client.

func (*Client) Embed added in v0.2.1

func (c *Client) Embed() *embed.Client

Embed returns the embeddings service client.

func (*Client) EnsureGRPCConnection

func (c *Client) EnsureGRPCConnection() error

EnsureGRPCConnection ensures that the gRPC connection is healthy and reconnect if needed.

func (*Client) Files added in v0.3.0

func (c *Client) Files() *files.Client

Files returns the files service client.

func (*Client) GRPCConnection

func (c *Client) GRPCConnection() *grpc.ClientConn

GRPCConnection returns the underlying gRPC connection. This should be used sparingly and callers should not close this connection.

func (*Client) GetHealthStatus

func (c *Client) GetHealthStatus() HealthStatus

GetHealthStatus returns the current health status of the client.

func (*Client) HealthCheck

func (c *Client) HealthCheck(_ context.Context) error

HealthCheck performs a health check on the client connection.

func (*Client) Images

func (c *Client) Images() *image.Client

Images returns the image generation service client.

func (*Client) IsClosed

func (c *Client) IsClosed() bool

IsClosed returns whether the client has been closed.

func (*Client) Metadata

func (c *Client) Metadata() *metadata.SDKMetadata

Metadata returns the client's SDK metadata.

func (*Client) Models

func (c *Client) Models() *models.Client

Models returns the models service client.

func (*Client) NewChatRequest

func (c *Client) NewChatRequest(model string, opts ...chat.RequestOption) *chat.Request

NewChatRequest creates a new chat request with the specified model.

func (*Client) NewContext

func (c *Client) NewContext(ctx context.Context) context.Context

NewContext creates a new context with the client's configuration and metadata.

func (*Client) NewContextWithCancel

func (c *Client) NewContextWithCancel(ctx context.Context) (context.Context, context.CancelFunc)

NewContextWithCancel creates a new context with a cancel function.

func (*Client) NewContextWithDeadline

func (c *Client) NewContextWithDeadline(ctx context.Context, deadline time.Time) (context.Context, context.CancelFunc)

NewContextWithDeadline creates a new context with a deadline.

func (*Client) NewContextWithTimeout

func (c *Client) NewContextWithTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc)

NewContextWithTimeout creates a new context with a specific timeout.

func (*Client) Sample added in v0.3.0

func (c *Client) Sample() *sample.Client

Sample returns the sample/completion service client (legacy).

func (*Client) String

func (c *Client) String() string

String returns a string representation of the client.

func (*Client) Tokenizer

func (c *Client) Tokenizer() *tokenizer.Client

Tokenizer returns the tokenization service client.

func (*Client) WithAPIKey

func (c *Client) WithAPIKey(apiKey string) *Client

WithAPIKey creates a new client with a different API key.

func (*Client) WithTimeout

func (c *Client) WithTimeout(timeout time.Duration) *Client

WithTimeout creates a new client with a different timeout setting.

type Config

type Config struct {
	// APIKey is the xAI API key for authentication.
	APIKey string `json:"api_key"`

	// Host is the xAI API host (default: api.x.ai).
	Host string `json:"host"`

	// GRPCPort is the gRPC port (default: 443).
	GRPCPort string `json:"grpc_port"`

	// HTTPHost is the HTTP API host (default: api.x.ai).
	HTTPHost string `json:"http_host"`

	// HTTPPort is the HTTP API port (default: 80).
	HTTPPort string `json:"http_port"`

	// Timeout is the default request timeout (default: 30s).
	Timeout time.Duration `json:"timeout"`

	// ConnectTimeout is the connection timeout (default: 10s).
	ConnectTimeout time.Duration `json:"connect_timeout"`

	// KeepAliveTimeout is the keep-alive timeout (default: 20s).
	KeepAliveTimeout time.Duration `json:"keep_alive_timeout"`

	// StreamTimeout is the streaming timeout (default: 300s).
	StreamTimeout time.Duration `json:"stream_timeout"`

	// Insecure controls whether to use TLS (default: false).
	// WARNING: Setting this to true disables TLS encryption entirely.
	// Only use in local development or testing environments. Never use in production.
	Insecure bool `json:"insecure"`

	// SkipVerify controls whether to skip TLS certificate verification (default: false).
	// WARNING: Setting this to true disables certificate validation, making connections
	// vulnerable to man-in-the-middle attacks. Only use in local development or testing
	// environments with self-signed certificates. Never use in production.
	SkipVerify bool `json:"skip_verify"`

	// MaxRetries is the maximum number of retries (default: 3).
	MaxRetries int `json:"max_retries"`

	// RetryBackoff is the retry backoff duration (default: 1s).
	RetryBackoff time.Duration `json:"retry_backoff"`

	// MaxBackoff is the maximum backoff duration (default: 60s).
	MaxBackoff time.Duration `json:"max_backoff"`

	// Environment is the deployment environment (default: production).
	Environment string `json:"environment"`

	// UserAgent is the user agent string (default: xai-sdk-go/version).
	UserAgent string `json:"user_agent"`

	// EnableTelemetry controls whether to enable telemetry (default: true).
	EnableTelemetry bool `json:"enable_telemetry"`

	// CustomTLSConfig allows providing a custom TLS configuration.
	CustomTLSConfig *tls.Config `json:"-"`
}

Config represents the client configuration.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a Config with default values.

func NewConfig

func NewConfig() *Config

NewConfig creates a new Config with default values and applies environment variable overrides.

func NewConfigWithAPIKey

func NewConfigWithAPIKey(apiKey string) *Config

NewConfigWithAPIKey creates a new Config with the provided API key.

func (*Config) CreateGRPCDialOptions

func (c *Config) CreateGRPCDialOptions() ([]grpc.DialOption, error)

CreateGRPCDialOptions creates gRPC dial options based on the configuration.

func (*Config) GRPCAddress

func (c *Config) GRPCAddress() string

GRPCAddress returns the gRPC address for this configuration.

func (*Config) HTTPAddress

func (c *Config) HTTPAddress() string

HTTPAddress returns the HTTP address for this configuration.

func (*Config) LoadFromEnvironment

func (c *Config) LoadFromEnvironment()

LoadFromEnvironment loads configuration from environment variables.

func (*Config) String

func (c *Config) String() string

String returns a string representation of the config (without sensitive data).

func (*Config) ToSDKMetadata

func (c *Config) ToSDKMetadata() *metadata.SDKMetadata

ToSDKMetadata converts the config to SDK metadata.

func (*Config) Validate

func (c *Config) Validate() error

Validate validates the configuration.

func (*Config) WithAPIKey

func (c *Config) WithAPIKey(apiKey string) *Config

WithAPIKey sets the API key.

func (*Config) WithEnableTelemetry

func (c *Config) WithEnableTelemetry(enable bool) *Config

WithEnableTelemetry sets the telemetry enabled flag.

func (*Config) WithEnvironment

func (c *Config) WithEnvironment(env string) *Config

WithEnvironment sets the environment.

func (*Config) WithHost

func (c *Config) WithHost(host string) *Config

WithHost sets the API host.

func (*Config) WithInsecure

func (c *Config) WithInsecure(insecure bool) *Config

WithInsecure sets the insecure flag.

func (*Config) WithMaxBackoff

func (c *Config) WithMaxBackoff(backoff time.Duration) *Config

WithMaxBackoff sets the maximum backoff duration.

func (*Config) WithMaxRetries

func (c *Config) WithMaxRetries(maxRetries int) *Config

WithMaxRetries sets the maximum number of retries.

func (*Config) WithRetryBackoff

func (c *Config) WithRetryBackoff(backoff time.Duration) *Config

WithRetryBackoff sets the retry backoff duration.

func (*Config) WithSkipVerify

func (c *Config) WithSkipVerify(skipVerify bool) *Config

WithSkipVerify sets the skip verify flag.

func (*Config) WithTLSConfig

func (c *Config) WithTLSConfig(tlsConfig *tls.Config) *Config

WithTLSConfig sets a custom TLS configuration.

func (*Config) WithTimeout

func (c *Config) WithTimeout(timeout time.Duration) *Config

WithTimeout sets the request timeout.

func (*Config) WithUserAgent

func (c *Config) WithUserAgent(userAgent string) *Config

WithUserAgent sets the user agent.

type HealthStatus

type HealthStatus struct {
	Status    string    `json:"status"`
	Timestamp time.Time `json:"timestamp"`
	Message   string    `json:"message,omitempty"`
}

HealthStatus represents the health status of the client.

Directories

Path Synopsis
Package auth provides a client for the xAI Auth API.
Package auth provides a client for the xAI Auth API.
Package chat provides chat completion functionality for the xAI SDK.
Package chat provides chat completion functionality for the xAI SDK.
Package collections provides a client for the xAI Collections API.
Package collections provides a client for the xAI Collections API.
Package deferred provides a client for the xAI Deferred Completions API.
Package deferred provides a client for the xAI Deferred Completions API.
Package documents provides a client for the xAI Document Search API.
Package documents provides a client for the xAI Document Search API.
Package embed provides client functionality for the xAI Embeddings API.
Package embed provides client functionality for the xAI Embeddings API.
Package files provides a client for the xAI Files API.
Package files provides a client for the xAI Files API.
Package image provides a client for the xAI Image Generation API.
Package image provides a client for the xAI Image Generation API.
internal
auth
Package auth provides authentication interceptors for gRPC requests.
Package auth provides authentication interceptors for gRPC requests.
constants
Package constants provides default constants used throughout the xAI SDK.
Package constants provides default constants used throughout the xAI SDK.
errors
Package errors provides error types and utilities for the xAI SDK.
Package errors provides error types and utilities for the xAI SDK.
grpcutil
Package grpcutil provides gRPC utility functions and interceptors.
Package grpcutil provides gRPC utility functions and interceptors.
metadata
Package metadata provides utilities for working with gRPC metadata and headers.
Package metadata provides utilities for working with gRPC metadata and headers.
rest
Package rest provides a REST client for xAI APIs.
Package rest provides a REST client for xAI APIs.
version
Package version provides version information for the xAI SDK.
Package version provides version information for the xAI SDK.
Package models provides model information functionality for xAI SDK.
Package models provides model information functionality for xAI SDK.
Package sample provides a client for the xAI Sample/Completion API (legacy).
Package sample provides a client for the xAI Sample/Completion API (legacy).
Package tokenizer provides a client for the xAI Tokenization API.
Package tokenizer provides a client for the xAI Tokenization API.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL