Skip to content

Commit c55b9a3

Browse files
authored
Merge pull request #210 from tharropoulos/v29.0-changes
feat: add v29.0 changes
1 parent 9c95474 commit c55b9a3

File tree

7 files changed

+232
-3
lines changed

7 files changed

+232
-3
lines changed

typesense/api/client_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

typesense/client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ func (c *Client) Preset(presetName string) PresetInterface {
7676
return &preset{apiClient: c.apiClient, presetName: presetName}
7777
}
7878

79+
func (c *Client) NLSearchModels() NLSearchModelsInterface {
80+
return &nlSearchModels{apiClient: c.apiClient}
81+
}
82+
83+
func (c *Client) NLSearchModel(modelID string) NLSearchModelInterface {
84+
return &nlSearchModel{apiClient: c.apiClient, modelID: modelID}
85+
}
86+
7987
func (c *Client) Stopwords() StopwordsInterface {
8088
return &stopwords{apiClient: c.apiClient}
8189
}

typesense/nl_search_model.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package typesense
2+
3+
import (
4+
"context"
5+
6+
"github.com/typesense/typesense-go/v3/typesense/api"
7+
)
8+
9+
type NLSearchModelInterface interface {
10+
Retrieve(ctx context.Context) (*api.NLSearchModelSchema, error)
11+
Update(ctx context.Context, model *api.NLSearchModelUpdateSchema) (*api.NLSearchModelSchema, error)
12+
Delete(ctx context.Context) (*api.NLSearchModelDeleteSchema, error)
13+
}
14+
15+
type nlSearchModel struct {
16+
apiClient APIClientInterface
17+
modelID string
18+
}
19+
20+
func (n *nlSearchModel) Retrieve(ctx context.Context) (*api.NLSearchModelSchema, error) {
21+
response, err := n.apiClient.RetrieveNLSearchModelWithResponse(ctx, n.modelID)
22+
if err != nil {
23+
return nil, err
24+
}
25+
if response.JSON200 == nil {
26+
return nil, &HTTPError{Status: response.StatusCode(), Body: response.Body}
27+
}
28+
return response.JSON200, nil
29+
}
30+
31+
func (n *nlSearchModel) Update(ctx context.Context, model *api.NLSearchModelUpdateSchema) (*api.NLSearchModelSchema, error) {
32+
response, err := n.apiClient.UpdateNLSearchModelWithResponse(ctx, n.modelID, *model)
33+
if err != nil {
34+
return nil, err
35+
}
36+
if response.JSON200 == nil {
37+
return nil, &HTTPError{Status: response.StatusCode(), Body: response.Body}
38+
}
39+
return response.JSON200, nil
40+
}
41+
42+
func (n *nlSearchModel) Delete(ctx context.Context) (*api.NLSearchModelDeleteSchema, error) {
43+
response, err := n.apiClient.DeleteNLSearchModelWithResponse(ctx, n.modelID)
44+
if err != nil {
45+
return nil, err
46+
}
47+
if response.JSON200 == nil {
48+
return nil, &HTTPError{Status: response.StatusCode(), Body: response.Body}
49+
}
50+
return response.JSON200, nil
51+
}

typesense/nl_search_models.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package typesense
2+
3+
import (
4+
"context"
5+
6+
"github.com/typesense/typesense-go/v3/typesense/api"
7+
)
8+
9+
type NLSearchModelsInterface interface {
10+
Retrieve(ctx context.Context) ([]*api.NLSearchModelSchema, error)
11+
Create(ctx context.Context, model *api.NLSearchModelCreateSchema) (*api.NLSearchModelSchema, error)
12+
}
13+
14+
type nlSearchModels struct {
15+
apiClient APIClientInterface
16+
}
17+
18+
func (n *nlSearchModels) Retrieve(ctx context.Context) ([]*api.NLSearchModelSchema, error) {
19+
response, err := n.apiClient.RetrieveAllNLSearchModelsWithResponse(ctx)
20+
if err != nil {
21+
return nil, err
22+
}
23+
if response.JSON200 == nil {
24+
return nil, &HTTPError{Status: response.StatusCode(), Body: response.Body}
25+
}
26+
27+
// Convert []NLSearchModelSchema to []*NLSearchModelSchema
28+
result := make([]*api.NLSearchModelSchema, len(*response.JSON200))
29+
for i, model := range *response.JSON200 {
30+
modelCopy := model // Create a copy to get address
31+
result[i] = &modelCopy
32+
}
33+
return result, nil
34+
}
35+
36+
func (n *nlSearchModels) Create(ctx context.Context, model *api.NLSearchModelCreateSchema) (*api.NLSearchModelSchema, error) {
37+
response, err := n.apiClient.CreateNLSearchModelWithResponse(ctx, *model)
38+
if err != nil {
39+
return nil, err
40+
}
41+
if response.JSON201 == nil {
42+
return nil, &HTTPError{Status: response.StatusCode(), Body: response.Body}
43+
}
44+
return response.JSON201, nil
45+
}

