|
| 1 | +import OpenAI from 'openai'; |
| 2 | +import z from 'zod'; |
| 3 | +import { zodFunction } from 'openai/helpers/zod'; |
| 4 | + |
| 5 | +const Table = z.enum(['orders', 'customers', 'products']); |
| 6 | +const Column = z.enum([ |
| 7 | + 'id', |
| 8 | + 'status', |
| 9 | + 'expected_delivery_date', |
| 10 | + 'delivered_at', |
| 11 | + 'shipped_at', |
| 12 | + 'ordered_at', |
| 13 | + 'canceled_at', |
| 14 | +]); |
| 15 | +const Operator = z.enum(['=', '>', '<', '<=', '>=', '!=']); |
| 16 | +const OrderBy = z.enum(['asc', 'desc']); |
| 17 | + |
| 18 | +const DynamicValue = z.object({ |
| 19 | + column_name: z.string(), |
| 20 | +}); |
| 21 | + |
| 22 | +const Condition = z.object({ |
| 23 | + column: z.string(), |
| 24 | + operator: Operator, |
| 25 | + value: z.union([z.string(), z.number(), DynamicValue]), |
| 26 | +}); |
| 27 | + |
| 28 | +const openai = new OpenAI(); |
| 29 | + |
| 30 | +async function main() { |
| 31 | + const runner = openai.beta.chat.completions |
| 32 | + .runTools({ |
| 33 | + model: 'gpt-4o-2024-08-06', |
| 34 | + messages: [{ role: 'user', content: `What are the last 10 orders?` }], |
| 35 | + stream: true, |
| 36 | + tools: [ |
| 37 | + zodFunction({ |
| 38 | + name: 'query', |
| 39 | + function: (args) => { |
| 40 | + return { table_name: args.table_name, data: fakeOrders }; |
| 41 | + }, |
| 42 | + parameters: z.object({ |
| 43 | + location: z.string(), |
| 44 | + table_name: Table, |
| 45 | + columns: z.array(Column), |
| 46 | + conditions: z.array(Condition), |
| 47 | + order_by: OrderBy, |
| 48 | + }), |
| 49 | + }), |
| 50 | + ], |
| 51 | + }) |
| 52 | + .on('tool_calls.function.arguments.done', (props) => |
| 53 | + console.log(`parsed function arguments: ${props.parsed_arguments}`), |
| 54 | + ); |
| 55 | + |
| 56 | + await runner.done(); |
| 57 | + |
| 58 | + console.dir(runner.messages, { depth: 10 }); |
| 59 | +} |
| 60 | + |
| 61 | +const fakeOrders = [ |
| 62 | + { |
| 63 | + orderId: 'ORD-001', |
| 64 | + customerName: 'Alice Johnson', |
| 65 | + products: [{ name: 'Wireless Headphones', quantity: 1, price: 89.99 }], |
| 66 | + totalPrice: 89.99, |
| 67 | + orderDate: '2024-08-02', |
| 68 | + }, |
| 69 | + { |
| 70 | + orderId: 'ORD-002', |
| 71 | + customerName: 'Bob Smith', |
| 72 | + products: [ |
| 73 | + { name: 'Smartphone Case', quantity: 2, price: 19.99 }, |
| 74 | + { name: 'Screen Protector', quantity: 1, price: 9.99 }, |
| 75 | + ], |
| 76 | + totalPrice: 49.97, |
| 77 | + orderDate: '2024-08-03', |
| 78 | + }, |
| 79 | + { |
| 80 | + orderId: 'ORD-003', |
| 81 | + customerName: 'Carol Davis', |
| 82 | + products: [ |
| 83 | + { name: 'Laptop', quantity: 1, price: 999.99 }, |
| 84 | + { name: 'Mouse', quantity: 1, price: 29.99 }, |
| 85 | + ], |
| 86 | + totalPrice: 1029.98, |
| 87 | + orderDate: '2024-08-04', |
| 88 | + }, |
| 89 | + { |
| 90 | + orderId: 'ORD-004', |
| 91 | + customerName: 'David Wilson', |
| 92 | + products: [{ name: 'Coffee Maker', quantity: 1, price: 79.99 }], |
| 93 | + totalPrice: 79.99, |
| 94 | + orderDate: '2024-08-05', |
| 95 | + }, |
| 96 | + { |
| 97 | + orderId: 'ORD-005', |
| 98 | + customerName: 'Eva Brown', |
| 99 | + products: [ |
| 100 | + { name: 'Fitness Tracker', quantity: 1, price: 129.99 }, |
| 101 | + { name: 'Water Bottle', quantity: 2, price: 14.99 }, |
| 102 | + ], |
| 103 | + totalPrice: 159.97, |
| 104 | + orderDate: '2024-08-06', |
| 105 | + }, |
| 106 | + { |
| 107 | + orderId: 'ORD-006', |
| 108 | + customerName: 'Frank Miller', |
| 109 | + products: [ |
| 110 | + { name: 'Gaming Console', quantity: 1, price: 499.99 }, |
| 111 | + { name: 'Controller', quantity: 2, price: 59.99 }, |
| 112 | + ], |
| 113 | + totalPrice: 619.97, |
| 114 | + orderDate: '2024-08-07', |
| 115 | + }, |
| 116 | + { |
| 117 | + orderId: 'ORD-007', |
| 118 | + customerName: 'Grace Lee', |
| 119 | + products: [{ name: 'Bluetooth Speaker', quantity: 1, price: 69.99 }], |
| 120 | + totalPrice: 69.99, |
| 121 | + orderDate: '2024-08-08', |
| 122 | + }, |
| 123 | + { |
| 124 | + orderId: 'ORD-008', |
| 125 | + customerName: 'Henry Taylor', |
| 126 | + products: [ |
| 127 | + { name: 'Smartwatch', quantity: 1, price: 199.99 }, |
| 128 | + { name: 'Watch Band', quantity: 2, price: 24.99 }, |
| 129 | + ], |
| 130 | + totalPrice: 249.97, |
| 131 | + orderDate: '2024-08-09', |
| 132 | + }, |
| 133 | + { |
| 134 | + orderId: 'ORD-009', |
| 135 | + customerName: 'Isla Garcia', |
| 136 | + products: [ |
| 137 | + { name: 'Tablet', quantity: 1, price: 349.99 }, |
| 138 | + { name: 'Tablet Case', quantity: 1, price: 29.99 }, |
| 139 | + { name: 'Stylus', quantity: 1, price: 39.99 }, |
| 140 | + ], |
| 141 | + totalPrice: 419.97, |
| 142 | + orderDate: '2024-08-10', |
| 143 | + }, |
| 144 | + { |
| 145 | + orderId: 'ORD-010', |
| 146 | + customerName: 'Jack Robinson', |
| 147 | + products: [{ name: 'Wireless Charger', quantity: 2, price: 34.99 }], |
| 148 | + totalPrice: 69.98, |
| 149 | + orderDate: '2024-08-11', |
| 150 | + }, |
| 151 | +]; |
| 152 | + |
| 153 | +main(); |
0 commit comments