agenticaichat 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +83 -26
- package/bin/cli.js +301 -84
- package/dist/core/ChatbotEngine.d.ts +9 -1
- package/dist/core/ChatbotEngine.js +52 -19
- package/dist/core/EngineFactory.d.ts +45 -0
- package/dist/core/EngineFactory.js +126 -0
- package/dist/core/NLToSQLConverter.d.ts +6 -15
- package/dist/core/NLToSQLConverter.js +9 -114
- package/dist/engines/base/DatabaseEngine.d.ts +46 -0
- package/dist/engines/base/DatabaseEngine.js +42 -0
- package/dist/engines/nosql/MongoEngine.d.ts +13 -0
- package/dist/engines/nosql/MongoEngine.js +194 -0
- package/dist/engines/nosql/NoSqlEngine.d.ts +20 -0
- package/dist/engines/nosql/NoSqlEngine.js +29 -0
- package/dist/engines/sql/SqlEngine.d.ts +19 -0
- package/dist/engines/sql/SqlEngine.js +52 -0
- package/dist/engines/sql/dialects/MySQLDialect.d.ts +12 -0
- package/dist/engines/sql/dialects/MySQLDialect.js +109 -0
- package/dist/engines/sql/dialects/PostgresDialect.d.ts +11 -0
- package/dist/engines/sql/dialects/PostgresDialect.js +109 -0
- package/dist/engines/sql/dialects/SQLiteDialect.d.ts +10 -0
- package/dist/engines/sql/dialects/SQLiteDialect.js +101 -0
- package/dist/index.d.ts +17 -4
- package/dist/index.js +44 -8
- package/dist/llm/LLMFactory.d.ts +25 -0
- package/dist/llm/LLMFactory.js +137 -0
- package/dist/llm/providers/ClaudeProvider.d.ts +13 -0
- package/dist/llm/providers/ClaudeProvider.js +132 -0
- package/dist/llm/providers/DeepInfraProvider.d.ts +14 -0
- package/dist/llm/providers/DeepInfraProvider.js +144 -0
- package/dist/llm/providers/GeminiProvider.d.ts +13 -0
- package/dist/llm/providers/GeminiProvider.js +105 -0
- package/dist/llm/providers/GrokProvider.d.ts +14 -0
- package/dist/llm/providers/GrokProvider.js +144 -0
- package/dist/llm/providers/GroqProvider.d.ts +0 -0
- package/dist/llm/providers/GroqProvider.js +148 -0
- package/dist/llm/providers/OpenAIProvider.d.ts +13 -0
- package/dist/llm/providers/OpenAIProvider.js +136 -0
- package/dist/llm/providers/TogetherAIProvider.d.ts +14 -0
- package/dist/llm/providers/TogetherAIProvider.js +144 -0
- package/dist/llm/types.d.ts +34 -0
- package/dist/llm/types.js +2 -0
- package/dist/types/index.d.ts +23 -3
- package/dist/widget/ChatbotWidget.d.ts +1 -1
- package/dist/widget/ChatbotWidget.js +406 -125
- package/package.json +19 -4
- package/scripts/postinstall.js +126 -0
- package/templates/api-route.template.ts +24 -14
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,20 @@
|
|
|
1
1
|
export { ChatbotWidget } from './widget';
|
|
2
|
-
export type { ChatbotWidgetProps } from './widget/ChatbotWidget';
|
|
3
|
-
export { DatabaseConnector } from './core/DatabaseConnector';
|
|
4
|
-
export { NLToSQLConverter } from './core/NLToSQLConverter';
|
|
5
2
|
export { ChatbotEngine } from './core/ChatbotEngine';
|
|
6
|
-
export
|
|
3
|
+
export { EngineFactory } from './core/EngineFactory';
|
|
4
|
+
export { DatabaseEngine } from './engines/base/DatabaseEngine';
|
|
5
|
+
export { SqlEngine } from './engines/sql/SqlEngine';
|
|
6
|
+
export { NoSqlEngine } from './engines/nosql/NoSqlEngine';
|
|
7
|
+
export { PostgresDialect } from './engines/sql/dialects/PostgresDialect';
|
|
8
|
+
export { MySQLDialect } from './engines/sql/dialects/MySQLDialect';
|
|
9
|
+
export { SQLiteDialect } from './engines/sql/dialects/SQLiteDialect';
|
|
10
|
+
export { MongoEngine } from './engines/nosql/MongoEngine';
|
|
11
|
+
export { LLMFactory } from './llm/LLMFactory';
|
|
12
|
+
export type { ILLMProvider } from './llm/types';
|
|
13
|
+
export { OpenAIProvider } from './llm/providers/OpenAIProvider';
|
|
14
|
+
export { GeminiProvider } from './llm/providers/GeminiProvider';
|
|
15
|
+
export { ClaudeProvider } from './llm/providers/ClaudeProvider';
|
|
16
|
+
export { GrokProvider } from './llm/providers/GrokProvider';
|
|
17
|
+
export { TogetherAIProvider } from './llm/providers/TogetherAIProvider';
|
|
18
|
+
export { DeepInfraProvider } from './llm/providers/DeepInfraProvider';
|
|
19
|
+
export type { DatabaseCategory, SqlDatabaseType, NoSqlDatabaseType, SupportedDatabaseType, DatabaseConfig, DatabaseSchema, TableSchema, ColumnSchema, LLMConfig, LLMProviderType, ChatMessage, QueryRequest, QueryResponse, QueryResult, EngineStatus, ChatbotConfig } from './types';
|
|
7
20
|
export { Logger, logger } from './utils/logger';
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,53 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.logger = exports.Logger = exports.
|
|
4
|
-
//
|
|
3
|
+
exports.logger = exports.Logger = exports.DeepInfraProvider = exports.TogetherAIProvider = exports.GrokProvider = exports.ClaudeProvider = exports.GeminiProvider = exports.OpenAIProvider = exports.LLMFactory = exports.MongoEngine = exports.SQLiteDialect = exports.MySQLDialect = exports.PostgresDialect = exports.NoSqlEngine = exports.SqlEngine = exports.DatabaseEngine = exports.EngineFactory = exports.ChatbotEngine = exports.ChatbotWidget = void 0;
|
|
4
|
+
// ============================================
|
|
5
|
+
// CLIENT-SIDE EXPORTS (Safe for browser)
|
|
6
|
+
// ============================================
|
|
5
7
|
var widget_1 = require("./widget");
|
|
6
8
|
Object.defineProperty(exports, "ChatbotWidget", { enumerable: true, get: function () { return widget_1.ChatbotWidget; } });
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
Object.defineProperty(exports, "NLToSQLConverter", { enumerable: true, get: function () { return NLToSQLConverter_1.NLToSQLConverter; } });
|
|
9
|
+
// ============================================
|
|
10
|
+
// SERVER-SIDE EXPORTS (Node.js only)
|
|
11
|
+
// ============================================
|
|
12
|
+
// Note: Import these ONLY in API routes or server components
|
|
13
|
+
// Core classes
|
|
13
14
|
var ChatbotEngine_1 = require("./core/ChatbotEngine");
|
|
14
15
|
Object.defineProperty(exports, "ChatbotEngine", { enumerable: true, get: function () { return ChatbotEngine_1.ChatbotEngine; } });
|
|
16
|
+
var EngineFactory_1 = require("./core/EngineFactory");
|
|
17
|
+
Object.defineProperty(exports, "EngineFactory", { enumerable: true, get: function () { return EngineFactory_1.EngineFactory; } });
|
|
18
|
+
// Engines
|
|
19
|
+
var DatabaseEngine_1 = require("./engines/base/DatabaseEngine");
|
|
20
|
+
Object.defineProperty(exports, "DatabaseEngine", { enumerable: true, get: function () { return DatabaseEngine_1.DatabaseEngine; } });
|
|
21
|
+
var SqlEngine_1 = require("./engines/sql/SqlEngine");
|
|
22
|
+
Object.defineProperty(exports, "SqlEngine", { enumerable: true, get: function () { return SqlEngine_1.SqlEngine; } });
|
|
23
|
+
var NoSqlEngine_1 = require("./engines/nosql/NoSqlEngine");
|
|
24
|
+
Object.defineProperty(exports, "NoSqlEngine", { enumerable: true, get: function () { return NoSqlEngine_1.NoSqlEngine; } });
|
|
25
|
+
// SQL dialects
|
|
26
|
+
var PostgresDialect_1 = require("./engines/sql/dialects/PostgresDialect");
|
|
27
|
+
Object.defineProperty(exports, "PostgresDialect", { enumerable: true, get: function () { return PostgresDialect_1.PostgresDialect; } });
|
|
28
|
+
var MySQLDialect_1 = require("./engines/sql/dialects/MySQLDialect");
|
|
29
|
+
Object.defineProperty(exports, "MySQLDialect", { enumerable: true, get: function () { return MySQLDialect_1.MySQLDialect; } });
|
|
30
|
+
var SQLiteDialect_1 = require("./engines/sql/dialects/SQLiteDialect");
|
|
31
|
+
Object.defineProperty(exports, "SQLiteDialect", { enumerable: true, get: function () { return SQLiteDialect_1.SQLiteDialect; } });
|
|
32
|
+
// NoSQL engines
|
|
33
|
+
var MongoEngine_1 = require("./engines/nosql/MongoEngine");
|
|
34
|
+
Object.defineProperty(exports, "MongoEngine", { enumerable: true, get: function () { return MongoEngine_1.MongoEngine; } });
|
|
35
|
+
// LLM classes
|
|
36
|
+
var LLMFactory_1 = require("./llm/LLMFactory");
|
|
37
|
+
Object.defineProperty(exports, "LLMFactory", { enumerable: true, get: function () { return LLMFactory_1.LLMFactory; } });
|
|
38
|
+
// LLM providers
|
|
39
|
+
var OpenAIProvider_1 = require("./llm/providers/OpenAIProvider");
|
|
40
|
+
Object.defineProperty(exports, "OpenAIProvider", { enumerable: true, get: function () { return OpenAIProvider_1.OpenAIProvider; } });
|
|
41
|
+
var GeminiProvider_1 = require("./llm/providers/GeminiProvider");
|
|
42
|
+
Object.defineProperty(exports, "GeminiProvider", { enumerable: true, get: function () { return GeminiProvider_1.GeminiProvider; } });
|
|
43
|
+
var ClaudeProvider_1 = require("./llm/providers/ClaudeProvider");
|
|
44
|
+
Object.defineProperty(exports, "ClaudeProvider", { enumerable: true, get: function () { return ClaudeProvider_1.ClaudeProvider; } });
|
|
45
|
+
var GrokProvider_1 = require("./llm/providers/GrokProvider");
|
|
46
|
+
Object.defineProperty(exports, "GrokProvider", { enumerable: true, get: function () { return GrokProvider_1.GrokProvider; } });
|
|
47
|
+
var TogetherAIProvider_1 = require("./llm/providers/TogetherAIProvider");
|
|
48
|
+
Object.defineProperty(exports, "TogetherAIProvider", { enumerable: true, get: function () { return TogetherAIProvider_1.TogetherAIProvider; } });
|
|
49
|
+
var DeepInfraProvider_1 = require("./llm/providers/DeepInfraProvider");
|
|
50
|
+
Object.defineProperty(exports, "DeepInfraProvider", { enumerable: true, get: function () { return DeepInfraProvider_1.DeepInfraProvider; } });
|
|
15
51
|
// Utils
|
|
16
52
|
var logger_1 = require("./utils/logger");
|
|
17
53
|
Object.defineProperty(exports, "Logger", { enumerable: true, get: function () { return logger_1.Logger; } });
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ILLMProvider } from './types';
|
|
2
|
+
import { LLMConfig } from '../types';
|
|
3
|
+
export declare class LLMFactory {
|
|
4
|
+
/**
|
|
5
|
+
* Create appropriate LLM provider based on config
|
|
6
|
+
*/
|
|
7
|
+
static createProvider(config: LLMConfig): ILLMProvider;
|
|
8
|
+
/**
|
|
9
|
+
* Get list of supported providers with metadata
|
|
10
|
+
*/
|
|
11
|
+
static getSupportedProviders(): {
|
|
12
|
+
name: string;
|
|
13
|
+
value: string;
|
|
14
|
+
description: string;
|
|
15
|
+
models: string[];
|
|
16
|
+
website: string;
|
|
17
|
+
}[];
|
|
18
|
+
/**
|
|
19
|
+
* Validate provider configuration
|
|
20
|
+
*/
|
|
21
|
+
static validateConfig(config: LLMConfig): {
|
|
22
|
+
valid: boolean;
|
|
23
|
+
error?: string;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LLMFactory = void 0;
|
|
4
|
+
const OpenAIProvider_1 = require("./providers/OpenAIProvider");
|
|
5
|
+
const GeminiProvider_1 = require("./providers/GeminiProvider");
|
|
6
|
+
const ClaudeProvider_1 = require("./providers/ClaudeProvider");
|
|
7
|
+
const GrokProvider_1 = require("./providers/GrokProvider");
|
|
8
|
+
const TogetherAIProvider_1 = require("./providers/TogetherAIProvider");
|
|
9
|
+
const DeepInfraProvider_1 = require("./providers/DeepInfraProvider");
|
|
10
|
+
// import { GroqProvider } from './providers/GroqProvider';
|
|
11
|
+
const logger_1 = require("../utils/logger");
|
|
12
|
+
class LLMFactory {
|
|
13
|
+
/**
|
|
14
|
+
* Create appropriate LLM provider based on config
|
|
15
|
+
*/
|
|
16
|
+
static createProvider(config) {
|
|
17
|
+
logger_1.logger.info(`Creating LLM provider: ${config.provider}`);
|
|
18
|
+
const providerConfig = {
|
|
19
|
+
apiKey: config.apiKey,
|
|
20
|
+
model: config.model
|
|
21
|
+
};
|
|
22
|
+
switch (config.provider) {
|
|
23
|
+
case 'openai':
|
|
24
|
+
return new OpenAIProvider_1.OpenAIProvider(providerConfig);
|
|
25
|
+
case 'gemini':
|
|
26
|
+
return new GeminiProvider_1.GeminiProvider(providerConfig);
|
|
27
|
+
case 'claude':
|
|
28
|
+
return new ClaudeProvider_1.ClaudeProvider(providerConfig);
|
|
29
|
+
case 'grok':
|
|
30
|
+
return new GrokProvider_1.GrokProvider(providerConfig);
|
|
31
|
+
case 'together':
|
|
32
|
+
return new TogetherAIProvider_1.TogetherAIProvider(providerConfig);
|
|
33
|
+
case 'deepinfra':
|
|
34
|
+
return new DeepInfraProvider_1.DeepInfraProvider(providerConfig);
|
|
35
|
+
// case 'groq':
|
|
36
|
+
// return new GroqProvider(providerConfig);
|
|
37
|
+
default:
|
|
38
|
+
throw new Error(`Unsupported LLM provider: ${config.provider}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Get list of supported providers with metadata
|
|
43
|
+
*/
|
|
44
|
+
static getSupportedProviders() {
|
|
45
|
+
return [
|
|
46
|
+
{
|
|
47
|
+
name: 'OpenAI (GPT-3.5/GPT-4)',
|
|
48
|
+
value: 'openai',
|
|
49
|
+
description: 'Most popular, reliable, high quality',
|
|
50
|
+
models: ['gpt-3.5-turbo', 'gpt-4', 'gpt-4-turbo'],
|
|
51
|
+
website: 'https://platform.openai.com'
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
name: 'Google Gemini (1.5 Pro/Flash)',
|
|
55
|
+
value: 'gemini',
|
|
56
|
+
description: 'Fast, affordable, multimodal support',
|
|
57
|
+
models: ['gemini-1.5-pro', 'gemini-1.5-flash'],
|
|
58
|
+
website: 'https://ai.google.dev'
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
name: 'Anthropic Claude (Sonnet/Opus/Haiku)',
|
|
62
|
+
value: 'claude',
|
|
63
|
+
description: 'Best reasoning, longest context window',
|
|
64
|
+
models: [
|
|
65
|
+
'claude-3-sonnet-20240229',
|
|
66
|
+
'claude-3-opus-20240229',
|
|
67
|
+
'claude-3-haiku-20240307'
|
|
68
|
+
],
|
|
69
|
+
website: 'https://www.anthropic.com'
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
name: 'xAI Grok',
|
|
73
|
+
value: 'grok',
|
|
74
|
+
description: 'Real-time data, Twitter integration',
|
|
75
|
+
models: ['grok-beta'],
|
|
76
|
+
website: 'https://x.ai'
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
name: 'Together AI',
|
|
80
|
+
value: 'together',
|
|
81
|
+
description: 'Open-source models, cost-effective',
|
|
82
|
+
models: [
|
|
83
|
+
'mistralai/Mixtral-8x7B-Instruct-v0.1',
|
|
84
|
+
'meta-llama/Llama-3-70b-chat-hf'
|
|
85
|
+
],
|
|
86
|
+
website: 'https://www.together.ai'
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: 'DeepInfra',
|
|
90
|
+
value: 'deepinfra',
|
|
91
|
+
description: 'Multiple models, serverless infrastructure',
|
|
92
|
+
models: [
|
|
93
|
+
'meta-llama/Meta-Llama-3-70B-Instruct',
|
|
94
|
+
'mistralai/Mixtral-8x7B-Instruct-v0.1'
|
|
95
|
+
],
|
|
96
|
+
website: 'https://deepinfra.com'
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
name: 'Groq',
|
|
100
|
+
value: 'groq',
|
|
101
|
+
description: 'Ultra-fast inference, hardware acceleration',
|
|
102
|
+
models: ['llama3-70b-8192', 'mixtral-8x7b-32768'],
|
|
103
|
+
website: 'https://groq.com'
|
|
104
|
+
}
|
|
105
|
+
];
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Validate provider configuration
|
|
109
|
+
*/
|
|
110
|
+
static validateConfig(config) {
|
|
111
|
+
// Check provider is supported
|
|
112
|
+
const supportedProviders = this.getSupportedProviders();
|
|
113
|
+
const providerExists = supportedProviders.some(p => p.value === config.provider);
|
|
114
|
+
if (!providerExists) {
|
|
115
|
+
return {
|
|
116
|
+
valid: false,
|
|
117
|
+
error: `Unsupported provider: ${config.provider}. Supported: ${supportedProviders.map(p => p.value).join(', ')}`
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
// Check API key
|
|
121
|
+
if (!config.apiKey || config.apiKey.trim() === '') {
|
|
122
|
+
return {
|
|
123
|
+
valid: false,
|
|
124
|
+
error: 'API key is required'
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
// Check model
|
|
128
|
+
if (!config.model || config.model.trim() === '') {
|
|
129
|
+
return {
|
|
130
|
+
valid: false,
|
|
131
|
+
error: 'Model name is required'
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
return { valid: true };
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
exports.LLMFactory = LLMFactory;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ILLMProvider, ClaudeProviderConfig } from '../types';
|
|
2
|
+
import { DatabaseSchema } from '../../types';
|
|
3
|
+
export declare class ClaudeProvider implements ILLMProvider {
|
|
4
|
+
private client;
|
|
5
|
+
private model;
|
|
6
|
+
private configured;
|
|
7
|
+
constructor(config: ClaudeProviderConfig);
|
|
8
|
+
isConfigured(): boolean;
|
|
9
|
+
generateSQL(userQuery: string, schema: DatabaseSchema): Promise<string>;
|
|
10
|
+
generateHumanResponse(userQuery: string, queryResult: any): Promise<string>;
|
|
11
|
+
private buildSQLPrompt;
|
|
12
|
+
private formatSchema;
|
|
13
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ClaudeProvider = void 0;
|
|
7
|
+
const sdk_1 = __importDefault(require("@anthropic-ai/sdk"));
|
|
8
|
+
const logger_1 = require("../../utils/logger");
|
|
9
|
+
class ClaudeProvider {
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.configured = false;
|
|
12
|
+
if (!config.apiKey) {
|
|
13
|
+
logger_1.logger.error('Claude API key is required');
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
this.client = new sdk_1.default({
|
|
17
|
+
apiKey: config.apiKey
|
|
18
|
+
});
|
|
19
|
+
this.model = config.model || 'claude-3-sonnet-20240229';
|
|
20
|
+
this.configured = true;
|
|
21
|
+
logger_1.logger.info(`Claude Provider initialized with model: ${this.model}`);
|
|
22
|
+
}
|
|
23
|
+
isConfigured() {
|
|
24
|
+
return this.configured;
|
|
25
|
+
}
|
|
26
|
+
async generateSQL(userQuery, schema) {
|
|
27
|
+
if (!this.configured) {
|
|
28
|
+
throw new Error('Claude Provider not configured');
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
logger_1.logger.info('Generating SQL with Claude...');
|
|
32
|
+
const schemaDescription = this.formatSchema(schema);
|
|
33
|
+
const prompt = this.buildSQLPrompt(userQuery, schemaDescription);
|
|
34
|
+
const message = await this.client.messages.create({
|
|
35
|
+
model: this.model,
|
|
36
|
+
max_tokens: 500,
|
|
37
|
+
temperature: 0.2,
|
|
38
|
+
messages: [
|
|
39
|
+
{
|
|
40
|
+
role: 'user',
|
|
41
|
+
content: prompt
|
|
42
|
+
}
|
|
43
|
+
],
|
|
44
|
+
system: 'You are an expert SQL query generator. Generate ONLY valid SQL queries without any explanation or markdown.'
|
|
45
|
+
});
|
|
46
|
+
const sqlQuery = message.content[0].type === 'text'
|
|
47
|
+
? message.content[0].text.trim()
|
|
48
|
+
: '';
|
|
49
|
+
// Remove markdown code blocks
|
|
50
|
+
const cleanedSQL = sqlQuery
|
|
51
|
+
.replace(/```sql\n?/g, '')
|
|
52
|
+
.replace(/```\n?/g, '')
|
|
53
|
+
.trim();
|
|
54
|
+
logger_1.logger.debug_log('Generated SQL:', cleanedSQL);
|
|
55
|
+
return cleanedSQL;
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
logger_1.logger.error('Claude SQL generation failed', error);
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async generateHumanResponse(userQuery, queryResult) {
|
|
63
|
+
if (!this.configured) {
|
|
64
|
+
throw new Error('Claude Provider not configured');
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
logger_1.logger.info('Generating human response with Claude...');
|
|
68
|
+
const prompt = `
|
|
69
|
+
User asked: "${userQuery}"
|
|
70
|
+
|
|
71
|
+
Database returned this result:
|
|
72
|
+
${JSON.stringify(queryResult, null, 2)}
|
|
73
|
+
|
|
74
|
+
Please provide a clear, conversational answer in Hindi/English mix (Hinglish) that explains this data to a business owner. Be specific with numbers.
|
|
75
|
+
`;
|
|
76
|
+
const message = await this.client.messages.create({
|
|
77
|
+
model: this.model,
|
|
78
|
+
max_tokens: 300,
|
|
79
|
+
temperature: 0.7,
|
|
80
|
+
messages: [
|
|
81
|
+
{
|
|
82
|
+
role: 'user',
|
|
83
|
+
content: prompt
|
|
84
|
+
}
|
|
85
|
+
],
|
|
86
|
+
system: 'You are a helpful business analytics assistant. Explain data clearly in Hinglish (Hindi-English mix).'
|
|
87
|
+
});
|
|
88
|
+
const answer = message.content[0].type === 'text'
|
|
89
|
+
? message.content[0].text.trim()
|
|
90
|
+
: 'Sorry, I could not generate a response.';
|
|
91
|
+
logger_1.logger.debug_log('Generated response:', answer);
|
|
92
|
+
return answer;
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
logger_1.logger.error('Claude response generation failed', error);
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
buildSQLPrompt(userQuery, schemaDescription) {
|
|
100
|
+
return `
|
|
101
|
+
You are a SQL expert. Generate a SQL query based on the user's question.
|
|
102
|
+
|
|
103
|
+
Database Schema:
|
|
104
|
+
${schemaDescription}
|
|
105
|
+
|
|
106
|
+
User Question: "${userQuery}"
|
|
107
|
+
|
|
108
|
+
Rules:
|
|
109
|
+
1. Return ONLY the SQL query, nothing else
|
|
110
|
+
2. Use correct table and column names from the schema
|
|
111
|
+
3. Add WHERE clauses for date filtering when needed
|
|
112
|
+
4. Use SUM, COUNT, AVG for aggregations
|
|
113
|
+
5. Handle both singular and plural forms
|
|
114
|
+
6. Use CURRENT_DATE for time-based queries
|
|
115
|
+
7. Make sure the query is syntactically correct
|
|
116
|
+
|
|
117
|
+
SQL Query:
|
|
118
|
+
`;
|
|
119
|
+
}
|
|
120
|
+
formatSchema(schema) {
|
|
121
|
+
let description = '';
|
|
122
|
+
for (const table of schema.tables) {
|
|
123
|
+
description += `\nTable: ${table.name}\n`;
|
|
124
|
+
description += 'Columns:\n';
|
|
125
|
+
for (const column of table.columns) {
|
|
126
|
+
description += ` - ${column.name} (${column.type})${column.nullable ? ' [nullable]' : ''}\n`;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return description;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
exports.ClaudeProvider = ClaudeProvider;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ILLMProvider, DeepInfraProviderConfig } from '../types';
|
|
2
|
+
import { DatabaseSchema } from '../../types';
|
|
3
|
+
export declare class DeepInfraProvider implements ILLMProvider {
|
|
4
|
+
private apiKey;
|
|
5
|
+
private model;
|
|
6
|
+
private configured;
|
|
7
|
+
private baseURL;
|
|
8
|
+
constructor(config: DeepInfraProviderConfig);
|
|
9
|
+
isConfigured(): boolean;
|
|
10
|
+
generateSQL(userQuery: string, schema: DatabaseSchema): Promise<string>;
|
|
11
|
+
generateHumanResponse(userQuery: string, queryResult: any): Promise<string>;
|
|
12
|
+
private buildSQLPrompt;
|
|
13
|
+
private formatSchema;
|
|
14
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.DeepInfraProvider = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const logger_1 = require("../../utils/logger");
|
|
9
|
+
class DeepInfraProvider {
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.configured = false;
|
|
12
|
+
this.baseURL = 'https://api.deepinfra.com/v1/openai';
|
|
13
|
+
if (!config.apiKey) {
|
|
14
|
+
logger_1.logger.error('DeepInfra API key is required');
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
this.apiKey = config.apiKey;
|
|
18
|
+
this.model = config.model || 'meta-llama/Meta-Llama-3-70B-Instruct';
|
|
19
|
+
this.configured = true;
|
|
20
|
+
logger_1.logger.info(`DeepInfra Provider initialized with model: ${this.model}`);
|
|
21
|
+
}
|
|
22
|
+
isConfigured() {
|
|
23
|
+
return this.configured;
|
|
24
|
+
}
|
|
25
|
+
async generateSQL(userQuery, schema) {
|
|
26
|
+
if (!this.configured) {
|
|
27
|
+
throw new Error('DeepInfra Provider not configured');
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
logger_1.logger.info('Generating SQL with DeepInfra...');
|
|
31
|
+
const schemaDescription = this.formatSchema(schema);
|
|
32
|
+
const prompt = this.buildSQLPrompt(userQuery, schemaDescription);
|
|
33
|
+
const response = await axios_1.default.post(`${this.baseURL}/chat/completions`, {
|
|
34
|
+
model: this.model,
|
|
35
|
+
messages: [
|
|
36
|
+
{
|
|
37
|
+
role: 'system',
|
|
38
|
+
content: 'You are an expert SQL query generator. Generate ONLY valid SQL queries without any explanation or markdown.'
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
role: 'user',
|
|
42
|
+
content: prompt
|
|
43
|
+
}
|
|
44
|
+
],
|
|
45
|
+
temperature: 0.2,
|
|
46
|
+
max_tokens: 500
|
|
47
|
+
}, {
|
|
48
|
+
headers: {
|
|
49
|
+
'Authorization': `Bearer ${this.apiKey}`,
|
|
50
|
+
'Content-Type': 'application/json'
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
const sqlQuery = response.data.choices[0]?.message?.content?.trim() || '';
|
|
54
|
+
// Remove markdown code blocks
|
|
55
|
+
const cleanedSQL = sqlQuery
|
|
56
|
+
.replace(/```sql\n?/g, '')
|
|
57
|
+
.replace(/```\n?/g, '')
|
|
58
|
+
.trim();
|
|
59
|
+
logger_1.logger.debug_log('Generated SQL:', cleanedSQL);
|
|
60
|
+
return cleanedSQL;
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
logger_1.logger.error('DeepInfra SQL generation failed', error);
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async generateHumanResponse(userQuery, queryResult) {
|
|
68
|
+
if (!this.configured) {
|
|
69
|
+
throw new Error('DeepInfra Provider not configured');
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
logger_1.logger.info('Generating human response with DeepInfra...');
|
|
73
|
+
const prompt = `
|
|
74
|
+
User asked: "${userQuery}"
|
|
75
|
+
|
|
76
|
+
Database returned this result:
|
|
77
|
+
${JSON.stringify(queryResult, null, 2)}
|
|
78
|
+
|
|
79
|
+
Please provide a clear, conversational answer in Hindi/English mix (Hinglish) that explains this data to a business owner. Be specific with numbers.
|
|
80
|
+
`;
|
|
81
|
+
const response = await axios_1.default.post(`${this.baseURL}/chat/completions`, {
|
|
82
|
+
model: this.model,
|
|
83
|
+
messages: [
|
|
84
|
+
{
|
|
85
|
+
role: 'system',
|
|
86
|
+
content: 'You are a helpful business analytics assistant. Explain data clearly in Hinglish (Hindi-English mix).'
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
role: 'user',
|
|
90
|
+
content: prompt
|
|
91
|
+
}
|
|
92
|
+
],
|
|
93
|
+
temperature: 0.7,
|
|
94
|
+
max_tokens: 300
|
|
95
|
+
}, {
|
|
96
|
+
headers: {
|
|
97
|
+
'Authorization': `Bearer ${this.apiKey}`,
|
|
98
|
+
'Content-Type': 'application/json'
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
const answer = response.data.choices[0]?.message?.content?.trim() ||
|
|
102
|
+
'Sorry, I could not generate a response.';
|
|
103
|
+
logger_1.logger.debug_log('Generated response:', answer);
|
|
104
|
+
return answer;
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
logger_1.logger.error('DeepInfra response generation failed', error);
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
buildSQLPrompt(userQuery, schemaDescription) {
|
|
112
|
+
return `
|
|
113
|
+
You are a SQL expert. Generate a SQL query based on the user's question.
|
|
114
|
+
|
|
115
|
+
Database Schema:
|
|
116
|
+
${schemaDescription}
|
|
117
|
+
|
|
118
|
+
User Question: "${userQuery}"
|
|
119
|
+
|
|
120
|
+
Rules:
|
|
121
|
+
1. Return ONLY the SQL query, nothing else
|
|
122
|
+
2. Use correct table and column names from the schema
|
|
123
|
+
3. Add WHERE clauses for date filtering when needed
|
|
124
|
+
4. Use SUM, COUNT, AVG for aggregations
|
|
125
|
+
5. Handle both singular and plural forms
|
|
126
|
+
6. Use CURRENT_DATE for time-based queries
|
|
127
|
+
7. Make sure the query is syntactically correct
|
|
128
|
+
|
|
129
|
+
SQL Query:
|
|
130
|
+
`;
|
|
131
|
+
}
|
|
132
|
+
formatSchema(schema) {
|
|
133
|
+
let description = '';
|
|
134
|
+
for (const table of schema.tables) {
|
|
135
|
+
description += `\nTable: ${table.name}\n`;
|
|
136
|
+
description += 'Columns:\n';
|
|
137
|
+
for (const column of table.columns) {
|
|
138
|
+
description += ` - ${column.name} (${column.type})${column.nullable ? ' [nullable]' : ''}\n`;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return description;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
exports.DeepInfraProvider = DeepInfraProvider;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ILLMProvider, GeminiProviderConfig } from '../types';
|
|
2
|
+
import { DatabaseSchema } from '../../types';
|
|
3
|
+
export declare class GeminiProvider implements ILLMProvider {
|
|
4
|
+
private client;
|
|
5
|
+
private model;
|
|
6
|
+
private configured;
|
|
7
|
+
constructor(config: GeminiProviderConfig);
|
|
8
|
+
isConfigured(): boolean;
|
|
9
|
+
generateSQL(userQuery: string, schema: DatabaseSchema): Promise<string>;
|
|
10
|
+
generateHumanResponse(userQuery: string, queryResult: any): Promise<string>;
|
|
11
|
+
private buildSQLPrompt;
|
|
12
|
+
private formatSchema;
|
|
13
|
+
}
|