ai

package
v1.33.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package ai provides AI-powered product recommendations and embeddings via Hanzo Cloud-Backend (Rust inference API) and Hanzo Cloud (Go API).

Index

Constants

View Source
const (
	DefaultCloudEndpoint  = "https://cloud.hanzo.ai/api"
	DefaultModel          = "gpt-4o"
	DefaultEmbeddingModel = "text-embedding-3-small"
	DefaultTimeout        = 30 * time.Second
)

Default endpoints for Hanzo Cloud services. All inference routes through the Go cloud-api which handles native provider routing (DO-AI, Fireworks, OpenAI Direct). LiteLLM is removed.

Variables

This section is empty.

Functions

func GetEmbeddingDefault

func GetEmbeddingDefault(ctx context.Context, text string) ([]float32, error)

GetEmbeddingDefault generates an embedding using the default client

func Initialize

func Initialize(config *AIConfig)

Initialize initializes the default AI client

Types

type AIConfig

type AIConfig struct {
	// Endpoint is the base URL for the Hanzo Cloud API.
	// All inference routes through cloud-api natively (no LiteLLM middleman).
	// Default: https://cloud.hanzo.ai/api
	Endpoint string `json:"endpoint"`

	// CloudEndpoint is an alias for Endpoint (kept for backward compatibility).
	CloudEndpoint string `json:"cloudEndpoint"`

	// APIKey is the Bearer token for authentication
	APIKey string `json:"apiKey"`

	// Model is the default model for chat completions
	// Options: gpt-4o, gpt-5, claude-opus-4-6, zen4-pro, zen4-coder-pro
	Model string `json:"model"`

	// EmbeddingModel is the model used for generating embeddings
	// Options: text-embedding-3-small, text-embedding-3-large, text-embedding-ada-002
	EmbeddingModel string `json:"embeddingModel"`

	// Temperature controls randomness in responses (0.0-2.0)
	Temperature float32 `json:"temperature"`

	// MaxTokens limits the response length
	MaxTokens int `json:"maxTokens"`

	// Timeout for API requests
	Timeout time.Duration `json:"timeout"`

	// GRPOEnabled enables Group Relative Policy Optimization for better responses
	GRPOEnabled bool `json:"grpoEnabled"`
}

AIConfig holds configuration for the Hanzo AI services

func NewAIConfig

func NewAIConfig() *AIConfig

NewAIConfig creates a new AIConfig with defaults from environment variables. All inference now routes through the Go cloud-api (no LiteLLM middleman).

type APIError

type APIError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Details string `json:"details,omitempty"`
}

APIError represents an error from the API

func (*APIError) Error

func (e *APIError) Error() string

type ChatChoice

type ChatChoice struct {
	Index        int         `json:"index"`
	Message      ChatMessage `json:"message"`
	FinishReason string      `json:"finish_reason"`
}

ChatChoice represents a single completion choice

type ChatCompletionRequest

type ChatCompletionRequest struct {
	Model       string        `json:"model"`
	Messages    []ChatMessage `json:"messages"`
	Temperature *float32      `json:"temperature,omitempty"`
	MaxTokens   *int          `json:"max_tokens,omitempty"`
	Stream      bool          `json:"stream,omitempty"`
	GRPOEnabled bool          `json:"grpo_enabled,omitempty"`
	Groundtruth *string       `json:"groundtruth,omitempty"`
}

ChatCompletionRequest is the request body for chat completions

type ChatCompletionResponse

type ChatCompletionResponse struct {
	ID      string       `json:"id"`
	Object  string       `json:"object"`
	Created int64        `json:"created"`
	Model   string       `json:"model"`
	Choices []ChatChoice `json:"choices"`
	Usage   Usage        `json:"usage"`
	// GRPO metadata when enabled
	GRPOMetadata *GRPOMetadata `json:"grpo_metadata,omitempty"`
}

