feat(matching): rank with semantic embeddings and explain matches by evidence
This commit is contained in:
1 parent
facc1789b1
commit
24b64d698a
215 files changed
+16951
-327
No files matched your search
@@ -156,6 +156,9 @@ type Config struct {
|
||||
// assignment. Reviewed source provenance is required separately.
|
||||
AssessmentDemoCategoriesEnabled bool
|
||||
|
||||
// Semantic configures semantic matching and repository checks.
|
||||
Semantic SemanticSettings
|
||||
|
||||
// CodeRunner configures the client of the own code runner. It is disabled
|
||||
// unless CODE_RUNNER_URL is set.
|
||||
CodeRunner CodeRunnerSettings
|
||||
@@ -254,6 +257,11 @@ func (config *Config) loadIdentityAndProxySettings(lookup LookupEnv) error {
|
||||
if err := config.loadAssessment(lookup); err != nil {
|
||||
return err
|
||||
}
|
||||
semantic, err := LoadSemanticSettings(lookup)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
config.Semantic = semantic
|
||||
codeRunner, err := LoadCodeRunnerSettings(lookup)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
// Semantic matching is off by default: a stand without the ML service
|
||||
// ranks with the neutral Sem, as before.
|
||||
defaultSemanticMatchingEnabled = false
|
||||
// defaultMLRequestTimeout bounds one embedding request. A whole card is at
|
||||
// most a dozen texts; on four CPU threads each takes well under a second.
|
||||
defaultMLRequestTimeout = 30 * time.Second
|
||||
maxMLRequestTimeout = 5 * time.Minute
|
||||
// After this many consecutive failures the client stops calling the ML
|
||||
// service for the open duration, so a dead service costs no timeouts.
|
||||
defaultMLCircuitFailures = 3
|
||||
maxMLCircuitFailures = 100
|
||||
defaultMLCircuitOpen = 30 * time.Second
|
||||
maxMLCircuitOpen = time.Hour
|
||||
// Repository checks call api.github.com; they are off unless enabled.
|
||||
defaultGitHubChecksEnabled = false
|
||||
defaultGitHubTimeout = 10 * time.Second
|
||||
maxGitHubTimeout = time.Minute
|
||||
// defaultRepositoryCheckInterval is the shortest time between two check
|
||||
// requests of one candidate through the refresh operation.
|
||||
defaultRepositoryCheckInterval = 10 * time.Minute
|
||||
maxRepositoryCheckInterval = 24 * time.Hour
|
||||
)
|
||||
|
||||
// SemanticSettings configure semantic matching and repository checks.
|
||||
type SemanticSettings struct {
|
||||
// SemanticMatchingEnabled schedules vector jobs on changes and runs them
|
||||
// against MLServiceURL. The search reads stored vectors either way.
|
||||
SemanticMatchingEnabled bool
|
||||
MLServiceURL string
|
||||
MLRequestTimeout time.Duration
|
||||
MLCircuitFailures int
|
||||
MLCircuitOpen time.Duration
|
||||
// GitHubChecksEnabled schedules and runs repository checks against the
|
||||
// public GitHub API.
|
||||
GitHubChecksEnabled bool
|
||||
// GitHubToken is an optional read-only token that raises the GitHub rate
|
||||
// limit. It is a secret: never log it.
|
||||
GitHubToken string
|
||||
GitHubTimeout time.Duration
|
||||
RepositoryCheckInterval time.Duration
|
||||
}
|
||||
|
||||
// LoadSemanticSettings reads the semantic settings; the operator commands
|
||||
// that need only them use it directly.
|
||||
func LoadSemanticSettings(lookup LookupEnv) (SemanticSettings, error) {
|
||||
settings := SemanticSettings{
|
||||
MLServiceURL: strings.TrimSpace(valueOrDefault(lookup, "ML_SERVICE_URL", "")),
|
||||
GitHubToken: strings.TrimSpace(valueOrDefault(lookup, "GITHUB_TOKEN", "")),
|
||||
}
|
||||
var err error
|
||||
if settings.SemanticMatchingEnabled, err = boolOrDefault(lookup, "SEMANTIC_MATCHING_ENABLED", defaultSemanticMatchingEnabled); err != nil {
|
||||
return SemanticSettings{}, err
|
||||
}
|
||||
if settings.MLRequestTimeout, err = durationOrDefault(lookup, "ML_REQUEST_TIMEOUT", defaultMLRequestTimeout); err != nil {
|
||||
return SemanticSettings{}, err
|
||||
}
|
||||
if settings.MLCircuitFailures, err = intOrDefault(lookup, "ML_CIRCUIT_FAILURES", defaultMLCircuitFailures); err != nil {
|
||||
return SemanticSettings{}, err
|
||||
}
|
||||
if settings.MLCircuitOpen, err = durationOrDefault(lookup, "ML_CIRCUIT_OPEN", defaultMLCircuitOpen); err != nil {
|
||||
return SemanticSettings{}, err
|
||||
}
|
||||
if settings.GitHubChecksEnabled, err = boolOrDefault(lookup, "GITHUB_REPOSITORY_CHECKS_ENABLED", defaultGitHubChecksEnabled); err != nil {
|
||||
return SemanticSettings{}, err
|
||||
}
|
||||
if settings.GitHubTimeout, err = durationOrDefault(lookup, "GITHUB_TIMEOUT", defaultGitHubTimeout); err != nil {
|
||||
return SemanticSettings{}, err
|
||||
}
|
||||
if settings.RepositoryCheckInterval, err = durationOrDefault(lookup, "REPOSITORY_CHECK_INTERVAL", defaultRepositoryCheckInterval); err != nil {
|
||||
return SemanticSettings{}, err
|
||||
}
|
||||
return settings, settings.Validate()
|
||||
}
|
||||
|
||||
// Validate checks the values even while the features are off, so enabling
|
||||
// them later cannot surface an old mistake.
|
||||
func (settings SemanticSettings) Validate() error {
|
||||
if err := settings.validateMLService(); err != nil {
|
||||
return err
|
||||
}
|
||||
return settings.validateLimits()
|
||||
}
|
||||
|
||||
func (settings SemanticSettings) validateMLService() error {
|
||||
const location = "SemanticSettings.Validate"
|
||||
if settings.SemanticMatchingEnabled && settings.MLServiceURL == "" {
|
||||
return errors.New(location + " - ML_SERVICE_URL is required when SEMANTIC_MATCHING_ENABLED is true")
|
||||
}
|
||||
if settings.MLServiceURL != "" {
|
||||
parsed, err := url.Parse(settings.MLServiceURL)
|
||||
if err != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") || parsed.Host == "" ||
|
||||
parsed.User != nil || parsed.RawQuery != "" || parsed.Fragment != "" || strings.Trim(parsed.Path, "/") != "" {
|
||||
return errors.New(location + " - ML_SERVICE_URL must be an http or https origin without credentials or path")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (settings SemanticSettings) validateLimits() error {
|
||||
const location = "SemanticSettings.Validate"
|
||||
switch {
|
||||
case settings.MLRequestTimeout <= 0 || settings.MLRequestTimeout > maxMLRequestTimeout:
|
||||
return errors.New(location + " - ML_REQUEST_TIMEOUT must be positive and at most 5m")
|
||||
case settings.MLCircuitFailures < 1 || settings.MLCircuitFailures > maxMLCircuitFailures:
|
||||
return errors.New(location + " - ML_CIRCUIT_FAILURES must be between 1 and 100")
|
||||
case settings.MLCircuitOpen <= 0 || settings.MLCircuitOpen > maxMLCircuitOpen:
|
||||
return errors.New(location + " - ML_CIRCUIT_OPEN must be positive and at most 1h")
|
||||
case settings.GitHubTimeout <= 0 || settings.GitHubTimeout > maxGitHubTimeout:
|
||||
return errors.New(location + " - GITHUB_TIMEOUT must be positive and at most 1m")
|
||||
case settings.RepositoryCheckInterval <= 0 || settings.RepositoryCheckInterval > maxRepositoryCheckInterval:
|
||||
return errors.New(location + " - REPOSITORY_CHECK_INTERVAL must be positive and at most 24h")
|
||||
case strings.ContainsAny(settings.GitHubToken, " \t\r\n"):
|
||||
return errors.New(location + " - GITHUB_TOKEN must not contain white space")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func lookupOf(values map[string]string) LookupEnv {
|
||||
return func(key string) (string, bool) {
|
||||
value, ok := values[key]
|
||||
return value, ok
|
||||
}
|
||||
}
|
||||
|
||||
func TestSemanticSettingsDefaultOffAndValidateTheMLOrigin(t *testing.T) {
|
||||
settings, err := LoadSemanticSettings(lookupOf(nil))
|
||||
if err != nil || settings.SemanticMatchingEnabled || settings.GitHubChecksEnabled ||
|
||||
settings.MLRequestTimeout != 30*time.Second || settings.RepositoryCheckInterval != 10*time.Minute {
|
||||
t.Fatalf("defaults = %+v, %v", settings, err)
|
||||
}
|
||||
enabled, err := LoadSemanticSettings(lookupOf(map[string]string{
|
||||
"SEMANTIC_MATCHING_ENABLED": "true", "ML_SERVICE_URL": "http://ml:8000", "GITHUB_TOKEN": "token-value",
|
||||
}))
|
||||
if err != nil || !enabled.SemanticMatchingEnabled || enabled.GitHubToken != "token-value" {
|
||||
t.Fatalf("enabled = %+v, %v", enabled, err)
|
||||
}
|
||||
for name, values := range map[string]map[string]string{
|
||||
"missing URL": {"SEMANTIC_MATCHING_ENABLED": "true"},
|
||||
"credentials": {"ML_SERVICE_URL": "http://user:secret@ml:8000"},
|
||||
"path": {"ML_SERVICE_URL": "http://ml:8000/v1"},
|
||||
"scheme": {"ML_SERVICE_URL": "file:///tmp/ml"},
|
||||
"timeout": {"ML_REQUEST_TIMEOUT": "0s"},
|
||||
"circuit": {"ML_CIRCUIT_FAILURES": "0"},
|
||||
"interval": {"REPOSITORY_CHECK_INTERVAL": "48h"},
|
||||
"token whitespace": {"GITHUB_TOKEN": "a b"},
|
||||
} {
|
||||
_, err := LoadSemanticSettings(lookupOf(values))
|
||||
if err == nil {
|
||||
t.Errorf("%s: invalid settings were accepted", name)
|
||||
continue
|
||||
}
|
||||
if strings.Contains(err.Error(), "secret") || strings.Contains(err.Error(), "a b") {
|
||||
t.Errorf("%s: the error quotes the value: %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user