Skip to content
This repository was archived by the owner on Aug 5, 2025. It is now read-only.

Commit 6e5b523

Browse files
authored
Merge pull request #69 from Chainlit/willy/integration-lazy-loading
fix: use getters to lazy load modules
2 parents cae92d5 + 4d962bd commit 6e5b523

File tree

3 files changed

+71
-58
lines changed

3 files changed

+71
-58
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@literalai/client",
3-
"version": "0.0.517",
3+
"version": "0.0.518",
44
"description": "",
55
"exports": {
66
".": {

src/instrumentation/index.ts

Lines changed: 66 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,75 +19,85 @@ export default (client: LiteralClient) => ({
1919
* @param options.metadata Metadata to attach to all generations.
2020
* @returns
2121
*/
22-
openai: (options?: OpenAIGlobalOptions) => {
23-
try {
24-
// eslint-disable-next-line @typescript-eslint/no-var-requires
25-
const { default: instrumentOpenAI } = require('./openai');
26-
return instrumentOpenAI(client, options);
27-
} catch (error) {
28-
throw new Error(
29-
'Failed to load OpenAI. Please ensure openai is installed.'
30-
);
31-
}
32-
},
33-
34-
langchain: {
35-
literalCallback: (params?: {
36-
threadId?: string;
37-
chainTypesToIgnore?: string[];
38-
}) => {
22+
get openai() {
23+
return (options?: OpenAIGlobalOptions) => {
3924
try {
4025
// eslint-disable-next-line @typescript-eslint/no-var-requires
41-
const { LiteralCallbackHandler } = require('./langchain');
42-
return new LiteralCallbackHandler(
43-
client,
44-
params?.threadId,
45-
params?.chainTypesToIgnore
46-
);
26+
const { default: instrumentOpenAI } = require('./openai');
27+
return instrumentOpenAI(client, options);
4728
} catch (error) {
4829
throw new Error(
49-
'Failed to load langchain. Please ensure langchain is installed.'
30+
'Failed to load OpenAI. Please ensure openai is installed.'
5031
);
5132
}
52-
}
33+
};
5334
},
5435

55-
vercel: {
56-
instrument: <TFunction extends AllVercelFn>(fn: TFunction) => {
57-
try {
58-
// eslint-disable-next-line @typescript-eslint/no-var-requires
59-
const { makeInstrumentVercelSDK } = require('./vercel-sdk');
60-
return makeInstrumentVercelSDK(client)(fn);
61-
} catch (error) {
62-
throw new Error(
63-
'Failed to load Vercel SDK. Please ensure @vercel/ai is installed.'
64-
);
36+
get langchain() {
37+
return {
38+
literalCallback: (params?: {
39+
threadId?: string;
40+
chainTypesToIgnore?: string[];
41+
}) => {
42+
try {
43+
// eslint-disable-next-line @typescript-eslint/no-var-requires
44+
const { LiteralCallbackHandler } = require('./langchain');
45+
return new LiteralCallbackHandler(
46+
client,
47+
params?.threadId,
48+
params?.chainTypesToIgnore
49+
);
50+
} catch (error) {
51+
throw new Error(
52+
'Failed to load langchain. Please ensure langchain is installed.'
53+
);
54+
}
6555
}
66-
}
56+
};
6757
},
6858

69-
llamaIndex: {
70-
instrument: () => {
71-
try {
72-
// eslint-disable-next-line @typescript-eslint/no-var-requires
73-
const { instrumentLlamaIndex } = require('./llamaindex');
74-
return instrumentLlamaIndex(client);
75-
} catch (error) {
76-
throw new Error(
77-
'Failed to load LlamaIndex. Please ensure llamaindex is installed.'
78-
);
59+
get vercel() {
60+
return {
61+
instrument: <TFunction extends AllVercelFn>(fn: TFunction) => {
62+
try {
63+
// eslint-disable-next-line @typescript-eslint/no-var-requires
64+
const { makeInstrumentVercelSDK } = require('./vercel-sdk');
65+
return makeInstrumentVercelSDK(client)(fn);
66+
} catch (error) {
67+
throw new Error(
68+
'Failed to load Vercel SDK. Please ensure @vercel/ai is installed.'
69+
);
70+
}
7971
}
72+
};
73+
},
74+
75+
llamaIndex: {
76+
get instrument() {
77+
return () => {
78+
try {
79+
// eslint-disable-next-line @typescript-eslint/no-var-requires
80+
const { instrumentLlamaIndex } = require('./llamaindex');
81+
return instrumentLlamaIndex(client);
82+
} catch (error) {
83+
throw new Error(
84+
'Failed to load LlamaIndex. Please ensure llamaindex is installed.'
85+
);
86+
}
87+
};
8088
},
81-
withThread: <R>(thread: Thread, callback: () => R) => {
82-
try {
83-
// eslint-disable-next-line @typescript-eslint/no-var-requires
84-
const { withThread } = require('./llamaindex');
85-
return withThread(thread, callback);
86-
} catch (error) {
87-
throw new Error(
88-
'Failed to load LlamaIndex. Please ensure llamaindex is installed.'
89-
);
90-
}
89+
get withThread() {
90+
return <R>(thread: Thread, callback: () => R) => {
91+
try {
92+
// eslint-disable-next-line @typescript-eslint/no-var-requires
93+
const { withThread } = require('./llamaindex');
94+
return withThread(thread, callback);
95+
} catch (error) {
96+
throw new Error(
97+
'Failed to load LlamaIndex. Please ensure llamaindex is installed.'
98+
);
99+
}
100+
};
91101
}
92102
}
93103
});

src/prompt-engineering/prompt.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66

77
import { API } from '../api';
88
import { DatasetItem } from '../evaluation/dataset';
9-
import { CustomChatPromptTemplate } from '../instrumentation/langchain';
109
import {
1110
GenerationType,
1211
IGenerationMessage
@@ -133,6 +132,10 @@ export class Prompt extends PromptFields {
133132
* @returns A custom chat prompt template configured with the prompt's data.
134133
*/
135134
toLangchainChatPromptTemplate() {
135+
const {
136+
CustomChatPromptTemplate
137+
// eslint-disable-next-line @typescript-eslint/no-var-requires
138+
} = require('../instrumentation/langchain');
136139
const lcMessages: [string, string][] = this.templateMessages.map((m) => [
137140
m.role,
138141
m.content as string

0 commit comments

Comments
 (0)