-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathvector-db.ts
More file actions
253 lines (225 loc) · 6.87 KB
/
vector-db.ts
File metadata and controls
253 lines (225 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* Vector DB — Embedding generation, storage, and semantic search
*
* Demonstrates vector database operations:
* - Generate embeddings from text
* - Store embeddings in a vector DB
* - Search embeddings for semantic similarity
* - Index and search text (combined operations)
*
* Prerequisites:
* - An embedding model integration (e.g., OpenAI text-embedding-3-small)
* - A vector DB integration (e.g., Pinecone, Weaviate, pgvector)
*
* Run:
* CONDUCTOR_SERVER_URL=http://localhost:8080 npx ts-node examples/advanced/vector-db.ts
*/
import {
OrkesClients,
ConductorWorkflow,
llmGenerateEmbeddingsTask,
llmStoreEmbeddingsTask,
llmSearchEmbeddingsTask,
llmIndexTextTask,
llmSearchIndexTask,
llmQueryEmbeddingsTask,
inlineTask,
} from "../../src/sdk";
async function main() {
const clients = await OrkesClients.from();
const workflowClient = clients.getWorkflowClient();
const embeddingProvider =
process.env.EMBEDDING_PROVIDER ?? "openai_integration";
const embeddingModel =
process.env.EMBEDDING_MODEL ?? "text-embedding-3-small";
const vectorDb = process.env.VECTOR_DB ?? "pinecone_integration";
const vectorIndex = process.env.VECTOR_INDEX ?? "vector-db-example";
// ── 1. Generate Embeddings Workflow ───────────────────────────────
const embedWf = new ConductorWorkflow(
workflowClient,
"vector_generate_embeddings"
)
.description("Generate embeddings from text using an embedding model");
embedWf.add(
llmGenerateEmbeddingsTask(
"embed_ref",
embeddingProvider,
embeddingModel,
"${workflow.input.text}",
{
dimensions: 1536,
}
)
);
embedWf.add(
inlineTask(
"info_ref",
`(function() {
var embeddings = $.embed_ref.output.result || [];
return {
dimensions: embeddings.length,
preview: embeddings.slice(0, 5)
};
})()`,
"javascript"
)
);
embedWf.outputParameters({
text: "${workflow.input.text}",
embeddings: "${embed_ref.output.result}",
info: "${info_ref.output.result}",
});
await embedWf.register(true);
console.log("Registered workflow:", embedWf.getName());
// ── 2. Store Embeddings Workflow ──────────────────────────────────
const storeWf = new ConductorWorkflow(
workflowClient,
"vector_store_embeddings"
)
.description("Store pre-computed embeddings in vector DB");
storeWf.add(
llmStoreEmbeddingsTask(
"store_ref",
vectorDb,
vectorIndex,
[0.1, 0.2, 0.3], // placeholder — in practice use actual embeddings
{
docId: "${workflow.input.docId}",
namespace: "${workflow.input.namespace}",
metadata: {
source: "${workflow.input.source}",
},
}
)
);
storeWf.outputParameters({
stored: true,
docId: "${workflow.input.docId}",
});
await storeWf.register(true);
console.log("Registered workflow:", storeWf.getName());
// ── 3. Text Index + Search Workflow ───────────────────────────────
const textSearchWf = new ConductorWorkflow(
workflowClient,
"vector_text_search"
)
.description(
"Index text and search — embedding generation handled by Conductor"
);
// Index the text
textSearchWf.add(
llmIndexTextTask(
"index_ref",
vectorDb,
vectorIndex,
{ provider: embeddingProvider, model: embeddingModel },
"${workflow.input.text}",
"${workflow.input.docId}",
{
namespace: "text-search-demo",
chunkSize: 200,
chunkOverlap: 20,
}
)
);
// Search the index
textSearchWf.add(
llmSearchIndexTask(
"search_ref",
vectorDb,
vectorIndex,
{ provider: embeddingProvider, model: embeddingModel },
"${workflow.input.query}",
{
namespace: "text-search-demo",
maxResults: 3,
}
)
);
textSearchWf.outputParameters({
indexed: true,
searchResults: "${search_ref.output.result}",
});
await textSearchWf.register(true);
console.log("Registered workflow:", textSearchWf.getName());
// ── 4. Embedding Search Workflow ──────────────────────────────────
const embSearchWf = new ConductorWorkflow(
workflowClient,
"vector_embedding_search"
)
.description("Search vector DB using raw embeddings");
embSearchWf.add(
llmSearchEmbeddingsTask(
"search_emb_ref",
vectorDb,
vectorIndex,
[0.1, 0.2, 0.3], // placeholder
{
namespace: "${workflow.input.namespace}",
maxResults: 5,
}
)
);
embSearchWf.outputParameters({
results: "${search_emb_ref.output.result}",
});
await embSearchWf.register(true);
console.log("Registered workflow:", embSearchWf.getName());
// ── 5. Query Embeddings Workflow ──────────────────────────────────
const queryWf = new ConductorWorkflow(
workflowClient,
"vector_query_embeddings"
)
.description("Query stored embeddings from vector DB");
queryWf.add(
llmQueryEmbeddingsTask(
"query_ref",
vectorDb,
vectorIndex,
[0.1, 0.2, 0.3], // placeholder
{
namespace: "${workflow.input.namespace}",
}
)
);
queryWf.outputParameters({
results: "${query_ref.output.result}",
});
await queryWf.register(true);
console.log("Registered workflow:", queryWf.getName());
// ── Execute examples ──────────────────────────────────────────────
console.log("\n--- Generating embeddings ---");
try {
const run = await embedWf.execute({
text: "Conductor is a workflow orchestration engine",
});
console.log("Status:", run.status);
console.log("Info:", JSON.stringify((run.output as Record<string, unknown>)?.info, null, 2));
} catch (err) {
console.log(
"Skipped (requires embedding model):",
(err as Error).message
);
}
console.log("\n--- Text index + search ---");
try {
const run = await textSearchWf.execute({
text: "The TypeScript SDK provides task builders, workflow builders, and worker decorators.",
docId: "ts-sdk-intro",
query: "What does the TypeScript SDK provide?",
});
console.log("Status:", run.status);
console.log("Results:", JSON.stringify((run.output as Record<string, unknown>)?.searchResults, null, 2));
} catch (err) {
console.log(
"Skipped (requires vector DB):",
(err as Error).message
);
}
console.log("\nDone.");
process.exit(0);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});