Skip to content

Commit

Permalink
Merge pull request #262 from dvonthenen/implement-get-models
Browse files Browse the repository at this point in the history
Implement Get Models
  • Loading branch information
dvonthenen authored Aug 14, 2024
2 parents 8636069 + 5700744 commit 4de178e
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ Management API exercise the full [CRUD](https://en.wikipedia.org/wiki/Create,_re
- Invitations - [examples/manage/invitations](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/manage/invitations/main.go)
- Keys - [examples/manage/keys](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/manage/keys/main.go)
- Members - [examples/manage/members](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/manage/members/main.go)
- Models - [examples/manage/models](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/manage/models/main.go)

- Projects - [examples/manage/projects](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/manage/projects/main.go)
- Scopes - [examples/manage/scopes](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/manage/scopes/main.go)
- Usage - [examples/manage/usage](https://github.com/deepgram/deepgram-go-sdk/blob/main/examples/manage/usage/main.go)
Expand Down
112 changes: 112 additions & 0 deletions examples/manage/models/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2024 Deepgram SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

package main

import (
"context"
"fmt"
"os"

api "github.com/deepgram/deepgram-go-sdk/pkg/api/manage/v1"
client "github.com/deepgram/deepgram-go-sdk/pkg/client/manage"
)

func main() {
// init library
client.InitWithDefault()

// context
ctx := context.Background()

//client
dg := client.NewWithDefaults()
mgClient := api.New(dg)

// list projects
respList, err := mgClient.ListProjects(ctx)
if err != nil {
fmt.Printf("ListProjects failed. Err: %v\n", err)
os.Exit(1)
}

// save 1 project
var projectID string
for _, item := range respList.Projects {
projectID = item.ProjectID
name := item.Name
fmt.Printf("ListProjects() - Name: %s, ID: %s\n", name, projectID)
break
}
fmt.Printf("\n\n\n")

// list models
respModels, err := mgClient.ListModels(ctx, nil)
if err != nil {
fmt.Printf("ListModels() failed. Err: %v\n", err)
os.Exit(1)
}

modelId := ""
if respModels == nil {
fmt.Printf("ListModels() - No models found\n")
} else {
for _, item := range respModels.Stt {
id := item.UUID
name := item.Name
fmt.Printf("STT - ID: %s, Scope: %s\n", id, name)
modelId = id // save one model id
}
for _, item := range respModels.Tts {
id := item.UUID
name := item.Name
fmt.Printf("TTS - ID: %s, Scope: %s\n", id, name)
}
}
fmt.Printf("\n\n\n")

// get model
respModel, err := mgClient.GetModel(ctx, modelId)
if err != nil {
fmt.Printf("GetModel failed. Err: %v\n", err)
os.Exit(1)
} else {
fmt.Printf("GetModel() - ID: %s, Name: %s\n", respModel.UUID, respModel.Name)
}
fmt.Printf("\n\n\n")

// list project models
respModels, err = mgClient.ListProjectModels(ctx, projectID, nil)
if err != nil {
fmt.Printf("ListProjectModels failed. Err: %v\n", err)
os.Exit(1)
}

modelId = ""
if respModels == nil {
fmt.Printf("ListModels() - No models found\n")
} else {
for _, item := range respModels.Stt {
id := item.UUID
name := item.Name
fmt.Printf("STT - ID: %s, Scope: %s\n", id, name)
modelId = id // save one model id
}
for _, item := range respModels.Tts {
id := item.UUID
name := item.Name
fmt.Printf("TTS - ID: %s, Scope: %s\n", id, name)
}
}
fmt.Printf("\n\n\n")

// get model
respModel, err = mgClient.GetProjectModel(ctx, projectID, modelId)
if err != nil {
fmt.Printf("GetProjectModel failed. Err: %v\n", err)
os.Exit(1)
} else {
fmt.Printf("GetProjectModel() - ID: %s, Name: %s\n", respModel.UUID, respModel.Name)
}
}
56 changes: 56 additions & 0 deletions pkg/api/manage/v1/interfaces/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,39 @@ type ProjectList struct {
Projects []Project `json:"projects,omitempty"`
}

// Stt provides a STT info
type Stt struct {
Name string `json:"name,omitempty"`
CanonicalName string `json:"canonical_name,omitempty"`
Architecture string `json:"architecture,omitempty"`
Languages []string `json:"languages,omitempty"`
Version string `json:"version,omitempty"`
UUID string `json:"uuid,omitempty"`
Batch bool `json:"batch,omitempty"`
Streaming bool `json:"streaming,omitempty"`
FormattedOutput bool `json:"formatted_output,omitempty"`
}

// Metadata provides a metadata about a given model
type Metadata struct {
Accent string `json:"accent,omitempty"`
Color string `json:"color,omitempty"`
Image string `json:"image,omitempty"`
Sample string `json:"sample,omitempty"`
Tags []string `json:"tags,omitempty"`
}

// Tts provides a TTS info
type Tts struct {
Name string `json:"name,omitempty"`
CanonicalName string `json:"canonical_name,omitempty"`
Architecture string `json:"architecture,omitempty"`
Languages []string `json:"languages,omitempty"`
Version string `json:"version,omitempty"`
UUID string `json:"uuid,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
}

// Token provides a token
type Token struct {
In int `json:"in,omitempty"`
Expand Down Expand Up @@ -240,6 +273,11 @@ type ProjectUpdateRequest struct {
Company string `json:"company,omitempty" url:"company,omitempty"`
}

// ModelRequest provides a model request
type ModelRequest struct {
IncludeOutdated bool `json:"include_outdated,omitempty" url:"include_outdated,omitempty"`
}

// InvitationRequest provides an invitation request
type InvitationRequest struct {
Email string `json:"email,omitempty" url:"email,omitempty"`
Expand Down Expand Up @@ -352,6 +390,24 @@ type ProjectResult struct {
Project
}

// ModelsResult provides a result with a list of models
type ModelsResult struct {
Stt []Stt `json:"stt,omitempty"`
Tts []Tts `json:"tts,omitempty"`
}

// ModelResult provides a result with a single model
type ModelResult struct {
Name string `json:"name,omitempty"`
CanonicalName string `json:"canonical_name,omitempty"`
Architecture string `json:"architecture,omitempty"`
Language string `json:"language,omitempty"`
Version string `json:"version,omitempty"`
UUID string `json:"uuid,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
}

// ScopeResult provides a result with a list of scopes
type ScopeResult struct {
ScopeList
}
Expand Down
175 changes: 175 additions & 0 deletions pkg/api/manage/v1/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// Copyright 2024 Deepgram SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

/*
Models API:
https://developers.deepgram.com/docs/model-metadata
*/
package manage

import (
"bytes"
"context"
"encoding/json"

klog "k8s.io/klog/v2"

api "github.com/deepgram/deepgram-go-sdk/pkg/api/manage/v1/interfaces"
version "github.com/deepgram/deepgram-go-sdk/pkg/api/version"
)

// ListModels lists all models available
// NOTE: This is a wrapper around GetModels
//
// Args:
//
// ctx: context
// model: model request options
//
// Returns:
//
// *api.ModelsResult: list of models
func (c *Client) ListModels(ctx context.Context, model *api.ModelRequest) (*api.ModelsResult, error) {
return c.GetModels(ctx, model)
}

// GetModels lists all models available
//
// Args:
//
// ctx: context
// model: model request options
//
// Returns:
//
// *api.ModelsResult: list of models
func (c *Client) GetModels(ctx context.Context, model *api.ModelRequest) (*api.ModelsResult, error) {
klog.V(6).Infof("manage.GetModels() ENTER\n")

if model == nil {
model = &api.ModelRequest{}
}

jsonStr, err := json.Marshal(model)
if err != nil {
klog.V(1).Infof("json.Marshal failed. Err: %v\n", err)
klog.V(6).Infof("manage.GetModels() LEAVE\n")
return nil, err
}

var resp api.ModelsResult
err = c.APIRequest(ctx, "GET", version.ModelsURI, bytes.NewBuffer(jsonStr), &resp)
if err != nil {
klog.V(1).Infof("GetModels failed. Err: %v\n", err)
} else {
klog.V(3).Infof("GetModels Succeeded\n")
}

klog.V(6).Infof("manage.GetModels() LEAVE\n")
return &resp, nil
}

// GetModel gets a model by ID
//
// Args:
//
// ctx: context
// modelID: model ID
//
// Returns:
//
// *api.ModelResult: specific model
func (c *Client) GetModel(ctx context.Context, modelID string) (*api.ModelResult, error) {
klog.V(6).Infof("manage.GetModel() ENTER\n")

var resp api.ModelResult
err := c.APIRequest(ctx, "GET", version.ModelsByIDURI, nil, &resp, modelID)
if err != nil {
klog.V(1).Infof("GetModel failed. Err: %v\n", err)
} else {
klog.V(3).Infof("GetModel Succeeded\n")
}

klog.V(6).Infof("manage.GetModel() LEAVE\n")
return &resp, nil
}

// ListProjectModels lists all models available
// NOTE: This is a wrapper around GetProjectModels
//
// Args:
//
// ctx: context
// projectID: project ID
// model: model request options
//
// Returns:
//
// *api.ModelsResult: list of models
func (c *Client) ListProjectModels(ctx context.Context, projectID string, model *api.ModelRequest) (*api.ModelsResult, error) {
return c.GetProjectModels(ctx, projectID, model)
}

// GetProjectModels lists all models available
//
// Args:
//
// ctx: context
// projectID: project ID
// model: model request options
//
// Returns:
//
// *api.ModelsResult: list of models
func (c *Client) GetProjectModels(ctx context.Context, projectID string, model *api.ModelRequest) (*api.ModelsResult, error) {
klog.V(6).Infof("manage.GetProjectModels() ENTER\n")

if model == nil {
model = &api.ModelRequest{}
}

jsonStr, err := json.Marshal(model)
if err != nil {
klog.V(1).Infof("json.Marshal failed. Err: %v\n", err)
klog.V(6).Infof("manage.GetProjectModels() LEAVE\n")
return nil, err
}

var resp api.ModelsResult
err = c.APIRequest(ctx, "GET", version.ModelsProjectURI, bytes.NewBuffer(jsonStr), &resp, projectID)
if err != nil {
klog.V(1).Infof("GetProjectModels failed. Err: %v\n", err)
} else {
klog.V(3).Infof("GetProjectModels Succeeded\n")
}

klog.V(6).Infof("manage.GetProjectModels() LEAVE\n")
return &resp, nil
}

// GetProjectModel gets a single model within the project by ID
//
// Args:
//
// ctx: context
// projectID: project ID
// modelID: model ID
//
// Returns:
//
// *api.ModelResult: specific model
func (c *Client) GetProjectModel(ctx context.Context, projectID, modelID string) (*api.ModelResult, error) {
klog.V(6).Infof("manage.GetProjectModel() ENTER\n")

var resp api.ModelResult
err := c.APIRequest(ctx, "GET", version.ModelsProjectByIDURI, nil, &resp, projectID, modelID)
if err != nil {
klog.V(1).Infof("GetProjectModel failed. Err: %v\n", err)
} else {
klog.V(3).Infof("GetProjectModel Succeeded\n")
}

klog.V(6).Infof("manage.GetProjectModel() LEAVE\n")
return &resp, nil
}
6 changes: 6 additions & 0 deletions pkg/api/version/manage-version.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ const (
MembersURI string = "projects/%s/members"
MembersByIDURI string = "projects/%s/members/%s"

// models
ModelsURI string = "models"
ModelsByIDURI string = "models/%s"
ModelsProjectURI string = "projects/%s/models"
ModelsProjectByIDURI string = "projects/%s/models/%s"

// projects
ProjectsURI string = "projects"
ProjectsByIDURI string = "projects/%s"
Expand Down

0 comments on commit 4de178e

Please sign in to comment.