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

Minor database tool improvements #591

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions lib/langchain/tool/database/database.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "function",
"function": {
"name": "database__describe_tables",
"description": "Database Tool: Returns the schema for a list of tables",
"description": "Database Tool: Returns the schema for a list of tables. Use this to determine the correct column names for a query.",
"parameters": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -36,7 +36,7 @@
"properties": {
"input": {
"type": "string",
"description": "SQL query to be executed"
"description": "SQL query to be executed. Never query for all the columns from a specific table, only ask for a the few relevant columns given the question. Pay attention to use only the column names that you can see in the schema description. Be careful to not query for columns that do not exist. Pay attention to which column is in which table. Also, qualify column names with the table name when needed."
}
},
"required": ["input"]
Expand Down
10 changes: 8 additions & 2 deletions lib/langchain/tool/database/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ def initialize(connection_string:, tables: [], exclude_tables: [])
end

# Database Tool: Returns a list of tables in the database
#
# @return [String] List of tables
def list_tables
db.tables
db.tables.join(", ")
end

# Database Tool: Returns the schema for a list of tables
Expand All @@ -59,6 +61,10 @@ def dump_schema
schema
end

# Database Tool: Returns a table descripton
#
# @param table [String] The table to describe.
# @return [String] Database definition of the table
def describe_table(table, schema)
primary_key_columns = []
primary_key_column_count = db.schema(table).count { |column| column[1][:primary_key] == true }
Expand Down Expand Up @@ -93,7 +99,7 @@ def execute(input:)

db[input].to_a
rescue Sequel::DatabaseError => e
Langchain.logger.error(e.message, for: self.class)
e.message
end
end
end