Skip to main content

DatabaseTools

Namespace: Wisej.AI.Tools

Assembly: Wisej.AI

Represents a set of tools for interacting with a database, including schema initialization and SQL execution.

public class DatabaseTools : ToolsContainer

This class provides functionalities to manage database connections, retrieve schema information, and execute SQL queries. It utilizes the ITokenizerService to truncate the generated context to fit within the MaxContextTokens value. By overriding the virtual properties in this class, you can provide predefined values for connection, schema, columns, and server type. This allows for greater flexibility and customization in how the class is used.

Constructors

DatabaseTools(connection, columns, schema, serverType)

Initializes a new instance of the DatabaseTools class with optional parameters.

NameTypeDescription
connectionDbConnectionThe database connection to use. Default is null.
columnsDataTableThe data table containing column information. Default is null.
schemaStringThe schema of the database. Default is null.
serverTypeStringThe type of the database server. Default is null.

You can provide the schema in three ways:

  • Provide the columns data table, which can be retrieved using connection.GetSchema("Columns").
  • Provide the schema string, formatted in a way that is understandable by the LLM.
  • If both parameters are null, you can override either the Columns or the Schema properties.

Properties

Columns

DataTable: Gets the data table containing column information. (Default: null)

Connection

DbConnection: Gets the database connection. (Default: null)

MaxContextTokens

Int32: Gets or sets the maximum number of context tokens. (Default: 4096)

Schema

String: Gets the schema of the database. (Default: null)

ServerType

String: Gets the type of the database server.

TokenizerService

ITokenizerService: Gets or sets the tokenizer service used for truncating context tokens.

Methods

BuildDatabaseSchema(columns)

Builds a database schema representation from the provided columns.

ParameterTypeDescription
columnsDataTableThe data table containing column information.

Returns: String. A string representation of the database schema.

If the columns parameter is null, the method returns "Missing columns schema".

GetSQL(message)

Returns the JSON string returned in the message by stripping the enclosing markers (sql and ) if present.

ParameterTypeDescription
messageMessageMessage with the response text that may be a JSON string.

Returns: String. JSON string.

select(sql)

Executes a SQL select query and returns the result as a string.

ParameterTypeDescription
sqlStringThe SQL query to execute.

Returns: String. The result of the SQL query as a string.

Implements

NameDescription
IToolsContainerRepresents a container for tools, providing access to a hub, adapter, and a collection of parameters.