diff --git a/examples/nodejs/package.json b/examples/nodejs/package.json index 876ad15..4eedbb8 100644 --- a/examples/nodejs/package.json +++ b/examples/nodejs/package.json @@ -1,55 +1,57 @@ { - "private": true, - "name": "everything", - "version": "0.0.0", - "description": "Everything example", - "type": "module", - "main": "index.js", - "scripts": { - "pipe.create": "npx tsx ./pipes/pipe.create.ts", - "pipe.run.chat": "npx tsx ./pipes/pipe.run.chat.ts", - "pipe.run.stream.chat": "npx tsx ./pipes/pipe.run.stream.chat.ts", - "memory.create": "npx tsx ./memory/memory.create.ts", - "memory.list": "npx tsx ./memory/memory.list.ts", - "memory.delete": "npx tsx ./memory/memory.delete.ts", - "memory.retrieve": "npx tsx ./memory/memory.retrieve.ts", - "memory.retrieve.filters.In": "npx tsx ./memory/memory.retrieve.filters.In.ts", - "memory.retrieve.filters.NotIn": "npx tsx ./memory/memory.retrieve.filters.NotIn.ts", - "memory.retrieve.filters.Eq": "npx tsx ./memory/memory.retrieve.filters.Eq.ts", - "memory.retrieve.filters.NotEq": "npx tsx ./memory/memory.retrieve.filters.NotEq.ts", - "memory.retrieve.filters.Or": "npx tsx ./memory/memory.retrieve.filters.Or.ts", - "memory.retrieve.filters.advanced": "npx tsx ./memory/memory.retrieve.filters.advanced.ts", - "memory.docs.list": "npx tsx ./memory/memory.docs.list.ts", - "memory.docs.delete": "npx tsx ./memory/memory.docs.delete.ts", - "memory.docs.upload": "npx tsx ./memory/memory.docs.upload.ts", - "memory.docs.retry-embed": "npx tsx ./memory/memory.docs.retry-embed.ts", - "pipe.update": "npx tsx ./pipes/pipe.update.ts", - "pipe.list": "npx tsx ./pipes/pipe.list.ts", - "pipe.run": "npx tsx ./pipes/pipe.run.ts", - "pipe.tool": "npx tsx ./pipes/pipe.tool.ts", - "pipe.tool.stream": "npx tsx ./pipes/pipe.tool.stream.ts", - "pipe.run.stream": "npx tsx ./pipes/pipe.run.stream.ts", - "pipe.run.stream.llmkey": "npx tsx ./pipes/pipe.run.stream.llmkey.ts", - "tools.web-search": "npx tsx ./tools/web-search.ts", - "tools.crawl": "npx tsx ./tools/crawl.ts", - "embed": "npx tsx ./embed/index.ts", - "chunk": "npx tsx ./chunk/index.ts", - "parse": "npx tsx ./parse/index.ts", - "threads.create": "npx tsx ./threads/threads.create.ts", - "threads.update": "npx tsx ./threads/threads.update.ts", - "threads.delete": "npx tsx ./threads/threads.delete.ts", - "threads.append": "npx tsx ./threads/threads.append.ts", - "threads.messages.list": "npx tsx ./threads/threads.messages.list.ts", - "threads.get": "npx tsx ./threads/threads.get.ts", - "workflow": "npx tsx ./workflows/workflows.ts" - }, - "keywords": [], - "author": "Ahmad Awais (https://twitter.com/MrAhmadAwais)", - "license": "UNLICENSED", - "dependencies": { - "@langbase/cli": "workspace:*", - "dotenv": "^16.4.5", - "langbase": "1.1.52", - "uuid": "^11.1.0" - } + "private": true, + "name": "everything", + "version": "0.0.0", + "description": "Everything example", + "type": "module", + "main": "index.js", + "scripts": { + "pipe.create": "npx tsx ./pipes/pipe.create.ts", + "pipe.run.chat": "npx tsx ./pipes/pipe.run.chat.ts", + "pipe.run.stream.chat": "npx tsx ./pipes/pipe.run.stream.chat.ts", + "memory.create": "npx tsx ./memory/memory.create.ts", + "memory.list": "npx tsx ./memory/memory.list.ts", + "memory.delete": "npx tsx ./memory/memory.delete.ts", + "memory.retrieve": "npx tsx ./memory/memory.retrieve.ts", + "memory.retrieve.filters.In": "npx tsx ./memory/memory.retrieve.filters.In.ts", + "memory.retrieve.filters.NotIn": "npx tsx ./memory/memory.retrieve.filters.NotIn.ts", + "memory.retrieve.filters.Eq": "npx tsx ./memory/memory.retrieve.filters.Eq.ts", + "memory.retrieve.filters.NotEq": "npx tsx ./memory/memory.retrieve.filters.NotEq.ts", + "memory.retrieve.filters.Or": "npx tsx ./memory/memory.retrieve.filters.Or.ts", + "memory.retrieve.filters.advanced": "npx tsx ./memory/memory.retrieve.filters.advanced.ts", + "memory.docs.list": "npx tsx ./memory/memory.docs.list.ts", + "memory.docs.delete": "npx tsx ./memory/memory.docs.delete.ts", + "memory.docs.upload": "npx tsx ./memory/memory.docs.upload.ts", + "memory.docs.retry-embed": "npx tsx ./memory/memory.docs.retry-embed.ts", + "pipe.update": "npx tsx ./pipes/pipe.update.ts", + "pipe.list": "npx tsx ./pipes/pipe.list.ts", + "pipe.run": "npx tsx ./pipes/pipe.run.ts", + "pipe.tool": "npx tsx ./pipes/pipe.tool.ts", + "pipe.tool.stream": "npx tsx ./pipes/pipe.tool.stream.ts", + "pipe.run.stream": "npx tsx ./pipes/pipe.run.stream.ts", + "pipe.run.stream.llmkey": "npx tsx ./pipes/pipe.run.stream.llmkey.ts", + "pipe.structured.outputs": "npx tsx ./pipes/pipe.structured.outputs.ts", + "tools.web-search": "npx tsx ./tools/web-search.ts", + "tools.crawl": "npx tsx ./tools/crawl.ts", + "embed": "npx tsx ./embed/index.ts", + "chunk": "npx tsx ./chunk/index.ts", + "parse": "npx tsx ./parse/index.ts", + "threads.create": "npx tsx ./threads/threads.create.ts", + "threads.update": "npx tsx ./threads/threads.update.ts", + "threads.delete": "npx tsx ./threads/threads.delete.ts", + "threads.append": "npx tsx ./threads/threads.append.ts", + "threads.messages.list": "npx tsx ./threads/threads.messages.list.ts", + "threads.get": "npx tsx ./threads/threads.get.ts", + "workflow": "npx tsx ./workflows/workflows.ts" + }, + "keywords": [], + "author": "Ahmad Awais (https://twitter.com/MrAhmadAwais)", + "license": "UNLICENSED", + "dependencies": { + "@langbase/cli": "workspace:*", + "dotenv": "^16.4.5", + "langbase": "1.1.52", + "uuid": "^11.1.0", + "zod": "^3.21.4" + } } diff --git a/examples/nodejs/pipes/pipe.structured.outputs.ts b/examples/nodejs/pipes/pipe.structured.outputs.ts new file mode 100644 index 0000000..00485fc --- /dev/null +++ b/examples/nodejs/pipes/pipe.structured.outputs.ts @@ -0,0 +1,69 @@ +import 'dotenv/config'; +import {Langbase} from 'langbase'; +import {z} from 'zod'; + +const langbase = new Langbase({ + apiKey: process.env.LANGBASE_API_KEY!, +}); + +// Define the Strucutred Output JSON schema with Zod +const MathResponseSchema = z.object({ + steps: z.array( + z.object({ + explanation: z.string(), + output: z.string(), + }), + ), + final_answer: z.string(), +}); + +async function createMathTutorPipe() { + const pipe = await langbase.pipes.create({ + name: 'math-tutor', + model: 'openai:gpt-4o', + messages: [ + { + role: 'system', + content: + 'You are a helpful math tutor. Guide the user through the solution step by step.', + }, + ], + json: true, + response_format: { + type: 'json_schema', + json_schema: { + name: 'math_response', + schema: MathResponseSchema.shape, // Use Zod schema shape + strict: true, + }, + }, + }); + + console.log('✅ Math Tutor pipe created:', pipe); +} + +async function runMathTutorPipe(question: string) { + const {completion} = await langbase.pipes.run({ + name: 'math-tutor', + messages: [{role: 'user', content: question}], + stream: false, + }); + + // Parse and validate the response using Zod + const solution = MathResponseSchema.parse(JSON.parse(completion)); + + console.log('✅ Structured Output Response:', solution); +} + +async function main() { + if (!process.env.LANGBASE_API_KEY) { + console.error('❌ Missing LANGBASE_API_KEY in environment variables.'); + process.exit(1); + } + + // Run this only once to create the pipe. Uncomment if it's your first time setting it up. + // await createMathTutorPipe(); + await runMathTutorPipe('How can I solve 8x - 7 = -23?'); +} + +main();