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

add mssql to the compatible database list for SqlDatabaseChain #1169

Merged
merged 3 commits into from
May 12, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions langchain/src/chains/sql_db/sql_db_prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,23 @@ Only use the following tables:
Question: {input}`,
inputVariables: ["dialect", "table_info", "input", "top_k"],
});

export const SQL_MSSQL_PROMPT = /*#__PURE__*/ new PromptTemplate({
template: `You are an MS SQL expert. Given an input question, first create a syntactically correct MS SQL query to run, then look at the results of the query and return the answer to the input question.
Unless the user specifies in the question a specific number of examples to obtain, query for at most {top_k} results using the TOP clause as per MS SQL. You can order the results to return the most informative data in the database.
Never query for all columns from a table. You must query only the columns that are needed to answer the question. Wrap each column name in square brackets ([]) to denote them as delimited identifiers.
Pay attention to use only the column names you can see in the tables below. Be careful to not query for columns that do not exist. Also, pay attention to which column is in which table.

Use the following format:

Question: "Question here"
SQLQuery: "SQL Query to run"
SQLResult: "Result of the SQLQuery"
Answer: "Final answer here"

Only use the following tables:
{table_info}

Question: {input}`,
inputVariables: ["dialect", "table_info", "input", "top_k"],
});
22 changes: 22 additions & 0 deletions langchain/src/util/sql_utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { DataSource, DataSourceOptions } from "typeorm";
import {
DEFAULT_SQL_DATABASE_PROMPT,
SQL_MSSQL_PROMPT,
SQL_MYSQL_PROMPT,
SQL_POSTGRES_PROMPT,
SQL_SQLITE_PROMPT,
Expand Down Expand Up @@ -173,6 +174,21 @@ export const getTableAndColumnsName = async (
return formatToSqlTable(rep);
}

if (appDataSource.options.type === "mssql") {
sql =
"SELECT " +
"TABLE_NAME AS table_name, " +
"COLUMN_NAME AS column_name, " +
"DATA_TYPE AS data_type, " +
"IS_NULLABLE AS is_nullable " +
"FROM INFORMATION_SCHEMA.COLUMNS " +
"ORDER BY TABLE_NAME, ORDINAL_POSITION;";

const rep = await appDataSource.query(sql);

return formatToSqlTable(rep);
}

throw new Error("Database type not implemented yet");
};

Expand Down Expand Up @@ -228,6 +244,8 @@ export const generateTableInfoFromTables = async (
} else if (appDataSource.options.type === "postgres") {
const schema = appDataSource.options?.schema ?? "public";
sqlSelectInfoQuery = `SELECT * FROM "${schema}"."${currentTable.tableName}" LIMIT ${nbSampleRow};\n`;
} else if (appDataSource.options.type === "mssql") {
sqlSelectInfoQuery = `SELECT TOP ${nbSampleRow} * FROM [${currentTable.tableName}];\n`;
} else {
sqlSelectInfoQuery = `SELECT * FROM "${currentTable.tableName}" LIMIT ${nbSampleRow};\n`;
}
Expand Down Expand Up @@ -274,5 +292,9 @@ export const getPromptTemplateFromDataSource = (
return SQL_MYSQL_PROMPT;
}

if (appDataSource.options.type === "mssql") {
return SQL_MSSQL_PROMPT;
}

return DEFAULT_SQL_DATABASE_PROMPT;
};