selfplay

package
v1.1.11 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package selfplay provides self-play capabilities for arena testing scenarios. It enables LLM-driven user simulation and audio generation for duplex conversations.

Index

Constants

View Source
const (
	TTSProviderOpenAI     = "openai"
	TTSProviderElevenLabs = "elevenlabs"
	TTSProviderCartesia   = "cartesia"
)

Supported TTS provider names.

View Source
const (
	// TTSProviderMock is the provider name for the mock TTS service.
	TTSProviderMock = "mock"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AudioContentGenerator added in v1.1.6

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

AudioContentGenerator wraps a ContentGenerator and adds TTS synthesis.

func NewAudioContentGenerator added in v1.1.6

func NewAudioContentGenerator(
	textGenerator *ContentGenerator,
	ttsService tts.Service,
	ttsConfig *config.TTSConfig,
) *AudioContentGenerator

NewAudioContentGenerator creates a new audio content generator.

func (*AudioContentGenerator) GetTTSService added in v1.1.6

func (g *AudioContentGenerator) GetTTSService() tts.Service

GetTTSService returns the TTS service for direct access if needed.

func (*AudioContentGenerator) GetTextGenerator added in v1.1.6

func (g *AudioContentGenerator) GetTextGenerator() *ContentGenerator

GetTextGenerator returns the underlying text generator.

func (*AudioContentGenerator) NextUserTurnAudio added in v1.1.6

func (g *AudioContentGenerator) NextUserTurnAudio(
	ctx context.Context,
	history []types.Message,
	scenarioID string,
	opts *GeneratorOptions,
) (*AudioResult, error)

NextUserTurnAudio generates a user message and converts it to audio.

type AudioGenerator added in v1.1.6

type AudioGenerator interface {
	// NextUserTurnAudio generates a user message and converts it to audio.
	// Returns the text result and audio data.
	// The opts parameter is optional and can be nil.
	NextUserTurnAudio(
		ctx context.Context,
		history []types.Message,
		scenarioID string,
		opts *GeneratorOptions,
	) (*AudioResult, error)
}

AudioGenerator generates user messages with audio output for duplex self-play. It wraps a text ContentGenerator and adds TTS synthesis.

type AudioProvider added in v1.1.6

type AudioProvider interface {
	Provider

	// GetAudioContentGenerator returns an audio generator for the given role and persona.
	// The TTS config specifies the voice and provider for text-to-speech synthesis.
	GetAudioContentGenerator(role, personaID string, ttsConfig *config.TTSConfig) (AudioGenerator, error)
}

AudioProvider extends Provider with audio generation capabilities for duplex mode.

type AudioResult added in v1.1.6

type AudioResult struct {
	// TextResult contains the text generation result with cost info and metadata.
	TextResult *pipeline.ExecutionResult

	// Audio contains the synthesized audio data.
	Audio []byte

	// AudioFormat describes the audio encoding.
	AudioFormat tts.AudioFormat

	// SampleRate is the sample rate of the audio data in Hz.
	SampleRate int
}

AudioResult contains both text and audio output from audio generation.

type CacheKey

type CacheKey struct {
	Role      string
	PersonaID string
}

CacheKey represents a structured cache key for user generators

type CacheStats

type CacheStats struct {
	Size        int     // Number of cached entries
	Hits        int     // Number of cache hits
	Misses      int     // Number of cache misses
	HitRate     float64 // Cache hit rate (0.0 to 1.0)
	CachedPairs []CacheKey
}

CacheStats provides observability into cache performance

type ContentGenerator

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

ContentGenerator generates user messages using an LLM

func NewContentGenerator

func NewContentGenerator(provider providers.Provider, persona *config.UserPersonaPack) *ContentGenerator

NewContentGenerator creates a new content generator with a specific provider and persona

func (*ContentGenerator) NextUserTurn

func (cg *ContentGenerator) NextUserTurn(
	ctx context.Context,
	history []types.Message,
	scenarioID string,
	opts *GeneratorOptions,
) (*pipeline.ExecutionResult, error)

NextUserTurn generates a user message using the LLM through a stage pipeline. The opts parameter is optional and can be nil.

type Generator

type Generator interface {
	NextUserTurn(
		ctx context.Context,
		history []types.Message,
		scenarioID string,
		opts *GeneratorOptions,
	) (*pipeline.ExecutionResult, error)
}

Generator generates user messages for self-play scenarios. Each generator is configured with a specific persona and LLM provider, and produces user turns based on conversation history. Returns the full pipeline ExecutionResult which includes trace data, costs, and metadata. The opts parameter is optional and can be nil.

type GeneratorOptions added in v1.1.6

type GeneratorOptions struct {
	// SelfplayTurnIndex is the 1-indexed selfplay turn number (first selfplay turn = 1).
	// If set, this overrides the automatic turn counting from history.
	// This is important for scenarios with mixed file-based and selfplay turns.
	SelfplayTurnIndex int
}

GeneratorOptions provides optional configuration for self-play generation.

type MockTTSService added in v1.1.6

type MockTTSService struct {
	// SampleRate is the audio sample rate (default: 24000).
	SampleRate int

	// AudioFiles is a list of PCM audio files to load and rotate through.
	// If empty, falls back to generating silence.
	AudioFiles []string

	// Latency simulates network/processing delay before returning audio.
	Latency time.Duration
	// contains filtered or unexported fields
}

MockTTSService is a mock TTS service for testing. It loads audio from PCM files to provide realistic speech for testing.

func NewMockTTS added in v1.1.6

func NewMockTTS() *MockTTSService

NewMockTTS creates a new mock TTS service with default settings.

func NewMockTTSWithFiles added in v1.1.6

func NewMockTTSWithFiles(audioFiles []string) *MockTTSService

NewMockTTSWithFiles creates a mock TTS service that loads audio from files.

func NewMockTTSWithLatency added in v1.1.6

func NewMockTTSWithLatency(latency time.Duration) *MockTTSService

NewMockTTSWithLatency creates a mock TTS service with simulated latency.

func (*MockTTSService) Name added in v1.1.6

func (m *MockTTSService) Name() string

Name returns the provider name.

func (*MockTTSService) SupportedFormats added in v1.1.6

func (m *MockTTSService) SupportedFormats() []tts.AudioFormat

SupportedFormats returns supported audio output formats.

func (*MockTTSService) SupportedVoices added in v1.1.6

func (m *MockTTSService) SupportedVoices() []tts.Voice

SupportedVoices returns available mock voices.

func (*MockTTSService) Synthesize added in v1.1.6

func (m *MockTTSService) Synthesize(
	ctx context.Context, text string, config tts.SynthesisConfig,
) (io.ReadCloser, error)

Synthesize converts text to mock PCM audio. If audio files are configured, returns audio from those files (rotating through them). Otherwise falls back to generating silence.

type Provider

type Provider interface {
	GetContentGenerator(role, personaID string) (Generator, error)
}

Provider provides access to content generators for self-play scenarios. This is the main interface that the engine and turn executors use to obtain content generators based on role and persona.

type Registry

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

Registry manages self-play providers, personas, and user generator creation

func NewRegistry

func NewRegistry(
	providerRegistry *providers.Registry,
	providerMap map[string]string,
	personas map[string]*config.UserPersonaPack,
	roles []config.SelfPlayRoleGroup,
) *Registry

NewRegistry creates a new self-play registry

func NewRegistryWithTTS added in v1.1.6

func NewRegistryWithTTS(
	providerRegistry *providers.Registry,
	providerMap map[string]string,
	personas map[string]*config.UserPersonaPack,
	roles []config.SelfPlayRoleGroup,
	ttsRegistry *TTSRegistry,
) *Registry

NewRegistryWithTTS creates a new self-play registry with a custom TTS registry. This is useful for testing or when using pre-configured TTS services.

func (*Registry) ClearCache

func (r *Registry) ClearCache()

ClearCache clears all cached user generators and resets statistics

func (*Registry) Close

func (r *Registry) Close() error

Close closes the self-play provider registry and cleans up resources

func (*Registry) GetAudioContentGenerator added in v1.1.6

func (r *Registry) GetAudioContentGenerator(
	role, personaID string,
	ttsConfig *config.TTSConfig,
) (AudioGenerator, error)

GetAudioContentGenerator implements AudioProvider interface. Returns an AudioGenerator for the given role, persona, and TTS configuration. Unlike GetContentGenerator, audio generators are not cached since TTS config may vary.

func (*Registry) GetAvailableProviders

func (r *Registry) GetAvailableProviders() []string

GetAvailableProviders returns a list of available providers

func (*Registry) GetAvailableRoles

func (r *Registry) GetAvailableRoles() []string

GetAvailableRoles returns a list of available self-play roles

func (*Registry) GetCacheStats

func (r *Registry) GetCacheStats() CacheStats

GetCacheStats returns current cache statistics

func (*Registry) GetContentGenerator

func (r *Registry) GetContentGenerator(role, personaID string) (Generator, error)

GetContentGenerator implements Provider interface Returns a Generator for the given role and persona (cached for efficiency)

func (*Registry) GetTTSRegistry added in v1.1.6

func (r *Registry) GetTTSRegistry() *TTSRegistry

GetTTSRegistry returns the TTS registry for direct access if needed.

func (*Registry) IsValidRole

func (r *Registry) IsValidRole(role string) bool

IsValidRole checks if a role is configured for self-play

func (*Registry) PrewarmCache

func (r *Registry) PrewarmCache(rolePairs []CacheKey) error

PrewarmCache creates and caches ContentGenerators for common role+persona combinations This can improve performance by avoiding cold starts during actual execution

type TTSRegistry added in v1.1.6

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

TTSRegistry manages TTS service instances by provider name. It supports lazy initialization and caching of TTS services.

func NewTTSRegistry added in v1.1.6

func NewTTSRegistry() *TTSRegistry

NewTTSRegistry creates a new TTS registry.

func (*TTSRegistry) Clear added in v1.1.6

func (r *TTSRegistry) Clear()

Clear removes all cached services.

func (*TTSRegistry) Get added in v1.1.6

func (r *TTSRegistry) Get(provider string) (tts.Service, error)

Get returns a TTS service for the given provider name. Services are lazily initialized on first request and cached. For mock provider with custom audio files, use GetWithConfig instead.

func (*TTSRegistry) GetWithConfig added in v1.1.6

func (r *TTSRegistry) GetWithConfig(cfg *config.TTSConfig) (tts.Service, error)

GetWithConfig returns a TTS service configured with the given TTSConfig. For mock provider, this allows specifying audio files directly in the config. Services with custom configs are NOT cached since audio files may vary per scenario.

func (*TTSRegistry) Register added in v1.1.6

func (r *TTSRegistry) Register(provider string, svc tts.Service)

Register adds a pre-configured TTS service to the registry. This is useful for testing or when using custom configurations.

func (*TTSRegistry) SupportedProviders added in v1.1.6

func (r *TTSRegistry) SupportedProviders() []string

SupportedProviders returns a list of supported TTS provider names.

Jump to

Keyboard shortcuts

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