82 lines
2.3 KiB
Go
82 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
|
|
"gitea.maximumdirect.net/eric/ConfigKit"
|
|
"gitea.maximumdirect.net/eric/promptkit"
|
|
)
|
|
|
|
type summary struct {
|
|
PromptID string `json:"prompt_id"`
|
|
PromptVersion string `json:"prompt_version"`
|
|
SelectedProfile string `json:"selected_profile"`
|
|
Model string `json:"model"`
|
|
MessageCount int `json:"message_count"`
|
|
}
|
|
|
|
func main() {
|
|
endpoint, model := "https://example.invalid/v1", "offline-model"
|
|
engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{
|
|
Underlays: []configkit.Underlay{{Inputs: []configkit.Input{{Source: promptkit.ProfileSource("example profile", map[string]promptkit.ProfileConfig{
|
|
"offline-example": {Endpoint: &endpoint, Model: &model},
|
|
})}}}},
|
|
Main: &configkit.Input{Source: configkit.File("examples/go-library/prepare/prompt.yaml")},
|
|
}})
|
|
if err != nil {
|
|
exit(err)
|
|
}
|
|
view, err := engine.InspectConfiguration()
|
|
if err != nil {
|
|
exit(err)
|
|
}
|
|
if _, found := view.Explain(configkit.Keys("profiles", "offline-example", "model")); !found {
|
|
exit(fmt.Errorf("configured profile model was not found"))
|
|
}
|
|
requirements, err := engine.InspectRequirements(context.Background(), promptkit.RequirementInspectionRequest{
|
|
PromptID: "example.prepare",
|
|
})
|
|
if err != nil {
|
|
exit(err)
|
|
}
|
|
if !requirements.Complete || len(requirements.Inputs) != 1 ||
|
|
requirements.Inputs[0].Name != "note" ||
|
|
requirements.Inputs[0].Kind != promptkit.RequirementRequired {
|
|
exit(fmt.Errorf("required note input was not discovered before preparation"))
|
|
}
|
|
|
|
request := promptkit.RunRequest{
|
|
PromptID: "example.prepare",
|
|
Inputs: map[string]promptkit.ArtifactRef{
|
|
"note": promptkit.Inline("Ada finished the migration review."),
|
|
},
|
|
}
|
|
if err := engine.ValidateRequest(context.Background(), request); err != nil {
|
|
exit(err)
|
|
}
|
|
prepared, err := engine.Prepare(context.Background(), request)
|
|
if err != nil {
|
|
exit(err)
|
|
}
|
|
|
|
encoder := json.NewEncoder(os.Stdout)
|
|
encoder.SetIndent("", " ")
|
|
if err := encoder.Encode(summary{
|
|
PromptID: prepared.PromptID,
|
|
PromptVersion: prepared.PromptVersion,
|
|
SelectedProfile: prepared.SelectedProfileID,
|
|
Model: prepared.EffectiveModelParams.Model,
|
|
MessageCount: len(prepared.Messages),
|
|
}); err != nil {
|
|
exit(err)
|
|
}
|
|
}
|
|
|
|
func exit(err error) {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|