forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: update SQL popular chain docs to use runnables (langchain-ai#2877
) * fix: added legacy files and updated base sql example to use runables * del * feat: added with sql output example * fix: removed unnecessary examples * fix: comment * fix: added info cards explaining lcel vs non lcel * fix: removed references to typeorm * fix: create legacy page and update copy on non legacy * fix: remove mention of default props on lcel docs * fix: add comments * chore: added comments to code examples * chore: lint files * fix: mention default prompts * chore: lint files * fix: typeorm install doc * fix: example import * fix: remove ? * chore: lint files * fix: remove unused imports * chore: eslint no unused imports to examples, ran lint w/ fix
- Loading branch information
1 parent
1b98cdb
commit 2e54f1a
Showing
19 changed files
with
442 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
docs/docs_skeleton/docs/modules/chains/popular/sqlite_legacy.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# SQL | ||
|
||
This example demonstrates the use of the `SQLDatabaseChain` for answering questions over a SQL database. | ||
|
||
import Example from "@snippets/modules/chains/popular/sqlite_legacy.mdx" | ||
|
||
<Example/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import CodeBlock from "@theme/CodeBlock"; | ||
import SqlDBExample from "@examples/chains/sql_db_legacy.ts"; | ||
import SqlDBSqlOutputExample from "@examples/chains/sql_db_sql_output_legacy.ts"; | ||
import SqlDBSAPHANAExample from "@examples/chains/sql_db_saphana_legacy.ts"; | ||
import SqlDBSqlCustomPromptExample from "@examples/chains/sql_db_custom_prompt_legacy.ts"; | ||
|
||
This example uses Chinook database, which is a sample database available for SQL Server, Oracle, MySQL, etc. | ||
|
||
:::info | ||
These are legacy docs. It is now recommended to use LCEL over legacy implementations. | ||
|
||
Looking for the LCEL docs? Click [here](/docs/modules/chains/popular/sqlite). | ||
::: | ||
|
||
## Set up | ||
|
||
First install `typeorm`: | ||
|
||
```bash npm2yarn | ||
npm install typeorm | ||
``` | ||
|
||
Then install the dependencies needed for your database. For example, for SQLite: | ||
|
||
```bash npm2yarn | ||
npm install sqlite3 | ||
``` | ||
|
||
Currently, LangChain.js has default prompts for | ||
Postgres, SQLite, Microsoft SQL Server, MySQL, and SAP HANA. | ||
|
||
Finally follow the instructions on https://database.guide/2-sample-databases-sqlite/ to get the sample database for this example. | ||
|
||
<CodeBlock language="typescript">{SqlDBExample}</CodeBlock> | ||
|
||
You can include or exclude tables when creating the `SqlDatabase` object to help the chain focus on the tables you want. | ||
It can also reduce the number of tokens used in the chain. | ||
|
||
```typescript | ||
const db = await SqlDatabase.fromDataSourceParams({ | ||
appDataSource: datasource, | ||
includesTables: ["Track"], | ||
}); | ||
``` | ||
|
||
If desired, you can return the used SQL command when calling the chain. | ||
|
||
<CodeBlock language="typescript">{SqlDBSqlOutputExample}</CodeBlock> | ||
|
||
## SAP Hana | ||
|
||
Here's an example of using the chain with a SAP HANA database: | ||
|
||
<CodeBlock language="typescript">{SqlDBSAPHANAExample}</CodeBlock> | ||
|
||
## Custom prompt | ||
|
||
You can also customize the prompt that is used. Here is an example prompting the model to understand that "foobar" is the same as the Employee table: | ||
|
||
<CodeBlock language="typescript">{SqlDBSqlCustomPromptExample}</CodeBlock> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { DataSource } from "typeorm"; | ||
import { OpenAI } from "langchain/llms/openai"; | ||
import { SqlDatabase } from "langchain/sql_db"; | ||
import { SqlDatabaseChain } from "langchain/chains/sql_db"; | ||
import { PromptTemplate } from "langchain/prompts"; | ||
|
||
const template = `Given an input question, first create a syntactically correct {dialect} query to run, then look at the results of the query and return the answer. | ||
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} | ||
If someone asks for the table foobar, they really mean the employee table. | ||
Question: {input}`; | ||
|
||
const prompt = PromptTemplate.fromTemplate(template); | ||
|
||
/** | ||
* This example uses Chinook database, which is a sample database available for SQL Server, Oracle, MySQL, etc. | ||
* To set it up follow the instructions on https://database.guide/2-sample-databases-sqlite/, placing the .db file | ||
* in the examples folder. | ||
*/ | ||
const datasource = new DataSource({ | ||
type: "sqlite", | ||
database: "data/Chinook.db", | ||
}); | ||
|
||
const db = await SqlDatabase.fromDataSourceParams({ | ||
appDataSource: datasource, | ||
}); | ||
|
||
const chain = new SqlDatabaseChain({ | ||
llm: new OpenAI({ temperature: 0 }), | ||
database: db, | ||
sqlOutputKey: "sql", | ||
prompt, | ||
}); | ||
|
||
const res = await chain.call({ | ||
query: "How many employees are there in the foobar table?", | ||
}); | ||
console.log(res); | ||
|
||
/* | ||
{ | ||
result: ' There are 8 employees in the foobar table.', | ||
sql: ' SELECT COUNT(*) FROM Employee;' | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { DataSource } from "typeorm"; | ||
import { OpenAI } from "langchain/llms/openai"; | ||
import { SqlDatabase } from "langchain/sql_db"; | ||
import { SqlDatabaseChain } from "langchain/chains/sql_db"; | ||
|
||
/** | ||
* This example uses Chinook database, which is a sample database available for SQL Server, Oracle, MySQL, etc. | ||
* To set it up follow the instructions on https://database.guide/2-sample-databases-sqlite/, placing the .db file | ||
* in the examples folder. | ||
*/ | ||
const datasource = new DataSource({ | ||
type: "sqlite", | ||
database: "Chinook.db", | ||
}); | ||
|
||
const db = await SqlDatabase.fromDataSourceParams({ | ||
appDataSource: datasource, | ||
}); | ||
|
||
const chain = new SqlDatabaseChain({ | ||
llm: new OpenAI({ temperature: 0 }), | ||
database: db, | ||
}); | ||
|
||
const res = await chain.run("How many tracks are there?"); | ||
console.log(res); | ||
// There are 3503 tracks. |
Oops, something went wrong.