-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
134 lines (117 loc) · 3.68 KB
/
main.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"context"
"log"
"os"
"github.com/philippgille/chromem-go"
"gocloud.dev/blob"
_ "gocloud.dev/blob/s3blob"
)
func main() {
ctx := context.Background()
// As S3-style storage we use a local MinIO instance in this example. It has
// default credentials which we set in environment variables in order to be
// read when calling `blob.OpenBucket`, because that call implies using the
// AWS SDK default "shared config" loader.
// That loader checks the environment variables, but can also fall back to a
// file in `~/.aws/config` from `aws sso login` or similar.
// See https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html#specifying-credentials
// for details about credential loading.
err := os.Setenv("AWS_ACCESS_KEY_ID", "minioadmin")
if err != nil {
panic(err)
}
err = os.Setenv("AWS_SECRET_ACCESS_KEY", "minioadmin")
if err != nil {
panic(err)
}
// A region configuration is also required. Alternatively it can be passed in
// the connection string with "®ion=us-west-1" for example.
err = os.Setenv("AWS_DEFAULT_REGION", "us-west-1")
if err != nil {
panic(err)
}
// Export DB
err = exportDB(ctx)
if err != nil {
panic(err)
}
log.Println("Successfully exported DB to S3 storage.")
// Import DB
err = importDB(ctx)
if err != nil {
panic(err)
}
log.Println("Successfully imported DB from S3 storage.")
}
func exportDB(ctx context.Context) error {
// Create and fill DB
db := chromem.NewDB()
c, err := db.CreateCollection("knowledge-base", nil, nil)
if err != nil {
return err
}
err = c.AddDocument(ctx, chromem.Document{
ID: "1",
Content: "The sky is blue because of Rayleigh scattering.",
})
if err != nil {
return err
}
// Open S3 bucket. We're using a local MinIO instance here, but it can be any
// S3-compatible storage. We're also using the gocloud.dev/blob package instead
// of the AWS SDK for Go directly, because it provides a unified Writer/Reader
// API for different cloud storage providers.
bucket, err := blob.OpenBucket(ctx, "s3://mybucket?"+
"endpoint=localhost:9000&"+
"disableSSL=true&"+
"s3ForcePathStyle=true")
if err != nil {
return err
}
// Create writer to an S3 object
w, err := bucket.NewWriter(ctx, "chromem.gob.gz", nil)
if err != nil {
return err
}
// Instead of deferring w.Close() here, we close it at the end of the function
// to handle its errors, as the close is important for the actual write to happen
// and can lead to errors such as "The specified bucket does not exist" etc.
// Another option is to use a named return value and defer a function that
// overwrites the error with the close error or uses [errors.Join] or similar.
// Persist the DB to the S3 object
err = db.ExportToWriter(w, true, "")
if err != nil {
return err
}
return w.Close()
}
func importDB(ctx context.Context) error {
// Open S3 bucket. We're using a local MinIO instance here, but it can be any
// S3-compatible storage. We're also using the gocloud.dev/blob package instead
// of the AWS SDK for Go directly, because it provides a unified Writer/Reader
// API for different cloud storage providers.
bucket, err := blob.OpenBucket(ctx, "s3://mybucket?"+
"endpoint=localhost:9000&"+
"disableSSL=true&"+
"s3ForcePathStyle=true")
if err != nil {
return err
}
// Open reader to the S3 object
r, err := bucket.NewReader(ctx, "chromem.gob.gz", nil)
if err != nil {
return err
}
defer r.Close()
// Create empty DB
db := chromem.NewDB()
// Import the DB from the S3 object
err = db.ImportFromReader(r, "")
if err != nil {
return err
}
c := db.GetCollection("knowledge-base", nil)
log.Printf("Imported collection with %d documents\n", c.Count())
return nil
}