examples

package
v1.0.60 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: MIT Imports: 4 Imported by: 0

README

Interface Segregation Examples

This directory demonstrates how to use the segregated provider interfaces for different use cases, following the Interface Segregation Principle.

Overview

The Interface Segregation Principle (ISP) states that clients should not depend on interfaces they don't use. The AI Provider Kit follows this principle by defining smaller, focused interfaces that can be composed as needed:

  • CoreProvider - Basic provider information
  • ModelProvider - Model discovery functionality
  • ChatProvider - Chat completion functionality
  • ToolCallingProvider - Tool invocation functionality
  • HealthCheckProvider - Health monitoring functionality
  • Provider - Complete interface combining all above

Examples in this File

1. ModelDiscoveryService

Demonstrates depending only on ModelProvider when you just need to discover available models.

type ModelDiscoveryService struct {
    providers []ModelProvider
}
2. HealthMonitoringService

Demonstrates depending only on HealthCheckProvider when you only need to check health status.

type HealthMonitoringService struct {
    providers []HealthCheckProvider
}
3. ChatService

Demonstrates depending only on ChatProvider when you only need chat completion.

type ChatService struct {
    provider ChatProvider
}
4. ToolExecutionService

Demonstrates depending only on ToolCallingProvider when you only need tool invocation.

type ToolExecutionService struct {
    provider ToolCallingProvider
}
5. ProviderInfoService

Demonstrates depending only on CoreProvider for basic provider information.

type ProviderInfoService struct {
    providers []CoreProvider
}
6. MultiPurposeService

Demonstrates using the full Provider interface when you need comprehensive functionality.

type MultiPurposeService struct {
    provider Provider
}
7. FlexibleProviderFactory

Demonstrates creating providers with specific interface requirements, showing how to depend only on needed interfaces.

Benefits of Interface Segregation

  1. Loose Coupling: Services depend only on the interfaces they actually use
  2. Easy Testing: Mock implementations only need to implement relevant methods
  3. Clear Intent: The interface a service depends on immediately reveals its requirements
  4. Flexibility: Can swap implementations without affecting unrelated functionality

Note

This file contains educational examples demonstrating interface usage patterns. The FlexibleMockProvider is a mock implementation for demonstration purposes only and should not be used in production.

Documentation

Overview

Package examples provides code examples demonstrating how to use the AI Provider Kit's segregated provider interfaces following the Interface Segregation Principle (ISP).

NOTE: This file contains educational examples. The FlexibleMockProvider is a mock implementation for demonstration purposes only and should NOT be used in production.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthConfig

type AuthConfig = types.AuthConfig

Re-export commonly used types for convenience

type ChatCompletionChunk

type ChatCompletionChunk = types.ChatCompletionChunk

Re-export commonly used types for convenience

type ChatCompletionStream

type ChatCompletionStream = types.ChatCompletionStream

Re-export commonly used types for convenience

type ChatProvider

type ChatProvider = types.ChatProvider

Re-export commonly used types for convenience

type ChatService

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

ChatService only needs to generate completions, so it depends only on ChatProvider

func NewChatService

func NewChatService(provider ChatProvider) *ChatService

func (*ChatService) GenerateResponse

func (s *ChatService) GenerateResponse(ctx context.Context, options GenerateOptions) (ChatCompletionStream, error)

type CoreProvider

type CoreProvider = types.CoreProvider

Re-export commonly used types for convenience

type FlexibleMockProvider

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

FlexibleMockProvider is a mock implementation that satisfies all provider interfaces for demonstration purposes in the FlexibleProviderFactory example.

IMPORTANT: This is an example implementation that returns simulated data. Do NOT use this in production code.

func (*FlexibleMockProvider) Authenticate

func (p *FlexibleMockProvider) Authenticate(ctx context.Context, authConfig types.AuthConfig) error

Authenticate authenticates the mock provider (always succeeds)

