Skip to content
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

Fix env detection for Deno in Supabase Edge Functions #1298

Merged
merged 3 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,6 @@
"@dqbd/tiktoken": "^1.0.7",
"ansi-styles": "^5.0.0",
"binary-extensions": "^2.2.0",
"browser-or-node": "^2.1.1",
"expr-eval": "^2.0.2",
"flat": "^5.0.2",
"jsonpointer": "^5.0.1",
Expand Down
2 changes: 1 addition & 1 deletion langchain/src/chat_models/openai.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isNode } from "browser-or-node";
import {
Configuration,
OpenAIApi,
Expand All @@ -8,6 +7,7 @@ import {
ChatCompletionResponseMessageRoleEnum,
ChatCompletionRequestMessage,
} from "openai";
import { isNode } from "../util/env.js";
import {
AzureOpenAIInput,
OpenAICallOptions,
Expand Down
2 changes: 1 addition & 1 deletion langchain/src/embeddings/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
CreateEmbeddingRequest,
ConfigurationParameters,
} from "openai";
import { isNode } from "browser-or-node";
import type { AxiosRequestConfig } from "axios";
import { isNode } from "../util/env.js";
import { AzureOpenAIInput } from "../types/openai-types.js";
import fetchAdapter from "../util/axios-fetch-adapter.js";
import { chunkArray } from "../util/chunk.js";
Expand Down
2 changes: 1 addition & 1 deletion langchain/src/llms/openai-chat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isNode } from "browser-or-node";
import {
Configuration,
OpenAIApi,
Expand All @@ -8,6 +7,7 @@ import {
ChatCompletionResponseMessageRoleEnum,
CreateChatCompletionResponse,
} from "openai";
import { isNode } from "../util/env.js";
import {
AzureOpenAIInput,
OpenAICallOptions,
Expand Down
2 changes: 1 addition & 1 deletion langchain/src/llms/openai.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { TiktokenModel } from "@dqbd/tiktoken";
import { isNode } from "browser-or-node";
import {
Configuration,
ConfigurationParameters,
Expand All @@ -8,6 +7,7 @@ import {
CreateCompletionResponseChoicesInner,
OpenAIApi,
} from "openai";
import { isNode } from "../util/env.js";
import {
AzureOpenAIInput,
OpenAICallOptions,
Expand Down
2 changes: 1 addition & 1 deletion langchain/src/tools/webbrowser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axiosMod, { AxiosRequestConfig, AxiosStatic } from "axios";
import { isNode } from "browser-or-node";
import * as cheerio from "cheerio";
import { isNode } from "../util/env.js";
import { BaseLanguageModel } from "../base_language/index.js";
import { RecursiveCharacterTextSplitter } from "../text_splitter.js";
import { MemoryVectorStore } from "../vectorstores/memory.js";
Expand Down
42 changes: 35 additions & 7 deletions langchain/src/util/env.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
import {
isBrowser,
isNode,
isWebWorker,
isJsDom,
isDeno,
} from "browser-or-node";
// Inlined from https://github.com/flexdinesh/browser-or-node
declare global {
const Deno:
| {
version: {
deno: string;
};
}
| undefined;
}

export const isBrowser =
typeof window !== "undefined" && typeof window.document !== "undefined";

export const isWebWorker =
typeof globalThis === "object" &&
globalThis.constructor &&
globalThis.constructor.name === "DedicatedWorkerGlobalScope";

export const isJsDom =
(typeof window !== "undefined" && window.name === "nodejs") ||
(typeof navigator !== "undefined" &&
(navigator.userAgent.includes("Node.js") ||
navigator.userAgent.includes("jsdom")));

// Supabase Edge Function provides a `Deno` global object
// without `version` property
export const isDeno = typeof Deno !== "undefined";

// Mark not-as-node if in Supabase Edge Function
export const isNode =
typeof process !== "undefined" &&
process.versions != null &&
process.versions.node != null &&
!isDeno;

export const getEnv = () => {
let env: string;
Expand Down
8 changes: 0 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10196,13 +10196,6 @@ __metadata:
languageName: node
linkType: hard

"browser-or-node@npm:^2.1.1":
version: 2.1.1
resolution: "browser-or-node@npm:2.1.1"
checksum: b1650b579345574d2c1905c73d385915a032523066ef9589036cf129b23b27b85096b6713476dffb88feb83cda798ed0774363e699117aa2724a0409adea25aa
languageName: node
linkType: hard

"browser-process-hrtime@npm:^1.0.0":
version: 1.0.0
resolution: "browser-process-hrtime@npm:1.0.0"
Expand Down Expand Up @@ -18073,7 +18066,6 @@ __metadata:
apify-client: ^2.7.1
axios: ^0.26.0
binary-extensions: ^2.2.0
browser-or-node: ^2.1.1
cheerio: ^1.0.0-rc.12
chromadb: ^1.4.0
cohere-ai: ^5.0.2
Expand Down