victor HF Staff commited on
Commit
e569cd5
·
1 Parent(s): 4359c33

refactor: Implement lazy initialization for API clients

Browse files

- Convert OpenAI and Hugging Face clients to use lazy initialization
- Reduce startup time by deferring client instantiation
- Maintain backward compatibility with proxy pattern
- Support all existing environment variable configurations

Files changed (2) hide show
  1. lib/hf-client.ts +18 -3
  2. lib/openai-client.ts +21 -6
lib/hf-client.ts CHANGED
@@ -1,6 +1,21 @@
1
  import OpenAI from "openai";
2
 
3
- export const hf = new OpenAI({
4
- apiKey: process.env.HF_TOKEN,
5
- baseURL: "https://router.huggingface.co/v1",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  });
 
1
  import OpenAI from "openai";
2
 
3
+ let hfInstance: OpenAI | null = null;
4
+
5
+ export function getHF(): OpenAI {
6
+ if (!hfInstance) {
7
+ hfInstance = new OpenAI({
8
+ apiKey: process.env.HF_TOKEN,
9
+ baseURL: "https://router.huggingface.co/v1",
10
+ });
11
+ }
12
+ return hfInstance;
13
+ }
14
+
15
+ // Export a getter that matches the original export name
16
+ export const hf = new Proxy({} as OpenAI, {
17
+ get(_target, prop, _receiver) {
18
+ const client = getHF();
19
+ return Reflect.get(client, prop, client);
20
+ }
21
  });
lib/openai-client.ts CHANGED
@@ -1,9 +1,24 @@
1
  import OpenAI from "openai";
2
 
3
- export const openai = new OpenAI({
4
- apiKey: process.env.OPENAI_API_KEY || "",
5
- baseURL: process.env.OPENAI_BASE_URL || undefined,
6
- defaultHeaders: process.env.OPENAI_EXTRA_HEADERS
7
- ? JSON.parse(process.env.OPENAI_EXTRA_HEADERS)
8
- : undefined,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  });
 
1
  import OpenAI from "openai";
2
 
3
+ let openaiInstance: OpenAI | null = null;
4
+
5
+ export function getOpenAI(): OpenAI {
6
+ if (!openaiInstance) {
7
+ openaiInstance = new OpenAI({
8
+ apiKey: process.env.OPENAI_API_KEY || "",
9
+ baseURL: process.env.OPENAI_BASE_URL || undefined,
10
+ defaultHeaders: process.env.OPENAI_EXTRA_HEADERS
11
+ ? JSON.parse(process.env.OPENAI_EXTRA_HEADERS)
12
+ : undefined,
13
+ });
14
+ }
15
+ return openaiInstance;
16
+ }
17
+
18
+ // Export a getter that matches the original export name
19
+ export const openai = new Proxy({} as OpenAI, {
20
+ get(_target, prop, _receiver) {
21
+ const client = getOpenAI();
22
+ return Reflect.get(client, prop, client);
23
+ }
24
  });