...
1 package chromem
2
3 import (
4 "context"
5 "errors"
6 )
7
8
9 type Document struct {
10 ID string
11 Metadata map[string]string
12 Embedding []float32
13 Content string
14
15
16
17 }
18
19
20
21
22
23
24
25
26
27
28 func NewDocument(ctx context.Context, id string, metadata map[string]string, embedding []float32, content string, embeddingFunc EmbeddingFunc) (Document, error) {
29 if id == "" {
30 return Document{}, errors.New("id is empty")
31 }
32 if len(embedding) == 0 && content == "" {
33 return Document{}, errors.New("either embedding or content must be filled")
34 }
35 if embeddingFunc == nil {
36 embeddingFunc = NewEmbeddingFuncDefault()
37 }
38
39 if len(embedding) == 0 {
40 var err error
41 embedding, err = embeddingFunc(ctx, content)
42 if err != nil {
43 return Document{}, err
44 }
45 }
46
47 return Document{
48 ID: id,
49 Metadata: metadata,
50 Embedding: embedding,
51 Content: content,
52 }, nil
53 }
54
View as plain text