Spaces:
Running
Running
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
- lib/hf-client.ts +18 -3
- lib/openai-client.ts +21 -6
lib/hf-client.ts
CHANGED
@@ -1,6 +1,21 @@
|
|
1 |
import OpenAI from "openai";
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
});
|