Repository navigation
Expand file tree
/
Copy pathfunction-result-formatter.ts
More file actions
102 lines (89 loc) · 2.73 KB
/
Copy pathfunction-result-formatter.ts
File metadata and controls
102 lines (89 loc) · 2.73 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
import { ai, ax, axGlobals } from '@ax-llm/ax';
console.log('=== Function Result Formatter Demo ===\n');
// Create an AI instance (using openai for the example)
const _ai = ai({
name: 'openai',
apiKey: process.env.OPENAI_APIKEY || 'test',
});
// Simple function that returns an object
const _getUserInfo = {
name: 'get_user_info',
description: 'Get user information',
parameters: {
type: 'object',
properties: {
userId: { type: 'string', description: 'User ID' },
},
required: ['userId'],
},
func: async (args: { userId: string }) => {
return {
id: args.userId,
name: 'John Doe',
email: 'john@example.com',
preferences: {
theme: 'dark',
notifications: true,
},
};
},
};
console.log('1. Default Global Formatter (JSON.stringify with null, 2):');
console.log(
' Result:',
axGlobals.functionResultFormatter({ name: 'John', age: 30 })
);
console.log();
console.log('2. Custom Global Formatter:');
// Change the global formatter
axGlobals.functionResultFormatter = (result: unknown) => {
if (typeof result === 'object' && result !== null) {
return `[CUSTOM] ${JSON.stringify(result, null, 0)}`;
}
return `[CUSTOM] ${String(result)}`;
};
console.log(
' Result:',
axGlobals.functionResultFormatter({ name: 'John', age: 30 })
);
console.log();
console.log('3. Per-call Custom Formatter:');
// Per-call formatter that overrides the global one
const perCallFormatter = (result: unknown): string => {
if (typeof result === 'object' && result !== null) {
return `[PER-CALL] User data: ${JSON.stringify(result, null, 0)}`;
}
return `[PER-CALL] ${String(result)}`;
};
console.log(' Result:', perCallFormatter({ name: 'John', age: 30 }));
console.log();
// Create a generator
const _gen = ax(
'userRequest:string "User request" -> responseText:string "AI response"'
);
console.log('4. Integration Example:');
console.log(
' - The functionResultFormatter can be passed as an option to forward()'
);
console.log(
' - It will be used to format function results before storing in memory'
);
console.log(' - Falls back to global formatter if not provided');
console.log();
console.log(' Example usage:');
console.log(' ```typescript');
console.log(' await gen.forward(ai, { userRequest: "Get user 123" }, {');
console.log(' functions: [getUserInfo],');
console.log(' functionResultFormatter: customFormatter');
console.log(' });');
console.log(' ```');
console.log();
// Reset global formatter to default
axGlobals.functionResultFormatter = (result: unknown) => {
return typeof result === 'string'
? result
: result === undefined || result === null
? ''
: JSON.stringify(result, null, 2);
};
console.log('Demo completed! ✅');