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

Allow creation of a SQL toolkit with a custom language model #1321

Merged
merged 1 commit into from
May 19, 2023
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
2 changes: 1 addition & 1 deletion examples/src/agents/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export const run = async () => {
const db = await SqlDatabase.fromDataSourceParams({
appDataSource: datasource,
});
const toolkit = new SqlToolkit(db);
const model = new OpenAI({ temperature: 0 });
const toolkit = new SqlToolkit(db, model);
const executor = createSqlAgent(model, toolkit);

const input = `List the total sales per country. Which country's customers spent the most?`;
Expand Down
4 changes: 2 additions & 2 deletions langchain/src/agents/agent_toolkits/sql/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export class SqlToolkit extends Toolkit {

dialect = "sqlite";

constructor(db: SqlDatabase) {
constructor(db: SqlDatabase, llm?: BaseLanguageModel) {
super();
this.db = db;
this.tools = [
new QuerySqlTool(db),
new InfoSqlTool(db),
new ListTablesSqlTool(db),
new QueryCheckerTool(),
new QueryCheckerTool({ llm }),
];
}
}
Expand Down
34 changes: 23 additions & 11 deletions langchain/src/tools/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { LLMChain } from "../chains/llm_chain.js";
import { PromptTemplate } from "../prompts/prompt.js";
import type { SqlDatabase } from "../sql_db.js";
import { SqlTable } from "../util/sql_utils.js";
import { BaseLanguageModel } from "../base_language/index.js";

interface SqlTool {
db: SqlDatabase;
Expand All @@ -29,7 +30,7 @@ export class QuerySqlTool extends Tool implements SqlTool {
}

description = `Input to this tool is a detailed and correct SQL query, output is a result from the database.
If the query is not correct, an error message will be returned.
If the query is not correct, an error message will be returned.
If an error is returned, rewrite the query, check the query, and try again.`;
}

Expand All @@ -55,7 +56,7 @@ export class InfoSqlTool extends Tool implements SqlTool {

description = `Input to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables.
Be sure that the tables actually exist by calling list-tables-sql first!

Example Input: "table1, table2, table3.`;
}

Expand Down Expand Up @@ -84,6 +85,12 @@ export class ListTablesSqlTool extends Tool implements SqlTool {
description = `Input is an empty string, output is a comma separated list of tables in the database.`;
}

type QueryCheckerToolArgs = {
llmChain?: LLMChain;
llm?: BaseLanguageModel;
_chainType?: never;
};

export class QueryCheckerTool extends Tool {
name = "query-checker";

Expand All @@ -103,17 +110,22 @@ If there are any of the above mistakes, rewrite the query. If there are no mista

llmChain: LLMChain;

constructor(llmChain?: LLMChain) {
constructor(llmChainOrOptions?: LLMChain | QueryCheckerToolArgs) {
super();
if (llmChain) {
this.llmChain = llmChain;
if (typeof llmChainOrOptions?._chainType === "function") {
Copy link
Collaborator Author

@jacoblee93 jacoblee93 May 18, 2023

Choose a reason for hiding this comment

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

CC @nfcampos saw this elsewhere to determine whether an object was a chain, but is this paradigm ok with you?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes this is fine

this.llmChain = llmChainOrOptions as LLMChain;
} else {
const model = new OpenAI({ temperature: 0 });
const prompt = new PromptTemplate({
template: this.template,
inputVariables: ["query"],
});
this.llmChain = new LLMChain({ llm: model, prompt });
const options = llmChainOrOptions as QueryCheckerToolArgs;
if (options?.llmChain !== undefined) {
this.llmChain = options.llmChain;
} else {
const prompt = new PromptTemplate({
template: this.template,
inputVariables: ["query"],
});
const llm = options?.llm ?? new OpenAI({ temperature: 0 });
this.llmChain = new LLMChain({ llm, prompt });
}
}
}

Expand Down