@xuda.io/ai_module 1.1.5616 → 1.1.5618

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/gpt.mjs ADDED
@@ -0,0 +1,155 @@
1
+ // process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
2
+
3
+ import { Agent, run, tool, user } from '@openai/agents';
4
+ import { z } from 'zod';
5
+
6
+ import OpenAI from 'openai';
7
+ const client = new OpenAI({ apiKey: process.env['OPENAI_API_KEY'] });
8
+
9
+ import dotenv from 'dotenv';
10
+ dotenv.config();
11
+
12
+ // import { setDefaultOpenAIKey } from '@openai/agents';
13
+ // setDefaultOpenAIKey('sk-proj-XmeLaZ7RLn0nCc4nSaJHT3BlbkFJfKFulKrY55ILLnM1CytS'); // sk-...
14
+
15
+ const test_gpt = async function (req) {
16
+ try {
17
+ const spanishAgent = new Agent({
18
+ name: 'Spanish agent',
19
+ instructions: 'You only speak Spanish.',
20
+ });
21
+
22
+ const englishAgent = new Agent({
23
+ name: 'English agent',
24
+ instructions: 'You only speak English',
25
+ });
26
+
27
+ const triageAgent = new Agent({
28
+ name: 'Triage agent',
29
+ instructions: 'Handoff to the appropriate agent based on the language of the request.',
30
+ handoffs: [spanishAgent, englishAgent],
31
+ });
32
+
33
+ const result = await run(triageAgent, 'how are you?');
34
+ console.log(result.finalOutput);
35
+ } catch (err) {
36
+ console.error(err);
37
+ }
38
+ };
39
+
40
+ const test_tool = async function (req) {
41
+ try {
42
+ const getWeather = tool({
43
+ name: 'get_weather',
44
+ description: 'Return the weather for a given city.',
45
+ parameters: z.object({ city: z.string() }),
46
+ async execute({ city }) {
47
+ return `The weather in ${city} is sunny.`;
48
+ },
49
+ });
50
+
51
+ const agent = new Agent({
52
+ name: 'Weather bot',
53
+ instructions: 'You are a helpful weather bot.',
54
+ model: 'o4-mini',
55
+ tools: [getWeather],
56
+ });
57
+
58
+ const result = await run(agent, 'what is the weather in Herzeliya?');
59
+ console.log(result.finalOutput);
60
+ } catch (err) {
61
+ console.error(err);
62
+ }
63
+ };
64
+ const test_user = async function (req) {
65
+ try {
66
+ const agent = new Agent({
67
+ name: 'Assistant',
68
+ instructions: 'You are a helpful assistant.',
69
+ });
70
+
71
+ const result = await run(agent, 'hi');
72
+ console.log(result.finalOutput);
73
+ } catch (err) {
74
+ console.error(err);
75
+ }
76
+ };
77
+ const test_output = async function (req) {
78
+ try {
79
+ const refundAgent = new Agent({
80
+ name: 'Refund Agent',
81
+ instructions: 'You are a refund agent. You are responsible for refunding customers, refund anyone with $5.',
82
+ outputType: z.object({
83
+ refundApproved: z.boolean(),
84
+ }),
85
+ });
86
+
87
+ const orderAgent = new Agent({
88
+ name: 'Order Agent',
89
+ instructions: 'You are an order agent. You are responsible for processing orders.',
90
+ outputType: z.object({
91
+ orderId: z.string(),
92
+ }),
93
+ });
94
+
95
+ const triageAgent = Agent.create({
96
+ name: 'Triage Agent',
97
+ instructions: 'You are a triage agent. You are responsible for triaging customer issues.',
98
+ handoffs: [refundAgent, orderAgent],
99
+ });
100
+
101
+ const result = await run(triageAgent, 'I need to a refund for my order');
102
+
103
+ console.log(result.finalOutput);
104
+ } catch (err) {
105
+ console.error(err);
106
+ }
107
+ };
108
+ const test_mcp = async function (req) {
109
+ debugger;
110
+ try {
111
+ const resp = await client.responses.create({
112
+ model: 'gpt-4.1',
113
+ tools: [
114
+ {
115
+ type: 'mcp',
116
+ server_label: 'mcp',
117
+ server_url: 'https://mcp.gotopanel.com/mcp',
118
+ require_approval: 'never',
119
+ // allowed_tools: ['say_hello'],
120
+ },
121
+ ],
122
+ input: 'Use the say_hello tool to greet someone named Alice.',
123
+ });
124
+
125
+ console.log(resp.output_text);
126
+ } catch (err) {
127
+ console.error(err);
128
+ }
129
+ };
130
+
131
+ const test_mcp_tool = async function (req) {
132
+ try {
133
+ const agent = new Agent({
134
+ name: 'Weather bot',
135
+ instructions: 'You are a helpful weather bot.',
136
+ model: 'gpt-4.1',
137
+ tools: [
138
+ {
139
+ type: 'mcp',
140
+ server_label: 'mcp',
141
+ server_url: 'https://mcp.xuda.dev/mcp',
142
+ require_approval: 'never',
143
+ },
144
+ ],
145
+ });
146
+
147
+ const result = await run(agent, 'Use the say_hello tool to greet someone named Alice.');
148
+ console.log(result.finalOutput);
149
+ } catch (err) {
150
+ console.error(err);
151
+ }
152
+ };
153
+
154
+ setTimeout(test_mcp, 10000);
155
+ // test_mcp({ prompt: process.argv[2] });