Skip to content

Commit

Permalink
Support Retrieve model API (sashabaranov#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
vvatanabe committed Jun 3, 2023
1 parent 61ba5f3 commit 3e82973
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
14 changes: 14 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openai

import (
"context"
"fmt"
"net/http"
)

Expand Down Expand Up @@ -48,3 +49,16 @@ func (c *Client) ListModels(ctx context.Context) (models ModelsList, err error)
err = c.sendRequest(req, &models)
return
}

// GetModel Retrieves a model instance, providing basic information about
// the model such as the owner and permissioning.
func (c *Client) GetModel(ctx context.Context, modelID string) (model Model, err error) {
urlSuffix := fmt.Sprintf("/models/%s", modelID)
req, err := c.requestBuilder.Build(ctx, http.MethodGet, c.fullURL(urlSuffix), nil)
if err != nil {
return
}

err = c.sendRequest(req, &model)
return
}
41 changes: 41 additions & 0 deletions models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,44 @@ func handleModelsEndpoint(w http.ResponseWriter, _ *http.Request) {
resBytes, _ := json.Marshal(ModelsList{})
fmt.Fprintln(w, string(resBytes))
}

// TestGetModel Tests the retrieve model endpoint of the API using the mocked server.
func TestGetModel(t *testing.T) {
server := test.NewTestServer()
server.RegisterHandler("/v1/models/text-davinci-003", handleGetModelEndpoint)
// create the test server
ts := server.OpenAITestServer()
ts.Start()
defer ts.Close()

config := DefaultConfig(test.GetTestToken())
config.BaseURL = ts.URL + "/v1"
client := NewClientWithConfig(config)
ctx := context.Background()

_, err := client.GetModel(ctx, "text-davinci-003")
checks.NoError(t, err, "GetModel error")
}

func TestAzureGetModel(t *testing.T) {
server := test.NewTestServer()
server.RegisterHandler("/openai/models/text-davinci-003", handleModelsEndpoint)
// create the test server
ts := server.OpenAITestServer()
ts.Start()
defer ts.Close()

config := DefaultAzureConfig(test.GetTestToken(), "https://dummylab.openai.azure.com/")
config.BaseURL = ts.URL
client := NewClientWithConfig(config)
ctx := context.Background()

_, err := client.GetModel(ctx, "text-davinci-003")
checks.NoError(t, err, "GetModel error")
}

// handleModelsEndpoint Handles the models endpoint by the test server.
func handleGetModelEndpoint(w http.ResponseWriter, _ *http.Request) {
resBytes, _ := json.Marshal(Model{})
fmt.Fprintln(w, string(resBytes))
}

0 comments on commit 3e82973

Please sign in to comment.