-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #262 from dvonthenen/implement-get-models
Implement Get Models
- Loading branch information
Showing
5 changed files
with
351 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters