-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
94 lines (84 loc) · 2.81 KB
/
index.ts
File metadata and controls
94 lines (84 loc) · 2.81 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
import { Langbase, Workflow } from 'langbase';
import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';
import dotenv from 'dotenv';
dotenv.config();
// Define schema for structured output
const mathSolutionSchema = z.object({
problem: z.string().describe('The original math problem'),
steps: z.array(
z.object({
step: z.number().describe('Step number'),
description: z.string().describe('Description of this step'),
calculation: z
.string()
.describe('The calculation or mathematical work for this step'),
explanation: z
.string()
.describe('Explanation of the reasoning behind this step')
})
),
finalAnswer: z.string().describe('The final answer to the problem'),
additionalNotes: z
.string()
.describe('Any additional notes or alternative approaches')
});
// Convert zod schema to JSON schema
const mathSolutionJsonSchema = zodToJsonSchema(mathSolutionSchema, {
target: 'openAi'
});
async function mathSolverWorkflow(input: string) {
const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY!
});
const { step } = new Workflow({
debug: true
});
// Step 2: Solve the math problem with structured output
const solution = await step({
id: 'solve_problem',
run: async () => {
const response = await langbase.agent.run({
model: 'openai:gpt-4.1-mini',
apiKey: process.env.OPENAI_API_KEY!,
instructions: `You are an expert math tutor. Solve the given math problem step by step, showing all your work clearly.
Break down complex problems into manageable steps. Explain your reasoning at each step.
For algebraic problems, show each transformation of the equation.
For calculus problems, explain the concepts and techniques you're using.
For geometry problems, include relevant theorems and properties.
For word problems, explain how you translate the problem into mathematical form.
Always verify your answer by checking if it satisfies the original problem.`,
input: [{ role: 'user', content: input }],
stream: false,
response_format: {
type: 'json_schema',
json_schema: {
name: 'MathSolution',
schema: mathSolutionJsonSchema,
strict: true
}
}
});
return JSON.parse(response.output);
}
});
return solution;
}
async function main() {
try {
// Handle both direct input and file uploads
let question =
'There are 49 dogs signed up for a dog show. There are 36 more small dogs than large dogs. How many small dogs have signed up to compete?';
console.log('🔢 Question:', question);
const result = await mathSolverWorkflow(question);
return result;
} catch (error) {
console.error('Error in math solver workflow:', error);
return {
error:
error || 'An error occurred while processing the math problem',
status: 500
};
}
}
main();