Skip to content

Commit 1b14d25

Browse files
committed
feat: Added GetClient utility method
- Updated/Added a few examples - Updated docs
1 parent e0077e9 commit 1b14d25

File tree

6 files changed

+78
-32
lines changed

6 files changed

+78
-32
lines changed

docs/modules/chroma.md

+18-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ Since testcontainers-go <a href="https://github.com/testcontainers/testcontainer
66

77
The Testcontainers module for Chroma.
88

9+
## Resources
10+
11+
- [Chroma Docs](https://docs.trychroma.com/getting-started) - Chroma official documentation.
12+
- [Chroma Cookbook](http://cookbook.chromadb.dev) - Community-driven Chroma cookbook.
13+
914
## Adding this module to your project dependencies
1015

1116
Please run the following command to add the Chroma module to your Go dependencies:
@@ -38,7 +43,7 @@ When starting the Chroma container, you can pass options in a variadic way to co
3843
#### Image
3944

4045
If you need to set a different Chroma Docker image, you can use `testcontainers.WithImage` with a valid Docker image
41-
for Chroma. E.g. `testcontainers.WithImage("chromadb/chroma:0.4.22.dev44")`.
46+
for Chroma. E.g. `testcontainers.WithImage("chromadb/chroma:0.4.24")`.
4247

4348
{% include "../features/common_functional_options.md" %}
4449

@@ -54,6 +59,14 @@ This method returns the REST endpoint of the Chroma container, using the default
5459
[Get REST endpoint](../../modules/chroma/chroma_test.go) inside_block:restEndpoint
5560
<!--/codeinclude-->
5661

62+
#### GetClient
63+
64+
This method returns a `chroma-go` client that can be used to interact with the Chroma container.
65+
66+
<!--codeinclude-->
67+
[GetClient](../../modules/chroma/chroma_test.go) inside_block:getClient
68+
<!--/codeinclude-->
69+
5770
## Examples
5871

5972
### Getting a Chroma client
@@ -65,20 +78,22 @@ First of all, you need to import the Chroma module and the Swagger client:
6578
```golang
6679
import (
6780
chromago "github.com/amikos-tech/chroma-go"
68-
chromaopenapi "github.com/amikos-tech/chroma-go/swagger"
81+
"github.com/amikos-tech/chroma-go/types"
6982
)
7083
```
7184

7285
Then, you can create a Chroma client using the Chroma module:
7386

7487
<!--codeinclude-->
75-
[Get the client](../../modules/chroma/examples_test.go) inside_block:createClient
88+
[Get the client](../../modules/chroma/examples_test.go) inside_block:getClient
7689
<!--/codeinclude-->
7790

7891
### Working with Collections
7992

8093
<!--codeinclude-->
8194
[Create Collection](../../modules/chroma/examples_test.go) inside_block:createCollection
8295
[List Collections](../../modules/chroma/examples_test.go) inside_block:listCollections
96+
[Add Data to Collection](../../modules/chroma/examples_test.go) inside_block:addData
97+
[Query Collection](../../modules/chroma/examples_test.go) inside_block:queryCollection
8398
[Delete Collection](../../modules/chroma/examples_test.go) inside_block:deleteCollection
8499
<!--/codeinclude-->

modules/chroma/chroma.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package chroma
33
import (
44
"context"
55
"fmt"
6+
67
chromago "github.com/amikos-tech/chroma-go"
78

89
"github.com/testcontainers/testcontainers-go"
@@ -17,7 +18,7 @@ type ChromaContainer struct {
1718
// RunContainer creates an instance of the Chroma container type
1819
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*ChromaContainer, error) {
1920
req := testcontainers.ContainerRequest{
20-
Image: "chromadb/chroma:0.4.22",
21+
Image: "chromadb/chroma:0.4.24",
2122
ExposedPorts: []string{"8000/tcp"},
2223
WaitingFor: wait.ForAll(
2324
wait.ForListeningPort("8000/tcp"),
@@ -60,11 +61,10 @@ func (c *ChromaContainer) RESTEndpoint(ctx context.Context) (string, error) {
6061
return fmt.Sprintf("http://%s:%s", host, containerPort.Port()), nil
6162
}
6263

63-
func (c *ChromaContainer) GetClient(opt chromago.ClientOption) (*chromago.Client, error) {
64+
func (c *ChromaContainer) GetClient(opt ...chromago.ClientOption) (*chromago.Client, error) {
6465
endpoint, err := c.RESTEndpoint(context.Background())
6566
if err != nil {
6667
return nil, err
6768
}
68-
return chromago.NewClient(endpoint, opt)
69-
69+
return chromago.NewClient(endpoint, opt...)
7070
}

modules/chroma/chroma_test.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ import (
55
"net/http"
66
"testing"
77

8+
chromago "github.com/amikos-tech/chroma-go"
9+
"github.com/stretchr/testify/require"
10+
811
"github.com/testcontainers/testcontainers-go"
912
"github.com/testcontainers/testcontainers-go/modules/chroma"
1013
)
1114

1215
func TestChroma(t *testing.T) {
1316
ctx := context.Background()
1417

15-
container, err := chroma.RunContainer(ctx, testcontainers.WithImage("chromadb/chroma:0.4.22.dev44"))
18+
container, err := chroma.RunContainer(ctx, testcontainers.WithImage("chromadb/chroma:0.4.24"))
1619
if err != nil {
1720
t.Fatal(err)
1821
}
@@ -43,4 +46,17 @@ func TestChroma(t *testing.T) {
4346
tt.Fatalf("unexpected status code: %d", resp.StatusCode)
4447
}
4548
})
49+
50+
t.Run("GetClient", func(tt *testing.T) {
51+
// restEndpoint {
52+
chromaClient, err := container.GetClient(chromago.WithDebug(true))
53+
// }
54+
if err != nil {
55+
tt.Fatalf("failed to get REST endpoint: %s", err)
56+
}
57+
58+
hb, err := chromaClient.Heartbeat(context.TODO())
59+
require.NoError(tt, err)
60+
require.NotNil(tt, hb["nanosecond heartbeat"])
61+
})
4662
}

modules/chroma/examples_test.go

+24-22
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package chroma_test
33
import (
44
"context"
55
"fmt"
6+
"log"
7+
68
chromago "github.com/amikos-tech/chroma-go"
7-
chromaopenapi "github.com/amikos-tech/chroma-go/swagger"
89
"github.com/amikos-tech/chroma-go/types"
10+
911
"github.com/testcontainers/testcontainers-go"
1012
"github.com/testcontainers/testcontainers-go/modules/chroma"
11-
"log"
1213
)
1314

1415
func ExampleRunContainer() {
@@ -55,21 +56,9 @@ func ExampleChromaContainer_connectWithClient() {
5556
}
5657
}()
5758

58-
connectionStr, err := chromaContainer.RESTEndpoint(ctx)
59+
chromaClient, err := chromaContainer.GetClient()
5960
if err != nil {
60-
log.Fatalf("failed to get REST endpoint: %s", err) // nolint:gocritic
61-
}
62-
63-
// create the client connection and confirm that we can access the server with it
64-
configuration := chromaopenapi.NewConfiguration()
65-
configuration.Servers = chromaopenapi.ServerConfigurations{
66-
{
67-
URL: connectionStr,
68-
Description: "Chromadb server url for this store",
69-
},
70-
}
71-
chromaClient := &chromago.Client{
72-
ApiClient: chromaopenapi.NewAPIClient(configuration),
61+
log.Fatalf("failed to get client: %s", err) // nolint:gocritic
7362
}
7463

7564
hbs, errHb := chromaClient.Heartbeat(context.Background())
@@ -88,7 +77,7 @@ func ExampleChromaContainer_connectWithClient() {
8877
func ExampleChromaContainer_collections() {
8978
ctx := context.Background()
9079

91-
chromaContainer, err := chroma.RunContainer(ctx, testcontainers.WithImage("chromadb/chroma:0.4.24"))
80+
chromaContainer, err := chroma.RunContainer(ctx, testcontainers.WithImage("chromadb/chroma:0.4.24"), testcontainers.WithEnv(map[string]string{"ALLOW_RESET": "true"}))
9281
if err != nil {
9382
log.Fatalf("failed to start container: %s", err)
9483
}
@@ -99,8 +88,20 @@ func ExampleChromaContainer_collections() {
9988
}
10089
}()
10190

91+
// getClient {
10292
// create the client connection and confirm that we can access the server with it
10393
chromaClient, err := chromaContainer.GetClient(chromago.WithDebug(true))
94+
// }
95+
if err != nil {
96+
log.Fatalf("failed to get client: %s", err) // nolint:gocritic
97+
}
98+
// reset {
99+
reset, err := chromaClient.Reset(context.Background())
100+
// }
101+
if err != nil {
102+
log.Fatalf("failed to reset: %s", err) // nolint:gocritic
103+
}
104+
fmt.Printf("Reset successful: %v\n", reset)
104105

105106
// createCollection {
106107
// for testing we use a dummy hashing function NewConsistentHashEmbeddingFunction
@@ -116,10 +117,10 @@ func ExampleChromaContainer_collections() {
116117
// verify it's possible to add data to the collection
117118
col1, err := col.Add(
118119
context.Background(),
119-
types.NewEmbeddingsFromFloat32([][]float32{{1, 2, 3}, {4, 5, 6}}), // or set this to nil to use the EmbeddingFunction
120-
[]map[string]interface{}{}, // metadata
121-
[]string{"test-doc-1", "test-doc-2"}, // documents
122-
[]string{"test-label-1", "test-label-2"}, //ids
120+
nil, // or set the embedding types.NewEmbeddingsFromFloat32([][]float32{{1, 2, 3}, {4, 5, 6}})
121+
[]map[string]interface{}{}, // metadata
122+
[]string{"test-doc-1", "test-doc-2"}, // documents
123+
[]string{"test-label-1", "test-label-2"}, // ids
123124
)
124125
// }
125126
if err != nil {
@@ -140,7 +141,6 @@ func ExampleChromaContainer_collections() {
140141
if err != nil {
141142
log.Fatalf("failed to query collection: %s", err) // nolint:gocritic
142143
}
143-
// }
144144

145145
fmt.Printf("Result of query: %v\n", queryResults)
146146

@@ -163,8 +163,10 @@ func ExampleChromaContainer_collections() {
163163
fmt.Println(err)
164164

165165
// Output:
166+
// Reset successful: true
166167
// Collection created: test-collection
167168
// 2 <nil>
169+
// Result of query: &{[[test-doc-1]] [[test-label-1]] [[map[]]] []}
168170
// 1
169171
// <nil>
170172
}

modules/chroma/go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.21
44

55
require (
66
github.com/amikos-tech/chroma-go v0.1.2
7+
github.com/stretchr/testify v1.9.0
78
github.com/testcontainers/testcontainers-go v0.29.1
89
)
910

@@ -17,6 +18,7 @@ require (
1718
github.com/containerd/containerd v1.7.12 // indirect
1819
github.com/containerd/log v0.1.0 // indirect
1920
github.com/cpuguy83/dockercfg v0.3.1 // indirect
21+
github.com/davecgh/go-spew v1.1.1 // indirect
2022
github.com/distribution/reference v0.5.0 // indirect
2123
github.com/docker/docker v25.0.3+incompatible // indirect
2224
github.com/docker/go-connections v0.5.0 // indirect
@@ -29,6 +31,7 @@ require (
2931
github.com/golang/protobuf v1.5.3 // indirect
3032
github.com/google/uuid v1.6.0 // indirect
3133
github.com/klauspost/compress v1.16.0 // indirect
34+
github.com/kr/pretty v0.3.1 // indirect
3235
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
3336
github.com/magiconair/properties v1.8.7 // indirect
3437
github.com/moby/patternmatcher v0.6.0 // indirect
@@ -40,6 +43,7 @@ require (
4043
github.com/opencontainers/go-digest v1.0.0 // indirect
4144
github.com/opencontainers/image-spec v1.1.0 // indirect
4245
github.com/pkg/errors v0.9.1 // indirect
46+
github.com/pmezard/go-difflib v1.0.0 // indirect
4347
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
4448
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
4549
github.com/shoenig/go-m1cpu v0.1.6 // indirect
@@ -58,6 +62,7 @@ require (
5862
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
5963
google.golang.org/grpc v1.58.3 // indirect
6064
google.golang.org/protobuf v1.33.0 // indirect
65+
gopkg.in/yaml.v3 v3.0.1 // indirect
6166
)
6267

6368
replace github.com/testcontainers/testcontainers-go => ../..

modules/chroma/go.sum

+10-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
2020
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
2121
github.com/cpuguy83/dockercfg v0.3.1 h1:/FpZ+JaygUR/lZP2NlFI2DVfrOEMAIKP5wWEJdoYe9E=
2222
github.com/cpuguy83/dockercfg v0.3.1/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc=
23+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
2324
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
2425
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
2526
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -56,12 +57,14 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
5657
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5758
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 h1:YBftPWNWd4WwGqtY2yeZL2ef8rHAxPBD8KFhJpmcqms=
5859
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0/go.mod h1:YN5jB8ie0yfIUg6VvR9Kz84aCaG7AsGZnLjhHbUqwPg=
59-
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
60-
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
6160
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
6261
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
6362
github.com/klauspost/compress v1.16.0 h1:iULayQNOReoYUe+1qtKOqw9CwJv3aNQu8ivo7lw1HU4=
6463
github.com/klauspost/compress v1.16.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
64+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
65+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
66+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
67+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
6568
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
6669
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
6770
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
@@ -82,12 +85,15 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8
8285
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
8386
github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=
8487
github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
88+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
8589
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
8690
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
8791
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8892
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
8993
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
9094
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
95+
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
96+
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
9197
github.com/shirou/gopsutil/v3 v3.23.12 h1:z90NtUkp3bMtmICZKpC4+WaknU1eXtp5vtbQ11DgpE4=
9298
github.com/shirou/gopsutil/v3 v3.23.12/go.mod h1:1FrWgea594Jp7qmjHUUPlJDTPgcsb9mGnXDxavtikzM=
9399
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
@@ -189,6 +195,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
189195
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
190196
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
191197
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
198+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
199+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
192200
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
193201
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
194202
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)