Skip to content

Commit

Permalink
test: retrieve, update, delete a conversation model
Browse files Browse the repository at this point in the history
  • Loading branch information
phiHero committed Jul 10, 2024
1 parent 463ea57 commit 41b745b
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 30 deletions.
8 changes: 4 additions & 4 deletions typesense/api/client_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions typesense/api/generator/generator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ components:
type: integer
required:
- id
ConversationModelCreateSchema:
ConversationModelCreateAndUpdateSchema:
properties:
account_id:
description: LLM service's account ID (only applicable for Cloudflare)
Expand Down Expand Up @@ -250,10 +250,12 @@ components:
properties:
id:
type: string
required:
- id
type: object
ConversationModelSchema:
allOf:
- $ref: '#/components/schemas/ConversationModelCreateSchema'
- $ref: '#/components/schemas/ConversationModelCreateAndUpdateSchema'
- properties:
id:
type: string
Expand Down Expand Up @@ -2518,14 +2520,14 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/ConversationModelCreateSchema'
$ref: '#/components/schemas/ConversationModelCreateAndUpdateSchema'
required: true
responses:
201:
content:
application/json:
schema:
$ref: '#/components/schemas/ConversationModelCreateSchema'
$ref: '#/components/schemas/ConversationModelCreateAndUpdateSchema'
description: Created Conversation Model
400:
content:
Expand Down Expand Up @@ -2590,14 +2592,14 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/ConversationModelCreateSchema'
$ref: '#/components/schemas/ConversationModelCreateAndUpdateSchema'
required: true
responses:
200:
content:
application/json:
schema:
$ref: '#/components/schemas/ConversationModelCreateSchema'
$ref: '#/components/schemas/ConversationModelCreateAndUpdateSchema'
description: The conversation model was successfully updated
summary: Update a conversation model
tags:
Expand Down
14 changes: 8 additions & 6 deletions typesense/api/generator/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -990,14 +990,14 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/ConversationModelCreateSchema'
$ref: '#/components/schemas/ConversationModelCreateAndUpdateSchema'
required: true
responses:
201:
content:
application/json:
schema:
$ref: '#/components/schemas/ConversationModelCreateSchema'
$ref: '#/components/schemas/ConversationModelCreateAndUpdateSchema'
description: Created Conversation Model
400:
content:
Expand Down Expand Up @@ -1035,7 +1035,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/ConversationModelCreateSchema'
$ref: '#/components/schemas/ConversationModelCreateAndUpdateSchema'
required: true
parameters:
- name: modelId
Expand All @@ -1049,7 +1049,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/ConversationModelCreateSchema'
$ref: '#/components/schemas/ConversationModelCreateAndUpdateSchema'
description: The conversation model was successfully updated
summary: Update a conversation model
tags:
Expand Down Expand Up @@ -3029,7 +3029,7 @@ components:
items:
$ref: '#/components/schemas/ConversationSchema'
x-go-type: '[]*ConversationSchema'
ConversationModelCreateSchema:
ConversationModelCreateAndUpdateSchema:
properties:
model_name:
description: Name of the LLM model offered by OpenAI, Cloudflare or vLLM
Expand Down Expand Up @@ -3059,10 +3059,12 @@ components:
properties:
id:
type: string
required:
- id
type: object
ConversationModelSchema:
allOf:
- $ref: '#/components/schemas/ConversationModelCreateSchema'
- $ref: '#/components/schemas/ConversationModelCreateAndUpdateSchema'
- properties:
id:
type: string
Expand Down
10 changes: 5 additions & 5 deletions typesense/api/types_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions typesense/conversation_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type ConversationModelInterface interface {
Retrieve(ctx context.Context) (*api.ConversationModelSchema, error)
Update(ctx context.Context, model *api.ConversationModelCreateSchema) (*api.ConversationModelCreateSchema, error)
Update(ctx context.Context, model *api.ConversationModelCreateAndUpdateSchema) (*api.ConversationModelCreateAndUpdateSchema, error)
Delete(ctx context.Context) (*api.ConversationModelDeleteSchema, error)
}

Expand All @@ -28,8 +28,8 @@ func (c *conversationModel) Retrieve(ctx context.Context) (*api.ConversationMode
return response.JSON200, nil
}