func (*FlexibleMockProvider) Configure

func (p *FlexibleMockProvider) Configure(config types.ProviderConfig) error

Configure configures the mock provider with the given config

func (*FlexibleMockProvider) Description

func (p *FlexibleMockProvider) Description() string

func (*FlexibleMockProvider) GenerateChatCompletion

func (p *FlexibleMockProvider) GenerateChatCompletion(ctx context.Context, options types.GenerateOptions) (types.ChatCompletionStream, error)

GenerateChatCompletion generates a mock chat completion stream

func (*FlexibleMockProvider) GetConfig

func (p *FlexibleMockProvider) GetConfig() types.ProviderConfig

func (*FlexibleMockProvider) GetDefaultModel

func (p *FlexibleMockProvider) GetDefaultModel() string

func (*FlexibleMockProvider) GetMetrics

func (p *FlexibleMockProvider) GetMetrics() types.ProviderMetrics

func (*FlexibleMockProvider) GetModels

func (p *FlexibleMockProvider) GetModels(ctx context.Context) ([]types.Model, error)

GetModels returns mock models for the provider

func (*FlexibleMockProvider) GetToolFormat

func (p *FlexibleMockProvider) GetToolFormat() types.ToolFormat

func (*FlexibleMockProvider) HealthCheck

func (p *FlexibleMockProvider) HealthCheck(ctx context.Context) error

HealthCheck performs a health check on the provider

func (*FlexibleMockProvider) InvokeServerTool

func (p *FlexibleMockProvider) InvokeServerTool(ctx context.Context, toolName string, params interface{}) (interface{}, error)

func (*FlexibleMockProvider) IsAuthenticated

func (p *FlexibleMockProvider) IsAuthenticated() bool

func (*FlexibleMockProvider) Logout

func (p *FlexibleMockProvider) Logout(ctx context.Context) error

func (*FlexibleMockProvider) Name

func (p *FlexibleMockProvider) Name() string

Name returns the mock provider name

func (*FlexibleMockProvider) SupportsResponsesAPI

func (p *FlexibleMockProvider) SupportsResponsesAPI() bool

SupportsResponsesAPI indicates if the provider supports the responses API

func (*FlexibleMockProvider) SupportsStreaming

func (p *FlexibleMockProvider) SupportsStreaming() bool

SupportsStreaming indicates if the provider supports streaming responses

func (*FlexibleMockProvider) SupportsToolCalling

func (p *FlexibleMockProvider) SupportsToolCalling() bool

SupportsToolCalling returns true for mock provider

func (*FlexibleMockProvider) Type

type FlexibleMockStream

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

FlexibleMockStream implements ChatCompletionStream for mock responses

func (*FlexibleMockStream) Close

func (m *FlexibleMockStream) Close() error

func (*FlexibleMockStream) Next

type FlexibleProviderFactory

type FlexibleProviderFactory struct{}

FlexibleProviderFactory demonstrates how to create providers with different interface requirements.

IMPORTANT: This is an example implementation for educational purposes only. It creates mock providers that return simulated data and should NOT be used in production. For production use, integrate with the actual provider factory implementations.

func (*FlexibleProviderFactory) CreateChatProvider

func (f *FlexibleProviderFactory) CreateChatProvider(providerType types.ProviderType, config types.ProviderConfig) (types.ChatProvider, error)

CreateChatProvider creates a provider that implements only the ChatProvider interface. This allows clients to depend only on chat completion functionality.

func (*FlexibleProviderFactory) CreateHealthCheckProvider

func (f *FlexibleProviderFactory) CreateHealthCheckProvider(providerType types.ProviderType, config types.ProviderConfig) (types.HealthCheckProvider, error)

CreateHealthCheckProvider creates a provider that implements only the HealthCheckProvider interface. This allows clients to depend only on health checking functionality.

func (*FlexibleProviderFactory) CreateModelProvider