ChatCompletionResponse is the response from chat completions

func ChatCompletionDefault

func ChatCompletionDefault(ctx context.Context, messages []ChatMessage) (*ChatCompletionResponse, error)

ChatCompletionDefault sends a chat completion using the default client

type ChatMessage

type ChatMessage struct {
	Role    string `json:"role"`    // system, user, assistant
	Content string `json:"content"` // message content
}

ChatMessage represents a message in a chat completion request

type Client

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

Client is the AI client for Hanzo Cloud services

func GetClient

func GetClient() *Client

GetClient returns the default AI client, initializing if necessary

func NewClient

func NewClient(config *AIConfig) *Client

NewClient creates a new AI client with the given configuration

func (*Client) ChatCompletion

func (c *Client) ChatCompletion(ctx context.Context, messages []ChatMessage) (*ChatCompletionResponse, error)

ChatCompletion sends a chat completion request to the API

func (*Client) ChatCompletionWithRequest

func (c *Client) ChatCompletionWithRequest(ctx context.Context, req ChatCompletionRequest) (*ChatCompletionResponse, error)

ChatCompletionWithRequest sends a custom chat completion request

func (*Client) CustomerSupport

func (c *Client) CustomerSupport(ctx context.Context, message string) (*CustomerSupportResponse, error)

CustomerSupport handles AI-powered customer support queries

func (*Client) CustomerSupportWithRequest

func (c *Client) CustomerSupportWithRequest(ctx context.Context, req CustomerSupportRequest) (*CustomerSupportResponse, error)

CustomerSupportWithRequest handles customer support with a custom request

func (*Client) GetEmbedding

func (c *Client) GetEmbedding(ctx context.Context, text string) ([]float32, error)

GetEmbedding generates an embedding vector for the given text

func (*Client) GetEmbeddings

func (c *Client) GetEmbeddings(ctx context.Context, texts []string) ([][]float32, error)

GetEmbeddings generates embedding vectors for multiple texts

func (*Client) GetRecommendations

func (c *Client) GetRecommendations(ctx context.Context, userID string, products []ProductInfo) (*RecommendationResponse, error)

GetRecommendations generates AI-powered product recommendations

func (*Client) GetRecommendationsWithRequest

func (c *Client) GetRecommendationsWithRequest(ctx context.Context, req RecommendationRequest) (*RecommendationResponse, error)

GetRecommendationsWithRequest generates recommendations with a custom request

func (*Client) HealthCheck

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

HealthCheck verifies the AI service is available

func (*Client) ListCloudModels

func (c *Client) ListCloudModels(ctx context.Context) ([]ModelInfo, error)

ListCloudModels returns the available models from the Hanzo Cloud API (Go backend), which includes the full routing table with premium flags. Use this instead of ListModels when you need billing-aware model info.

func (*Client) ListModels

func (c *Client) ListModels(ctx context.Context) ([]ModelInfo, error)

ListModels returns the available AI models

func (*Client) SimilaritySearch

func (c *Client) SimilaritySearch(ctx context.Context, query string, products []ProductInfo, topK int) ([]Recommendation, error)

SimilaritySearch finds products similar to the given text using embeddings

type CustomerSupportRequest

type CustomerSupportRequest struct {
	UserID       string        `json:"userId,omitempty"`
	Message      string        `json:"message"`
	Context      string        `json:"context,omitempty"`
	History      []ChatMessage `json:"history,omitempty"`
	OrderContext *OrderContext `json:"orderContext,omitempty"`
}

CustomerSupportRequest is the request for customer support AI

type CustomerSupportResponse

type CustomerSupportResponse struct {
	Response      string   `json:"response"`
	SuggestedNext []string `json:"suggestedNext,omitempty"`
	Sentiment     string   `json:"sentiment,omitempty"`
	RequiresHuman bool     `json:"requiresHuman"`
	Confidence    float64  `json:"confidence"`
}

