File tree Expand file tree Collapse file tree 5 files changed +42
-8
lines changed Expand file tree Collapse file tree 5 files changed +42
-8
lines changed Original file line number Diff line number Diff line change 1
- import { STUDIO_BASE_URL } from "./Constants" ;
2
1
import * as Core from "./Core" ;
2
+ import { AI21EnvConfig } from "./EnvConfig" ;
3
+ import { MissingAPIKeyError } from "./errors" ;
3
4
import { Chat } from "./resources/chat" ;
4
5
5
6
export interface AI21Options {
6
7
apiKey : string ;
7
8
baseURL : string ;
8
9
via : string | null ;
10
+ timeout : number ;
11
+ maxRetries : number ;
9
12
}
10
13
export class AI21 extends Core . APIClient {
14
+ private apiKey : string ;
11
15
private _options : AI21Options ;
12
16
13
17
constructor (
14
- apiKey : string = process . env . AI21_API_KEY ?? '' ,
15
- baseURL : string = process . env . AI21_BASE_URL ?? STUDIO_BASE_URL ,
18
+ apiKey : string = AI21EnvConfig . API_KEY ,
19
+ baseURL : string = AI21EnvConfig . BASE_URL ,
20
+ timeout : number = AI21EnvConfig . TIMEOUT_SECONDS ,
21
+ maxRetries : number = AI21EnvConfig . MAX_RETRIES ,
16
22
via : string | null = null ,
17
23
) {
18
24
const options : AI21Options = {
19
25
apiKey,
20
26
baseURL,
21
27
via,
28
+ timeout,
29
+ maxRetries,
22
30
} ;
23
31
24
32
super ( {
25
33
baseURL,
26
- timeout : 60000 ,
34
+ timeout,
27
35
apiKey,
36
+ maxRetries,
28
37
options : { }
29
38
} ) ;
30
- this . apiKey = apiKey
39
+
40
+ if ( ! apiKey ) {
41
+ throw new MissingAPIKeyError ( ) ;
42
+ }
43
+
44
+ this . apiKey = apiKey ;
31
45
this . _options = options ;
32
46
}
33
47
Original file line number Diff line number Diff line change 1
- export const STUDIO_BASE_URL = 'https://api.ai21.com/studio/v1' ;
1
+ export const STUDIO_BASE_URL = 'https://api.ai21.com/studio/v1' ;
2
+ export const DEFAULT_TIMEOUT = 60000 ;
Original file line number Diff line number Diff line change @@ -143,7 +143,6 @@ export class APIPromise<T> extends Promise<T> {
143
143
144
144
export abstract class APIClient {
145
145
protected options : ClientOptions ;
146
- protected apiKey : string ;
147
146
protected baseURL : string ;
148
147
protected maxRetries : number ;
149
148
protected timeout : number ;
@@ -165,7 +164,6 @@ export abstract class APIClient {
165
164
this . maxRetries = validatePositiveInteger ( 'maxRetries' , maxRetries ) ;
166
165
this . timeout = validatePositiveInteger ( 'timeout' , timeout ) ;
167
166
168
- this . apiKey = apiKey ;
169
167
this . options = options ;
170
168
}
171
169
get < Req , Rsp > ( path : string , opts ?: PromiseOrValue < RequestOptions < Req > > ) : APIPromise < Rsp > {
Original file line number Diff line number Diff line change
1
+ import { DEFAULT_TIMEOUT } from "./Constants" ;
2
+
3
+ import { STUDIO_BASE_URL } from "./Constants" ;
4
+
5
+ export class AI21EnvConfig {
6
+ static readonly API_KEY = process . env . AI21_API_KEY ?? '' ;
7
+ static readonly BASE_URL = process . env . AI21_BASE_URL ?? STUDIO_BASE_URL ;
8
+ static readonly TIMEOUT_SECONDS = process . env . AI21_TIMEOUT_SECONDS ?
9
+ Number ( process . env . AI21_TIMEOUT_SECONDS ) :
10
+ DEFAULT_TIMEOUT ;
11
+ static readonly MAX_RETRIES = process . env . AI21_MAX_RETRIES ?
12
+ Number ( process . env . AI21_MAX_RETRIES ) :
13
+ 3 ;
14
+ static readonly LOG_LEVEL = process . env . AI21_LOG_LEVEL ?? 'info' ;
15
+ }
Original file line number Diff line number Diff line change 1
1
export class AI21Error extends Error { }
2
+
3
+ export class MissingAPIKeyError extends AI21Error {
4
+ constructor ( ) {
5
+ super ( "API key is required" ) ;
6
+ }
7
+ }
You can’t perform that action at this time.
0 commit comments