@ai-sdk/deepgram 2.0.8 → 2.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,190 @@
1
+ ---
2
+ title: Deepgram
3
+ description: Learn how to use the Deepgram provider for the AI SDK.
4
+ ---
5
+
6
+ # Deepgram Provider
7
+
8
+ The [Deepgram](https://deepgram.com/) provider contains language model support for the Deepgram transcription API.
9
+
10
+ ## Setup
11
+
12
+ The Deepgram provider is available in the `@ai-sdk/deepgram` module. You can install it with
13
+
14
+ <Tabs items={['pnpm', 'npm', 'yarn', 'bun']}>
15
+ <Tab>
16
+ <Snippet text="pnpm add @ai-sdk/deepgram" dark />
17
+ </Tab>
18
+ <Tab>
19
+ <Snippet text="npm install @ai-sdk/deepgram" dark />
20
+ </Tab>
21
+ <Tab>
22
+ <Snippet text="yarn add @ai-sdk/deepgram" dark />
23
+ </Tab>
24
+
25
+ <Tab>
26
+ <Snippet text="bun add @ai-sdk/deepgram" dark />
27
+ </Tab>
28
+ </Tabs>
29
+
30
+ ## Provider Instance
31
+
32
+ You can import the default provider instance `deepgram` from `@ai-sdk/deepgram`:
33
+
34
+ ```ts
35
+ import { deepgram } from '@ai-sdk/deepgram';
36
+ ```
37
+
38
+ If you need a customized setup, you can import `createDeepgram` from `@ai-sdk/deepgram` and create a provider instance with your settings:
39
+
40
+ ```ts
41
+ import { createDeepgram } from '@ai-sdk/deepgram';
42
+
43
+ const deepgram = createDeepgram({
44
+ // custom settings, e.g.
45
+ fetch: customFetch,
46
+ });
47
+ ```
48
+
49
+ You can use the following optional settings to customize the Deepgram provider instance:
50
+
51
+ - **apiKey** _string_
52
+
53
+ API key that is being sent using the `Authorization` header.
54
+ It defaults to the `DEEPGRAM_API_KEY` environment variable.
55
+
56
+ - **headers** _Record&lt;string,string&gt;_
57
+
58
+ Custom headers to include in the requests.
59
+
60
+ - **fetch** _(input: RequestInfo, init?: RequestInit) => Promise&lt;Response&gt;_
61
+
62
+ Custom [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) implementation.
63
+ Defaults to the global `fetch` function.
64
+ You can use it as a middleware to intercept requests,
65
+ or to provide a custom fetch implementation for e.g. testing.
66
+
67
+ ## Transcription Models
68
+
69
+ You can create models that call the [Deepgram transcription API](https://developers.deepgram.com/docs/pre-recorded-audio)
70
+ using the `.transcription()` factory method.
71
+
72
+ The first argument is the model id e.g. `nova-3`.
73
+
74
+ ```ts
75
+ const model = deepgram.transcription('nova-3');
76
+ ```
77
+
78
+ You can also pass additional provider-specific options using the `providerOptions` argument. For example, supplying the `summarize` option will enable summaries for sections of content.
79
+
80
+ ```ts highlight="6"
81
+ import { experimental_transcribe as transcribe } from 'ai';
82
+ import { deepgram } from '@ai-sdk/deepgram';
83
+ import { readFile } from 'fs/promises';
84
+
85
+ const result = await transcribe({
86
+ model: deepgram.transcription('nova-3'),
87
+ audio: await readFile('audio.mp3'),
88
+ providerOptions: { deepgram: { summarize: true } },
89
+ });
90
+ ```
91
+
92
+ The following provider options are available:
93
+
94
+ - **language** _string_
95
+
96
+ Language code for the audio.
97
+ Supports numerous ISO-639-1 and ISO-639-3 language codes.
98
+ Optional.
99
+
100
+ - **smartFormat** _boolean_
101
+
102
+ Whether to apply smart formatting to the transcription.
103
+ Optional.
104
+
105
+ - **punctuate** _boolean_
106
+
107
+ Whether to add punctuation to the transcription.
108
+ Optional.
109
+
110
+ - **paragraphs** _boolean_
111
+
112
+ Whether to format the transcription into paragraphs.
113
+ Optional.
114
+
115
+ - **summarize** _enum | boolean_
116
+
117
+ Whether to generate a summary of the transcription.
118
+ Allowed values: `'v2'`, `false`.
119
+ Optional.
120
+
121
+ - **topics** _boolean_
122
+
123
+ Whether to detect topics in the transcription.
124
+ Optional.
125
+
126
+ - **intents** _boolean_
127
+
128
+ Whether to detect intents in the transcription.
129
+ Optional.
130
+
131
+ - **sentiment** _boolean_
132
+
133
+ Whether to perform sentiment analysis on the transcription.
134
+ Optional.
135
+
136
+ - **detectEntities** _boolean_
137
+
138
+ Whether to detect entities in the transcription.
139
+ Optional.
140
+
141
+ - **redact** _string | array of strings_
142
+
143
+ Specifies what content to redact from the transcription.
144
+ Optional.
145
+
146
+ - **replace** _string_
147
+
148
+ Replacement string for redacted content.
149
+ Optional.
150
+
151
+ - **search** _string_
152
+
153
+ Search term to find in the transcription.
154
+ Optional.
155
+
156
+ - **keyterm** _string_
157
+
158
+ Key terms to identify in the transcription.
159
+ Optional.
160
+
161
+ - **diarize** _boolean_
162
+
163
+ Whether to identify different speakers in the transcription.
164
+ Defaults to `true`.
165
+ Optional.
166
+
167
+ - **utterances** _boolean_
168
+
169
+ Whether to segment the transcription into utterances.
170
+ Optional.
171
+
172
+ - **uttSplit** _number_
173
+
174
+ Threshold for splitting utterances.
175
+ Optional.
176
+
177
+ - **fillerWords** _boolean_
178
+
179
+ Whether to include filler words (um, uh, etc.) in the transcription.
180
+ Optional.
181
+
182
+ ### Model Capabilities
183
+
184
+ | Model | Transcription | Duration | Segments | Language |
185
+ | -------------------------------------------------------------------------------------------------- | ------------------- | ------------------- | ------------------- | ------------------- |
186
+ | `nova-3` (+ [variants](https://developers.deepgram.com/docs/models-languages-overview#nova-3)) | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
187
+ | `nova-2` (+ [variants](https://developers.deepgram.com/docs/models-languages-overview#nova-2)) | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
188
+ | `nova` (+ [variants](https://developers.deepgram.com/docs/models-languages-overview#nova)) | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
189
+ | `enhanced` (+ [variants](https://developers.deepgram.com/docs/models-languages-overview#enhanced)) | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
190
+ | `base` (+ [variants](https://developers.deepgram.com/docs/models-languages-overview#base)) | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/deepgram",
3
- "version": "2.0.8",
3
+ "version": "2.0.10",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -8,9 +8,14 @@
8
8
  "types": "./dist/index.d.ts",
9
9
  "files": [
10
10
  "dist/**/*",
11
+ "docs/**/*",
12
+ "src",
11
13
  "CHANGELOG.md",
12
14
  "README.md"
13
15
  ],
16
+ "directories": {
17
+ "doc": "./docs"
18
+ },
14
19
  "exports": {
15
20
  "./package.json": "./package.json",
16
21
  ".": {
@@ -28,7 +33,7 @@
28
33
  "tsup": "^8",
29
34
  "typescript": "5.6.3",
30
35
  "zod": "3.25.76",
31
- "@ai-sdk/test-server": "1.0.1",
36
+ "@ai-sdk/test-server": "1.0.2",
32
37
  "@vercel/ai-tsconfig": "0.0.0"
33
38
  },
34
39
  "peerDependencies": {
@@ -54,7 +59,7 @@
54
59
  "scripts": {
55
60
  "build": "tsup --tsconfig tsconfig.build.json",
56
61
  "build:watch": "tsup --tsconfig tsconfig.build.json --watch",
57
- "clean": "del-cli dist",
62
+ "clean": "del-cli dist docs",
58
63
  "lint": "eslint \"./**/*.ts*\"",
59
64
  "type-check": "tsc --noEmit",
60
65
  "prettier-check": "prettier --check \"./**/*.ts*\"",
@@ -0,0 +1,36 @@
1
+ export type DeepgramTranscriptionAPITypes = {
2
+ // Base parameters
3
+ language?: string;
4
+ detect_language?: boolean;
5
+ model?: string;
6
+
7
+ // Formatting options
8
+ smart_format?: boolean;
9
+ punctuate?: boolean;
10
+ paragraphs?: boolean;
11
+
12
+ // Summarization and analysis
13
+ summarize?: 'v2' | false;
14
+ topics?: boolean;
15
+ intents?: boolean;
16
+ sentiment?: boolean;
17
+
18
+ // Entity detection
19
+ detect_entities?: boolean;
20
+
21
+ // Redaction options
22
+ redact?: string | string[];
23
+ replace?: string;
24
+
25
+ // Search and keywords
26
+ search?: string;
27
+ keyterm?: string;
28
+
29
+ // Speaker-related features
30
+ diarize?: boolean;
31
+ utterances?: boolean;
32
+ utt_split?: number;
33
+
34
+ // Miscellaneous
35
+ filler_words?: boolean;
36
+ };
@@ -0,0 +1,9 @@
1
+ import { FetchFunction } from '@ai-sdk/provider-utils';
2
+
3
+ export type DeepgramConfig = {
4
+ provider: string;
5
+ url: (options: { modelId: string; path: string }) => string;
6
+ headers: () => Record<string, string | undefined>;
7
+ fetch?: FetchFunction;
8
+ generateId?: () => string;
9
+ };
@@ -0,0 +1,34 @@
1
+ import { safeParseJSON } from '@ai-sdk/provider-utils';
2
+ import { deepgramErrorDataSchema } from './deepgram-error';
3
+ import { describe, expect, it } from 'vitest';
4
+
5
+ describe('deepgramErrorDataSchema', () => {
6
+ it('should parse Deepgram resource exhausted error', async () => {
7
+ const error = `
8
+ {"error":{"message":"{\\n \\"error\\": {\\n \\"code\\": 429,\\n \\"message\\": \\"Resource has been exhausted (e.g. check quota).\\",\\n \\"status\\": \\"RESOURCE_EXHAUSTED\\"\\n }\\n}\\n","code":429}}
9
+ `;
10
+
11
+ const result = await safeParseJSON({
12
+ text: error,
13
+ schema: deepgramErrorDataSchema,
14
+ });
15
+
16
+ expect(result).toStrictEqual({
17
+ success: true,
18
+ value: {
19
+ error: {
20
+ message:
21
+ '{\n "error": {\n "code": 429,\n "message": "Resource has been exhausted (e.g. check quota).",\n "status": "RESOURCE_EXHAUSTED"\n }\n}\n',
22
+ code: 429,
23
+ },
24
+ },
25
+ rawValue: {
26
+ error: {
27
+ message:
28
+ '{\n "error": {\n "code": 429,\n "message": "Resource has been exhausted (e.g. check quota).",\n "status": "RESOURCE_EXHAUSTED"\n }\n}\n',
29
+ code: 429,
30
+ },
31
+ },
32
+ });
33
+ });
34
+ });
@@ -0,0 +1,16 @@
1
+ import { z } from 'zod/v4';
2
+ import { createJsonErrorResponseHandler } from '@ai-sdk/provider-utils';
3
+
4
+ export const deepgramErrorDataSchema = z.object({
5
+ error: z.object({
6
+ message: z.string(),
7
+ code: z.number(),
8
+ }),
9
+ });
10
+
11
+ export type DeepgramErrorData = z.infer<typeof deepgramErrorDataSchema>;
12
+
13
+ export const deepgramFailedResponseHandler = createJsonErrorResponseHandler({
14
+ errorSchema: deepgramErrorDataSchema,
15
+ errorToMessage: data => data.error.message,
16
+ });
@@ -0,0 +1,139 @@
1
+ import {
2
+ TranscriptionModelV3,
3
+ SpeechModelV3,
4
+ ProviderV3,
5
+ NoSuchModelError,
6
+ } from '@ai-sdk/provider';
7
+ import {
8
+ FetchFunction,
9
+ loadApiKey,
10
+ withUserAgentSuffix,
11
+ } from '@ai-sdk/provider-utils';
12
+ import { DeepgramTranscriptionModel } from './deepgram-transcription-model';
13
+ import { DeepgramTranscriptionModelId } from './deepgram-transcription-options';
14
+ import { DeepgramSpeechModel } from './deepgram-speech-model';
15
+ import { DeepgramSpeechModelId } from './deepgram-speech-options';
16
+ import { VERSION } from './version';
17
+
18
+ export interface DeepgramProvider extends ProviderV3 {
19
+ (
20
+ modelId: 'nova-3',
21
+ settings?: {},
22
+ ): {
23
+ transcription: DeepgramTranscriptionModel;
24
+ };
25
+
26
+ /**
27
+ Creates a model for transcription.
28
+ */
29
+ transcription(modelId: DeepgramTranscriptionModelId): TranscriptionModelV3;
30
+
31
+ /**
32
+ Creates a model for speech generation.
33
+ */
34
+ speech(modelId: DeepgramSpeechModelId): SpeechModelV3;
35
+
36
+ /**
37
+ * @deprecated Use `embeddingModel` instead.
38
+ */
39
+ textEmbeddingModel(modelId: string): never;
40
+ }
41
+
42
+ export interface DeepgramProviderSettings {
43
+ /**
44
+ API key for authenticating requests.
45
+ */
46
+ apiKey?: string;
47
+
48
+ /**
49
+ Custom headers to include in the requests.
50
+ */
51
+ headers?: Record<string, string>;
52
+
53
+ /**
54
+ Custom fetch implementation. You can use it as a middleware to intercept requests,
55
+ or to provide a custom fetch implementation for e.g. testing.
56
+ */
57
+ fetch?: FetchFunction;
58
+ }
59
+
60
+ /**
61
+ Create an Deepgram provider instance.
62
+ */
63
+ export function createDeepgram(
64
+ options: DeepgramProviderSettings = {},
65
+ ): DeepgramProvider {
66
+ const getHeaders = () =>
67
+ withUserAgentSuffix(
68
+ {
69
+ authorization: `Token ${loadApiKey({
70
+ apiKey: options.apiKey,
71
+ environmentVariableName: 'DEEPGRAM_API_KEY',
72
+ description: 'Deepgram',
73
+ })}`,
74
+ ...options.headers,
75
+ },
76
+ `ai-sdk/deepgram/${VERSION}`,
77
+ );
78
+
79
+ const createTranscriptionModel = (modelId: DeepgramTranscriptionModelId) =>
80
+ new DeepgramTranscriptionModel(modelId, {
81
+ provider: `deepgram.transcription`,
82
+ url: ({ path }) => `https://api.deepgram.com${path}`,
83
+ headers: getHeaders,
84
+ fetch: options.fetch,
85
+ });
86
+
87
+ const createSpeechModel = (modelId: DeepgramSpeechModelId) =>
88
+ new DeepgramSpeechModel(modelId, {
89
+ provider: `deepgram.speech`,
90
+ url: ({ path }) => `https://api.deepgram.com${path}`,
91
+ headers: getHeaders,
92
+ fetch: options.fetch,
93
+ });
94
+
95
+ const provider = function (modelId: DeepgramTranscriptionModelId) {
96
+ return {
97
+ transcription: createTranscriptionModel(modelId),
98
+ };
99
+ };
100
+
101
+ provider.specificationVersion = 'v3' as const;
102
+ provider.transcription = createTranscriptionModel;
103
+ provider.transcriptionModel = createTranscriptionModel;
104
+ provider.speech = createSpeechModel;
105
+ provider.speechModel = createSpeechModel;
106
+
107
+ // Required ProviderV3 methods that are not supported
108
+ provider.languageModel = (modelId: string) => {
109
+ throw new NoSuchModelError({
110
+ modelId,
111
+ modelType: 'languageModel',
112
+ message: 'Deepgram does not provide language models',
113
+ });
114
+ };
115
+
116
+ provider.embeddingModel = (modelId: string) => {
117
+ throw new NoSuchModelError({
118
+ modelId,
119
+ modelType: 'embeddingModel',
120
+ message: 'Deepgram does not provide text embedding models',
121
+ });
122
+ };
123
+ provider.textEmbeddingModel = provider.embeddingModel;
124
+
125
+ provider.imageModel = (modelId: string) => {
126
+ throw new NoSuchModelError({
127
+ modelId,
128
+ modelType: 'imageModel',
129
+ message: 'Deepgram does not provide image models',
130
+ });
131
+ };
132
+
133
+ return provider as DeepgramProvider;
134
+ }
135
+
136
+ /**
137
+ Default Deepgram provider instance.
138
+ */
139
+ export const deepgram = createDeepgram();
@@ -0,0 +1,15 @@
1
+ export type DeepgramSpeechAPITypes = {
2
+ // Request body
3
+ text: string;
4
+
5
+ // Query parameters (these are set via query params, not body)
6
+ model?: string;
7
+ encoding?: string;
8
+ sample_rate?: number;
9
+ bit_rate?: number | string;
10
+ container?: string;
11
+ callback?: string;
12
+ callback_method?: 'POST' | 'PUT';
13
+ mip_opt_out?: boolean;
14
+ tag?: string | string[];
15
+ };