Skip to content

Commit

Permalink
Add eslint rule to detect (some) side effects (that prevent tree shak…
Browse files Browse the repository at this point in the history
…ing)
  • Loading branch information
nfcampos committed Mar 14, 2023
1 parent 29d5631 commit 7e235e1
Show file tree
Hide file tree
Showing 25 changed files with 161 additions and 30 deletions.
3 changes: 2 additions & 1 deletion langchain/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ module.exports = {
project: "./tsconfig.json",
sourceType: "module",
},
plugins: ["@typescript-eslint"],
plugins: ["@typescript-eslint", "tree-shaking"],
ignorePatterns: [".eslintrc.cjs", "create-entrypoints.js", "node_modules"],
rules: {
"tree-shaking/no-side-effects-in-initialization": 2,
"@typescript-eslint/explicit-module-boundary-types": 0,
"@typescript-eslint/no-empty-function": 0,
"@typescript-eslint/no-shadow": 0,
Expand Down
4 changes: 3 additions & 1 deletion langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"@types/uuid": "^9",
"@typescript-eslint/eslint-plugin": "^5.51.0",
"@typescript-eslint/parser": "^5.51.0",
"agadoo": "^3.0.0",
"babel-jest": "^29.4.2",
"cheerio": "^1.0.0-rc.12",
"cohere-ai": "^5.0.2",
Expand All @@ -88,6 +89,7 @@
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-tree-shaking": "^1.10.0",
"hnswlib-node": "^1.4.2",
"husky": "^8.0.3",
"jest": "^29.4.2",
Expand Down Expand Up @@ -268,4 +270,4 @@
"import": "./output_parsers.js"
}
}
}
}
5 changes: 5 additions & 0 deletions langchain/src/agents/tests/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"tree-shaking/no-side-effects-in-initialization": 0
}
}
11 changes: 9 additions & 2 deletions langchain/src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable tree-shaking/no-side-effects-in-initialization */
import crypto from "node:crypto";
import type { RedisClientType } from "redis";
import { Generation } from "./schema/index.js";
Expand All @@ -24,12 +25,14 @@ export abstract class BaseCache<T = Generation[]> {
abstract update(prompt: string, llmKey: string, value: T): Promise<void>;
}

const GLOBAL_MAP = new Map();

