@@ -4,11 +4,9 @@ import (
4
4
"context"
5
5
"fmt"
6
6
"log"
7
- "os"
8
7
9
8
chromago "github.com/amikos-tech/chroma-go"
10
- "github.com/amikos-tech/chroma-go/openai"
11
- chromaopenapi "github.com/amikos-tech/chroma-go/swagger"
9
+ "github.com/amikos-tech/chroma-go/types"
12
10
13
11
"github.com/testcontainers/testcontainers-go"
14
12
"github.com/testcontainers/testcontainers-go/modules/chroma"
@@ -18,7 +16,7 @@ func ExampleRunContainer() {
18
16
// runChromaContainer {
19
17
ctx := context .Background ()
20
18
21
- chromaContainer , err := chroma .RunContainer (ctx , testcontainers .WithImage ("chromadb/chroma:0.4.22 " ))
19
+ chromaContainer , err := chroma .RunContainer (ctx , testcontainers .WithImage ("chromadb/chroma:0.4.24 " ))
22
20
if err != nil {
23
21
log .Fatalf ("failed to start container: %s" , err )
24
22
}
@@ -46,7 +44,7 @@ func ExampleChromaContainer_connectWithClient() {
46
44
// createClient {
47
45
ctx := context .Background ()
48
46
49
- chromaContainer , err := chroma .RunContainer (ctx , testcontainers .WithImage ("chromadb/chroma:0.4.22 " ))
47
+ chromaContainer , err := chroma .RunContainer (ctx , testcontainers .WithImage ("chromadb/chroma:0.4.24 " ))
50
48
if err != nil {
51
49
log .Fatalf ("failed to start container: %s" , err )
52
50
}
@@ -58,21 +56,13 @@ func ExampleChromaContainer_connectWithClient() {
58
56
}
59
57
}()
60
58
61
- connectionStr , err := chromaContainer .RESTEndpoint (ctx )
59
+ endpoint , err := chromaContainer .RESTEndpoint (context . Background () )
62
60
if err != nil {
63
61
log .Fatalf ("failed to get REST endpoint: %s" , err ) // nolint:gocritic
64
62
}
65
-
66
- // create the client connection and confirm that we can access the server with it
67
- configuration := chromaopenapi .NewConfiguration ()
68
- configuration .Servers = chromaopenapi.ServerConfigurations {
69
- {
70
- URL : connectionStr ,
71
- Description : "Chromadb server url for this store" ,
72
- },
73
- }
74
- chromaClient := & chromago.Client {
75
- ApiClient : chromaopenapi .NewAPIClient (configuration ),
63
+ chromaClient , err := chromago .NewClient (endpoint )
64
+ if err != nil {
65
+ log .Fatalf ("failed to get client: %s" , err ) // nolint:gocritic
76
66
}
77
67
78
68
hbs , errHb := chromaClient .Heartbeat (context .Background ())
@@ -91,7 +81,7 @@ func ExampleChromaContainer_connectWithClient() {
91
81
func ExampleChromaContainer_collections () {
92
82
ctx := context .Background ()
93
83
94
- chromaContainer , err := chroma .RunContainer (ctx , testcontainers .WithImage ("chromadb/chroma:0.4.22" ))
84
+ chromaContainer , err := chroma .RunContainer (ctx , testcontainers .WithImage ("chromadb/chroma:0.4.24" ), testcontainers . WithEnv ( map [ string ] string { "ALLOW_RESET" : "true" } ))
95
85
if err != nil {
96
86
log .Fatalf ("failed to start container: %s" , err )
97
87
}
@@ -102,51 +92,66 @@ func ExampleChromaContainer_collections() {
102
92
}
103
93
}()
104
94
105
- connectionStr , err := chromaContainer .RESTEndpoint (ctx )
95
+ // getClient {
96
+ // create the client connection and confirm that we can access the server with it
97
+ endpoint , err := chromaContainer .RESTEndpoint (context .Background ())
106
98
if err != nil {
107
99
log .Fatalf ("failed to get REST endpoint: %s" , err ) // nolint:gocritic
108
100
}
109
-
110
- // create the client connection and confirm that we can access the server with it
111
- configuration := chromaopenapi .NewConfiguration ()
112
- configuration .Servers = chromaopenapi.ServerConfigurations {
113
- {
114
- URL : connectionStr ,
115
- Description : "Chromadb server url for this store" ,
116
- },
101
+ chromaClient , err := chromago .NewClient (endpoint )
102
+ // }
103
+ if err != nil {
104
+ log .Fatalf ("failed to get client: %s" , err ) // nolint:gocritic
117
105
}
118
- chromaClient := & chromago.Client {
119
- ApiClient : chromaopenapi .NewAPIClient (configuration ),
106
+ // reset {
107
+ reset , err := chromaClient .Reset (context .Background ())
108
+ // }
109
+ if err != nil {
110
+ log .Fatalf ("failed to reset: %s" , err ) // nolint:gocritic
120
111
}
112
+ fmt .Printf ("Reset successful: %v\n " , reset )
121
113
122
114
// createCollection {
123
- // for testing purposes, the OPENAI_API_KEY environment variable can be empty
124
- // therefore this test is expected to succeed even though the API key is not set.
125
- embeddingFunction := openai .NewOpenAIEmbeddingFunction (os .Getenv ("OPENAI_API_KEY" ))
126
- distanceFunction := chromago .L2
127
-
128
- col , err := chromaClient .CreateCollection (context .Background (), "test-collection" , map [string ]any {}, true , embeddingFunction , distanceFunction )
115
+ // for testing we use a dummy hashing function NewConsistentHashEmbeddingFunction
116
+ col , err := chromaClient .CreateCollection (context .Background (), "test-collection" , map [string ]any {}, true , types .NewConsistentHashEmbeddingFunction (), types .L2 )
129
117
// }
130
118
if err != nil {
131
119
log .Fatalf ("failed to create collection: %s" , err ) // nolint:gocritic
132
120
}
133
121
134
122
fmt .Println ("Collection created:" , col .Name )
135
123
124
+ // addData {
136
125
// verify it's possible to add data to the collection
137
126
col1 , err := col .Add (
138
127
context .Background (),
139
- [][] float32 {{ 1 , 2 , 3 }, { 4 , 5 , 6 }},
140
- []map [string ]interface {}{},
141
- []string {"test-doc-1" , "test-doc-2" },
142
- []string {"test-label-1" , "test-label-2" },
128
+ nil , // embeddings
129
+ []map [string ]interface {}{}, // metadata
130
+ []string {"test-doc-1" , "test-doc-2" }, // documents
131
+ []string {"test-label-1" , "test-label-2" }, // ids
143
132
)
133
+ // }
144
134
if err != nil {
145
135
log .Fatalf ("failed to add data to collection: %s" , err ) // nolint:gocritic
146
136
}
147
137
148
138
fmt .Println (col1 .Count (context .Background ()))
149
139
140
+ // queryCollection {
141
+ // verify it's possible to query the collection
142
+ queryResults , err := col1 .QueryWithOptions (
143
+ context .Background (),
144
+ types .WithQueryTexts ([]string {"test-doc-1" }),
145
+ types .WithInclude (types .IDocuments , types .IEmbeddings , types .IMetadatas ),
146
+ types .WithNResults (1 ),
147
+ )
148
+ // }
149
+ if err != nil {
150
+ log .Fatalf ("failed to query collection: %s" , err ) // nolint:gocritic
151
+ }
152
+
153
+ fmt .Printf ("Result of query: %v\n " , queryResults )
154
+
150
155
// listCollections {
151
156
cols , err := chromaClient .ListCollections (context .Background ())
152
157
// }
@@ -166,8 +171,10 @@ func ExampleChromaContainer_collections() {
166
171
fmt .Println (err )
167
172
168
173
// Output:
174
+ // Reset successful: true
169
175
// Collection created: test-collection
170
176
// 2 <nil>
177
+ // Result of query: &{[[test-doc-1]] [[test-label-1]] [[map[]]] []}
171
178
// 1
172
179
// <nil>
173
180
}
0 commit comments