DatabaseTools

Wisej.AI.Tools.DatabaseTools

Namespace: Wisej.AI.Tools

Assembly: Wisej.AI (3.5.0.0)

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.

Name
Type
Description

connection

The database connection to use. Default is null.

columns

The data table containing column information. Default is null.

schema

The schema of the database. Default is null.

serverType

The 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.

Parameter
Type
Description

columns

The 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.

Parameter
Type
Description

message

Message 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.

Parameter
Type
Description

sql

The SQL query to execute.

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

Implements

Name
Description

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

Last updated