export class InMemoryCache<T = Generation[]> extends BaseCache<T> {
#cache: Map<string, T>;

constructor() {
constructor(map?: Map<string, T>) {
super();
this.#cache = new Map();
this.#cache = map ?? new Map();
}

lookup(prompt: string, llmKey: string): Promise<T | null> {
Expand All @@ -41,6 +44,10 @@ export class InMemoryCache<T = Generation[]> extends BaseCache<T> {
async update(prompt: string, llmKey: string, value: T): Promise<void> {
this.#cache.set(getCacheKey(prompt, llmKey), value);
}

static global(): InMemoryCache {
return new InMemoryCache(GLOBAL_MAP);
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions langchain/src/callbacks/tests/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"tree-shaking/no-side-effects-in-initialization": 0
}
}
10 changes: 4 additions & 6 deletions langchain/src/chains/chat_vector_db_chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@ Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:`;
const question_generator_prompt = PromptTemplate.fromTemplate(
question_generator_template
);

const qa_template = `Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
{context}
Question: {question}
Helpful Answer:`;
const qa_prompt = PromptTemplate.fromTemplate(qa_template);

export interface ChatVectorDBQAChainInput {
vectorstore: VectorStore;
Expand Down Expand Up @@ -189,9 +185,11 @@ export class ChatVectorDBQAChain
returnSourceDocuments?: boolean;
}
): ChatVectorDBQAChain {
const qaChain = loadQAStuffChain(llm, { prompt: qa_prompt });
const qaChain = loadQAStuffChain(llm, {
prompt: PromptTemplate.fromTemplate(qa_template),
});
const questionGeneratorChain = new LLMChain({
prompt: question_generator_prompt,
prompt: PromptTemplate.fromTemplate(question_generator_template),
llm,
});
const instance = new this({
Expand Down
12 changes: 6 additions & 6 deletions langchain/src/chains/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ Current conversation:
Human: {input}
AI:`;

const defaultPrompt = new PromptTemplate({
template: defaultTemplate,
inputVariables: ["history", "input"],
});

export class ConversationChain extends LLMChain {
constructor(fields: {
llm: BaseLanguageModel;
Expand All @@ -24,7 +19,12 @@ export class ConversationChain extends LLMChain {
memory?: BaseMemory;
}) {
super({
prompt: fields.prompt ?? defaultPrompt,
prompt:
fields.prompt ??
new PromptTemplate({
template: defaultTemplate,
inputVariables: ["history", "input"],
}),
llm: fields.llm,
outputKey: fields.outputKey ?? "response",
});
Expand Down
12 changes: 6 additions & 6 deletions langchain/src/chains/llm_chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,6 @@ Current conversation:
Human: {input}
AI:`;

const defaultPrompt = new PromptTemplate({
template: defaultTemplate,
inputVariables: ["history", "input"],
});

export class ConversationChain extends LLMChain {
constructor(fields: {
llm: BaseLanguageModel;
Expand All @@ -142,7 +137,12 @@ export class ConversationChain extends LLMChain {
memory?: BaseMemory;
}) {
super({
prompt: fields.prompt ?? defaultPrompt,
prompt:
fields.prompt ??
new PromptTemplate({
template: defaultTemplate,
inputVariables: ["history", "input"],
}),
llm: fields.llm,
outputKey: fields.outputKey ?? "response",
});
Expand Down
5 changes: 5 additions & 0 deletions langchain/src/chains/question_answering/tests/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"tree-shaking/no-side-effects-in-initialization": 0
}
}
5 changes: 5 additions & 0 deletions langchain/src/chains/summarization/tests/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"tree-shaking/no-side-effects-in-initialization": 0
}
}
5 changes: 5 additions & 0 deletions langchain/src/chains/tests/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"tree-shaking/no-side-effects-in-initialization": 0
}
}
5 changes: 5 additions & 0 deletions langchain/src/chat_models/tests/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"tree-shaking/no-side-effects-in-initialization": 0
}
}
5 changes: 5 additions & 0 deletions langchain/src/document_loaders/tests/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"tree-shaking/no-side-effects-in-initialization": 0
}
}
5 changes: 5 additions & 0 deletions langchain/src/embeddings/tests/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"tree-shaking/no-side-effects-in-initialization": 0
}
}
4 changes: 1 addition & 3 deletions langchain/src/llms/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
BaseLanguageModelParams,
} from "../base_language/index.js";

const GLOBAL_CACHE: BaseCache = new InMemoryCache();

export type SerializedLLM = {
_model: string;
_type: string;
Expand Down Expand Up @@ -45,7 +43,7 @@ export abstract class BaseLLM extends BaseLanguageModel {
if (cache instanceof BaseCache) {
this.cache = cache;
} else if (cache) {
this.cache = GLOBAL_CACHE;
this.cache = InMemoryCache.global();
} else {
this.cache = undefined;
}
Expand Down
5 changes: 5 additions & 0 deletions langchain/src/llms/tests/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"tree-shaking/no-side-effects-in-initialization": 0
}
}
5 changes: 5 additions & 0 deletions langchain/src/memory/tests/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"tree-shaking/no-side-effects-in-initialization": 0
}
}
5 changes: 5 additions & 0 deletions langchain/src/output_parsers/tests/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"tree-shaking/no-side-effects-in-initialization": 0
}
}
5 changes: 5 additions & 0 deletions langchain/src/prompts/tests/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"tree-shaking/no-side-effects-in-initialization": 0
}
}
5 changes: 3 additions & 2 deletions langchain/src/sql_db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataSource, DataSourceOptions } from "typeorm";
import type { DataSource as DataSourceT, DataSourceOptions } from "typeorm";
import {
generateTableInfoFromTables,
getTableAndColumnsName,
Expand All @@ -16,7 +16,7 @@ export class SqlDatabase
{
appDataSourceOptions: DataSourceOptions;

appDataSource: DataSource;
appDataSource: DataSourceT;

allTables: Array<SqlTable> = [];

Expand Down Expand Up @@ -62,6 +62,7 @@ export class SqlDatabase
static async fromOptionsParams(
fields: SqlDatabaseOptionsParams
): Promise<SqlDatabase> {
const { DataSource } = await import("typeorm");
const dataSource = new DataSource(fields.appDataSourceOptions);
return SqlDatabase.fromDataSourceParams({
...fields,
Expand Down
5 changes: 5 additions & 0 deletions langchain/src/tests/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"tree-shaking/no-side-effects-in-initialization": 0
}
}
2 changes: 1 addition & 1 deletion langchain/src/util/sql_utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataSource, DataSourceOptions } from "typeorm";
import type { DataSource, DataSourceOptions } from "typeorm";

interface RawResultTableAndColumn {
table_name: string;
Expand Down
5 changes: 5 additions & 0 deletions langchain/src/util/tests/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"tree-shaking/no-side-effects-in-initialization": 0
}
}
5 changes: 5 additions & 0 deletions langchain/src/vectorstores/tests/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"tree-shaking/no-side-effects-in-initialization": 0
}
}
Loading

0 comments on commit 7e235e1

Please sign in to comment.