Skip to content

Commit e8ab438

Browse files
committed
👌 IMPROVE: Structured outputs example
1 parent f100048 commit e8ab438

File tree

2 files changed

+63
-79
lines changed

2 files changed

+63
-79
lines changed

examples/nodejs/package.json

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,58 @@
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-
"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.55",
54-
"uuid": "^11.1.0",
55-
"zod": "^3.21.4"
56-
}
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.55",
54+
"uuid": "^11.1.0",
55+
"zod": "^3.21.4",
56+
"zod-to-json-schema": "^3.24.5"
57+
}
5758
}

examples/nodejs/pipes/pipe.structured.outputs.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import 'dotenv/config';
22
import {Langbase} from 'langbase';
33
import {z} from 'zod';
4+
import {zodToJsonSchema} from 'zod-to-json-schema';
45

56
const langbase = new Langbase({
67
apiKey: process.env.LANGBASE_API_KEY!,
78
});
89

910
// Define the Strucutred Output JSON schema with Zod
10-
const MathResponseSchema = z.object({
11+
const MathReasoningSchema = z.object({
1112
steps: z.array(
1213
z.object({
1314
explanation: z.string(),
@@ -17,6 +18,8 @@ const MathResponseSchema = z.object({
1718
final_answer: z.string(),
1819
});
1920

21+
const jsonSchema = zodToJsonSchema(MathReasoningSchema, {target: 'openAi'});
22+
2023
async function createMathTutorPipe() {
2124
const pipe = await langbase.pipes.create({
2225
name: 'math-tutor',
@@ -33,27 +36,7 @@ async function createMathTutorPipe() {
3336
type: 'json_schema',
3437
json_schema: {
3538
name: 'math_reasoning',
36-
schema: {
37-
type: 'object',
38-
properties: {
39-
steps: {
40-
type: 'array',
41-
items: {
42-
type: 'object',
43-
properties: {
44-
explanation: {type: 'string'},
45-
output: {type: 'string'},
46-
},
47-
required: ['explanation', 'output'],
48-
additionalProperties: false,
49-
},
50-
},
51-
final_answer: {type: 'string'},
52-
},
53-
required: ['steps', 'final_answer'],
54-
additionalProperties: false,
55-
},
56-
strict: true,
39+
schema: jsonSchema,
5740
},
5841
},
5942
});
@@ -69,7 +52,7 @@ async function runMathTutorPipe(question: string) {
6952
});
7053

7154
// Parse and validate the response using Zod
72-
const solution = MathResponseSchema.parse(JSON.parse(completion));
55+
const solution = MathReasoningSchema.parse(JSON.parse(completion));
7356

7457
console.log('✅ Structured Output Response:', solution);
7558
}
@@ -81,7 +64,7 @@ async function main() {
8164
}
8265

8366
// Run this only once to create the pipe. Uncomment if it's your first time setting it up.
84-
await createMathTutorPipe();
67+
// await createMathTutorPipe();
8568
await runMathTutorPipe('How can I solve 8x + 22 = -23?');
8669
}
8770

0 commit comments

Comments
 (0)