Debian Codegraph @

examples/multimodal/main.go

raw
1 package main
2
3 import (
4 "context"
5 "encoding/base64"
6 "fmt"
7 "io"
8 "os"
9
10 "github.com/anthropics/anthropic-sdk-go"
11 )
12
13 func main() {
14 client := anthropic.NewClient()
15
16 content := "How many dogs are in this picture?"
17
18 println("[user]: " + content)
19
20 file, err := os.Open("./multimodal/nine_dogs.png")
21 if err != nil {
22 panic(fmt.Errorf("failed to open file: you should run this example from the root of the anthropic-go/examples directory: %w", err))
23 }
24 fileBytes, err := io.ReadAll(file)
25 if err != nil {
26 panic(err)
27 }
28 fileEncoded := base64.StdEncoding.EncodeToString(fileBytes)
29
30 message, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{
31 MaxTokens: 1024,
32 Messages: []anthropic.MessageParam{
33 anthropic.NewUserMessage(
34 anthropic.NewTextBlock(content),
35 anthropic.NewImageBlockBase64("image/png", fileEncoded),
36 ),
37 },
38 Model: anthropic.ModelClaudeSonnet4_5_20250929,
39 StopSequences: []string{"```\n"},
40 })
41 if err != nil {
42 panic(err)
43 }
44
45 println("[assistant]: " + message.Content[0].Text + message.StopSequence)
46 }