Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/docs/modules/agents/toolkits/sql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ hide_table_of_contents: true

This example shows how to load and use an agent with a SQL toolkit.

## Setup

You'll need to first install `typeorm`:

```bash npm2yarn
npm install typeorm
```

## Usage

import CodeBlock from "@theme/CodeBlock";
import Example from "@examples/agents/sql.ts";

Expand Down
10 changes: 5 additions & 5 deletions docs/docs/modules/agents/tools/integrations/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ LangChain provides the following tools you can use out of the box:
- [`JsonListKeysTool`][JsonListKeysTool] and [`JsonGetValueTool`][JsonGetValueTool] - Useful for extracting data from JSON objects. These tools can be used collectively in a [`JsonToolkit`][JsonToolkit].
- [`RequestsGetTool`][RequestsGetTool] and [`RequestsPostTool`][RequestsPostTool] - Useful for making HTTP requests.
- [`SerpAPI`][SerpAPI] - A wrapper around the SerpAPI API. Useful for when you need to answer questions about current events. Input should be a search query.
- [`QuerySqlTool`][QuerySqlTool], [`InfoSqlTool`][InfoSqlTool], [`ListTablesSqlTool`][ListTablesSqlTool], and [`QueryCheckerTool`][QueryCheckerTool] - Useful for interacting with SQL databases. Can be used together in a [`SqlToolkit`][SqlToolkit].
- [`QuerySqlTool`][QuerySqlTool], [`InfoSqlTool`][InfoSqlTool], [`ListTablesSqlTool`][ListTablesSqlTool], and [`QueryCheckerTool`][QueryCheckerTool] - Useful for interacting with SQL databases. Can be used together in a [`SqlToolkit`][SqlToolkit]. Requires `typeorm` as a peer dependency.
- [`VectorStoreQATool`][VectorStoreQATool] - Useful for retrieving relevant text data from a vector store.
- [`ZapierNLARunAction`][ZapierNLARunAction] - A wrapper around the Zapier NLP API. Useful for triggering Zapier actions with a natural language input. Best when used in a [`ZapierToolkit`][ZapierToolkit].

Expand All @@ -35,10 +35,10 @@ LangChain provides the following tools you can use out of the box:
[RequestsGetTool]: /docs/api/tools/classes/RequestsGetTool
[RequestsPostTool]: /docs/api/tools/classes/RequestsPostTool
[SerpAPI]: /docs/api/tools/classes/SerpAPI
[QuerySqlTool]: /docs/api/tools/classes/QuerySqlTool
[InfoSqlTool]: /docs/api/tools/classes/InfoSqlTool
[ListTablesSqlTool]: /docs/api/tools/classes/ListTablesSqlTool
[QueryCheckerTool]: /docs/api/tools/classes/QueryCheckerTool
[QuerySqlTool]: /docs/api/tools_sql/classes/QuerySqlTool
[InfoSqlTool]: /docs/api/tools_sql/classes/InfoSqlTool
[ListTablesSqlTool]: /docs/api/tools_sql/classes/ListTablesSqlTool
[QueryCheckerTool]: /docs/api/tools_sql/classes/QueryCheckerTool
[SqlToolkit]: ../../toolkits/sql
[VectorStoreQATool]: /docs/api/tools/classes/VectorStoreQATool
[ZapierNLARunAction]: /docs/api/tools/classes/ZapierNLARunAction
Expand Down
2 changes: 1 addition & 1 deletion examples/src/agents/aws_sfn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { OpenAI } from "langchain/llms/openai";
import {
createAWSSfnAgent,
AWSSfnToolkit,
} from "langchain/agents/agent_toolkits/aws_sfn";
} from "langchain/agents/toolkits/aws_sfn";

