Skip to content

Commit

Permalink
Export missing interfaces, Exclude some protected methods from docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nfcampos committed Apr 18, 2023
1 parent 6b77cbe commit ea6ad16
Show file tree
Hide file tree
Showing 25 changed files with 68 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ You'll also need to have an OpenSearch instance running. You can use the [offici
```typescript
import { Client } from "@opensearch-project/opensearch";
import { Document } from "langchain/document";
import { OpenAIEmbeddings } from "langchain/embeddings";
import { OpenSearchVectorStore } from "langchain/vectorstores";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { OpenSearchVectorStore } from "langchain/vectorstores/opensearch";

const client = new Client({
nodes: [process.env.OPENSEARCH_URL ?? "http://127.0.0.1:9200"],
Expand Down Expand Up @@ -63,9 +63,9 @@ await OpenSearchVectorStore.fromDocuments(docs, new OpenAIEmbeddings(), {
```typescript
import { Client } from "@opensearch-project/opensearch";
import { VectorDBQAChain } from "langchain/chains";
import { OpenAIEmbeddings } from "langchain/embeddings";
import { OpenAI } from "langchain/llms";
import { OpenSearchVectorStore } from "langchain/vectorstores";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { OpenAI } from "langchain/llms/openai";
import { OpenSearchVectorStore } from "langchain/vectorstores/opensearch";