func (f *FlexibleProviderFactory) CreateModelProvider(providerType types.ProviderType, config types.ProviderConfig) (types.ModelProvider, error)

CreateModelProvider creates a provider that implements only the ModelProvider interface. This allows clients to depend only on model discovery functionality.

type GenerateOptions

type GenerateOptions = types.GenerateOptions

Re-export commonly used types for convenience

type HealthCheckProvider

type HealthCheckProvider = types.HealthCheckProvider

Re-export commonly used types for convenience

type HealthMonitoringService

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

HealthMonitoringService only needs to check health, so it depends only on HealthCheckProvider

func NewHealthMonitoringService

func NewHealthMonitoringService() *HealthMonitoringService

func (*HealthMonitoringService) AddProvider

func (s *HealthMonitoringService) AddProvider(provider HealthCheckProvider)

func (*HealthMonitoringService) CheckAllHealth

func (s *HealthMonitoringService) CheckAllHealth(ctx context.Context) map[string]error

type HealthStatus

type HealthStatus = types.HealthStatus

Re-export commonly used types for convenience

type Model

type Model = types.Model

Re-export commonly used types for convenience

type ModelDiscoveryService

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

ModelDiscoveryService only needs to discover models, so it depends only on ModelProvider

func NewModelDiscoveryService

func NewModelDiscoveryService() *ModelDiscoveryService

func (*ModelDiscoveryService) AddProvider

func (s *ModelDiscoveryService) AddProvider(provider ModelProvider)

func (*ModelDiscoveryService) GetAllModels

func (s *ModelDiscoveryService) GetAllModels(ctx context.Context) (map[string][]Model, error)

type ModelProvider

type ModelProvider = types.ModelProvider

Provider interfaces from pkg/types

type MultiPurposeService

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

MultiPurposeService demonstrates how to compose multiple interfaces

func NewMultiPurposeService

func NewMultiPurposeService(provider Provider) *MultiPurposeService

func (*MultiPurposeService) GetHealth

func (s *MultiPurposeService) GetHealth(ctx context.Context) error

func (*MultiPurposeService) GetProviderInfo

func (s *MultiPurposeService) GetProviderInfo() string

func (*MultiPurposeService) SupportsTools

func (s *MultiPurposeService) SupportsTools() bool

type Pricing

type Pricing = types.Pricing

Re-export commonly used types for convenience

type Provider

type Provider = types.Provider

Re-export commonly used types for convenience

type ProviderConfig

type ProviderConfig = types.ProviderConfig

Re-export commonly used types for convenience

type ProviderInfo

type ProviderInfo = types.ProviderInfo

Re-export commonly used types for convenience

type ProviderInfoService

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

ProviderInfoService only needs basic provider info, so it depends only on CoreProvider

func NewProviderInfoService

func NewProviderInfoService() *ProviderInfoService

func (*ProviderInfoService) AddProvider

func (s *ProviderInfoService) AddProvider(provider CoreProvider)

func (*ProviderInfoService) ListProviders

func (s *ProviderInfoService) ListProviders() []ProviderInfo

type ProviderMetrics

type ProviderMetrics = types.ProviderMetrics

Re-export commonly used types for convenience

type ProviderType

type ProviderType = types.ProviderType

Common types from pkg/types

type ToolCallingProvider

type ToolCallingProvider = types.ToolCallingProvider

Re-export commonly used types for convenience

type ToolExecutionService

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

ToolExecutionService only needs to call tools, so it depends only on ToolCallingProvider

func NewToolExecutionService

func NewToolExecutionService(provider ToolCallingProvider) *ToolExecutionService

func (*ToolExecutionService) ExecuteTool

func (s *ToolExecutionService) ExecuteTool(ctx context.Context, toolName string, params interface{}) (interface{}, error)

type ToolFormat

type ToolFormat = types.ToolFormat

Re-export commonly used types for convenience

Jump to

Keyboard shortcuts

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