const _EXAMPLE_STATE_MACHINE_ASL = `
{
Expand Down
2 changes: 1 addition & 1 deletion examples/src/agents/sql.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OpenAI } from "langchain/llms/openai";
import { SqlDatabase } from "langchain/sql_db";
import { createSqlAgent, SqlToolkit } from "langchain/agents";
import { createSqlAgent, SqlToolkit } from "langchain/agents/toolkits/sql";
import { DataSource } from "typeorm";

/** This example uses Chinook database, which is a sample database available for SQL Server, Oracle, MySQL, etc.
Expand Down
2 changes: 1 addition & 1 deletion examples/src/chains/sql_db.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DataSource } from "typeorm";
import { OpenAI } from "langchain/llms/openai";
import { SqlDatabase } from "langchain/sql_db";
import { SqlDatabaseChain } from "langchain/chains";
import { SqlDatabaseChain } from "langchain/chains/sql_db";

/**
* This example uses Chinook database, which is a sample database available for SQL Server, Oracle, MySQL, etc.
Expand Down
2 changes: 1 addition & 1 deletion examples/src/chains/sql_db_sql_output.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DataSource } from "typeorm";
import { OpenAI } from "langchain/llms/openai";
import { SqlDatabase } from "langchain/sql_db";
import { SqlDatabaseChain } from "langchain/chains";
import { SqlDatabaseChain } from "langchain/chains/sql_db";

/**
* This example uses Chinook database, which is a sample database available for SQL Server, Oracle, MySQL, etc.
Expand Down
15 changes: 12 additions & 3 deletions langchain/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ agents.d.ts
agents/load.cjs
agents/load.js
agents/load.d.ts
agents/agent_toolkits/aws_sfn.cjs
agents/agent_toolkits/aws_sfn.js
agents/agent_toolkits/aws_sfn.d.ts
agents/toolkits/aws_sfn.cjs
agents/toolkits/aws_sfn.js
agents/toolkits/aws_sfn.d.ts
agents/toolkits/sql.cjs
agents/toolkits/sql.js
agents/toolkits/sql.d.ts
base_language.cjs
base_language.js
base_language.d.ts
Expand All @@ -28,6 +31,9 @@ tools/aws_sfn.d.ts
tools/calculator.cjs
tools/calculator.js
tools/calculator.d.ts
tools/sql.cjs
tools/sql.js
tools/sql.d.ts
tools/webbrowser.cjs
tools/webbrowser.js
tools/webbrowser.d.ts
Expand All @@ -43,6 +49,9 @@ chains/query_constructor.d.ts
chains/query_constructor/ir.cjs
chains/query_constructor/ir.js
chains/query_constructor/ir.d.ts
chains/sql_db.cjs
chains/sql_db.js
chains/sql_db.d.ts
embeddings.cjs
embeddings.js
embeddings.d.ts
Expand Down
38 changes: 31 additions & 7 deletions langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
"agents/load.cjs",
"agents/load.js",
"agents/load.d.ts",
"agents/agent_toolkits/aws_sfn.cjs",
"agents/agent_toolkits/aws_sfn.js",
"agents/agent_toolkits/aws_sfn.d.ts",
"agents/toolkits/aws_sfn.cjs",
"agents/toolkits/aws_sfn.js",
"agents/toolkits/aws_sfn.d.ts",
"agents/toolkits/sql.cjs",
"agents/toolkits/sql.js",
"agents/toolkits/sql.d.ts",
"base_language.cjs",
"base_language.js",
"base_language.d.ts",
Expand All @@ -40,6 +43,9 @@
"tools/calculator.cjs",
"tools/calculator.js",
"tools/calculator.d.ts",
"tools/sql.cjs",
"tools/sql.js",
"tools/sql.d.ts",
"tools/webbrowser.cjs",
"tools/webbrowser.js",
"tools/webbrowser.d.ts",
Expand All @@ -55,6 +61,9 @@
"chains/query_constructor/ir.cjs",
"chains/query_constructor/ir.js",
"chains/query_constructor/ir.d.ts",
"chains/sql_db.cjs",
"chains/sql_db.js",
"chains/sql_db.d.ts",
"embeddings.cjs",
"embeddings.js",
"embeddings.d.ts",
Expand Down Expand Up @@ -827,10 +836,15 @@
"import": "./agents/load.js",
"require": "./agents/load.cjs"
},
"./agents/agent_toolkits/aws_sfn": {
"types": "./agents/agent_toolkits/aws_sfn.d.ts",
"import": "./agents/agent_toolkits/aws_sfn.js",
"require": "./agents/agent_toolkits/aws_sfn.cjs"
"./agents/toolkits/aws_sfn": {
"types": "./agents/toolkits/aws_sfn.d.ts",
"import": "./agents/toolkits/aws_sfn.js",
"require": "./agents/toolkits/aws_sfn.cjs"
},
"./agents/toolkits/sql": {
"types": "./agents/toolkits/sql.d.ts",
"import": "./agents/toolkits/sql.js",
"require": "./agents/toolkits/sql.cjs"
},
"./base_language": {
"types": "./base_language.d.ts",
Expand All @@ -857,6 +871,11 @@
"import": "./tools/calculator.js",
"require": "./tools/calculator.cjs"
},
"./tools/sql": {
"types": "./tools/sql.d.ts",
"import": "./tools/sql.js",
"require": "./tools/sql.cjs"
},
"./tools/webbrowser": {
"types": "./tools/webbrowser.d.ts",
"import": "./tools/webbrowser.js",
Expand All @@ -882,6 +901,11 @@
"import": "./chains/query_constructor/ir.js",
"require": "./chains/query_constructor/ir.cjs"
},
"./chains/sql_db": {
"types": "./chains/sql_db.d.ts",
"import": "./chains/sql_db.js",
"require": "./chains/sql_db.cjs"
},
"./embeddings": {
"node": {
"types": "./embeddings.d.ts",
Expand Down
10 changes: 8 additions & 2 deletions langchain/scripts/create-entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ const entrypoints = {
// agents
agents: "agents/index",
"agents/load": "agents/load",
"agents/agent_toolkits/aws_sfn": "agents/agent_toolkits/aws_sfn",
"agents/toolkits/aws_sfn": "agents/toolkits/aws_sfn",
"agents/toolkits/sql": "agents/toolkits/sql/index",
// base language
base_language: "base_language/index",
// tools
tools: "tools/index",
"tools/aws_lambda": "tools/aws_lambda",
"tools/aws_sfn": "tools/aws_sfn",
"tools/calculator": "tools/calculator",
"tools/sql": "tools/sql",
"tools/webbrowser": "tools/webbrowser",
// chains
chains: "chains/index",
"chains/load": "chains/load",
"chains/query_constructor": "chains/query_constructor/index",
"chains/query_constructor/ir": "chains/query_constructor/ir",
"chains/sql_db": "chains/sql_db/index",
// embeddings
embeddings: "embeddings/index",
"embeddings/base": "embeddings/base",
Expand Down Expand Up @@ -190,12 +193,15 @@ const deprecatedNodeOnly = [
// Therefore they are not tested in the generated test-exports-* packages.
const requiresOptionalDependency = [
"agents/load",
"agents/agent_toolkits/aws_sfn",
"agents/toolkits/aws_sfn",
"agents/toolkits/sql",
"tools/aws_lambda",
"tools/aws_sfn",
"tools/calculator",
"tools/sql",
"tools/webbrowser",
"chains/load",
"chains/sql_db",
"embeddings/cohere",
"embeddings/googlevertexai",
"embeddings/tensorflow",
Expand Down
7 changes: 2 additions & 5 deletions langchain/src/agents/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@ export {
JsonToolkit,
OpenApiToolkit,
RequestsToolkit,
SqlToolkit,
VectorStoreInfo,
VectorStoreRouterToolkit,
VectorStoreToolkit,
ZapierToolKit,
createJsonAgent,
createOpenApiAgent,
createSqlAgent,
SqlCreatePromptArgs,
createVectorStoreAgent,
createVectorStoreRouterAgent,
} from "./agent_toolkits/index.js";
export { Toolkit } from "./agent_toolkits/base.js";
} from "./toolkits/index.js";
export { Toolkit } from "./toolkits/base.js";
export {
ChatAgent,
ChatAgentInput,
Expand Down
2 changes: 1 addition & 1 deletion langchain/src/agents/tests/aws_sfn.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { test } from "@jest/globals";
import { StartExecutionAWSSfnTool } from "../../tools/aws_sfn.js";

import { OpenAI } from "../../llms/openai.js";
import { AWSSfnToolkit, createAWSSfnAgent } from "../agent_toolkits/aws_sfn.js";
import { AWSSfnToolkit, createAWSSfnAgent } from "../toolkits/aws_sfn.js";

test.skip("StartExecutionAWSSfnTool invokes the correct state machine and returns the executionArn", async () => {
const sfn = new StartExecutionAWSSfnTool({
Expand Down
2 changes: 1 addition & 1 deletion langchain/src/agents/tests/zapier_toolkit.int.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, test } from "@jest/globals";
import { ZapierToolKit } from "../agent_toolkits/zapier/zapier.js";
import { ZapierToolKit } from "../toolkits/zapier/zapier.js";
import { ZapierNLAWrapper, ZapierValues } from "../../tools/zapier.js";

describe("ZapierNLAWrapper", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export { JsonToolkit, createJsonAgent } from "./json/json.js";
export { SqlToolkit, createSqlAgent, SqlCreatePromptArgs } from "./sql/sql.js";
export {
RequestsToolkit,
OpenApiToolkit,
Expand Down
1 change: 1 addition & 0 deletions langchain/src/agents/toolkits/sql/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { SqlCreatePromptArgs, SqlToolkit, createSqlAgent } from "./sql.js";
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SerializedConstitutionalPrinciple } from "../../chains/serde.js";
import { SerializedConstitutionalPrinciple } from "../serde.js";

export class ConstitutionalPrinciple {
critiqueRequest: string;
Expand Down
12 changes: 0 additions & 12 deletions langchain/src/chains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@ export {
loadSummarizationChain,
SummarizationChainParams,
} from "./summarization/load.js";
export {
SqlDatabaseChain,
SqlDatabaseChainInput,
} from "./sql_db/sql_db_chain.js";
export {
DEFAULT_SQL_DATABASE_PROMPT,
SQL_POSTGRES_PROMPT,
SQL_SQLITE_PROMPT,
SQL_MSSQL_PROMPT,
SQL_MYSQL_PROMPT,
} from "./sql_db/sql_db_prompt.js";
export {
ConversationalRetrievalQAChain,
ConversationalRetrievalQAChainInput,
Expand All @@ -67,7 +56,6 @@ export {
SerializedLLMChain,
SerializedSequentialChain,
SerializedSimpleSequentialChain,
SerializedSqlDatabaseChain,
SerializedAnalyzeDocumentChain,
SerializedAPIChain,
SerializedBaseChain,
Expand Down
8 changes: 0 additions & 8 deletions langchain/src/chains/serde.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { SerializedLLM } from "../llms/base.js";
import type { SerializedBasePromptTemplate } from "../prompts/serde.js";
import type { SerializedSqlDatabase } from "../util/sql_utils.js";

export type SerializedLLMChain = {
_type: "llm_chain";
Expand All @@ -20,12 +19,6 @@ export type SerializedSimpleSequentialChain = {
chains: Array<SerializedBaseChain>;
};

export type SerializedSqlDatabaseChain = {
_type: "sql_database_chain";
sql_database: SerializedSqlDatabase;
llm: SerializedLLM;
};

export type SerializedVectorDBQAChain = {
_type: "vector_db_qa";
k: number;
Expand Down Expand Up @@ -90,7 +83,6 @@ export type SerializedBaseChain =
| SerializedVectorDBQAChain
| SerializedAPIChain
| SerializedStuffDocumentsChain
| SerializedSqlDatabaseChain
| SerializedChatVectorDBQAChain
| SerializedMapReduceDocumentsChain
| SerializedAnalyzeDocumentChain
Expand Down
8 changes: 8 additions & 0 deletions langchain/src/chains/sql_db/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export { SqlDatabaseChain, SqlDatabaseChainInput } from "./sql_db_chain.js";
export {
DEFAULT_SQL_DATABASE_PROMPT,
SQL_POSTGRES_PROMPT,
SQL_SQLITE_PROMPT,
SQL_MSSQL_PROMPT,
SQL_MYSQL_PROMPT,
} from "./sql_db_prompt.js";
22 changes: 0 additions & 22 deletions langchain/src/chains/sql_db/sql_db_chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { OpenAI } from "../../llms/openai.js";
import { LLMChain } from "../llm_chain.js";
import type { SqlDatabase } from "../../sql_db.js";
import { ChainValues } from "../../schema/index.js";
import { SerializedSqlDatabaseChain } from "../serde.js";
import { BaseLanguageModel } from "../../base_language/index.js";
import {
calculateMaxTokens,
Expand Down Expand Up @@ -137,27 +136,6 @@ export class SqlDatabaseChain extends BaseChain {
return [this.outputKey];
}

static async deserialize(
data: SerializedSqlDatabaseChain,
SqlDatabaseFromOptionsParams: (typeof SqlDatabase)["fromOptionsParams"]
) {
const llm = await BaseLanguageModel.deserialize(data.llm);
const sqlDataBase = await SqlDatabaseFromOptionsParams(data.sql_database);

return new SqlDatabaseChain({
llm,
database: sqlDataBase,
});
}

serialize(): SerializedSqlDatabaseChain {
return {
_type: this._chainType(),
llm: this.llm.serialize(),
sql_database: this.database.serialize(),
};
}

private async verifyNumberOfTokens(
inputText: string,
tableinfo: string
Expand Down
Loading