Align table info (#999) (#1034)

Currently the chain is getting the column names and types on the one
side and the example rows on the other. It is easier for the llm to read
the table information if the column name and examples are shown together
so that it can easily understand to which columns do the examples refer
to. For an instantiation of this, please refer to the changes in the
`sqlite.ipynb` notebook.

Also changed `eval` for `ast.literal_eval` when interpreting the results
from the sample row query since it is a better practice.

---------

Co-authored-by: Francisco Ingham <>

---------

Co-authored-by: Francisco Ingham <fpingham@gmail.com>
This commit is contained in:
Harrison Chase
2023-02-13 21:48:41 -08:00
committed by GitHub
parent 8c45f06d58
commit ec727bf166
5 changed files with 63 additions and 48 deletions

View File

@@ -16,7 +16,7 @@ from sqlalchemy import (
schema,
)
from langchain.sql_database import SQLDatabase
from langchain.sql_database import _TEMPLATE_PREFIX, SQLDatabase
metadata_obj = MetaData()
@@ -46,10 +46,11 @@ def test_table_info() -> None:
metadata_obj.create_all(engine)
db = SQLDatabase(engine, schema="schema_a")
output = db.table_info
output = output[len(_TEMPLATE_PREFIX) :]
expected_output = (
"Table 'user' has columns: user_id (INTEGER), user_name (VARCHAR).",
"Table 'user' has columns: {'user_id': ['INTEGER'], 'user_name': ['VARCHAR']}"
)
assert sorted(output.split("\n")) == sorted(expected_output)
assert output == expected_output
def test_sql_database_run() -> None: