82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"go.maximumdirect.net/configkit"
|
|
"go.maximumdirect.net/promptkit"
|
|
)
|
|
|
|
type deterministicClient struct{}
|
|
|
|
func (deterministicClient) Generate(
|
|
ctx context.Context,
|
|
_ promptkit.GenerateRequest,
|
|
) (*promptkit.GenerateResponse, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &promptkit.GenerateResponse{
|
|
Content: "Ada finished the migration review.",
|
|
Usage: promptkit.TokenUsage{
|
|
PromptTokens: 12,
|
|
CompletionTokens: 6,
|
|
TotalTokens: 18,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
type summary struct {
|
|
Output string `json:"output"`
|
|
ValidationStatus promptkit.ValidationStatus `json:"validation_status"`
|
|
IsValid bool `json:"is_valid"`
|
|
Model string `json:"model"`
|
|
TotalTokens int `json:"total_tokens"`
|
|
}
|
|
|
|
func main() {
|
|
if err := run(context.Background(), "examples/go-library/run", os.Stdout); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run(ctx context.Context, exampleDir string, out io.Writer) error {
|
|
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", []promptkit.ProfileConfig{
|
|
{ID: "offline-example", Endpoint: &endpoint, Model: &model},
|
|
})}}}},
|
|
Main: &configkit.Input{Source: configkit.File(filepath.Join(exampleDir, "prompt.yaml"))},
|
|
}}, promptkit.WithLLMClient(deterministicClient{}))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
result, err := engine.Run(ctx, promptkit.RunRequest{
|
|
PromptID: "example.run",
|
|
Inputs: map[string]promptkit.ArtifactRef{
|
|
"note": promptkit.Inline("Ada finished the migration review."),
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
encoder := json.NewEncoder(out)
|
|
encoder.SetIndent("", " ")
|
|
return encoder.Encode(summary{
|
|
Output: result.RawOutput,
|
|
ValidationStatus: result.Validation.Status,
|
|
IsValid: result.Validation.IsValid,
|
|
Model: result.ModelName,
|
|
TotalTokens: result.Usage.TotalTokens,
|
|
})
|
|
}
|