-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathsql_db_chain.ts
337 lines (304 loc) Β· 9.99 KB
/
sql_db_chain.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import type {
BaseLanguageModel,
BaseLanguageModelInterface,
} from "@langchain/core/language_models/base";
import type { TiktokenModel } from "js-tiktoken/lite";
import type { OpenAI } from "@langchain/openai";
import { ChainValues } from "@langchain/core/utils/types";
import { BasePromptTemplate, PromptTemplate } from "@langchain/core/prompts";
import {
calculateMaxTokens,
getModelContextSize,
} from "@langchain/core/language_models/base";
import { CallbackManagerForChainRun } from "@langchain/core/callbacks/manager";
import {
RunnablePassthrough,
RunnableSequence,
} from "@langchain/core/runnables";
import { StringOutputParser } from "@langchain/core/output_parsers";
import {
DEFAULT_SQL_DATABASE_PROMPT,
SQL_PROMPTS_MAP,
SqlDialect,
} from "./sql_db_prompt.js";
import { BaseChain, ChainInputs } from "../base.js";
import { LLMChain } from "../llm_chain.js";
import type { SqlDatabase } from "../../sql_db.js";
import { getPromptTemplateFromDataSource } from "../../util/sql_utils.js";
/**
* Interface that extends the ChainInputs interface and defines additional
* fields specific to a SQL database chain. It represents the input fields
* for a SQL database chain.
*/
export interface SqlDatabaseChainInput extends ChainInputs {
llm: BaseLanguageModelInterface;
database: SqlDatabase;
topK?: number;
inputKey?: string;
outputKey?: string;
sqlOutputKey?: string;
prompt?: PromptTemplate;
}
/**
* Class that represents a SQL database chain in the LangChain framework.
* It extends the BaseChain class and implements the functionality
* specific to a SQL database chain.
*
* @security **Security Notice**
* This chain generates SQL queries for the given database.
* The SQLDatabase class provides a getTableInfo method that can be used
* to get column information as well as sample data from the table.
* To mitigate risk of leaking sensitive data, limit permissions
* to read and scope to the tables that are needed.
* Optionally, use the includesTables or ignoreTables class parameters
* to limit which tables can/cannot be accessed.
*
* @link See https://js.langchain.com/docs/security for more information.
* @example
* ```typescript
* const chain = new SqlDatabaseChain({
* llm: new OpenAI({ temperature: 0 }),
* database: new SqlDatabase({ ...config }),
* });
*
* const result = await chain.run("How many tracks are there?");
* ```
*/
export class SqlDatabaseChain extends BaseChain {
static lc_name() {
return "SqlDatabaseChain";
}
// LLM wrapper to use
llm: BaseLanguageModelInterface;
// SQL Database to connect to.
database: SqlDatabase;
// Prompt to use to translate natural language to SQL.
prompt = DEFAULT_SQL_DATABASE_PROMPT;
// Number of results to return from the query
topK = 5;
inputKey = "query";
outputKey = "result";
sqlOutputKey: string | undefined = undefined;
// Whether to return the result of querying the SQL table directly.
returnDirect = false;
constructor(fields: SqlDatabaseChainInput) {
super(fields);
this.llm = fields.llm;
this.database = fields.database;
this.topK = fields.topK ?? this.topK;
this.inputKey = fields.inputKey ?? this.inputKey;
this.outputKey = fields.outputKey ?? this.outputKey;
this.sqlOutputKey = fields.sqlOutputKey ?? this.sqlOutputKey;
this.prompt =
fields.prompt ??
getPromptTemplateFromDataSource(this.database.appDataSource);
}
/** @ignore */
async _call(
values: ChainValues,
runManager?: CallbackManagerForChainRun
): Promise<ChainValues> {
const llmChain = new LLMChain({
prompt: this.prompt,
llm: this.llm,
outputKey: this.outputKey,
memory: this.memory,
});
if (!(this.inputKey in values)) {
throw new Error(`Question key ${this.inputKey} not found.`);
}
const question: string = values[this.inputKey];
let inputText = `${question}\nSQLQuery:`;
const tablesToUse = values.table_names_to_use;
const tableInfo = await this.database.getTableInfo(tablesToUse);
const llmInputs = {
input: inputText,
top_k: this.topK,
dialect: this.database.appDataSourceOptions.type,
table_info: tableInfo,
stop: ["\nSQLResult:"],
};
await this.verifyNumberOfTokens(inputText, tableInfo);
const sqlCommand = await llmChain.predict(
llmInputs,
runManager?.getChild("sql_generation")
);
let queryResult = "";
try {
queryResult = await this.database.appDataSource.query(sqlCommand);
} catch (error) {
console.error(error);
}
let finalResult;
if (this.returnDirect) {
finalResult = { [this.outputKey]: queryResult };
} else {
inputText += `${sqlCommand}\nSQLResult: ${JSON.stringify(
queryResult
)}\nAnswer:`;
llmInputs.input = inputText;
finalResult = {
[this.outputKey]: await llmChain.predict(
llmInputs,
runManager?.getChild("result_generation")
),
};
}
if (this.sqlOutputKey != null) {
finalResult[this.sqlOutputKey] = sqlCommand;
}
return finalResult;
}
_chainType() {
return "sql_database_chain" as const;
}
get inputKeys(): string[] {
return [this.inputKey];
}
get outputKeys(): string[] {
if (this.sqlOutputKey != null) {
return [this.outputKey, this.sqlOutputKey];
}
return [this.outputKey];
}
/**
* Private method that verifies the number of tokens in the input text and
* table information. It throws an error if the number of tokens exceeds
* the maximum allowed by the language model.
* @param inputText The input text.
* @param tableinfo The table information.
* @returns A promise that resolves when the verification is complete.
*/
private async verifyNumberOfTokens(
inputText: string,
tableinfo: string
): Promise<void> {
// We verify it only for OpenAI for the moment
if (this.llm._llmType() !== "openai") {
return;
}
const llm = this.llm as OpenAI;
const promptTemplate = this.prompt.template;
const stringWeSend = `${inputText}${promptTemplate}${tableinfo}`;
const maxToken = await calculateMaxTokens({
prompt: stringWeSend,
// Cast here to allow for other models that may not fit the union
modelName: llm.model as TiktokenModel,
});
if (maxToken < (llm.maxTokens ?? -1)) {
throw new Error(`The combination of the database structure and your question is too big for the model ${
llm.model
} which can compute only a max tokens of ${getModelContextSize(
llm.model
)}.
We suggest you to use the includeTables parameters when creating the SqlDatabase object to select only a subset of the tables. You can also use a model which can handle more tokens.`);
}
}
}
export interface CreateSqlQueryChainFields {
llm: BaseLanguageModel;
db: SqlDatabase;
prompt?: BasePromptTemplate;
/**
* @default 5
*/
k?: number;
dialect: SqlDialect;
}
type SqlInput = {
question: string;
};
type SqlInoutWithTables = SqlInput & {
tableNamesToUse: string[];
};
const strip = (text: string) => {
// Replace escaped quotes with actual quotes
let newText = text.replace(/\\"/g, '"').trim();
// Remove wrapping quotes if the entire string is wrapped in quotes
if (newText.startsWith('"') && newText.endsWith('"')) {
newText = newText.substring(1, newText.length - 1);
}
return newText;
};
const difference = (setA: Set<string>, setB: Set<string>) =>
new Set([...setA].filter((x) => !setB.has(x)));
/**
* Create a SQL query chain that can create SQL queries for the given database.
* Returns a Runnable.
*
* @param {BaseLanguageModel} llm The language model to use in the chain.
* @param {SqlDatabase} db The database to use in the chain.
* @param {BasePromptTemplate | undefined} prompt The prompt to use in the chain.
* @param {BaseLanguageModel | undefined} k The amount of docs/results to return. Passed through the prompt input value `top_k`.
* @param {SqlDialect} dialect The SQL dialect to use in the chain.
* @returns {Promise<RunnableSequence<Record<string, unknown>, string>>} A runnable sequence representing the chain.
* @example ```typescript
* const datasource = new DataSource({
* type: "sqlite",
* database: "../../../../Chinook.db",
* });
* const db = await SqlDatabase.fromDataSourceParams({
* appDataSource: datasource,
* });
* const llm = new ChatOpenAI({ temperature: 0 });
* const chain = await createSqlQueryChain({
* llm,
* db,
* dialect: "sqlite",
* });
* ```
*/
export async function createSqlQueryChain({
llm,
db,
prompt,
k = 5,
dialect,
}: CreateSqlQueryChainFields) {
let promptToUse: BasePromptTemplate;
if (prompt) {
promptToUse = prompt;
} else if (SQL_PROMPTS_MAP[dialect]) {
promptToUse = SQL_PROMPTS_MAP[dialect];
} else {
promptToUse = DEFAULT_SQL_DATABASE_PROMPT;
}
if (
difference(
new Set(["input", "top_k", "table_info"]),
new Set(promptToUse.inputVariables)
).size > 0
) {
throw new Error(
`Prompt must have input variables: 'input', 'top_k', 'table_info'. Received prompt with input variables: ` +
`${promptToUse.inputVariables}. Full prompt:\n\n${promptToUse}`
);
}
if (promptToUse.inputVariables.includes("dialect")) {
promptToUse = await promptToUse.partial({ dialect });
}
promptToUse = await promptToUse.partial({ top_k: k.toString() });
const inputs = {
input: (x: Record<string, unknown>) => {
if ("question" in x) {
return `${(x as SqlInput).question}\nSQLQuery: `;
}
throw new Error("Input must include a question property.");
},
table_info: async (x: Record<string, unknown>) =>
db.getTableInfo((x as SqlInoutWithTables).tableNamesToUse),
};
return RunnableSequence.from([
RunnablePassthrough.assign(inputs),
(x) => {
const newInputs = { ...x };
delete newInputs.question;
delete newInputs.tableNamesToUse;
return newInputs;
},
promptToUse,
llm.bind({ stop: ["\nSQLResult:"] }),
new StringOutputParser(),
strip,
]);
}