Skip to content

fix: use getters to lazy load modules #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@literalai/client",
"version": "0.0.517",
"version": "0.0.518",
"description": "",
"exports": {
".": {
Expand Down
122 changes: 66 additions & 56 deletions src/instrumentation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,75 +19,85 @@ export default (client: LiteralClient) => ({
* @param options.metadata Metadata to attach to all generations.
* @returns
*/
openai: (options?: OpenAIGlobalOptions) => {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { default: instrumentOpenAI } = require('./openai');
return instrumentOpenAI(client, options);
} catch (error) {
throw new Error(
'Failed to load OpenAI. Please ensure openai is installed.'
);
}
},

langchain: {
literalCallback: (params?: {
threadId?: string;
chainTypesToIgnore?: string[];
}) => {
get openai() {
return (options?: OpenAIGlobalOptions) => {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { LiteralCallbackHandler } = require('./langchain');
return new LiteralCallbackHandler(
client,
params?.threadId,
params?.chainTypesToIgnore
);
const { default: instrumentOpenAI } = require('./openai');
return instrumentOpenAI(client, options);
} catch (error) {
throw new Error(
'Failed to load langchain. Please ensure langchain is installed.'
'Failed to load OpenAI. Please ensure openai is installed.'
);
}
}
};
},

vercel: {
instrument: <TFunction extends AllVercelFn>(fn: TFunction) => {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { makeInstrumentVercelSDK } = require('./vercel-sdk');
return makeInstrumentVercelSDK(client)(fn);
} catch (error) {
throw new Error(
'Failed to load Vercel SDK. Please ensure @vercel/ai is installed.'
);
get langchain() {
return {
literalCallback: (params?: {
threadId?: string;
chainTypesToIgnore?: string[];
}) => {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { LiteralCallbackHandler } = require('./langchain');
return new LiteralCallbackHandler(
client,
params?.threadId,
params?.chainTypesToIgnore
);
} catch (error) {
throw new Error(
'Failed to load langchain. Please ensure langchain is installed.'
);
}
}
}
};
},

llamaIndex: {
instrument: () => {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { instrumentLlamaIndex } = require('./llamaindex');
return instrumentLlamaIndex(client);
} catch (error) {
throw new Error(
'Failed to load LlamaIndex. Please ensure llamaindex is installed.'
);
get vercel() {
return {
instrument: <TFunction extends AllVercelFn>(fn: TFunction) => {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { makeInstrumentVercelSDK } = require('./vercel-sdk');
return makeInstrumentVercelSDK(client)(fn);
} catch (error) {
throw new Error(
'Failed to load Vercel SDK. Please ensure @vercel/ai is installed.'
);
}
}
};
},

llamaIndex: {
get instrument() {
return () => {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { instrumentLlamaIndex } = require('./llamaindex');
return instrumentLlamaIndex(client);
} catch (error) {
throw new Error(
'Failed to load LlamaIndex. Please ensure llamaindex is installed.'
);
}
};
},
withThread: <R>(thread: Thread, callback: () => R) => {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { withThread } = require('./llamaindex');
return withThread(thread, callback);
} catch (error) {
throw new Error(
'Failed to load LlamaIndex. Please ensure llamaindex is installed.'
);
}
get withThread() {
return <R>(thread: Thread, callback: () => R) => {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { withThread } = require('./llamaindex');
return withThread(thread, callback);
} catch (error) {
throw new Error(
'Failed to load LlamaIndex. Please ensure llamaindex is installed.'
);
}
};
}
}
});
5 changes: 4 additions & 1 deletion src/prompt-engineering/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {

import { API } from '../api';
import { DatasetItem } from '../evaluation/dataset';
import { CustomChatPromptTemplate } from '../instrumentation/langchain';
import {
GenerationType,
IGenerationMessage
Expand Down Expand Up @@ -133,6 +132,10 @@ export class Prompt extends PromptFields {
* @returns A custom chat prompt template configured with the prompt's data.
*/
toLangchainChatPromptTemplate() {
const {
CustomChatPromptTemplate
// eslint-disable-next-line @typescript-eslint/no-var-requires
} = require('../instrumentation/langchain');
const lcMessages: [string, string][] = this.templateMessages.map((m) => [
m.role,
m.content as string
Expand Down
Loading