-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathparsing-run-tools.ts
153 lines (144 loc) · 3.81 KB
/
parsing-run-tools.ts
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import OpenAI from 'openai';
import z from 'zod';
import { zodFunction } from 'openai/helpers/zod';
const Table = z.enum(['orders', 'customers', 'products']);
const Column = z.enum([
'id',
'status',
'expected_delivery_date',
'delivered_at',
'shipped_at',
'ordered_at',
'canceled_at',
]);
const Operator = z.enum(['=', '>', '<', '<=', '>=', '!=']);
const OrderBy = z.enum(['asc', 'desc']);
const DynamicValue = z.object({
column_name: z.string(),
});
const Condition = z.object({
column: z.string(),
operator: Operator,
value: z.union([z.string(), z.number(), DynamicValue]),
});
const openai = new OpenAI();
async function main() {
const runner = openai.beta.chat.completions
.runTools({
model: 'gpt-4o-2024-08-06',
messages: [{ role: 'user', content: `What are the last 10 orders?` }],
stream: true,
tools: [
zodFunction({
name: 'query',
function: (args) => {
return { table_name: args.table_name, data: fakeOrders };
},
parameters: z.object({
location: z.string(),
table_name: Table,
columns: z.array(Column),
conditions: z.array(Condition),
order_by: OrderBy,
}),
}),
],
})
.on('tool_calls.function.arguments.done', (props) =>
console.log(`parsed function arguments: ${props.parsed_arguments}`),
);
await runner.done();
console.dir(runner.messages, { depth: 10 });
}
const fakeOrders = [
{
orderId: 'ORD-001',
customerName: 'Alice Johnson',
products: [{ name: 'Wireless Headphones', quantity: 1, price: 89.99 }],
totalPrice: 89.99,
orderDate: '2024-08-02',
},
{
orderId: 'ORD-002',
customerName: 'Bob Smith',
products: [
{ name: 'Smartphone Case', quantity: 2, price: 19.99 },
{ name: 'Screen Protector', quantity: 1, price: 9.99 },
],
totalPrice: 49.97,
orderDate: '2024-08-03',
},
{
orderId: 'ORD-003',
customerName: 'Carol Davis',
products: [
{ name: 'Laptop', quantity: 1, price: 999.99 },
{ name: 'Mouse', quantity: 1, price: 29.99 },
],
totalPrice: 1029.98,
orderDate: '2024-08-04',
},
{
orderId: 'ORD-004',
customerName: 'David Wilson',
products: [{ name: 'Coffee Maker', quantity: 1, price: 79.99 }],
totalPrice: 79.99,
orderDate: '2024-08-05',
},
{
orderId: 'ORD-005',
customerName: 'Eva Brown',
products: [
{ name: 'Fitness Tracker', quantity: 1, price: 129.99 },
{ name: 'Water Bottle', quantity: 2, price: 14.99 },
],
totalPrice: 159.97,
orderDate: '2024-08-06',
},
{
orderId: 'ORD-006',
customerName: 'Frank Miller',
products: [
{ name: 'Gaming Console', quantity: 1, price: 499.99 },
{ name: 'Controller', quantity: 2, price: 59.99 },
],
totalPrice: 619.97,
orderDate: '2024-08-07',
},
{
orderId: 'ORD-007',
customerName: 'Grace Lee',
products: [{ name: 'Bluetooth Speaker', quantity: 1, price: 69.99 }],
totalPrice: 69.99,
orderDate: '2024-08-08',
},
{
orderId: 'ORD-008',
customerName: 'Henry Taylor',
products: [
{ name: 'Smartwatch', quantity: 1, price: 199.99 },
{ name: 'Watch Band', quantity: 2, price: 24.99 },
],
totalPrice: 249.97,
orderDate: '2024-08-09',
},
{
orderId: 'ORD-009',
customerName: 'Isla Garcia',
products: [
{ name: 'Tablet', quantity: 1, price: 349.99 },
{ name: 'Tablet Case', quantity: 1, price: 29.99 },
{ name: 'Stylus', quantity: 1, price: 39.99 },
],
totalPrice: 419.97,
orderDate: '2024-08-10',
},
{
orderId: 'ORD-010',
customerName: 'Jack Robinson',
products: [{ name: 'Wireless Charger', quantity: 2, price: 34.99 }],
totalPrice: 69.98,
orderDate: '2024-08-11',
},
];
main();