-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathfunction.ts
More file actions
140 lines (124 loc) · 3.39 KB
/
function.ts
File metadata and controls
140 lines (124 loc) · 3.39 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
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
import {
AxAI,
AxAIAnthropicModel,
type AxFunction,
AxFunctionError,
AxJSRuntime,
AxSignature,
agent,
} from '@ax-llm/ax';
// Restaurant booking function with validation
const bookRestaurantAPI = ({
date,
time,
partySize,
}: Readonly<{
date: string;
time: string;
partySize: string;
}>) => {
const errors: { field: string; message: string }[] = [];
// Validate date format (YYYY-MM-DD)
const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
if (!dateRegex.test(date)) {
errors.push({
field: 'date',
message: 'Date must be in YYYY-MM-DD format',
});
}
// Validate time format (HH:MM)
const timeRegex = /^([01]\d|2[0-3]):([0-5]\d)$/;
if (!timeRegex.test(time)) {
errors.push({
field: 'time',
message: 'Time must be in 24-hour HH:MM format',
});
}
if (!['small', 'medium', 'large'].includes(partySize)) {
errors.push({
field: 'partySize',
message: 'Party size must be small, medium, or large',
});
}
// If any validation errors, throw AxFunctionError
if (errors.length > 0) {
throw new AxFunctionError(errors);
}
// If validation passes, proceed with booking
return {
success: true,
confirmation: `Booking confirmed for ${partySize} people on ${date} at ${time}`,
details: {
reservationId: Math.random().toString(36).substring(7),
restaurant: 'Sample Restaurant',
},
};
};
// List of functions available to the AI
const functions: AxFunction[] = [
{
name: 'bookRestaurant',
description:
'Book a restaurant reservation. Date must be YYYY-MM-DD, time must be HH:MM in 24-hour format',
func: bookRestaurantAPI,
parameters: {
type: 'object',
properties: {
date: {
type: 'string',
description: 'Reservation date in YYYY-MM-DD format',
},
time: {
type: 'string',
description: 'Reservation time in HH:MM 24-hour format',
},
partySize: {
type: 'string',
description: 'Number of people',
},
},
required: ['date', 'time', 'partySize'],
},
},
];
// Define the signature for the booking agent
const signature = new AxSignature(
`customerQuery:string -> plan:string "detailed plan to book the restaurant",
confirmationNumber:string "reservation confirmation number",
details:string "booking details including date, time, and party size"`
);
// Create the booking agent
const gen = agent(signature, {
functions: { local: functions },
contextFields: [],
runtime: new AxJSRuntime(),
});
// const ai = new AxAI({
// name: 'openai',
// apiKey: process.env.OPENAI_APIKEY as string,
// config: { stream: true },
// })
const ai = new AxAI({
name: 'anthropic',
apiKey: process.env.ANTHROPIC_APIKEY as string,
config: {
stream: true,
model: AxAIAnthropicModel.Claude35Haiku,
maxTokens: 3000,
},
});
// const ai = new AxAI({
// name: 'google-gemini',
// apiKey: process.env.GOOGLE_APIKEY as string,
// config: { stream: true, model: AxAIGoogleGeminiModel.Gemini15Flash },
// })
// const ai = new AxAI({
// name: 'cohere',
// apiKey: process.env.COHERE_APIKEY as string,
// config: { stream: false },
// })
ai.setOptions({ debug: true });
// Example error case
const invalidQuery = 'Book me a table for 25 people at 8:30 PM on 2025/02/01';
const res = await gen.forward(ai, { customerQuery: invalidQuery });
console.log(res);