176 lines
5.8 KiB
Go
176 lines
5.8 KiB
Go
package llm
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
|
"gitea.maximumdirect.net/eric/promptkit"
|
|
)
|
|
|
|
type PromptKitProfileSourceConfig struct {
|
|
ProfileDir string
|
|
ProfileFile string
|
|
LocalBackend *PromptKitLocalBackendConfig
|
|
}
|
|
|
|
type PromptKitProfileInspectorConfig struct {
|
|
Source PromptKitProfileSourceConfig
|
|
Assets *AssetRegistry
|
|
}
|
|
|
|
func (c PromptKitProfileSourceConfig) localEndpoint() string {
|
|
if c.LocalBackend == nil {
|
|
return ""
|
|
}
|
|
return c.LocalBackend.Endpoint
|
|
}
|
|
|
|
type PromptKitProfileInspector struct {
|
|
engine *promptkit.Engine
|
|
}
|
|
|
|
type PromptKitProfileInspection struct {
|
|
ProfileID string
|
|
BackendID string
|
|
Model string
|
|
CredentialEnvironment string
|
|
CredentialRequired bool
|
|
}
|
|
|
|
type PromptKitProfileInspectionError struct {
|
|
ProfileID string
|
|
err error
|
|
}
|
|
|
|
func (e *PromptKitProfileInspectionError) Error() string {
|
|
switch {
|
|
case errors.Is(e.err, promptkit.ErrProfileNotFound):
|
|
return fmt.Sprintf("PromptKit profile %q is not configured", e.ProfileID)
|
|
case errors.Is(e.err, promptkit.ErrInvalidRequest):
|
|
return fmt.Sprintf("PromptKit profile ID %q is invalid", e.ProfileID)
|
|
case errors.Is(e.err, promptkit.ErrProfileLoad):
|
|
return fmt.Sprintf("PromptKit profile %q is invalid or unreadable", e.ProfileID)
|
|
default:
|
|
return fmt.Sprintf("PromptKit profile %q could not be inspected", e.ProfileID)
|
|
}
|
|
}
|
|
|
|
func (e *PromptKitProfileInspectionError) Unwrap() error {
|
|
return e.err
|
|
}
|
|
|
|
type promptKitProfileConfigurationError struct {
|
|
err error
|
|
}
|
|
|
|
func (e *promptKitProfileConfigurationError) Error() string {
|
|
return "PromptKit profile configuration is invalid or unreadable"
|
|
}
|
|
|
|
func (e *promptKitProfileConfigurationError) Unwrap() error {
|
|
return e.err
|
|
}
|
|
|
|
func NewPromptKitProfileInspector(cfg PromptKitProfileInspectorConfig) (*PromptKitProfileInspector, error) {
|
|
_, sources, options, err := promptKitEngineConfiguration(cfg.Source, cfg.Assets)
|
|
if err != nil {
|
|
return nil, &promptKitProfileConfigurationError{err: err}
|
|
}
|
|
engine, err := promptkit.NewEngine(promptkit.Config{
|
|
Sources: sources,
|
|
}, options...)
|
|
if err != nil {
|
|
return nil, &promptKitProfileConfigurationError{err: err}
|
|
}
|
|
return &PromptKitProfileInspector{engine: engine}, nil
|
|
}
|
|
|
|
func (i *PromptKitProfileInspector) InspectProfile(ctx context.Context, profileID string) (PromptKitProfileInspection, error) {
|
|
if i == nil || i.engine == nil {
|
|
return PromptKitProfileInspection{}, fmt.Errorf("PromptKit profile inspector must not be nil")
|
|
}
|
|
profileID = strings.TrimSpace(profileID)
|
|
inspection, err := i.engine.InspectProfile(ctx, profileID)
|
|
if err != nil {
|
|
if ctxErr := ctx.Err(); ctxErr != nil {
|
|
return PromptKitProfileInspection{}, ctxErr
|
|
}
|
|
return PromptKitProfileInspection{}, &PromptKitProfileInspectionError{
|
|
ProfileID: profileID,
|
|
err: err,
|
|
}
|
|
}
|
|
return PromptKitProfileInspection{
|
|
ProfileID: inspection.ProfileID,
|
|
BackendID: strings.TrimSpace(inspection.EffectiveModelParams.BackendID),
|
|
Model: strings.TrimSpace(inspection.EffectiveModelParams.Model),
|
|
CredentialEnvironment: strings.TrimSpace(inspection.EffectiveModelParams.APIKeyEnv),
|
|
CredentialRequired: inspection.APIKeyRequired,
|
|
}, nil
|
|
}
|
|
|
|
func promptKitEngineConfiguration(cfg PromptKitProfileSourceConfig, assets *AssetRegistry) (PromptKitProfileSourceConfig, promptkit.ConfigurationSources, []promptkit.Option, error) {
|
|
profileSource, profileSources, err := promptKitProfileConfigurationSources(cfg)
|
|
if err != nil {
|
|
return PromptKitProfileSourceConfig{}, promptkit.ConfigurationSources{}, nil, err
|
|
}
|
|
sources := promptkit.ConfigurationSources{}
|
|
var options []promptkit.Option
|
|
if assets != nil {
|
|
maintainedSources, maintainedOptions, err := assets.PromptKitConfigurationSources()
|
|
if err != nil {
|
|
return PromptKitProfileSourceConfig{}, promptkit.ConfigurationSources{}, nil, err
|
|
}
|
|
sources = maintainedSources
|
|
options = append(options, maintainedOptions...)
|
|
}
|
|
sources.Underlays = append(sources.Underlays, profileSources.Underlays...)
|
|
return profileSource, sources, options, nil
|
|
}
|
|
|
|
func promptKitProfileConfigurationSources(cfg PromptKitProfileSourceConfig) (PromptKitProfileSourceConfig, promptkit.ConfigurationSources, error) {
|
|
source := PromptKitProfileSourceConfig{
|
|
ProfileDir: strings.TrimSpace(cfg.ProfileDir),
|
|
ProfileFile: strings.TrimSpace(cfg.ProfileFile),
|
|
}
|
|
if source.ProfileDir != "" && source.ProfileFile != "" {
|
|
return PromptKitProfileSourceConfig{}, promptkit.ConfigurationSources{}, fmt.Errorf("PromptKit profile_dir and profile_file are mutually exclusive")
|
|
}
|
|
if cfg.LocalBackend != nil {
|
|
localBackend := *cfg.LocalBackend
|
|
localBackend.Endpoint = strings.TrimSpace(localBackend.Endpoint)
|
|
source.LocalBackend = &localBackend
|
|
}
|
|
|
|
var underlays []configkit.Underlay
|
|
if source.ProfileFile != "" {
|
|
underlays = append(underlays, configkit.Underlay{Inputs: []configkit.Input{{
|
|
Source: configkit.File(source.ProfileFile),
|
|
Imports: true,
|
|
}}})
|
|
} else if source.ProfileDir != "" {
|
|
inputs, err := promptkit.DirectoryInputs(source.ProfileDir)
|
|
if err != nil {
|
|
return PromptKitProfileSourceConfig{}, promptkit.ConfigurationSources{}, fmt.Errorf("select PromptKit profile directory: %w", err)
|
|
}
|
|
for index := range inputs {
|
|
inputs[index].Imports = true
|
|
}
|
|
if len(inputs) != 0 {
|
|
underlays = append(underlays, configkit.Underlay{Inputs: inputs})
|
|
}
|
|
}
|
|
if source.LocalBackend != nil {
|
|
backend := promptkit.LocalBackend(source.LocalBackend.Endpoint, source.LocalBackend.ConcurrencyLimit)
|
|
underlays = append(underlays, configkit.Underlay{Inputs: []configkit.Input{{
|
|
Source: promptkit.BackendSource("notarius-local-backend", map[string]promptkit.BackendConfig{
|
|
promptkit.BackendLocal: backend,
|
|
}),
|
|
}}})
|
|
}
|
|
return source, promptkit.ConfigurationSources{Underlays: underlays}, nil
|
|
}
|