-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
71 lines (60 loc) · 1.58 KB
/
main.go
File metadata and controls
71 lines (60 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Demo: writ end-to-end flow.
//
// Opens a Merkle audit chain, makes one LLM call through the writ gate,
// verifies the chain, and prints the result.
//
// Usage:
//
// ANTHROPIC_API_KEY=sk-ant-... go run ./examples/demo/
package main
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"github.com/anthropics/anthropic-sdk-go"
"github.com/opskernel-io/writ"
)
func main() {
if os.Getenv("ANTHROPIC_API_KEY") == "" {
log.Fatal("ANTHROPIC_API_KEY is not set")
}
chainPath := filepath.Join(os.TempDir(), "writ-demo-chain.jsonl")
// Locate the policy directory bundled with this demo.
_, thisFile, _, _ := runtime.Caller(0)
policyDir := filepath.Join(filepath.Dir(thisFile), "policy")
client, err := writ.New(writ.Config{
PolicyPath: policyDir,
AuditPath: chainPath,
CallerID: "writ-demo",
})
if err != nil {
log.Fatalf("writ.New: %v", err)
}
fmt.Printf("chain: %s\n", chainPath)
msg, err := client.Messages.New(context.Background(), anthropic.MessageNewParams{
Model: anthropic.ModelClaudeHaiku4_5,
MaxTokens: 64,
Messages: []anthropic.MessageParam{
anthropic.NewUserMessage(anthropic.NewTextBlock("Reply with exactly: writ demo ok")),
},
})
if err != nil {
log.Fatalf("Messages.New: %v", err)
}
if len(msg.Content) > 0 {
fmt.Printf("response: %s\n", msg.Content[0].Text)
}
result, err := client.VerifyFull()
if err != nil {
log.Fatalf("VerifyFull: %v", err)
}
if result.Valid {
fmt.Printf("chain verified: %d entries, root=%s\n", result.EntryCount, result.RootHash[:12])
} else {
fmt.Printf("chain INVALID\n")
os.Exit(1)
}
}