Skip to content

Commit 9f9656e

Browse files
committed
feat: added maestro functions
1 parent 2612aa2 commit 9f9656e

File tree

13 files changed

+249
-2
lines changed

13 files changed

+249
-2
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { AI21 } from 'ai21';
2+
3+
const TIMEOUT = 20000;
4+
const INTERVAL = 1500;
5+
6+
async function main() {
7+
const client = new AI21({ apiKey: process.env.AI21_API_KEY });
8+
9+
const response = await client.beta.maestro.runs.create_and_poll(
10+
{
11+
input: 'Hello, how are you? tell me a short story about a wizard',
12+
},
13+
{
14+
timeout: TIMEOUT,
15+
interval: INTERVAL,
16+
},
17+
);
18+
console.log(response);
19+
}
20+
21+
main().catch(console.error);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { AI21 } from 'ai21';
2+
3+
async function main() {
4+
const client = new AI21({ apiKey: process.env.AI21_API_KEY });
5+
6+
const { id } = await client.beta.maestro.runs.create({
7+
input: 'Hello, how are you? tell me a short story about a wizard',
8+
});
9+
10+
const response = await client.beta.maestro.runs.get(id);
11+
12+
console.log(response);
13+
}
14+
15+
main().catch(console.error);

src/AI21.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { AI21Error, MissingAPIKeyError } from './errors';
44
import { APIClient } from './APIClient';
55
import { Headers } from './types';
66
import * as Runtime from './runtime';
7-
import { Chat, ConversationalRag, Library } from './resources';
7+
import { Beta, Chat, ConversationalRag, Library } from './resources';
88

99
export interface ClientOptions {
1010
baseURL?: string | undefined;
@@ -67,6 +67,7 @@ export class AI21 extends APIClient {
6767
chat: Chat = new Chat(this);
6868
conversationalRag: ConversationalRag = new ConversationalRag(this);
6969
library: Library = new Library(this);
70+
beta: Beta = new Beta(this);
7071

7172
// eslint-disable-next-line @typescript-eslint/no-unused-vars
7273
protected override authHeaders(_: Types.FinalRequestOptions): Types.Headers {

src/resources/chat/completions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Models from '../../types';
22
import { APIResource } from '../../APIResource';
33
import { Stream } from '../../streaming';
44

5-
const deprecatedModels = ['jamba-1.5-mini', 'jamba-1.5-large'];
5+
const deprecatedModels = ['jamba-1.6-mini', 'jamba-1.6-large'];
66

77
export class Completions extends APIResource {
88
create(

src/resources/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { Chat, Completions } from './chat';
22
export { ConversationalRag } from './rag';
33
export { Library } from './library';
4+
export { Maestro, Beta, Runs } from './maestro';

src/resources/maestro/beta.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { APIResource } from '../../APIResource';
2+
import { Maestro } from './maestro';
3+
4+
export class Beta extends APIResource {
5+
maestro: Maestro = new Maestro(this.client);
6+
}

src/resources/maestro/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { Beta } from './beta';
2+
export { Maestro } from './maestro';
3+
export { Runs } from './runs';

src/resources/maestro/maestro.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { APIResource } from '../../APIResource';
2+
import { Runs } from './runs';
3+
4+
export class Maestro extends APIResource {
5+
runs: Runs = new Runs(this.client);
6+
}

src/resources/maestro/runs.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { APIResource } from '../../APIResource';
2+
import { MaestroRunRequest, MaestroRunResponse, RequestOptions, MaestroRunRequestOptions } from '../../types';
3+
4+
const MAESTRO_PATH = '/maestro/runs';
5+
const DEFAULT_TIMEOUT = 30000;
6+
const DEFAULT_INTERVAL = 1000;
7+
8+
export class Runs extends APIResource {
9+
async create(body: MaestroRunRequest): Promise<MaestroRunResponse> {
10+
return this.client.post<MaestroRunRequest, MaestroRunResponse>(MAESTRO_PATH, {
11+
body,
12+
} as RequestOptions<MaestroRunRequest>) as Promise<MaestroRunResponse>;
13+
}
14+
15+
async get(runId: string): Promise<MaestroRunResponse> {
16+
return this.client.get<string, MaestroRunResponse>(
17+
`${MAESTRO_PATH}/${runId}`,
18+
) as Promise<MaestroRunResponse>;
19+
}
20+
21+
private async poll({
22+
runId,
23+
timeout,
24+
interval,
25+
}: {
26+
runId: string;
27+
timeout: number;
28+
interval: number;
29+
}): Promise<MaestroRunResponse> {
30+
const startTime = Date.now();
31+
32+
while (Date.now() - startTime < timeout) {
33+
const response = await this.get(runId);
34+
if (response.status === 'completed') {
35+
return response;
36+
}
37+
38+
await new Promise((resolve) => setTimeout(resolve, interval));
39+
}
40+
41+
throw new Error(`Maestro run ${runId} timed out after ${timeout}ms`);
42+
}
43+
44+
async create_and_poll(
45+
body: MaestroRunRequest,
46+
options?: MaestroRunRequestOptions,
47+
): Promise<MaestroRunResponse> {
48+
const response = await this.create(body);
49+
return this.poll({
50+
runId: response.id,
51+
timeout: options?.timeout ?? DEFAULT_TIMEOUT,
52+
interval: options?.interval ?? DEFAULT_INTERVAL,
53+
});
54+
}
55+
}

src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,5 @@ export {
5353
type UpdateFileRequest,
5454
type FilePathOrFileObject,
5555
} from './files';
56+
57+
export { type MaestroRunRequest, type MaestroRunResponse, type MaestroRunRequestOptions } from './maestro';

0 commit comments

Comments
 (0)