CustomerSupportResponse is the response from customer support AI

func CustomerSupportDefault

func CustomerSupportDefault(ctx context.Context, message string) (*CustomerSupportResponse, error)

CustomerSupportDefault handles support queries using the default client

type EmbeddingData

type EmbeddingData struct {
	Object    string    `json:"object"`
	Index     int       `json:"index"`
	Embedding []float32 `json:"embedding"`
}

EmbeddingData contains a single embedding vector

type EmbeddingRequest

type EmbeddingRequest struct {
	Input []string `json:"input"`
	Model string   `json:"model"`
}

EmbeddingRequest is the request body for embeddings

type EmbeddingResponse

type EmbeddingResponse struct {
	Object string          `json:"object"`
	Data   []EmbeddingData `json:"data"`
	Model  string          `json:"model"`
	Usage  EmbeddingUsage  `json:"usage"`
}

EmbeddingResponse is the response from embeddings

type EmbeddingUsage

type EmbeddingUsage struct {
	PromptTokens int `json:"prompt_tokens"`
	TotalTokens  int `json:"total_tokens"`
}

EmbeddingUsage tracks token usage for embeddings

type GRPOMetadata

type GRPOMetadata struct {
	ExperiencesUsed []string `json:"experiences_used"`
	GroupSize       int      `json:"group_size"`
	BestReward      float64  `json:"best_reward"`
	AvgReward       float64  `json:"avg_reward"`
}

GRPOMetadata contains GRPO-specific response metadata

type ModelInfo

type ModelInfo struct {
	ID      string `json:"id"`
	Object  string `json:"object"`
	Created int64  `json:"created"`
	OwnedBy string `json:"owned_by"`
	Premium bool   `json:"premium,omitempty"`
}

ModelInfo represents information about an available model

type OrderContext

type OrderContext struct {
	OrderID     string    `json:"orderId"`
	Status      string    `json:"status"`
	CreatedAt   time.Time `json:"createdAt"`
	TotalAmount float64   `json:"totalAmount"`
	Items       []string  `json:"items"`
}

OrderContext provides order information for support queries

type ProductInfo

type ProductInfo struct {
	ID          string   `json:"id"`
	Name        string   `json:"name"`
	Description string   `json:"description"`
	Category    string   `json:"category"`
	Price       float64  `json:"price"`
	Tags        []string `json:"tags"`
	SKU         string   `json:"sku,omitempty"`
}

ProductInfo represents product data for recommendations

type Recommendation

type Recommendation struct {
	ProductID  string  `json:"productId"`
	Score      float64 `json:"score"`
	Reasoning  string  `json:"reasoning,omitempty"`
	Category   string  `json:"category,omitempty"`
	Similarity float64 `json:"similarity,omitempty"`
}

Recommendation represents a single product recommendation

type RecommendationRequest

type RecommendationRequest struct {
	UserID           string        `json:"userId"`
	Products         []ProductInfo `json:"products"`
	PurchaseHistory  []string      `json:"purchaseHistory,omitempty"`
	BrowsingHistory  []string      `json:"browsingHistory,omitempty"`
	MaxResults       int           `json:"maxResults,omitempty"`
	IncludeReasoning bool          `json:"includeReasoning,omitempty"`
}

RecommendationRequest is the request for product recommendations

type RecommendationResponse

type RecommendationResponse struct {
	UserID          string           `json:"userId"`
	Recommendations []Recommendation `json:"recommendations"`
	GeneratedAt     time.Time        `json:"generatedAt"`
	Model           string           `json:"model"`
}

RecommendationResponse is the response for product recommendations

func GetRecommendationsDefault

func GetRecommendationsDefault(ctx context.Context, userID string, products []ProductInfo) (*RecommendationResponse, error)

GetRecommendationsDefault generates recommendations using the default client

type Usage

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

Usage tracks token usage for billing

Jump to

Keyboard shortcuts

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