|
| 1 | +package prompt_scorer |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/JudgmentLabs/judgeval-go/pkg/data" |
| 7 | + "github.com/JudgmentLabs/judgeval-go/pkg/env" |
| 8 | + "github.com/JudgmentLabs/judgeval-go/pkg/internal/api" |
| 9 | + "github.com/JudgmentLabs/judgeval-go/pkg/internal/api/models" |
| 10 | + "github.com/JudgmentLabs/judgeval-go/pkg/scorers" |
| 11 | +) |
| 12 | + |
| 13 | +type BasePromptScorer struct { |
| 14 | + *scorers.APIScorer |
| 15 | + prompt string |
| 16 | + options map[string]float64 |
| 17 | + judgmentAPIKey string |
| 18 | + organizationID string |
| 19 | +} |
| 20 | + |
| 21 | +func NewBasePromptScorer( |
| 22 | + scoreType data.APIScorerType, |
| 23 | + name string, |
| 24 | + prompt string, |
| 25 | + threshold float64, |
| 26 | + options map[string]float64, |
| 27 | + judgmentAPIKey string, |
| 28 | + organizationID string, |
| 29 | +) *BasePromptScorer { |
| 30 | + apiScorer := scorers.NewAPIScorer(scoreType, scorers.WithName(name), scorers.WithThreshold(threshold)) |
| 31 | + |
| 32 | + return &BasePromptScorer{ |
| 33 | + APIScorer: apiScorer, |
| 34 | + prompt: prompt, |
| 35 | + options: options, |
| 36 | + judgmentAPIKey: judgmentAPIKey, |
| 37 | + organizationID: organizationID, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func ScorerExists(name, judgmentAPIKey, organizationID string) (bool, error) { |
| 42 | + client := api.NewClient(env.JudgmentAPIURL, judgmentAPIKey, organizationID) |
| 43 | + request := &models.ScorerExistsRequest{ |
| 44 | + Name: name, |
| 45 | + } |
| 46 | + |
| 47 | + response, err := client.ScorerExists(request) |
| 48 | + if err != nil { |
| 49 | + return false, fmt.Errorf("failed to check if scorer exists: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + return response.Exists, nil |
| 53 | +} |
| 54 | + |
| 55 | +func FetchPromptScorer(name, judgmentAPIKey, organizationID string) (*models.PromptScorer, error) { |
| 56 | + client := api.NewClient(env.JudgmentAPIURL, judgmentAPIKey, organizationID) |
| 57 | + request := &models.FetchPromptScorersRequest{ |
| 58 | + Names: []string{name}, |
| 59 | + } |
| 60 | + |
| 61 | + response, err := client.FetchScorers(request) |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("failed to fetch prompt scorer '%s': %v", name, err) |
| 64 | + } |
| 65 | + |
| 66 | + if len(response.Scorers) == 0 { |
| 67 | + return nil, fmt.Errorf("failed to fetch prompt scorer '%s': not found", name) |
| 68 | + } |
| 69 | + |
| 70 | + return &response.Scorers[0], nil |
| 71 | +} |
| 72 | + |
| 73 | +func PushPromptScorer( |
| 74 | + name string, |
| 75 | + prompt string, |
| 76 | + threshold float64, |
| 77 | + options map[string]float64, |
| 78 | + judgmentAPIKey string, |
| 79 | + organizationID string, |
| 80 | + isTrace bool, |
| 81 | +) (string, error) { |
| 82 | + client := api.NewClient(env.JudgmentAPIURL, judgmentAPIKey, organizationID) |
| 83 | + |
| 84 | + apiOptions := make(map[string]interface{}) |
| 85 | + for k, v := range options { |
| 86 | + apiOptions[k] = v |
| 87 | + } |
| 88 | + |
| 89 | + request := &models.SavePromptScorerRequest{ |
| 90 | + Name: name, |
| 91 | + Prompt: prompt, |
| 92 | + Threshold: threshold, |
| 93 | + Options: apiOptions, |
| 94 | + IsTrace: isTrace, |
| 95 | + } |
| 96 | + |
| 97 | + response, err := client.SaveScorer(request) |
| 98 | + if err != nil { |
| 99 | + return "", fmt.Errorf("failed to save prompt scorer: %v", err) |
| 100 | + } |
| 101 | + |
| 102 | + if response != nil { |
| 103 | + return response.Name, nil |
| 104 | + } |
| 105 | + return "", nil |
| 106 | +} |
| 107 | + |
| 108 | +func (bps *BasePromptScorer) GetPrompt() string { |
| 109 | + return bps.prompt |
| 110 | +} |
| 111 | + |
| 112 | +func (bps *BasePromptScorer) GetOptions() map[string]float64 { |
| 113 | + if bps.options == nil { |
| 114 | + return nil |
| 115 | + } |
| 116 | + result := make(map[string]float64) |
| 117 | + for k, v := range bps.options { |
| 118 | + result[k] = v |
| 119 | + } |
| 120 | + return result |
| 121 | +} |
0 commit comments