Skip to content

Commit

Permalink
Remove instanceof checks to support deno on supabase (#667)
Browse files Browse the repository at this point in the history
* Remove instanceof checks to support deno on supabase

* Add yarn lock

* Lint
  • Loading branch information
nfcampos authored Apr 7, 2023
1 parent 837c6fa commit 75c45d6
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 17 deletions.
3 changes: 2 additions & 1 deletion langchain/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
project: "./tsconfig.json",
sourceType: "module",
},
plugins: ["@typescript-eslint", "tree-shaking"],
plugins: ["@typescript-eslint", "tree-shaking", "no-instanceof"],
ignorePatterns: [
".eslintrc.cjs",
"scripts",
Expand Down Expand Up @@ -44,6 +44,7 @@ module.exports = {
},
],
"no-process-env": 2,
"no-instanceof/no-instanceof": 2,
"@typescript-eslint/explicit-module-boundary-types": 0,
"@typescript-eslint/no-empty-function": 0,
"@typescript-eslint/no-shadow": 0,
Expand Down
1 change: 1 addition & 0 deletions langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-no-instanceof": "^1.0.1",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-tree-shaking": "^1.10.0",
"hnswlib-node": "^1.4.2",
Expand Down
2 changes: 2 additions & 0 deletions langchain/src/agents/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ export abstract class Agent extends BaseSingleActionAgent {

return { returnValues: { output: action.log }, log: action.log };
} catch (err) {
// fine to use instanceof because we're in the same module
// eslint-disable-next-line no-instanceof/no-instanceof
if (!(err instanceof ParseError)) {
throw err;
}
Expand Down
2 changes: 2 additions & 0 deletions langchain/src/callbacks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export async function setTracerSession(
callbackManager: CallbackManager = getCallbackManager()
) {
for (const handler of callbackManager.handlers) {
// fine to use instanceof here because we're in the same package
// eslint-disable-next-line no-instanceof/no-instanceof
if (handler instanceof LangChainTracer) {
const sessionName = options?.sessionName;
if (sessionName) {
Expand Down
7 changes: 4 additions & 3 deletions langchain/src/chains/prompt_selector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BaseChatModel } from "../chat_models/base.js";
import { BasePromptTemplate } from "../prompts/base.js";
import { BaseLanguageModel } from "../base_language/index.js";
import { BaseLLM } from "../llms/base.js";

export abstract class BasePromptSelector {
abstract getPrompt(llm: BaseLanguageModel): BasePromptTemplate;
Expand Down Expand Up @@ -37,10 +38,10 @@ export class ConditionalPromptSelector extends BasePromptSelector {
}
}

export function isLLM(llm: BaseLanguageModel): llm is BaseLanguageModel {
return llm instanceof BaseLanguageModel;
export function isLLM(llm: BaseLanguageModel): llm is BaseLLM {
return llm._modelType() === "base_llm";
}

export function isChatModel(llm: BaseLanguageModel): llm is BaseChatModel {
return llm instanceof BaseChatModel;
return llm._modelType() === "base_chat_model";
}
2 changes: 1 addition & 1 deletion langchain/src/llms/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export abstract class BaseLLM extends BaseLanguageModel {

constructor({ cache, concurrency, ...rest }: BaseLLMParams) {
super(concurrency ? { maxConcurrency: concurrency, ...rest } : rest);
if (cache instanceof BaseCache) {
if (typeof cache === "object") {
this.cache = cache;
} else if (cache) {
this.cache = InMemoryCache.global();
Expand Down
18 changes: 6 additions & 12 deletions langchain/src/memory/base.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
BaseChatMessage,
HumanChatMessage,
AIChatMessage,
SystemChatMessage,
ChatMessage,
} from "../schema/index.js";
import { BaseChatMessage, ChatMessage } from "../schema/index.js";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type InputValues = Record<string, any>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -42,14 +36,14 @@ export function getBufferString(
const string_messages: string[] = [];
for (const m of messages) {
let role: string;
if (m instanceof HumanChatMessage) {
if (m._getType() === "human") {
role = human_prefix;
} else if (m instanceof AIChatMessage) {
} else if (m._getType() === "ai") {
role = ai_prefix;
} else if (m instanceof SystemChatMessage) {
} else if (m._getType() === "system") {
role = "System";
} else if (m instanceof ChatMessage) {
role = m.role;
} else if (m._getType() === "generic") {
role = (m as ChatMessage).role;
} else {
throw new Error(`Got unsupported message type: ${m}`);
}
Expand Down
1 change: 1 addition & 0 deletions langchain/src/output_parsers/fix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class OutputFixingParser extends BaseOutputParser {
try {
return await this.parser.parse(completion);
} catch (e) {
// eslint-disable-next-line no-instanceof/no-instanceof
if (e instanceof OutputParserException) {
const result = await this.retryChain.call({
instructions: this.parser.getFormatInstructions(),
Expand Down
1 change: 1 addition & 0 deletions langchain/src/output_parsers/structured.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-instanceof/no-instanceof */
import { z } from "zod";

import { BaseOutputParser, OutputParserException } from "../schema/index.js";
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7989,6 +7989,13 @@ __metadata:
languageName: node
linkType: hard

"eslint-plugin-no-instanceof@npm:^1.0.1":
version: 1.0.1
resolution: "eslint-plugin-no-instanceof@npm:1.0.1"
checksum: 80b152b13869fd88debb9d387ca225dd18e5a75fe33f9510e71a65070a4862bfca5a57dc42b508259d8150f318312bf26c09e570874c2871bf0f59c4b745cfe5
languageName: node
linkType: hard

"eslint-plugin-prettier@npm:^4.2.1":
version: 4.2.1
resolution: "eslint-plugin-prettier@npm:4.2.1"
Expand Down Expand Up @@ -11468,6 +11475,7 @@ __metadata:
eslint-config-airbnb-base: ^15.0.0
eslint-config-prettier: ^8.6.0
eslint-plugin-import: ^2.27.5
eslint-plugin-no-instanceof: ^1.0.1
eslint-plugin-prettier: ^4.2.1
eslint-plugin-tree-shaking: ^1.10.0
exponential-backoff: ^3.1.0
Expand Down

0 comments on commit 75c45d6

Please sign in to comment.