const client = new Client({
nodes: [process.env.OPENSEARCH_URL ?? "http://127.0.0.1:9200"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PrismaVectorStore } from "langchain/vectorstores/prisma";
import { OpenAIEmbeddings } from "langchain/embeddings";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { PrismaClient, Prisma, Document } from "@prisma/client";

export const run = async () => {
Expand Down
2 changes: 1 addition & 1 deletion langchain/src/agents/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type CreatePromptArgs = {
inputVariables?: string[];
};

type ChatAgentInput = Optional<AgentInput, "outputParser">;
export type ChatAgentInput = Optional<AgentInput, "outputParser">;

/**
* Agent for the MRKL chain.
Expand Down
2 changes: 1 addition & 1 deletion langchain/src/agents/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ChainValues,
} from "../schema/index.js";

interface AgentExecutorInput extends ChainInputs {
export interface AgentExecutorInput extends ChainInputs {
agent: BaseSingleActionAgent | BaseMultiActionAgent;
tools: Tool[];
returnIntermediateSteps?: boolean;
Expand Down
16 changes: 11 additions & 5 deletions langchain/src/agents/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
export { Tool } from "../tools/base.js";
export { Agent, BaseSingleActionAgent, LLMSingleActionAgent } from "./agent.js";
export {
Agent,
AgentArgs,
BaseSingleActionAgent,
LLMSingleActionAgent,
LLMSingleActionAgentInput,
} from "./agent.js";
export {
JsonToolkit,
OpenApiToolkit,
Expand All @@ -14,19 +19,20 @@ export {
createSqlAgent,
createVectorStoreAgent,
} from "./agent_toolkits/index.js";
export { ChatAgent } from "./chat/index.js";
export { ChatAgent, ChatAgentInput } from "./chat/index.js";
export { ChatAgentOutputParser } from "./chat/outputParser.js";
export {
ChatConversationalAgent,
ChatConversationalAgentInput,
} from "./chat_convo/index.js";
export { ChatConversationalAgentOutputParser } from "./chat_convo/outputParser.js";
export { AgentExecutor } from "./executor.js";
export { AgentExecutor, AgentExecutorInput } from "./executor.js";
export {
initializeAgentExecutor,
initializeAgentExecutorWithOptions,
InitializeAgentExecutorOptions,
} from "./initialize.js";
export { ZeroShotAgent } from "./mrkl/index.js";
export { ZeroShotAgent, ZeroShotAgentInput } from "./mrkl/index.js";
export { ZeroShotAgentOutputParser } from "./mrkl/outputParser.js";
export {
AgentActionOutputParser,
Expand Down
11 changes: 9 additions & 2 deletions langchain/src/agents/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const initializeAgentExecutor = async (
}
};

type AgentExecutorOptions =
export type InitializeAgentExecutorOptions =
| ({
agentType: "zero-shot-react-description";
agentArgs?: Parameters<typeof ZeroShotAgent.fromLLMAndTools>[2];
Expand All @@ -74,10 +74,17 @@ type AgentExecutorOptions =
"agent" | "tools"
>);

/**
* Initialize an agent executor with options
* @param tools Array of tools to use in the agent
* @param llm LLM or ChatModel to use in the agent
* @param options Options for the agent, including agentType, agentArgs, and other options for AgentExecutor.fromAgentAndTools
* @returns AgentExecutor
*/
export const initializeAgentExecutorWithOptions = async (
tools: Tool[],
llm: BaseLanguageModel,
options: AgentExecutorOptions = {
options: InitializeAgentExecutorOptions = {
agentType:
llm._modelType() === "base_chat_model"
? "chat-zero-shot-react-description"
Expand Down
3 changes: 2 additions & 1 deletion langchain/src/agents/tests/agent.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { expect, test } from "@jest/globals";
import { OpenAI } from "../../llms/openai.js";
import { OpenAIEmbeddings } from "../../embeddings/openai.js";
import { loadAgent } from "../load.js";
import { AgentExecutor, Tool } from "../index.js";
import { AgentExecutor } from "../index.js";
import { SerpAPI } from "../../tools/serpapi.js";
import { Calculator } from "../../tools/calculator.js";
import { initializeAgentExecutorWithOptions } from "../initialize.js";
import { WebBrowser } from "../../tools/webbrowser.js";
import { Tool } from "../../tools/base.js";

test("Run agent from hub", async () => {
const model = new OpenAI({ temperature: 0, modelName: "text-babbage-001" });
Expand Down
1 change: 1 addition & 0 deletions langchain/src/tools/IFTTTWebhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class IFTTTWebhook extends Tool {
this.description = description;
}

/** @ignore */
async _call(input: string): Promise<string> {
const headers = { "Content-Type": "application/json" };
const body = JSON.stringify({ this: input });
Expand Down
1 change: 1 addition & 0 deletions langchain/src/tools/aiplugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class AIPluginTool extends Tool implements AIPluginToolParams {
this.apiSpec = params.apiSpec;
}

/** @ignore */
async _call(_input: string) {
return this.apiSpec;
}
Expand Down
1 change: 1 addition & 0 deletions langchain/src/tools/aws_lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class AWSLambda extends DynamicTool {
this.lambdaConfig = rest;
}

/** @ignore */
async _func(input: string): Promise<string> {
const { Client, Invoker } = await LambdaImports();

Expand Down
1 change: 1 addition & 0 deletions langchain/src/tools/bingserpapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class BingSerpAPI extends Tool {
this.params = params;
}

/** @ignore */
async _call(input: string): Promise<string> {
const headers = { "Ocp-Apim-Subscription-Key": this.key };
const params = { q: input, textDecorations: "true", textFormat: "HTML" };
Expand Down
1 change: 1 addition & 0 deletions langchain/src/tools/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Tool } from "./base.js";
export class Calculator extends Tool {
name = "calculator";

/** @ignore */
async _call(input: string) {
try {
return Parser.evaluate(input).toString();
Expand Down
1 change: 1 addition & 0 deletions langchain/src/tools/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class ChainTool extends Tool {
this.returnDirect = fields.returnDirect ?? this.returnDirect;
}

/** @ignore */
async _call(input: string): Promise<string> {
return this.chain.run(input);
}
Expand Down
1 change: 1 addition & 0 deletions langchain/src/tools/dadjokeapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class DadJokeAPI extends Tool {
"a dad joke generator. get a dad joke about a specific topic. input should be a search term.";
}

/** @ignore */
async _call(input: string): Promise<string> {
const headers = { Accept: "application/json" };
const searchUrl = `https://icanhazdadjoke.com/search?term=${input}`;
Expand Down
1 change: 1 addition & 0 deletions langchain/src/tools/dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class DynamicTool extends Tool {
this.returnDirect = fields.returnDirect ?? this.returnDirect;
}

/** @ignore */
async _call(input: string): Promise<string> {
return this.func(input);
}
Expand Down
10 changes: 7 additions & 3 deletions langchain/src/tools/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { SerpAPI } from "./serpapi.js";
export { SerpAPI, SerpAPIParameters } from "./serpapi.js";
export { DadJokeAPI } from "./dadjokeapi.js";
export { BingSerpAPI } from "./bingserpapi.js";
export { Tool, ToolParams } from "./base.js";
Expand All @@ -20,6 +20,10 @@ export {
} from "./json.js";
export { RequestsGetTool, RequestsPostTool } from "./requests.js";
export { VectorStoreQATool } from "./vectorstore.js";
export { ZapierNLARunAction, ZapierNLAWrapper } from "./zapier.js";
export { Serper } from "./serper.js";
export {
ZapierNLARunAction,
ZapierNLAWrapper,
ZapiterNLAWrapperParams,
} from "./zapier.js";
export { Serper, SerperParameters } from "./serper.js";
export { AIPluginTool } from "./aiplugin.js";
2 changes: 2 additions & 0 deletions langchain/src/tools/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class JsonListKeysTool extends Tool {
super();
}

/** @ignore */
async _call(input: string) {
try {
return this.jsonSpec.getKeys(input);
Expand All @@ -84,6 +85,7 @@ export class JsonGetValueTool extends Tool {
super();
}

/** @ignore */
async _call(input: string) {
try {
return this.jsonSpec.getValue(input);
Expand Down
2 changes: 2 additions & 0 deletions langchain/src/tools/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class RequestsGetTool extends Tool implements RequestTool {
super();
}

/** @ignore */
async _call(input: string) {
const res = await fetch(input, {
headers: this.headers,
Expand All @@ -33,6 +34,7 @@ export class RequestsPostTool extends Tool implements RequestTool {
super();
}

/** @ignore */
async _call(input: string) {
try {
const { url, data } = JSON.parse(input);
Expand Down
10 changes: 4 additions & 6 deletions langchain/src/tools/serpapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface BaseParameters {
timeout?: number;
}

interface GoogleParameters extends BaseParameters {
export interface SerpAPIParameters extends BaseParameters {
/**
* Search Query
* Parameter defines the query you want to search. You can use anything that you
Expand Down Expand Up @@ -302,14 +302,14 @@ function buildUrl<P extends UrlParameters>(
export class SerpAPI extends Tool {
protected key: string;

protected params: Partial<GoogleParameters>;
protected params: Partial<SerpAPIParameters>;

constructor(
apiKey: string | undefined = typeof process !== "undefined"
? // eslint-disable-next-line no-process-env
process.env.SERPAPI_API_KEY
: undefined,
params: Partial<GoogleParameters> = {}
params: Partial<SerpAPIParameters> = {}
) {
super();

Expand All @@ -325,9 +325,7 @@ export class SerpAPI extends Tool {

name = "search";

/**
* Run query through SerpAPI and parse result
*/
/** @ignore */
async _call(input: string) {
const { timeout, ...params } = this.params;
const resp = await fetch(
Expand Down
10 changes: 4 additions & 6 deletions langchain/src/tools/serper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Tool } from "./base.js";

type GoogleParameters = {
export type SerperParameters = {
gl?: string;
hl?: string;
};
Expand All @@ -15,14 +15,14 @@ type GoogleParameters = {
export class Serper extends Tool {
protected key: string;

protected params: Partial<GoogleParameters>;
protected params: Partial<SerperParameters>;

constructor(
apiKey: string | undefined = typeof process !== "undefined"
? // eslint-disable-next-line no-process-env
process.env.SERPER_API_KEY
: undefined,
params: Partial<GoogleParameters> = {}
params: Partial<SerperParameters> = {}
) {
super();

Expand All @@ -38,9 +38,7 @@ export class Serper extends Tool {

name = "search";

/**
* Run query through SerpAPI and parse result
*/
/** @ignore */
async _call(input: string) {
const options = {
method: "POST",
Expand Down
8 changes: 5 additions & 3 deletions langchain/src/tools/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class QuerySqlTool extends Tool implements SqlTool {
this.db = db;
}

/** @ignore */
async _call(input: string) {
try {
return await this.db.run(input);
Expand All @@ -42,7 +43,7 @@ export class InfoSqlTool extends Tool implements SqlTool {
this.db = db;
}

// get the table names for a comma separated list of tables, return error message in strong format
/** @ignore */
async _call(input: string) {
try {
const tables = input.split(",").map((table) => table.trim());
Expand All @@ -68,6 +69,7 @@ export class ListTablesSqlTool extends Tool implements SqlTool {
this.db = db;
}

/** @ignore */
async _call(_: string) {
try {
const tables = this.db.allTables.map(
Expand Down Expand Up @@ -115,11 +117,11 @@ If there are any of the above mistakes, rewrite the query. If there are no mista
}
}

/** @ignore */
async _call(input: string) {
return this.llmChain.predict({ query: input });
}

description = `
Use this tool to double check if your query is correct before executing it.
description = `Use this tool to double check if your query is correct before executing it.
Always use this tool before executing a query with query-sql!`;
}
1 change: 1 addition & 0 deletions langchain/src/tools/vectorstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class VectorStoreQATool extends Tool implements VectorStoreTool {
return `Useful for when you need to answer questions about ${name}. Whenever you need information about ${description} you should ALWAYS use this. Input should be a fully formed question.`;
}

/** @ignore */
async _call(input: string) {
return this.chain.run(input);
}
Expand Down
3 changes: 2 additions & 1 deletion langchain/src/tools/webbrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ const DEFAULT_HEADERS = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Headers = Record<string, any>;

interface WebBrowserArgs {
export interface WebBrowserArgs {
model: BaseLanguageModel;

embeddings: Embeddings;
Expand Down Expand Up @@ -167,6 +167,7 @@ export class WebBrowser extends Tool {
};
}

/** @ignore */
async _call(inputs: string) {
const [baseUrl, task] = inputs.split(",").map((input) => {
let t = input.trim();
Expand Down
3 changes: 2 additions & 1 deletion langchain/src/tools/zapier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const zapierNLABaseDescription: string =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ZapierValues = Record<string, any>;

interface ZapiterNLAWrapperParams extends AsyncCallerParams {
export interface ZapiterNLAWrapperParams extends AsyncCallerParams {
apiKey?: string;
}

Expand Down Expand Up @@ -221,6 +221,7 @@ export class ZapierNLARunAction extends Tool {
});
}

/** @ignore */
async _call(arg: string): Promise<string> {
return this.apiWrapper.runAsString(this.actionId, arg, this.params);
}
Expand Down
Loading

0 comments on commit ea6ad16

Please sign in to comment.