generated from deepgram/oss-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new models endpoints to SDK
feat: new metadata models endpoints fix: some tests were hitting production feat: post-EA changes chore: update test to actually test the response body
- Loading branch information
1 parent
984591d
commit 630387d
Showing
9 changed files
with
264 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,24 @@ | ||
type Model = { | ||
name: string; | ||
canonical_name: string; | ||
architecture: string; | ||
languages?: string[]; | ||
version: string; | ||
uuid: string; | ||
batch?: boolean; | ||
streaming?: boolean; | ||
formatted_output?: boolean; | ||
metadata?: { | ||
accent: string; | ||
color: string; | ||
image: string; | ||
sample: string; | ||
}; | ||
}; | ||
|
||
export type GetModelResponse = Model; | ||
|
||
export type GetModelsResponse = { | ||
stt: Model[]; | ||
tts: Model[]; | ||
}; |
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,3 @@ | ||
export interface GetModelsSchema extends Record<string, unknown> { | ||
include_outdated?: boolean; | ||
} |
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
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,93 @@ | ||
import { isDeepgramError } from "../lib/errors"; | ||
import { | ||
DeepgramResponse, | ||
GetModelResponse, | ||
GetModelsResponse, | ||
GetModelsSchema, | ||
} from "../lib/types"; | ||
import { AbstractRestClient } from "./AbstractRestClient"; | ||
|
||
/** | ||
* Represents a REST client for interacting with the Deepgram API. | ||
* | ||
* The `ModelsRestClient` class provides methods for interacting with the Deepgram API to retrieve information about available models. | ||
* @extends AbstractRestClient | ||
*/ | ||
export class ModelsRestClient extends AbstractRestClient { | ||
public namespace: string = "models"; | ||
|
||
/** | ||
* Retrieves a list of all available models. | ||
* | ||
* @param endpoint - (optional) The endpoint to request. | ||
* @returns A promise that resolves with the response from the Deepgram API. | ||
* @example | ||
* ```typescript | ||
* import { createClient } from "@deepgram/sdk"; | ||
* | ||
* const deepgram = createClient(DEEPGRAM_API_KEY); | ||
* const { result: models, error } = deepgram.models.getAll(); | ||
* | ||
* if (error) { | ||
* console.error(error); | ||
* } else { | ||
* console.log(models); | ||
* } | ||
* ``` | ||
*/ | ||
async getAll( | ||
endpoint = ":version/models", | ||
options: GetModelsSchema = {} | ||
): Promise<DeepgramResponse<GetModelsResponse>> { | ||
try { | ||
const requestUrl = this.getRequestUrl(endpoint, {}, options); | ||
const result: GetModelsResponse = await this.get(requestUrl).then((result) => result.json()); | ||
|
||
return { result, error: null }; | ||
} catch (error) { | ||
if (isDeepgramError(error)) { | ||
return { result: null, error }; | ||
} | ||
|
||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves information about a specific model. | ||
* | ||
* @param modelId - The UUID of the model to retrieve. | ||
* @param endpoint - (optional) The endpoint to request. | ||
* @returns A promise that resolves with the response from the Deepgram API. | ||
* @example | ||
* ```typescript | ||
* import { createClient } from "@deepgram/sdk"; | ||
* | ||
* const deepgram = createClient(DEEPGRAM_API_KEY); | ||
* const { result: model, error } = deepgram.models.getModel("modelId"); | ||
* | ||
* if (error) { | ||
* console.error(error); | ||
* } else { | ||
* console.log(model); | ||
* } | ||
* ``` | ||
*/ | ||
async getModel( | ||
modelId: string, | ||
endpoint = ":version/models/:modelId" | ||
): Promise<DeepgramResponse<GetModelResponse>> { | ||
try { | ||
const requestUrl = this.getRequestUrl(endpoint, { modelId }); | ||
const result: GetModelResponse = await this.get(requestUrl).then((result) => result.json()); | ||
|
||
return { result, error: null }; | ||
} catch (error) { | ||
if (isDeepgramError(error)) { | ||
return { result: null, error }; | ||
} | ||
|
||
throw error; | ||
} | ||
} | ||
} |
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
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,30 @@ | ||
import { assert } from "chai"; | ||
import { createClient } from "../src"; | ||
import { faker } from "@faker-js/faker"; | ||
import DeepgramClient from "../src/DeepgramClient"; | ||
|
||
describe("making models REST requests", () => { | ||
let deepgram: DeepgramClient; | ||
|
||
beforeEach(() => { | ||
deepgram = createClient(faker.string.alphanumeric(40), { | ||
global: { url: "https://api.mock.deepgram.com" }, | ||
}); | ||
}); | ||
|
||
it("should retrieve a list of models", async () => { | ||
const { result, error } = await deepgram.models.getAll(); | ||
|
||
assert.isNull(error); | ||
assert.isNotNull(result); | ||
assert.containsAllDeepKeys(result, ["stt", "tts"]); | ||
}); | ||
|
||
it("should retrieve a model", async () => { | ||
const { result, error } = await deepgram.models.getModel(faker.string.uuid()); | ||
|
||
assert.isNull(error); | ||
assert.isNotNull(result); | ||
assert.containsAllDeepKeys(result, ["name", "canonical_name"]); | ||
}); | ||
}); |