typesense/test/dbhelpers_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package test
66
import (
77
"context"
88
"fmt"
9+
"os"
910
"testing"
1011
"time"
1112

@@ -441,3 +442,70 @@ func retrieveDocuments(t *testing.T, collectionName string, docIDs ...string) []
441442
}
442443
return results
443444
}
445+
446+
func newNLSearchModelCreateSchema() *api.NLSearchModelCreateSchema {
447+
apiKey := os.Getenv("NL_SEARCH_MODEL_API_KEY")
448+
449+
return &api.NLSearchModelCreateSchema{
450+
ModelName: pointer.String("openai/gpt-3.5-turbo"),
451+
ApiKey: pointer.String(apiKey),
452+
MaxBytes: pointer.Int(1000),
453+
Temperature: pointer.Float32(0.7),
454+
SystemPrompt: pointer.String("You are a helpful assistant."),
455+
TopP: pointer.Float32(0.9),
456+
TopK: pointer.Int(40),
457+
StopSequences: &[]string{"END", "STOP"},
458+
ApiVersion: pointer.String("v1"),
459+
}
460+
}
461+
462+
func newNLSearchModelSchema(modelID string) *api.NLSearchModelSchema {
463+
apiKey := os.Getenv("NL_SEARCH_MODEL_API_KEY")
464+
465+
return &api.NLSearchModelSchema{
466+
Id: modelID,
467+
ModelName: pointer.String("openai/gpt-3.5-turbo"),
468+
ApiKey: pointer.String(apiKey),
469+
MaxBytes: pointer.Int(1000),
470+
Temperature: pointer.Float32(0.7),
471+
SystemPrompt: pointer.String("You are a helpful assistant."),
472+
TopP: pointer.Float32(0.9),
473+
TopK: pointer.Int(40),
474+
StopSequences: &[]string{"END", "STOP"},
475+
ApiVersion: pointer.String("v1"),
476+
}
477+
}
478+
479+
func newNLSearchModelUpdateSchema() *api.NLSearchModelUpdateSchema {
480+
apiKey := os.Getenv("NL_SEARCH_MODEL_API_KEY")
481+
482+
return &api.NLSearchModelUpdateSchema{
483+
ModelName: pointer.String("openai/gpt-4"),
484+
ApiKey: pointer.String(apiKey),
485+
MaxBytes: pointer.Int(2000),
486+
Temperature: pointer.Float32(0.5),
487+
SystemPrompt: pointer.String("You are an expert assistant."),
488+
TopP: pointer.Float32(0.8),
489+
TopK: pointer.Int(50),
490+
StopSequences: &[]string{"END", "STOP", "QUIT"},
491+
ApiVersion: pointer.String("v1"),
492+
}
493+
}
494+
495+
func shouldSkipNLSearchModelTests(t *testing.T) {
496+
if os.Getenv("NL_SEARCH_MODEL_API_KEY") == "" {
497+
t.Skip("Skipping NL search model test: NL_SEARCH_MODEL_API_KEY not set")
498+
}
499+
}
500+
501+
func createNewNLSearchModel(t *testing.T) (string, *api.NLSearchModelSchema) {
502+
t.Helper()
503+
modelID := newUUIDName("nl-model-test")
504+
modelSchema := newNLSearchModelCreateSchema()
505+
modelSchema.Id = pointer.String(modelID)
506+
507+
result, err := typesenseClient.NLSearchModels().Create(context.Background(), modelSchema)
508+
509+
require.NoError(t, err)
510+
return modelID, result
511+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//go:build integration
2+
// +build integration
3+
4+
package test
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/stretchr/testify/require"
11+
"github.com/typesense/typesense-go/v3/typesense/api/pointer"
12+
)
13+
14+
func nlSearchModelsCleanUp() {
15+
result, _ := typesenseClient.NLSearchModels().Retrieve(context.Background())
16+
for _, model := range result {
17+
typesenseClient.NLSearchModel(model.Id).Delete(context.Background())
18+
}
19+
}
20+
21+
func TestNLSearchModel(t *testing.T) {
22+
shouldSkipNLSearchModelTests(t)
23+
t.Cleanup(nlSearchModelsCleanUp)
24+
25+
t.Run("Retrieve", func(t *testing.T) {
26+
modelID, expectedResult := createNewNLSearchModel(t)
27+
28+
result, err := typesenseClient.NLSearchModel(modelID).Retrieve(context.Background())
29+
30+
require.NoError(t, err)
31+
require.Equal(t, expectedResult, result)
32+
})
33+
34+
t.Run("Update", func(t *testing.T) {
35+
modelID, originalModel := createNewNLSearchModel(t)
36+
37+
updateSchema := newNLSearchModelUpdateSchema()
38+
updateSchema.Temperature = pointer.Float32(0.8)
39+
40+
result, err := typesenseClient.NLSearchModel(modelID).Update(context.Background(), updateSchema)
41+
42+
require.NoError(t, err)
43+
require.Equal(t, "openai/gpt-4", *result.ModelName)
44+
require.Equal(t, float32(0.8), *result.Temperature)
45+
require.Equal(t, originalModel.Id, result.Id)
46+
})
47+
48+
t.Run("Delete", func(t *testing.T) {
49+
modelID, expectedResult := createNewNLSearchModel(t)
50+
51+
result, err := typesenseClient.NLSearchModel(modelID).Delete(context.Background())
52+
53+
require.NoError(t, err)
54+
require.Equal(t, expectedResult.Id, result.Id)
55+
56+
_, err = typesenseClient.NLSearchModel(modelID).Retrieve(context.Background())
57+
require.Error(t, err)
58+
})
59+
}

typesense/test/search_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,6 @@ func TestCollectionSearchWithPreset(t *testing.T) {
156156
newDocument("123", withCompanyName("Company 1"), withNumEmployees(50)),
157157
newDocument("125", withCompanyName("Company 2"), withNumEmployees(150)),
158158
newDocument("127", withCompanyName("Company 3"), withNumEmployees(250)),
159-
newDocument("129", withCompanyName("Stark Industries 4"), withNumEmployees(500)),
160-
newDocument("131", withCompanyName("Stark Industries 5"), withNumEmployees(1000)),
161159
}
162160

163161
params := &api.ImportDocumentsParams{Action: pointer.Any(api.Create)}

0 commit comments

Comments
 (0)