Skip to content

Commit 6598233

Browse files
📦 NEW: Structure output example (#95)
1 parent 183dde7 commit 6598233

File tree

2 files changed

+124
-53
lines changed

2 files changed

+124
-53
lines changed

examples/nodejs/package.json

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,57 @@
11
{
2-
"private": true,
3-
"name": "everything",
4-
"version": "0.0.0",
5-
"description": "Everything example",
6-
"type": "module",
7-
"main": "index.js",
8-
"scripts": {
9-
"pipe.create": "npx tsx ./pipes/pipe.create.ts",
10-
"pipe.run.chat": "npx tsx ./pipes/pipe.run.chat.ts",
11-
"pipe.run.stream.chat": "npx tsx ./pipes/pipe.run.stream.chat.ts",
12-
"memory.create": "npx tsx ./memory/memory.create.ts",
13-
"memory.list": "npx tsx ./memory/memory.list.ts",
14-
"memory.delete": "npx tsx ./memory/memory.delete.ts",
15-
"memory.retrieve": "npx tsx ./memory/memory.retrieve.ts",
16-
"memory.retrieve.filters.In": "npx tsx ./memory/memory.retrieve.filters.In.ts",
17-
"memory.retrieve.filters.NotIn": "npx tsx ./memory/memory.retrieve.filters.NotIn.ts",
18-
"memory.retrieve.filters.Eq": "npx tsx ./memory/memory.retrieve.filters.Eq.ts",
19-
"memory.retrieve.filters.NotEq": "npx tsx ./memory/memory.retrieve.filters.NotEq.ts",
20-
"memory.retrieve.filters.Or": "npx tsx ./memory/memory.retrieve.filters.Or.ts",
21-
"memory.retrieve.filters.advanced": "npx tsx ./memory/memory.retrieve.filters.advanced.ts",
22-
"memory.docs.list": "npx tsx ./memory/memory.docs.list.ts",
23-
"memory.docs.delete": "npx tsx ./memory/memory.docs.delete.ts",
24-
"memory.docs.upload": "npx tsx ./memory/memory.docs.upload.ts",
25-
"memory.docs.retry-embed": "npx tsx ./memory/memory.docs.retry-embed.ts",
26-
"pipe.update": "npx tsx ./pipes/pipe.update.ts",
27-
"pipe.list": "npx tsx ./pipes/pipe.list.ts",
28-
"pipe.run": "npx tsx ./pipes/pipe.run.ts",
29-
"pipe.tool": "npx tsx ./pipes/pipe.tool.ts",
30-
"pipe.tool.stream": "npx tsx ./pipes/pipe.tool.stream.ts",
31-
"pipe.run.stream": "npx tsx ./pipes/pipe.run.stream.ts",
32-
"pipe.run.stream.llmkey": "npx tsx ./pipes/pipe.run.stream.llmkey.ts",
33-
"tools.web-search": "npx tsx ./tools/web-search.ts",
34-
"tools.crawl": "npx tsx ./tools/crawl.ts",
35-
"embed": "npx tsx ./embed/index.ts",
36-
"chunk": "npx tsx ./chunk/index.ts",
37-
"parse": "npx tsx ./parse/index.ts",
38-
"threads.create": "npx tsx ./threads/threads.create.ts",
39-
"threads.update": "npx tsx ./threads/threads.update.ts",
40-
"threads.delete": "npx tsx ./threads/threads.delete.ts",
41-
"threads.append": "npx tsx ./threads/threads.append.ts",
42-
"threads.messages.list": "npx tsx ./threads/threads.messages.list.ts",
43-
"threads.get": "npx tsx ./threads/threads.get.ts",
44-
"workflow": "npx tsx ./workflows/workflows.ts"
45-
},
46-
"keywords": [],
47-
"author": "Ahmad Awais <[email protected]> (https://twitter.com/MrAhmadAwais)",
48-
"license": "UNLICENSED",
49-
"dependencies": {
50-
"@langbase/cli": "workspace:*",
51-
"dotenv": "^16.4.5",
52-
"langbase": "1.1.52",
53-
"uuid": "^11.1.0"
54-
}
2+
"private": true,
3+
"name": "everything",
4+
"version": "0.0.0",
5+
"description": "Everything example",
6+
"type": "module",
7+
"main": "index.js",
8+
"scripts": {
9+
"pipe.create": "npx tsx ./pipes/pipe.create.ts",
10+
"pipe.run.chat": "npx tsx ./pipes/pipe.run.chat.ts",
11+
"pipe.run.stream.chat": "npx tsx ./pipes/pipe.run.stream.chat.ts",
12+
"memory.create": "npx tsx ./memory/memory.create.ts",
13+
"memory.list": "npx tsx ./memory/memory.list.ts",
14+
"memory.delete": "npx tsx ./memory/memory.delete.ts",
15+
"memory.retrieve": "npx tsx ./memory/memory.retrieve.ts",
16+
"memory.retrieve.filters.In": "npx tsx ./memory/memory.retrieve.filters.In.ts",
17+
"memory.retrieve.filters.NotIn": "npx tsx ./memory/memory.retrieve.filters.NotIn.ts",
18+
"memory.retrieve.filters.Eq": "npx tsx ./memory/memory.retrieve.filters.Eq.ts",
19+
"memory.retrieve.filters.NotEq": "npx tsx ./memory/memory.retrieve.filters.NotEq.ts",
20+
"memory.retrieve.filters.Or": "npx tsx ./memory/memory.retrieve.filters.Or.ts",
21+
"memory.retrieve.filters.advanced": "npx tsx ./memory/memory.retrieve.filters.advanced.ts",
22+
"memory.docs.list": "npx tsx ./memory/memory.docs.list.ts",
23+
"memory.docs.delete": "npx tsx ./memory/memory.docs.delete.ts",
24+
"memory.docs.upload": "npx tsx ./memory/memory.docs.upload.ts",
25+
"memory.docs.retry-embed": "npx tsx ./memory/memory.docs.retry-embed.ts",
26+
"pipe.update": "npx tsx ./pipes/pipe.update.ts",
27+
"pipe.list": "npx tsx ./pipes/pipe.list.ts",
28+
"pipe.run": "npx tsx ./pipes/pipe.run.ts",
29+
"pipe.tool": "npx tsx ./pipes/pipe.tool.ts",
30+
"pipe.tool.stream": "npx tsx ./pipes/pipe.tool.stream.ts",
31+
"pipe.run.stream": "npx tsx ./pipes/pipe.run.stream.ts",
32+
"pipe.run.stream.llmkey": "npx tsx ./pipes/pipe.run.stream.llmkey.ts",
33+
"pipe.structured.outputs": "npx tsx ./pipes/pipe.structured.outputs.ts",
34+
"tools.web-search": "npx tsx ./tools/web-search.ts",
35+
"tools.crawl": "npx tsx ./tools/crawl.ts",
36+
"embed": "npx tsx ./embed/index.ts",
37+
"chunk": "npx tsx ./chunk/index.ts",
38+
"parse": "npx tsx ./parse/index.ts",
39+
"threads.create": "npx tsx ./threads/threads.create.ts",
40+
"threads.update": "npx tsx ./threads/threads.update.ts",
41+
"threads.delete": "npx tsx ./threads/threads.delete.ts",
42+
"threads.append": "npx tsx ./threads/threads.append.ts",
43+
"threads.messages.list": "npx tsx ./threads/threads.messages.list.ts",
44+
"threads.get": "npx tsx ./threads/threads.get.ts",
45+
"workflow": "npx tsx ./workflows/workflows.ts"
46+
},
47+
"keywords": [],
48+
"author": "Ahmad Awais <[email protected]> (https://twitter.com/MrAhmadAwais)",
49+
"license": "UNLICENSED",
50+
"dependencies": {
51+
"@langbase/cli": "workspace:*",
52+
"dotenv": "^16.4.5",
53+
"langbase": "1.1.52",
54+
"uuid": "^11.1.0",
55+
"zod": "^3.21.4"
56+
}
5557
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import 'dotenv/config';
2+
import {Langbase} from 'langbase';
3+
import {z} from 'zod';
4+
5+
const langbase = new Langbase({
6+
apiKey: process.env.LANGBASE_API_KEY!,
7+
});
8+
9+
// Define the Strucutred Output JSON schema with Zod
10+
const MathResponseSchema = z.object({
11+
steps: z.array(
12+
z.object({
13+
explanation: z.string(),
14+
output: z.string(),
15+
}),
16+
),
17+
final_answer: z.string(),
18+
});
19+
20+
async function createMathTutorPipe() {
21+
const pipe = await langbase.pipes.create({
22+
name: 'math-tutor',
23+
model: 'openai:gpt-4o',
24+
messages: [
25+
{
26+
role: 'system',
27+
content:
28+
'You are a helpful math tutor. Guide the user through the solution step by step.',
29+
},
30+
],
31+
json: true,
32+
response_format: {
33+
type: 'json_schema',
34+
json_schema: {
35+
name: 'math_response',
36+
schema: MathResponseSchema.shape, // Use Zod schema shape
37+
strict: true,
38+
},
39+
},
40+
});
41+
42+
console.log('✅ Math Tutor pipe created:', pipe);
43+
}
44+
45+
async function runMathTutorPipe(question: string) {
46+
const {completion} = await langbase.pipes.run({
47+
name: 'math-tutor',
48+
messages: [{role: 'user', content: question}],
49+
stream: false,
50+
});
51+
52+
// Parse and validate the response using Zod
53+
const solution = MathResponseSchema.parse(JSON.parse(completion));
54+
55+
console.log('✅ Structured Output Response:', solution);
56+
}
57+
58+
async function main() {
59+
if (!process.env.LANGBASE_API_KEY) {
60+
console.error('❌ Missing LANGBASE_API_KEY in environment variables.');
61+
process.exit(1);
62+
}
63+
64+
// Run this only once to create the pipe. Uncomment if it's your first time setting it up.
65+
// await createMathTutorPipe();
66+
await runMathTutorPipe('How can I solve 8x - 7 = -23?');
67+
}
68+
69+
main();

0 commit comments

Comments
 (0)