agenticaichat 1.0.0 → 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 +24 -5
- package/scripts/postinstall.js +126 -0
- package/templates/api-route.template.ts +24 -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.TogetherAIProvider = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const logger_1 = require("../../utils/logger");
|
|
9
|
+
class TogetherAIProvider {
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.configured = false;
|
|
12
|
+
this.baseURL = 'https://api.together.xyz/v1';
|
|
13
|
+
if (!config.apiKey) {
|
|
14
|
+
logger_1.logger.error('Together AI API key is required');
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
this.apiKey = config.apiKey;
|
|
18
|
+
this.model = config.model || 'mistralai/Mixtral-8x7B-Instruct-v0.1';
|
|
19
|
+
this.configured = true;
|
|
20
|
+
logger_1.logger.info(`Together AI 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('Together AI Provider not configured');
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
logger_1.logger.info('Generating SQL with Together AI...');
|
|
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('Together AI SQL generation failed', error);
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async generateHumanResponse(userQuery, queryResult) {
|
|
68
|
+
if (!this.configured) {
|
|
69
|
+
throw new Error('Together AI Provider not configured');
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
logger_1.logger.info('Generating human response with Together AI...');
|
|
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('Together AI 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.TogetherAIProvider = TogetherAIProvider;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { DatabaseSchema } from '../types';
|
|
2
|
+
export interface ILLMProvider {
|
|
3
|
+
/**
|
|
4
|
+
* Convert natural language to SQL
|
|
5
|
+
*/
|
|
6
|
+
generateSQL(userQuery: string, schema: DatabaseSchema): Promise<string>;
|
|
7
|
+
/**
|
|
8
|
+
* Generate human-readable response from query results
|
|
9
|
+
*/
|
|
10
|
+
generateHumanResponse(userQuery: string, queryResult: any): Promise<string>;
|
|
11
|
+
/**
|
|
12
|
+
* Validate if provider is properly configured
|
|
13
|
+
*/
|
|
14
|
+
isConfigured(): boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface BaseProviderConfig {
|
|
17
|
+
apiKey: string;
|
|
18
|
+
model: string;
|
|
19
|
+
}
|
|
20
|
+
export interface OpenAIProviderConfig extends BaseProviderConfig {
|
|
21
|
+
organization?: string;
|
|
22
|
+
}
|
|
23
|
+
export interface GeminiProviderConfig extends BaseProviderConfig {
|
|
24
|
+
}
|
|
25
|
+
export interface ClaudeProviderConfig extends BaseProviderConfig {
|
|
26
|
+
}
|
|
27
|
+
export interface GrokProviderConfig extends BaseProviderConfig {
|
|
28
|
+
}
|
|
29
|
+
export interface TogetherAIProviderConfig extends BaseProviderConfig {
|
|
30
|
+
}
|
|
31
|
+
export interface DeepInfraProviderConfig extends BaseProviderConfig {
|
|
32
|
+
}
|
|
33
|
+
export interface GroqProviderConfig extends BaseProviderConfig {
|
|
34
|
+
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type DatabaseCategory = 'sql' | 'nosql';
|
|
2
|
+
export type SqlDatabaseType = 'postgresql' | 'mysql' | 'sqlite' | 'turso' | 'mssql';
|
|
3
|
+
export type NoSqlDatabaseType = 'mongodb';
|
|
4
|
+
export type SupportedDatabaseType = SqlDatabaseType | NoSqlDatabaseType;
|
|
2
5
|
export interface DatabaseConfig {
|
|
3
|
-
|
|
6
|
+
category: DatabaseCategory;
|
|
7
|
+
type: SupportedDatabaseType;
|
|
4
8
|
url: string;
|
|
5
9
|
host?: string;
|
|
6
10
|
port?: number;
|
|
@@ -8,6 +12,16 @@ export interface DatabaseConfig {
|
|
|
8
12
|
password?: string;
|
|
9
13
|
database?: string;
|
|
10
14
|
}
|
|
15
|
+
export interface QueryResult {
|
|
16
|
+
rows?: any[];
|
|
17
|
+
rowCount?: number;
|
|
18
|
+
fields?: any[];
|
|
19
|
+
}
|
|
20
|
+
export interface EngineStatus {
|
|
21
|
+
connected: boolean;
|
|
22
|
+
type: SupportedDatabaseType;
|
|
23
|
+
category: DatabaseCategory;
|
|
24
|
+
}
|
|
11
25
|
export interface DatabaseSchema {
|
|
12
26
|
tables: TableSchema[];
|
|
13
27
|
}
|
|
@@ -40,6 +54,12 @@ export interface QueryResponse {
|
|
|
40
54
|
}
|
|
41
55
|
export interface ChatbotConfig {
|
|
42
56
|
database: DatabaseConfig;
|
|
43
|
-
|
|
57
|
+
llm: LLMConfig;
|
|
44
58
|
debug?: boolean;
|
|
45
59
|
}
|
|
60
|
+
export type LLMProviderType = 'openai' | 'gemini' | 'claude' | 'grok' | 'together' | 'deepinfra' | 'groq';
|
|
61
|
+
export interface LLMConfig {
|
|
62
|
+
provider: LLMProviderType;
|
|
63
|
+
apiKey: string;
|
|
64
|
+
model: string;
|
|
65
|
+
}
|