UNPKG

azure-sql-ai-assistant

Version:

AI-powered assistant for Azure SQL Database that combines schema inspection, natural language queries, and intelligent result formatting

106 lines (105 loc) 3.55 kB
import * as sql from "mssql"; import { AzureOpenAI, OpenAI } from "openai"; export interface SQLQueryResult { [key: string]: any; } type Language = "english" | "ukrainian"; interface AIClientConfig { client: AzureOpenAI | OpenAI; model: string; language?: Language; } /** * Inspects and analyzes SQL Server database schema. * Provides detailed information about tables, columns, keys, and indexes. */ export declare class SQLSchemaInspector { private pool; private config; private retryConfig; /** * Creates an instance of SQLSchemaInspector. * @param config - SQL Server connection configuration */ constructor(config: sql.config); /** * Establishes connection to the database with retry mechanism. * @throws Error if connection fails after maximum retry attempts */ connect(): Promise<void>; /** * Safely closes the database connection. */ disconnect(): Promise<void>; /** * Executes a SQL query and returns the results. * @param query - SQL query to execute * @returns Promise resolving to an array of query results * @throws Error if query execution fails */ executeQuery<T = SQLQueryResult>(query: string): Promise<T[]>; /** * Performs comprehensive analysis of the database schema. * @returns Promise resolving to a formatted string containing schema details * @throws Error if schema inspection fails */ inspectSchema(): Promise<string>; } /** * Handles AI-powered SQL query generation and result formatting. */ export declare class SQLQueryAssistant { private aiConfig; /** * Creates an instance of SQLQueryAssistant. * @param config - Configuration for AI client including model and language settings */ constructor(config: AIClientConfig); /** * Generates SQL query based on natural language prompt and schema. * @param prompt - Natural language query description * @param schema - Database schema information * @returns Promise resolving to generated SQL query */ generateSQLQuery(prompt: string, schema: string): Promise<string>; /** * Formats SQL query results into natural language response. * @param query - Executed SQL query * @param results - Query execution results * @returns Promise resolving to formatted natural language response */ formatResponse(query: string, results: SQLQueryResult[]): Promise<string>; /** * Cleans and formats the generated SQL query. * @param query - Raw query string to clean * @returns Cleaned query string * @private */ private cleanQuery; } /** * Main class that combines schema inspection and AI query capabilities. * Provides end-to-end functionality for natural language database querying. */ export declare class SQLAnalyzer { private inspector; private assistant; /** * Creates an instance of SQLAnalyzer. * @param dbConfig - SQL Server connection configuration * @param aiConfig - AI client configuration */ constructor(dbConfig: sql.config, aiConfig: AIClientConfig); /** * Processes natural language query through the entire pipeline. * @param prompt - Natural language query * @returns Promise resolving to formatted query results * @throws Error if any step in the process fails */ analyzeAndQuery(prompt: string): Promise<string>; /** * Closes the database connection. */ disconnect(): Promise<void>; } export {};