@deepagents/text2sql 0.6.0 → 0.8.0
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/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1121 -453
- package/dist/index.js.map +4 -4
- package/dist/lib/adapters/mysql/column-stats.mysql.grounding.d.ts +14 -0
- package/dist/lib/adapters/mysql/column-stats.mysql.grounding.d.ts.map +1 -0
- package/dist/lib/adapters/mysql/column-values.mysql.grounding.d.ts +22 -0
- package/dist/lib/adapters/mysql/column-values.mysql.grounding.d.ts.map +1 -0
- package/dist/lib/adapters/mysql/constraint.mysql.grounding.d.ts +13 -0
- package/dist/lib/adapters/mysql/constraint.mysql.grounding.d.ts.map +1 -0
- package/dist/lib/adapters/mysql/index.d.ts +44 -0
- package/dist/lib/adapters/mysql/index.d.ts.map +1 -0
- package/dist/lib/adapters/mysql/index.js +1597 -0
- package/dist/lib/adapters/mysql/index.js.map +7 -0
- package/dist/lib/adapters/mysql/indexes.mysql.grounding.d.ts +13 -0
- package/dist/lib/adapters/mysql/indexes.mysql.grounding.d.ts.map +1 -0
- package/dist/lib/adapters/mysql/info.mysql.grounding.d.ts +13 -0
- package/dist/lib/adapters/mysql/info.mysql.grounding.d.ts.map +1 -0
- package/dist/lib/adapters/mysql/mysql.d.ts +33 -0
- package/dist/lib/adapters/mysql/mysql.d.ts.map +1 -0
- package/dist/lib/adapters/mysql/row-count.mysql.grounding.d.ts +13 -0
- package/dist/lib/adapters/mysql/row-count.mysql.grounding.d.ts.map +1 -0
- package/dist/lib/adapters/mysql/table.mysql.grounding.d.ts +21 -0
- package/dist/lib/adapters/mysql/table.mysql.grounding.d.ts.map +1 -0
- package/dist/lib/adapters/mysql/view.mysql.grounding.d.ts +18 -0
- package/dist/lib/adapters/mysql/view.mysql.grounding.d.ts.map +1 -0
- package/dist/lib/agents/bi.agent.d.ts +14 -0
- package/dist/lib/agents/bi.agent.d.ts.map +1 -0
- package/dist/lib/agents/chat1.agent.d.ts.map +1 -1
- package/dist/lib/agents/chat2.agent.d.ts.map +1 -1
- package/dist/lib/agents/developer.agent.d.ts +31 -0
- package/dist/lib/agents/developer.agent.d.ts.map +1 -0
- package/dist/lib/agents/question.agent.d.ts +1 -1
- package/dist/lib/agents/question.agent.d.ts.map +1 -1
- package/dist/lib/agents/sql.agent.d.ts +14 -32
- package/dist/lib/agents/sql.agent.d.ts.map +1 -1
- package/dist/lib/agents/text2sql.agent.d.ts.map +1 -1
- package/dist/lib/checkpoint.d.ts.map +1 -1
- package/dist/lib/sql.d.ts +42 -0
- package/dist/lib/sql.d.ts.map +1 -1
- package/dist/lib/synthesis/extractors/sql-extractor.d.ts.map +1 -1
- package/dist/lib/synthesis/index.js +267 -164
- package/dist/lib/synthesis/index.js.map +3 -3
- package/dist/lib/synthesis/synthesizers/breadth-evolver.d.ts.map +1 -1
- package/dist/lib/synthesis/synthesizers/depth-evolver.d.ts.map +1 -1
- package/dist/lib/synthesis/synthesizers/schema-synthesizer.d.ts.map +1 -1
- package/dist/lib/synthesis/synthesizers/styles.d.ts +2 -2
- package/dist/lib/synthesis/synthesizers/styles.d.ts.map +1 -1
- package/dist/lib/teach/teachings.d.ts.map +1 -1
- package/package.json +9 -3
|
@@ -706,7 +706,7 @@ import dedent4 from "dedent";
|
|
|
706
706
|
import z4 from "zod";
|
|
707
707
|
import { agent as agent4, generate as generate5, user as user5 } from "@deepagents/agent";
|
|
708
708
|
var complexityInstructions = {
|
|
709
|
-
|
|
709
|
+
simple: dedent4`
|
|
710
710
|
Generate simple questions that require:
|
|
711
711
|
- Basic SELECT with single table
|
|
712
712
|
- Simple WHERE clauses with one condition
|
|
@@ -714,7 +714,7 @@ var complexityInstructions = {
|
|
|
714
714
|
- No joins required
|
|
715
715
|
Examples: "How many customers do we have?", "List all products", "What is the total revenue?"
|
|
716
716
|
`,
|
|
717
|
-
|
|
717
|
+
moderate: dedent4`
|
|
718
718
|
Generate moderate questions that require:
|
|
719
719
|
- JOINs between 2-3 tables
|
|
720
720
|
- Multiple WHERE conditions (AND/OR)
|
|
@@ -723,7 +723,7 @@ var complexityInstructions = {
|
|
|
723
723
|
- Basic subqueries
|
|
724
724
|
Examples: "What are the top 5 customers by total orders?", "Which products have never been ordered?"
|
|
725
725
|
`,
|
|
726
|
-
|
|
726
|
+
complex: dedent4`
|
|
727
727
|
Generate complex questions that require:
|
|
728
728
|
- Multiple JOINs (3+ tables)
|
|
729
729
|
- Nested subqueries or CTEs
|
|
@@ -732,13 +732,14 @@ var complexityInstructions = {
|
|
|
732
732
|
- Date/time calculations
|
|
733
733
|
Examples: "What is the month-over-month growth rate?", "Which customers have increased spending compared to last year?"
|
|
734
734
|
`,
|
|
735
|
-
|
|
736
|
-
Generate
|
|
737
|
-
- ROW_NUMBER, RANK, DENSE_RANK
|
|
735
|
+
"high complex": dedent4`
|
|
736
|
+
Generate highly complex questions that require advanced SQL features:
|
|
737
|
+
- Window functions (ROW_NUMBER, RANK, DENSE_RANK)
|
|
738
738
|
- LAG, LEAD for comparisons
|
|
739
739
|
- Running totals (SUM OVER)
|
|
740
740
|
- Moving averages
|
|
741
741
|
- PARTITION BY clauses
|
|
742
|
+
- Complex CTEs with multiple levels
|
|
742
743
|
Examples: "What is the running total of sales per month?", "Rank customers by their purchase frequency within each region"
|
|
743
744
|
`
|
|
744
745
|
};
|
|
@@ -756,7 +757,7 @@ var questionGeneratorAgent = agent4({
|
|
|
756
757
|
}),
|
|
757
758
|
prompt: (state) => {
|
|
758
759
|
const count = state?.count;
|
|
759
|
-
const complexity = state?.complexity ?? "
|
|
760
|
+
const complexity = state?.complexity ?? "moderate";
|
|
760
761
|
return dedent4`
|
|
761
762
|
<identity>
|
|
762
763
|
You are a synthetic data generator specializing in creating realistic natural language questions
|
|
@@ -793,19 +794,40 @@ async function generateQuestions(params) {
|
|
|
793
794
|
const { introspection, complexity, count, prompt, model } = params;
|
|
794
795
|
const agentInstance = model ? questionGeneratorAgent.clone({ model }) : questionGeneratorAgent;
|
|
795
796
|
const userPrompt = prompt ?? `Generate ${count} questions at ${complexity} complexity given db schema.`;
|
|
796
|
-
const { experimental_output } = await generate5(
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
797
|
+
const { experimental_output } = await generate5(
|
|
798
|
+
agentInstance,
|
|
799
|
+
[user5(userPrompt)],
|
|
800
|
+
{
|
|
801
|
+
introspection,
|
|
802
|
+
complexity,
|
|
803
|
+
count
|
|
804
|
+
}
|
|
805
|
+
);
|
|
801
806
|
return { questions: experimental_output.questions };
|
|
802
807
|
}
|
|
803
808
|
|
|
804
809
|
// packages/text2sql/src/lib/agents/sql.agent.ts
|
|
805
810
|
import { groq as groq5 } from "@ai-sdk/groq";
|
|
806
|
-
import {
|
|
811
|
+
import {
|
|
812
|
+
APICallError,
|
|
813
|
+
JSONParseError,
|
|
814
|
+
NoContentGeneratedError,
|
|
815
|
+
NoObjectGeneratedError,
|
|
816
|
+
NoOutputGeneratedError,
|
|
817
|
+
TypeValidationError,
|
|
818
|
+
defaultSettingsMiddleware as defaultSettingsMiddleware2,
|
|
819
|
+
wrapLanguageModel as wrapLanguageModel2
|
|
820
|
+
} from "ai";
|
|
821
|
+
import { Console } from "node:console";
|
|
822
|
+
import { createWriteStream } from "node:fs";
|
|
823
|
+
import pRetry from "p-retry";
|
|
807
824
|
import z5 from "zod";
|
|
808
|
-
import {
|
|
825
|
+
import {
|
|
826
|
+
agent as agent5,
|
|
827
|
+
generate as generate6,
|
|
828
|
+
toOutput,
|
|
829
|
+
user as user6
|
|
830
|
+
} from "@deepagents/agent";
|
|
809
831
|
|
|
810
832
|
// packages/text2sql/src/lib/teach/xml.ts
|
|
811
833
|
function wrapBlock(tag, children) {
|
|
@@ -1174,6 +1196,11 @@ function toTeachables(generated) {
|
|
|
1174
1196
|
}
|
|
1175
1197
|
|
|
1176
1198
|
// packages/text2sql/src/lib/agents/sql.agent.ts
|
|
1199
|
+
var logger = new Console({
|
|
1200
|
+
stdout: createWriteStream("./sql-agent.log", { flags: "a" }),
|
|
1201
|
+
stderr: createWriteStream("./sql-agent-error.log", { flags: "a" }),
|
|
1202
|
+
inspectOptions: { depth: null }
|
|
1203
|
+
});
|
|
1177
1204
|
var RETRY_TEMPERATURES = [0, 0.2, 0.3];
|
|
1178
1205
|
var sqlQueryAgent = agent5({
|
|
1179
1206
|
name: "text2sql",
|
|
@@ -1201,107 +1228,131 @@ function extractSql(output) {
|
|
|
1201
1228
|
const match = output.match(/```sql\n?([\s\S]*?)```/);
|
|
1202
1229
|
return match ? match[1].trim() : output.trim();
|
|
1203
1230
|
}
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
}
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
user6(
|
|
1224
|
-
`<validation_error>Your previous SQL query had the following error: ${previousError}. Please fix the query.</validation_error>`
|
|
1225
|
-
)
|
|
1226
|
-
] : [user6(input)];
|
|
1227
|
-
try {
|
|
1228
|
-
const { experimental_output: output } = await generate6(
|
|
1229
|
-
agentInstance,
|
|
1230
|
-
messages,
|
|
1231
|
-
{
|
|
1232
|
-
teachings: toInstructions(
|
|
1233
|
-
"instructions",
|
|
1234
|
-
persona({
|
|
1235
|
-
name: "Freya",
|
|
1236
|
-
role: "You are an expert SQL query generator. You translate natural language questions into precise, efficient SQL queries based on the provided database schema."
|
|
1237
|
-
}),
|
|
1238
|
-
...instructions
|
|
1239
|
-
),
|
|
1240
|
-
introspection
|
|
1241
|
-
}
|
|
1242
|
-
);
|
|
1243
|
-
if ("error" in output) {
|
|
1244
|
-
return { success: false, error: output.error, isUnanswerable: true };
|
|
1245
|
-
}
|
|
1246
|
-
return { success: true, sql: extractSql(output.sql) };
|
|
1247
|
-
} catch (error) {
|
|
1248
|
-
if (error instanceof Error && (error.message.includes("Failed to validate JSON") || error.message.includes("response did not match schema"))) {
|
|
1249
|
-
return {
|
|
1250
|
-
success: false,
|
|
1251
|
-
error: `Schema validation failed: ${error.message}`
|
|
1252
|
-
};
|
|
1253
|
-
}
|
|
1254
|
-
throw error;
|
|
1231
|
+
var marker = Symbol("SQLValidationError");
|
|
1232
|
+
var SQLValidationError = class _SQLValidationError extends Error {
|
|
1233
|
+
[marker];
|
|
1234
|
+
constructor(message) {
|
|
1235
|
+
super(message);
|
|
1236
|
+
this.name = "SQLValidationError";
|
|
1237
|
+
this[marker] = true;
|
|
1238
|
+
}
|
|
1239
|
+
static isInstance(error) {
|
|
1240
|
+
return error instanceof _SQLValidationError && error[marker] === true;
|
|
1241
|
+
}
|
|
1242
|
+
};
|
|
1243
|
+
var UnanswerableSQLError = class _UnanswerableSQLError extends Error {
|
|
1244
|
+
constructor(message) {
|
|
1245
|
+
super(message);
|
|
1246
|
+
this.name = "UnanswerableSQLError";
|
|
1247
|
+
}
|
|
1248
|
+
static isInstance(error) {
|
|
1249
|
+
return error instanceof _UnanswerableSQLError;
|
|
1255
1250
|
}
|
|
1256
|
-
}
|
|
1257
|
-
var sqlGenerators = {
|
|
1258
|
-
generateSql
|
|
1259
1251
|
};
|
|
1260
|
-
async function generateAndValidate(options, temperature, previousError) {
|
|
1261
|
-
const result = await sqlGenerators.generateSql({
|
|
1262
|
-
input: options.input,
|
|
1263
|
-
model: options.model ?? sqlQueryAgent.model,
|
|
1264
|
-
temperature,
|
|
1265
|
-
introspection: options.introspection,
|
|
1266
|
-
instructions: options.instructions,
|
|
1267
|
-
previousError
|
|
1268
|
-
});
|
|
1269
|
-
if (!result.success) {
|
|
1270
|
-
return {
|
|
1271
|
-
ok: false,
|
|
1272
|
-
error: result.error,
|
|
1273
|
-
isUnanswerable: result.isUnanswerable
|
|
1274
|
-
};
|
|
1275
|
-
}
|
|
1276
|
-
const validationError = await options.adapter.validate(result.sql);
|
|
1277
|
-
if (validationError) {
|
|
1278
|
-
return { ok: false, error: validationError };
|
|
1279
|
-
}
|
|
1280
|
-
return { ok: true, sql: result.sql };
|
|
1281
|
-
}
|
|
1282
1252
|
async function toSql(options) {
|
|
1283
1253
|
const { maxRetries = 3 } = options;
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1254
|
+
return withRetry(
|
|
1255
|
+
async (attemptNumber, errors, attempts) => {
|
|
1256
|
+
const agentInstance = sqlQueryAgent.clone({
|
|
1257
|
+
model: wrapLanguageModel2({
|
|
1258
|
+
model: options.model ?? sqlQueryAgent.model,
|
|
1259
|
+
middleware: defaultSettingsMiddleware2({
|
|
1260
|
+
settings: {
|
|
1261
|
+
temperature: RETRY_TEMPERATURES[attemptNumber - 1] ?? 0.3,
|
|
1262
|
+
topP: 1
|
|
1263
|
+
}
|
|
1264
|
+
})
|
|
1265
|
+
})
|
|
1266
|
+
});
|
|
1267
|
+
const messages = errors.length ? [
|
|
1268
|
+
user6(options.input),
|
|
1269
|
+
user6(
|
|
1270
|
+
`<validation_error>Your previous SQL query had the following error: ${errors.at(-1)?.message}. Please fix the query.</validation_error>`
|
|
1271
|
+
)
|
|
1272
|
+
] : [user6(options.input)];
|
|
1273
|
+
const output = await toOutput(
|
|
1274
|
+
generate6(agentInstance, messages, {
|
|
1275
|
+
introspection: options.introspection,
|
|
1276
|
+
teachings: toInstructions(
|
|
1277
|
+
"instructions",
|
|
1278
|
+
persona({
|
|
1279
|
+
name: "Freya",
|
|
1280
|
+
role: "You are an expert SQL query generator. You translate natural language questions into precise, efficient SQL queries based on the provided database schema."
|
|
1281
|
+
}),
|
|
1282
|
+
...options.instructions
|
|
1283
|
+
)
|
|
1284
|
+
})
|
|
1285
|
+
);
|
|
1286
|
+
if ("error" in output) {
|
|
1287
|
+
throw new UnanswerableSQLError(output.error);
|
|
1288
|
+
}
|
|
1289
|
+
const sql = extractSql(output.sql);
|
|
1290
|
+
const validationError = await options.adapter.validate(sql);
|
|
1291
|
+
if (validationError) {
|
|
1292
|
+
throw new SQLValidationError(validationError);
|
|
1293
|
+
}
|
|
1293
1294
|
return {
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
errors: errors.length ? errors : void 0
|
|
1295
|
+
attempts,
|
|
1296
|
+
sql,
|
|
1297
|
+
errors: errors.length ? errors.map(formatErrorMessage) : void 0
|
|
1297
1298
|
};
|
|
1299
|
+
},
|
|
1300
|
+
{ retries: maxRetries - 1 }
|
|
1301
|
+
);
|
|
1302
|
+
}
|
|
1303
|
+
function formatErrorMessage(error) {
|
|
1304
|
+
if (APICallError.isInstance(error)) {
|
|
1305
|
+
if (error.message.startsWith("Failed to validate JSON")) {
|
|
1306
|
+
return `Schema validation failed: ${error.message}`;
|
|
1298
1307
|
}
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1308
|
+
return error.message;
|
|
1309
|
+
}
|
|
1310
|
+
if (SQLValidationError.isInstance(error)) {
|
|
1311
|
+
return `SQL Validation Error: ${error.message}`;
|
|
1303
1312
|
}
|
|
1304
|
-
return
|
|
1313
|
+
return error.message;
|
|
1314
|
+
}
|
|
1315
|
+
async function withRetry(computation, options = { retries: 3 }) {
|
|
1316
|
+
const errors = [];
|
|
1317
|
+
let attempts = 0;
|
|
1318
|
+
return pRetry(
|
|
1319
|
+
(attemptNumber) => {
|
|
1320
|
+
return computation(attemptNumber, errors, ++attempts);
|
|
1321
|
+
},
|
|
1322
|
+
{
|
|
1323
|
+
retries: options.retries,
|
|
1324
|
+
shouldRetry: (context2) => {
|
|
1325
|
+
if (UnanswerableSQLError.isInstance(context2.error)) {
|
|
1326
|
+
return false;
|
|
1327
|
+
}
|
|
1328
|
+
if (SQLValidationError.isInstance(context2.error)) {
|
|
1329
|
+
return true;
|
|
1330
|
+
}
|
|
1331
|
+
console.log({
|
|
1332
|
+
NoObjectGeneratedError: NoObjectGeneratedError.isInstance(
|
|
1333
|
+
context2.error
|
|
1334
|
+
),
|
|
1335
|
+
NoOutputGeneratedError: NoOutputGeneratedError.isInstance(
|
|
1336
|
+
context2.error
|
|
1337
|
+
),
|
|
1338
|
+
APICallError: APICallError.isInstance(context2.error),
|
|
1339
|
+
JSONParseError: JSONParseError.isInstance(context2.error),
|
|
1340
|
+
TypeValidationError: TypeValidationError.isInstance(context2.error),
|
|
1341
|
+
NoContentGeneratedError: NoContentGeneratedError.isInstance(
|
|
1342
|
+
context2.error
|
|
1343
|
+
)
|
|
1344
|
+
});
|
|
1345
|
+
return APICallError.isInstance(context2.error) || JSONParseError.isInstance(context2.error) || TypeValidationError.isInstance(context2.error) || NoObjectGeneratedError.isInstance(context2.error) || NoOutputGeneratedError.isInstance(context2.error) || NoContentGeneratedError.isInstance(context2.error);
|
|
1346
|
+
},
|
|
1347
|
+
onFailedAttempt(context2) {
|
|
1348
|
+
logger.error(`toSQL`, context2.error);
|
|
1349
|
+
console.log(
|
|
1350
|
+
`Attempt ${context2.attemptNumber} failed. There are ${context2.retriesLeft} retries left.`
|
|
1351
|
+
);
|
|
1352
|
+
errors.push(context2.error);
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
);
|
|
1305
1356
|
}
|
|
1306
1357
|
|
|
1307
1358
|
// packages/text2sql/src/lib/synthesis/synthesizers/schema-synthesizer.ts
|
|
@@ -1314,7 +1365,7 @@ var SchemaSynthesizer = class extends PairProducer {
|
|
|
1314
1365
|
super();
|
|
1315
1366
|
this.adapter = adapter;
|
|
1316
1367
|
this.options = options;
|
|
1317
|
-
this.#complexities = Array.isArray(this.options.complexity) ? this.options.complexity : [this.options.complexity ?? "
|
|
1368
|
+
this.#complexities = Array.isArray(this.options.complexity) ? this.options.complexity : [this.options.complexity ?? "moderate"];
|
|
1318
1369
|
this.#personas = this.options.personas ?? [void 0];
|
|
1319
1370
|
this.#limit = pLimit(this.options.concurrency ?? 5);
|
|
1320
1371
|
}
|
|
@@ -1365,15 +1416,28 @@ Generate ${this.options.count} questions at ${complexity} complexity.` : void 0;
|
|
|
1365
1416
|
);
|
|
1366
1417
|
const pairs = await Promise.all(
|
|
1367
1418
|
questions.map(async (question) => {
|
|
1368
|
-
const result = await this.#limit(
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1419
|
+
const result = await this.#limit(async () => {
|
|
1420
|
+
try {
|
|
1421
|
+
return await toSql({
|
|
1422
|
+
input: question,
|
|
1423
|
+
adapter: this.adapter,
|
|
1424
|
+
introspection,
|
|
1425
|
+
instructions: this.options.teachings ?? [],
|
|
1426
|
+
model: this.options.model
|
|
1427
|
+
});
|
|
1428
|
+
} catch (error) {
|
|
1429
|
+
if (UnanswerableSQLError.isInstance(error)) {
|
|
1430
|
+
return {
|
|
1431
|
+
attempts: 0,
|
|
1432
|
+
sql: "",
|
|
1433
|
+
errors: [
|
|
1434
|
+
`Cannot answer the question ${question} because ${error.message}`
|
|
1435
|
+
]
|
|
1436
|
+
};
|
|
1437
|
+
}
|
|
1438
|
+
throw error;
|
|
1439
|
+
}
|
|
1440
|
+
});
|
|
1377
1441
|
return {
|
|
1378
1442
|
question,
|
|
1379
1443
|
sql: result.sql,
|
|
@@ -1391,31 +1455,45 @@ import { defaultSettingsMiddleware as defaultSettingsMiddleware3, wrapLanguageMo
|
|
|
1391
1455
|
import dedent5 from "dedent";
|
|
1392
1456
|
import pLimit2 from "p-limit";
|
|
1393
1457
|
import z6 from "zod";
|
|
1394
|
-
import {
|
|
1458
|
+
import {
|
|
1459
|
+
agent as agent6,
|
|
1460
|
+
generate as generate7,
|
|
1461
|
+
toOutput as toOutput2,
|
|
1462
|
+
user as user7
|
|
1463
|
+
} from "@deepagents/agent";
|
|
1395
1464
|
|
|
1396
1465
|
// packages/text2sql/src/lib/synthesis/synthesizers/styles.ts
|
|
1397
|
-
var styleInstructions = {
|
|
1398
|
-
formal: "Use professional business language, complete sentences, no slang",
|
|
1399
|
-
colloquial: "Use casual everyday speech, contractions, informal tone",
|
|
1400
|
-
imperative: 'Phrase as commands: "Show me...", "Get...", "List..."',
|
|
1401
|
-
interrogative: 'Phrase as questions: "What is...", "How many...", "Which..."',
|
|
1402
|
-
descriptive: "Use detailed, verbose phrasing with extra context",
|
|
1403
|
-
concise: "Use minimal words, telegram-style brevity",
|
|
1404
|
-
vague: "Be intentionally ambiguous, use hedging language",
|
|
1405
|
-
metaphorical: "Use figurative language, analogies, creative phrasing",
|
|
1406
|
-
conversational: "Chat-like tone, as if talking to a colleague"
|
|
1407
|
-
};
|
|
1408
1466
|
var ALL_STYLES = [
|
|
1409
1467
|
"formal",
|
|
1468
|
+
// Professional business language
|
|
1410
1469
|
"colloquial",
|
|
1470
|
+
// Casual everyday speech
|
|
1411
1471
|
"imperative",
|
|
1472
|
+
// Commands: "Show me...", "Get..."
|
|
1412
1473
|
"interrogative",
|
|
1474
|
+
// Questions: "What is...", "How many..."
|
|
1413
1475
|
"descriptive",
|
|
1476
|
+
// Verbose, detailed
|
|
1414
1477
|
"concise",
|
|
1478
|
+
// Brief, minimal
|
|
1415
1479
|
"vague",
|
|
1480
|
+
// Ambiguous, hedging
|
|
1416
1481
|
"metaphorical",
|
|
1482
|
+
// Figurative language
|
|
1417
1483
|
"conversational"
|
|
1484
|
+
// Chat-like
|
|
1418
1485
|
];
|
|
1486
|
+
var styleInstructions = {
|
|
1487
|
+
formal: "Use professional business language, complete sentences, no slang",
|
|
1488
|
+
colloquial: "Use casual everyday speech, contractions, informal tone",
|
|
1489
|
+
imperative: 'Phrase as commands: "Show me...", "Get...", "List..."',
|
|
1490
|
+
interrogative: 'Phrase as questions: "What is...", "How many...", "Which..."',
|
|
1491
|
+
descriptive: "Use detailed, verbose phrasing with extra context",
|
|
1492
|
+
concise: "Use minimal words, telegram-style brevity",
|
|
1493
|
+
vague: "Be intentionally ambiguous, use hedging language",
|
|
1494
|
+
metaphorical: "Use figurative language, analogies, creative phrasing",
|
|
1495
|
+
conversational: "Chat-like tone, as if talking to a colleague"
|
|
1496
|
+
};
|
|
1419
1497
|
|
|
1420
1498
|
// packages/text2sql/src/lib/synthesis/synthesizers/breadth-evolver.ts
|
|
1421
1499
|
var paraphraserAgent = agent6({
|
|
@@ -1514,21 +1592,23 @@ var BreadthEvolver = class extends PairProducer {
|
|
|
1514
1592
|
for await (const chunk of this.from(this.source)) {
|
|
1515
1593
|
const tasks = chunk.map(
|
|
1516
1594
|
(pair) => this.#limit(async () => {
|
|
1517
|
-
const {
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1595
|
+
const { paraphrases } = await toOutput2(
|
|
1596
|
+
generate7(
|
|
1597
|
+
paraphraserAgent.clone({ model: this.options.model }),
|
|
1598
|
+
[
|
|
1599
|
+
user7(
|
|
1600
|
+
`Paraphrase this question ${this.options.count} times: "${pair.question}"`
|
|
1601
|
+
)
|
|
1602
|
+
],
|
|
1603
|
+
{
|
|
1604
|
+
question: pair.question,
|
|
1605
|
+
sql: pair.sql,
|
|
1606
|
+
count: this.options.count,
|
|
1607
|
+
persona: this.options.persona
|
|
1608
|
+
}
|
|
1609
|
+
)
|
|
1530
1610
|
);
|
|
1531
|
-
return
|
|
1611
|
+
return paraphrases.map((paraphrase) => ({
|
|
1532
1612
|
question: paraphrase,
|
|
1533
1613
|
sql: pair.sql,
|
|
1534
1614
|
context: pair.context,
|
|
@@ -1545,14 +1625,14 @@ var BreadthEvolver = class extends PairProducer {
|
|
|
1545
1625
|
// packages/text2sql/src/lib/synthesis/synthesizers/depth-evolver.ts
|
|
1546
1626
|
import { groq as groq7 } from "@ai-sdk/groq";
|
|
1547
1627
|
import {
|
|
1548
|
-
NoObjectGeneratedError,
|
|
1549
|
-
NoOutputGeneratedError,
|
|
1628
|
+
NoObjectGeneratedError as NoObjectGeneratedError2,
|
|
1629
|
+
NoOutputGeneratedError as NoOutputGeneratedError2,
|
|
1550
1630
|
defaultSettingsMiddleware as defaultSettingsMiddleware4,
|
|
1551
1631
|
wrapLanguageModel as wrapLanguageModel4
|
|
1552
1632
|
} from "ai";
|
|
1553
1633
|
import dedent6 from "dedent";
|
|
1554
1634
|
import pLimit3 from "p-limit";
|
|
1555
|
-
import
|
|
1635
|
+
import pRetry2 from "p-retry";
|
|
1556
1636
|
import z7 from "zod";
|
|
1557
1637
|
import { agent as agent7, generate as generate8, user as user8 } from "@deepagents/agent";
|
|
1558
1638
|
var techniqueInstructions = {
|
|
@@ -1699,7 +1779,7 @@ var DepthEvolver = class extends PairProducer {
|
|
|
1699
1779
|
}
|
|
1700
1780
|
}
|
|
1701
1781
|
async #processTask(pair, technique, introspection) {
|
|
1702
|
-
const { experimental_output } = await
|
|
1782
|
+
const { experimental_output } = await withRetry2(
|
|
1703
1783
|
() => generate8(
|
|
1704
1784
|
questionEvolverAgent.clone({
|
|
1705
1785
|
model: this.options?.model
|
|
@@ -1715,32 +1795,55 @@ var DepthEvolver = class extends PairProducer {
|
|
|
1715
1795
|
)
|
|
1716
1796
|
);
|
|
1717
1797
|
const evolvedQuestion = experimental_output.evolvedQuestion;
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1798
|
+
try {
|
|
1799
|
+
const sqlResult = await toSql({
|
|
1800
|
+
input: evolvedQuestion,
|
|
1801
|
+
adapter: this.adapter,
|
|
1802
|
+
introspection,
|
|
1803
|
+
instructions: [],
|
|
1804
|
+
model: this.options?.model
|
|
1805
|
+
});
|
|
1806
|
+
return {
|
|
1807
|
+
question: evolvedQuestion,
|
|
1808
|
+
sql: sqlResult.sql,
|
|
1809
|
+
context: pair.context,
|
|
1810
|
+
success: !sqlResult.errors || sqlResult.errors.length === 0
|
|
1811
|
+
};
|
|
1812
|
+
} catch (error) {
|
|
1813
|
+
if (UnanswerableSQLError.isInstance(error)) {
|
|
1814
|
+
return {
|
|
1815
|
+
question: evolvedQuestion,
|
|
1816
|
+
sql: "",
|
|
1817
|
+
context: pair.context,
|
|
1818
|
+
success: false,
|
|
1819
|
+
errors: [
|
|
1820
|
+
`Cannot answer the question ${evolvedQuestion} because ${error.message}`
|
|
1821
|
+
]
|
|
1822
|
+
};
|
|
1823
|
+
}
|
|
1824
|
+
throw error;
|
|
1825
|
+
}
|
|
1731
1826
|
}
|
|
1732
1827
|
};
|
|
1733
|
-
async function
|
|
1734
|
-
return
|
|
1828
|
+
async function withRetry2(computation) {
|
|
1829
|
+
return pRetry2(computation, {
|
|
1735
1830
|
retries: 3,
|
|
1736
1831
|
shouldRetry: (context2) => {
|
|
1737
|
-
|
|
1832
|
+
console.log({
|
|
1833
|
+
NoObjectGeneratedError: NoObjectGeneratedError2.isInstance(
|
|
1834
|
+
context2.error
|
|
1835
|
+
),
|
|
1836
|
+
NoOutputGeneratedError: NoOutputGeneratedError2.isInstance(
|
|
1837
|
+
context2.error
|
|
1838
|
+
)
|
|
1839
|
+
});
|
|
1840
|
+
return NoObjectGeneratedError2.isInstance(context2.error) || NoOutputGeneratedError2.isInstance(context2.error);
|
|
1738
1841
|
},
|
|
1739
1842
|
onFailedAttempt(context2) {
|
|
1740
1843
|
console.log(
|
|
1741
1844
|
`Attempt ${context2.attemptNumber} failed. There are ${context2.retriesLeft} retries left.`
|
|
1742
1845
|
);
|
|
1743
|
-
console.
|
|
1846
|
+
console.dir(context2.error, { depth: null });
|
|
1744
1847
|
}
|
|
1745
1848
|
});
|
|
1746
1849
|
}
|