From 23e33ef2bf6fb238e342764738231b46f1688b72 Mon Sep 17 00:00:00 2001 From: Willy Douhard Date: Tue, 17 Sep 2024 19:29:56 +0200 Subject: [PATCH 1/2] fix: use getters to lazy load modules --- package.json | 2 +- src/instrumentation/index.ts | 122 +++++++++++++++++++---------------- 2 files changed, 67 insertions(+), 57 deletions(-) diff --git a/package.json b/package.json index 47e7691..cfda88a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@literalai/client", - "version": "0.0.517", + "version": "0.0.518", "description": "", "exports": { ".": { diff --git a/src/instrumentation/index.ts b/src/instrumentation/index.ts index c50da06..6d024e6 100644 --- a/src/instrumentation/index.ts +++ b/src/instrumentation/index.ts @@ -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: (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: (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: (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 (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.' + ); + } + }; } } }); From 4d962bd291c878e2c4b95e38d2b5ac8307c39c58 Mon Sep 17 00:00:00 2001 From: Willy Douhard Date: Tue, 17 Sep 2024 19:45:36 +0200 Subject: [PATCH 2/2] lazy load CustomChatPromptTemplate --- src/prompt-engineering/prompt.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/prompt-engineering/prompt.ts b/src/prompt-engineering/prompt.ts index bee2abe..e21da43 100644 --- a/src/prompt-engineering/prompt.ts +++ b/src/prompt-engineering/prompt.ts @@ -6,7 +6,6 @@ import { import { API } from '../api'; import { DatasetItem } from '../evaluation/dataset'; -import { CustomChatPromptTemplate } from '../instrumentation/langchain'; import { GenerationType, IGenerationMessage @@ -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