-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweaviate.go
63 lines (55 loc) · 1.66 KB
/
weaviate.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
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
// Utilities for working with Weaviate.
import (
"cmp"
"context"
"fmt"
"os"
"github.com/weaviate/weaviate-go-client/v4/weaviate"
"github.com/weaviate/weaviate/entities/models"
)
// initWeaviate initializes a weaviate client for our application.
func initWeaviate(ctx context.Context) (*weaviate.Client, error) {
client, err := weaviate.NewClient(weaviate.Config{
Host: "localhost:" + cmp.Or(os.Getenv("WVPORT"), "9035"),
Scheme: "http",
})
if err != nil {
return nil, fmt.Errorf("initializing weaviate: %w", err)
}
// Create a new class (collection) in weaviate if it doesn't exist yet.
cls := &models.Class{
Class: "Document",
Vectorizer: "none",
}
exists, err := client.Schema().ClassExistenceChecker().WithClassName(cls.Class).Do(ctx)
if err != nil {
return nil, fmt.Errorf("weaviate error: %w", err)
}
if !exists {
err = client.Schema().ClassCreator().WithClass(cls).Do(ctx)
if err != nil {
return nil, fmt.Errorf("weaviate error: %w", err)
}
}
return client, nil
}
// combinedWeaviateError generates an error if err is non-nil or result has
// errors, and returns an error (or nil if there's no error). It's useful for
// the results of the Weaviate GraphQL API's "Do" calls.
func combinedWeaviateError(result *models.GraphQLResponse, err error) error {
if err != nil {
return err
}
if len(result.Errors) != 0 {
var ss []string
for _, e := range result.Errors {
ss = append(ss, e.Message)
}
return fmt.Errorf("weaviate error: %v", ss)
}
return nil
}