123 lines
5.5 KiB
Go
123 lines
5.5 KiB
Go
package format
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"gitea.maximumdirect.net/eric/promptkit"
|
|
)
|
|
|
|
type PromptInspection struct {
|
|
PromptID string `json:"prompt_id"`
|
|
PromptVersion string `json:"prompt_version"`
|
|
PromptHash string `json:"prompt_hash"`
|
|
DefaultProfileID string `json:"default_profile_id"`
|
|
Inputs []PromptInspectionInput `json:"inputs"`
|
|
OutputContract OutputContract `json:"output_contract"`
|
|
}
|
|
|
|
type PromptInspectionInput struct {
|
|
Name string `json:"name"`
|
|
Required bool `json:"required"`
|
|
ContentType string `json:"content_type"`
|
|
Description string `json:"description"`
|
|
}
|
|
|
|
type OutputContract struct {
|
|
Format string `json:"format"`
|
|
ValidationMode string `json:"validation_mode"`
|
|
SchemaPath string `json:"schema_path"`
|
|
RepairAttempts int `json:"repair_attempts"`
|
|
}
|
|
|
|
type ProfileInspection struct {
|
|
ProfileID string `json:"profile_id"`
|
|
EffectiveModelParams ProfileModelParams `json:"effective_model_params"`
|
|
APIKeyRequired bool `json:"api_key_required"`
|
|
}
|
|
|
|
type ProfileModelParams struct {
|
|
BackendID string `json:"backend_id"`
|
|
Endpoint string `json:"endpoint"`
|
|
Model string `json:"model"`
|
|
Temperature float64 `json:"temperature"`
|
|
MaxTokens int `json:"max_tokens"`
|
|
TopP float64 `json:"top_p"`
|
|
TimeoutSeconds int `json:"timeout_seconds"`
|
|
ServiceTier string `json:"service_tier"`
|
|
ReasoningEffort string `json:"reasoning_effort"`
|
|
APIKeyEnv string `json:"api_key_env"`
|
|
ExtraParams map[string]any `json:"extra_params"`
|
|
}
|
|
|
|
func FormatPromptInspection(value *promptkit.PromptInspection, outputFormat OutputFormat) ([]byte, error) {
|
|
if value == nil {
|
|
return nil, errors.New("prompt inspection is nil")
|
|
}
|
|
dto := PromptInspection{
|
|
PromptID: value.PromptID, PromptVersion: value.PromptVersion, PromptHash: value.PromptHash, DefaultProfileID: value.DefaultProfileID,
|
|
Inputs: make([]PromptInspectionInput, len(value.Inputs)),
|
|
OutputContract: OutputContract{Format: string(value.OutputContract.Format), ValidationMode: string(value.OutputContract.ValidationMode), SchemaPath: value.OutputContract.SchemaPath, RepairAttempts: value.OutputContract.RepairAttempts},
|
|
}
|
|
for i, input := range value.Inputs {
|
|
dto.Inputs[i] = PromptInspectionInput{Name: input.Name, Required: input.Required, ContentType: input.ContentType, Description: input.Description}
|
|
}
|
|
switch outputFormat {
|
|
case OutputFormatJSON:
|
|
data, err := json.MarshalIndent(dto, "", " ")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return append(data, '\n'), nil
|
|
case OutputFormatText:
|
|
var b bytes.Buffer
|
|
fmt.Fprintf(&b, "prompt_id: %s\nprompt_version: %s\nprompt_hash: %s\ndefault_profile_id: %s\ninputs:", dto.PromptID, dto.PromptVersion, dto.PromptHash, dto.DefaultProfileID)
|
|
if len(dto.Inputs) == 0 {
|
|
fmt.Fprintln(&b, " []")
|
|
} else {
|
|
fmt.Fprintln(&b)
|
|
for _, input := range dto.Inputs {
|
|
fmt.Fprintf(&b, " - name: %s\n required: %t\n content_type: %s\n description: %s\n", input.Name, input.Required, input.ContentType, strconv.Quote(input.Description))
|
|
}
|
|
}
|
|
fmt.Fprintf(&b, "output_contract:\n format: %s\n validation_mode: %s\n schema_path: %s\n repair_attempts: %d\n", dto.OutputContract.Format, dto.OutputContract.ValidationMode, dto.OutputContract.SchemaPath, dto.OutputContract.RepairAttempts)
|
|
return b.Bytes(), nil
|
|
default:
|
|
return nil, fmt.Errorf("%w: %q", ErrUnknownPreparedRunFormat, outputFormat)
|
|
}
|
|
}
|
|
|
|
func FormatProfileInspection(value *promptkit.ProfileInspection, outputFormat OutputFormat) ([]byte, error) {
|
|
if value == nil {
|
|
return nil, errors.New("profile inspection is nil")
|
|
}
|
|
target := value.EffectiveModelParams
|
|
params := map[string]any{}
|
|
for key, item := range target.ExtraParams {
|
|
params[key] = item
|
|
}
|
|
dto := ProfileInspection{ProfileID: value.ProfileID, APIKeyRequired: value.APIKeyRequired, EffectiveModelParams: ProfileModelParams{BackendID: target.BackendID, Endpoint: target.Endpoint, Model: target.Model, Temperature: target.Temperature, MaxTokens: target.MaxTokens, TopP: target.TopP, TimeoutSeconds: target.TimeoutSeconds, ServiceTier: target.ServiceTier, ReasoningEffort: target.ReasoningEffort, APIKeyEnv: target.APIKeyEnv, ExtraParams: params}}
|
|
switch outputFormat {
|
|
case OutputFormatJSON:
|
|
data, err := json.MarshalIndent(dto, "", " ")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return append(data, '\n'), nil
|
|
case OutputFormatText:
|
|
var b bytes.Buffer
|
|
fmt.Fprintf(&b, "profile_id: %s\neffective_model_params:\n backend_id: %s\n endpoint: %s\n model: %s\n temperature: %g\n max_tokens: %d\n top_p: %g\n timeout_seconds: %d\n service_tier: %s\n reasoning_effort: %s\n api_key_env: %s\n extra_params: ", dto.ProfileID, dto.EffectiveModelParams.BackendID, dto.EffectiveModelParams.Endpoint, dto.EffectiveModelParams.Model, dto.EffectiveModelParams.Temperature, dto.EffectiveModelParams.MaxTokens, dto.EffectiveModelParams.TopP, dto.EffectiveModelParams.TimeoutSeconds, dto.EffectiveModelParams.ServiceTier, dto.EffectiveModelParams.ReasoningEffort, dto.EffectiveModelParams.APIKeyEnv)
|
|
paramsJSON, err := json.Marshal(dto.EffectiveModelParams.ExtraParams)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
fmt.Fprintf(&b, "%s\napi_key_required: %t\n", paramsJSON, dto.APIKeyRequired)
|
|
return b.Bytes(), nil
|
|
default:
|
|
return nil, fmt.Errorf("%w: %q", ErrUnknownPreparedRunFormat, outputFormat)
|
|
}
|
|
}
|