@deepagents/text2sql 0.35.0 → 0.36.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/README.md +87 -38
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -23,9 +23,10 @@ Requires Node.js LTS (20+).
|
|
|
23
23
|
|
|
24
24
|
```typescript
|
|
25
25
|
import { groq } from '@ai-sdk/groq';
|
|
26
|
+
import { InMemoryFs } from 'just-bash';
|
|
26
27
|
import pg from 'pg';
|
|
27
28
|
|
|
28
|
-
import { InMemoryContextStore } from '@deepagents/context';
|
|
29
|
+
import { ContextEngine, InMemoryContextStore } from '@deepagents/context';
|
|
29
30
|
import { Text2Sql } from '@deepagents/text2sql';
|
|
30
31
|
import {
|
|
31
32
|
Postgres,
|
|
@@ -33,6 +34,7 @@ import {
|
|
|
33
34
|
constraints,
|
|
34
35
|
indexes,
|
|
35
36
|
info,
|
|
37
|
+
rowCount,
|
|
36
38
|
tables,
|
|
37
39
|
views,
|
|
38
40
|
} from '@deepagents/text2sql/postgres';
|
|
@@ -41,24 +43,37 @@ const pool = new pg.Pool({
|
|
|
41
43
|
connectionString: process.env.DATABASE_URL,
|
|
42
44
|
});
|
|
43
45
|
|
|
46
|
+
const store = new InMemoryContextStore();
|
|
47
|
+
const adapter = new Postgres({
|
|
48
|
+
execute: async (sql) => {
|
|
49
|
+
const result = await pool.query(sql);
|
|
50
|
+
return result.rows;
|
|
51
|
+
},
|
|
52
|
+
grounding: [
|
|
53
|
+
tables(),
|
|
54
|
+
views(),
|
|
55
|
+
info(),
|
|
56
|
+
indexes(),
|
|
57
|
+
constraints(),
|
|
58
|
+
rowCount(),
|
|
59
|
+
columnValues(),
|
|
60
|
+
],
|
|
61
|
+
});
|
|
62
|
+
|
|
44
63
|
const text2sql = new Text2Sql({
|
|
45
64
|
version: 'v1',
|
|
46
|
-
model: groq('gpt-oss-20b'),
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
columnValues(),
|
|
59
|
-
],
|
|
60
|
-
}),
|
|
61
|
-
store: new InMemoryContextStore(),
|
|
65
|
+
model: groq('openai/gpt-oss-20b'),
|
|
66
|
+
filesystem: new InMemoryFs(),
|
|
67
|
+
adapter,
|
|
68
|
+
context: (...fragments) => {
|
|
69
|
+
const engine = new ContextEngine({
|
|
70
|
+
store,
|
|
71
|
+
chatId: 'chat-123',
|
|
72
|
+
userId: 'user-456',
|
|
73
|
+
});
|
|
74
|
+
engine.set(...fragments);
|
|
75
|
+
return engine;
|
|
76
|
+
},
|
|
62
77
|
});
|
|
63
78
|
|
|
64
79
|
// Generate SQL
|
|
@@ -72,14 +87,31 @@ Text2SQL works with any model provider supported by the [Vercel AI SDK](https://
|
|
|
72
87
|
|
|
73
88
|
## Fragments
|
|
74
89
|
|
|
75
|
-
Inject domain knowledge using fragments from `@deepagents/context`
|
|
90
|
+
Inject domain knowledge using fragments from `@deepagents/context` inside the
|
|
91
|
+
`context` factory you pass to `Text2Sql`. Those fragments affect `chat()`
|
|
92
|
+
sessions. If you need direct SQL generation with extra fragments, use the
|
|
93
|
+
lower-level `toSql({ fragments })` helper exported from `@deepagents/text2sql`.
|
|
76
94
|
|
|
77
95
|
```typescript
|
|
78
|
-
import {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
96
|
+
import {
|
|
97
|
+
ContextEngine,
|
|
98
|
+
InMemoryContextStore,
|
|
99
|
+
example,
|
|
100
|
+
guardrail,
|
|
101
|
+
hint,
|
|
102
|
+
term,
|
|
103
|
+
} from '@deepagents/context';
|
|
104
|
+
|
|
105
|
+
const store = new InMemoryContextStore();
|
|
106
|
+
|
|
107
|
+
const context = (...fragments) => {
|
|
108
|
+
const engine = new ContextEngine({
|
|
109
|
+
store,
|
|
110
|
+
chatId: 'chat-123',
|
|
111
|
+
userId: 'user-456',
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
engine.set(
|
|
83
115
|
term('MRR', 'monthly recurring revenue'),
|
|
84
116
|
hint('Always exclude test accounts with email ending in @test.com'),
|
|
85
117
|
guardrail({
|
|
@@ -91,8 +123,11 @@ const text2sql = new Text2Sql({
|
|
|
91
123
|
question: 'show me churned customers',
|
|
92
124
|
answer: `SELECT * FROM customers WHERE status = 'churned' ORDER BY churned_at DESC`,
|
|
93
125
|
}),
|
|
94
|
-
|
|
95
|
-
|
|
126
|
+
...fragments,
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
return engine;
|
|
130
|
+
};
|
|
96
131
|
```
|
|
97
132
|
|
|
98
133
|
**Domain fragments** (11 types): `term`, `hint`, `guardrail`, `example`, `explain`, `clarification`, `workflow`, `quirk`, `styleGuide`, `analogy`, `glossary`.
|
|
@@ -118,26 +153,40 @@ Control what schema metadata the AI receives:
|
|
|
118
153
|
|
|
119
154
|
## Conversations
|
|
120
155
|
|
|
121
|
-
|
|
156
|
+
`chat()` persists history through the `ContextEngine` returned by your `context` factory. Reuse the same store, `chatId`, and `userId` to continue the same thread:
|
|
122
157
|
|
|
123
158
|
```typescript
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const stream = await text2sql.chat(
|
|
128
|
-
[{ role: 'user', content: 'Show me orders from last month' }],
|
|
129
|
-
{ chatId, userId },
|
|
130
|
-
);
|
|
159
|
+
const stream = await text2sql.chat([
|
|
160
|
+
{ role: 'user', content: 'Show me orders from last month' },
|
|
161
|
+
]);
|
|
131
162
|
|
|
132
163
|
for await (const chunk of stream) {
|
|
133
164
|
// handle streaming response
|
|
134
165
|
}
|
|
135
166
|
|
|
136
|
-
// Continue the
|
|
137
|
-
const followUp = await text2sql.chat(
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
167
|
+
// Continue the same conversation
|
|
168
|
+
const followUp = await text2sql.chat([
|
|
169
|
+
{ role: 'user', content: 'Now filter to only completed ones' },
|
|
170
|
+
]);
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Direct SQL Generation with Extra Fragments
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
import { term } from '@deepagents/context';
|
|
177
|
+
import { toSql } from '@deepagents/text2sql';
|
|
178
|
+
|
|
179
|
+
const result = await toSql({
|
|
180
|
+
input: 'Show ARR by plan',
|
|
181
|
+
adapter,
|
|
182
|
+
model: groq('openai/gpt-oss-20b'),
|
|
183
|
+
fragments: [
|
|
184
|
+
...(await adapter.introspect()),
|
|
185
|
+
term('ARR', 'annual recurring revenue'),
|
|
186
|
+
],
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
console.log(result.sql);
|
|
141
190
|
```
|
|
142
191
|
|
|
143
192
|
## Documentation
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deepagents/text2sql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.36.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -83,16 +83,16 @@
|
|
|
83
83
|
"dependencies": {
|
|
84
84
|
"@ai-sdk/groq": "^3.0.31",
|
|
85
85
|
"@ai-sdk/openai": "^3.0.49",
|
|
86
|
-
"@deepagents/agent": "0.
|
|
87
|
-
"@deepagents/context": "0.
|
|
88
|
-
"@deepagents/evals": "0.
|
|
86
|
+
"@deepagents/agent": "0.36.0",
|
|
87
|
+
"@deepagents/context": "0.36.0",
|
|
88
|
+
"@deepagents/evals": "0.36.0",
|
|
89
89
|
"@google-cloud/bigquery": "8.1.1",
|
|
90
90
|
"ai": "^6.0.141",
|
|
91
91
|
"autoevals": "0.0.132",
|
|
92
|
-
"bash-tool": "^1.3.
|
|
92
|
+
"bash-tool": "^1.3.16",
|
|
93
93
|
"chalk": "^5.6.0",
|
|
94
94
|
"dedent": "1.7.0",
|
|
95
|
-
"just-bash": "^2.14.
|
|
95
|
+
"just-bash": "^2.14.1",
|
|
96
96
|
"nano-spawn": "^1.0.3",
|
|
97
97
|
"node-sql-parser": "^5.4.0",
|
|
98
98
|
"openai": "5.12.2",
|