func (c *conversationModel) Update(ctx context.Context, model *api.ConversationModelCreateSchema) (*api.ConversationModelCreateSchema, error) {
response, err := c.apiClient.UpdateConversationModelWithResponse(ctx, c.modelId, api.UpdateConversationModelJSONRequestBody(*model))
func (c *conversationModel) Update(ctx context.Context, conversationModelCreateAndUpdateSchema *api.ConversationModelCreateAndUpdateSchema) (*api.ConversationModelCreateAndUpdateSchema, error) {
response, err := c.apiClient.UpdateConversationModelWithResponse(ctx, c.modelId, api.UpdateConversationModelJSONRequestBody(*conversationModelCreateAndUpdateSchema))
if err != nil {
return nil, err
}
Expand Down
122 changes: 122 additions & 0 deletions typesense/conversation_model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package typesense

import (
"context"
"encoding/json"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
"github.com/typesense/typesense-go/typesense/api"
)

func TestConversationModelRetrieve(t *testing.T) {
accountId, systemPrompt := "CLOUDFLARE_ACCOUNT_ID", "..."
expectedData := &api.ConversationModelSchema{
Id: "123",
ModelName: "cf/mistral/mistral-7b-instruct-v0.1",
ApiKey: "CLOUDFLARE_API_KEY",
AccountId: &accountId,
SystemPrompt: &systemPrompt,
MaxBytes: 16384,
}

server, client := newTestServerAndClient(func(w http.ResponseWriter, r *http.Request) {
validateRequestMetadata(t, r, "/conversations/models/123", http.MethodGet)
data := jsonEncode(t, expectedData)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(data)
})
defer server.Close()

res, err := client.Conversations().Model("123").Retrieve(context.Background())
assert.NoError(t, err)
assert.Equal(t, expectedData, res)
}

func TestConversationModelRetrieveOnHttpStatusErrorCodeReturnsError(t *testing.T) {
server, client := newTestServerAndClient(func(w http.ResponseWriter, r *http.Request) {
validateRequestMetadata(t, r, "/conversations/models/123", http.MethodGet)
w.WriteHeader(http.StatusNotFound)
})
defer server.Close()

_, err := client.Conversations().Model("123").Retrieve(context.Background())
assert.Error(t, err)
}

func TestConversationModelUpdate(t *testing.T) {
accountId, systemPrompt := "CLOUDFLARE_ACCOUNT_ID", "..."
expectedData := &api.ConversationModelCreateAndUpdateSchema{
ModelName: "cf/mistral/mistral-7b-instruct-v0.1",
ApiKey: "CLOUDFLARE_API_KEY",
AccountId: &accountId,
SystemPrompt: &systemPrompt,
MaxBytes: 16384,
}

server, client := newTestServerAndClient(func(w http.ResponseWriter, r *http.Request) {
validateRequestMetadata(t, r, "/conversations/models/123", http.MethodPut)

var reqBody api.ConversationModelCreateAndUpdateSchema
err := json.NewDecoder(r.Body).Decode(&reqBody)

assert.NoError(t, err)
assert.Equal(t, expectedData, &reqBody)

data := jsonEncode(t, expectedData)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(data)
})
defer server.Close()

res, err := client.Conversations().Model("123").Update(context.Background(), expectedData)
assert.NoError(t, err)
assert.Equal(t, expectedData, res)
}

func TestConversationModelUpdateOnHttpStatusErrorCodeReturnsError(t *testing.T) {
server, client := newTestServerAndClient(func(w http.ResponseWriter, r *http.Request) {
validateRequestMetadata(t, r, "/conversations/models/123", http.MethodPut)
w.WriteHeader(http.StatusConflict)
})
defer server.Close()

_, err := client.Conversations().Model("123").Update(context.Background(), &api.ConversationModelCreateAndUpdateSchema{
ModelName: "cf/mistral/mistral-7b-instruct-v0.1",
})
assert.Error(t, err)
}

func TestConversationModelDelete(t *testing.T) {
expectedData := &api.ConversationModelDeleteSchema{
Id: "123",
}

server, client := newTestServerAndClient(func(w http.ResponseWriter, r *http.Request) {
validateRequestMetadata(t, r, "/conversations/models/123", http.MethodDelete)

data := jsonEncode(t, expectedData)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(data)
})
defer server.Close()

res, err := client.Conversations().Model("123").Delete(context.Background())
assert.NoError(t, err)
assert.Equal(t, expectedData, res)
}

func TestConversationModelDeleteOnHttpStatusErrorCodeReturnsError(t *testing.T) {
server, client := newTestServerAndClient(func(w http.ResponseWriter, r *http.Request) {
validateRequestMetadata(t, r, "/conversations/models/123", http.MethodDelete)
w.WriteHeader(http.StatusConflict)
})
defer server.Close()

_, err := client.Conversations().Model("123").Delete(context.Background())
assert.Error(t, err)
}
6 changes: 3 additions & 3 deletions typesense/conversation_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// ConversationModelsInterface is a type for ConversationModels API operations
type ConversationModelsInterface interface {
Create(ctx context.Context, conversationModelCreateSchema *api.ConversationModelCreateSchema) (*api.ConversationModelCreateSchema, error)
Create(ctx context.Context, conversationModelCreateAndUpdateSchema *api.ConversationModelCreateAndUpdateSchema) (*api.ConversationModelCreateAndUpdateSchema, error)
Retrieve(ctx context.Context) ([]*api.ConversationModelSchema, error)
}

Expand All @@ -17,8 +17,8 @@ type conversationModels struct {
apiClient APIClientInterface
}

func (c *conversationModels) Create(ctx context.Context, conversationModelCreateSchema *api.ConversationModelCreateSchema) (*api.ConversationModelCreateSchema, error) {
response, err := c.apiClient.CreateConversationModelWithResponse(ctx, api.CreateConversationModelJSONRequestBody(*conversationModelCreateSchema))
func (c *conversationModels) Create(ctx context.Context, conversationModelCreateAndUpdateSchema *api.ConversationModelCreateAndUpdateSchema) (*api.ConversationModelCreateAndUpdateSchema, error) {
response, err := c.apiClient.CreateConversationModelWithResponse(ctx, api.CreateConversationModelJSONRequestBody(*conversationModelCreateAndUpdateSchema))
if err != nil {
return nil, err
}
Expand Down
12 changes: 10 additions & 2 deletions typesense/conversation_models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package typesense

import (
"context"
"encoding/json"
"net/http"
"testing"

Expand Down Expand Up @@ -47,7 +48,7 @@ func TestConversationModelsRetrieveOnHttpStatusErrorCodeReturnsError(t *testing.

func TestConversationModelsCreate(t *testing.T) {
accountId, systemPrompt := "CLOUDFLARE_ACCOUNT_ID", "..."
expectedData := &api.ConversationModelCreateSchema{
expectedData := &api.ConversationModelCreateAndUpdateSchema{
ModelName: "cf/mistral/mistral-7b-instruct-v0.1",
ApiKey: "CLOUDFLARE_API_KEY",
AccountId: &accountId,
Expand All @@ -57,6 +58,13 @@ func TestConversationModelsCreate(t *testing.T) {

server, client := newTestServerAndClient(func(w http.ResponseWriter, r *http.Request) {
validateRequestMetadata(t, r, "/conversations/models", http.MethodPost)

var reqBody api.ConversationModelCreateAndUpdateSchema
err := json.NewDecoder(r.Body).Decode(&reqBody)

assert.NoError(t, err)
assert.Equal(t, expectedData, &reqBody)

data := jsonEncode(t, expectedData)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
Expand All @@ -76,7 +84,7 @@ func TestConversationModelsCreateOnHttpStatusErrorCodeReturnsError(t *testing.T)
})
defer server.Close()

_, err := client.Conversations().Models().Create(context.Background(), &api.ConversationModelCreateSchema{
_, err := client.Conversations().Models().Create(context.Background(), &api.ConversationModelCreateAndUpdateSchema{
ModelName: "cf/mistral/mistral-7b-instruct-v0.1",
})
assert.Error(t, err)
Expand Down
Loading

0 comments on commit 41b745b

Please sign in to comment.