forked from gptscript-ai/node-gptscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgptscript.test.ts
364 lines (313 loc) · 9.98 KB
/
gptscript.test.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import * as gptscript from "../src/gptscript"
import path from "path"
const client = new gptscript.Client(process.env.GPTSCRIPT_URL, process.env.GPTSCRIPT_BIN)
describe("gptscript module", () => {
beforeAll(() => {
if (!process.env.OPENAI_API_KEY && !process.env.GPTSCRIPT_URL) {
throw new Error("neither OPENAI_API_KEY nor GPTSCRIPT_URL is set")
}
})
test("listTools returns available tools", async () => {
const tools = await client.listTools()
expect(tools).toBeDefined()
})
test("listModels returns a list of models", async () => {
// Similar structure to listTools
let models = await client.listModels()
expect(models).toBeDefined()
})
test("version returns a gptscript version", async () => {
// Similar structure to listTools
let version = await client.version()
expect(version).toContain("gptscript version")
})
test("evaluate executes a prompt correctly", async () => {
const t = {
instructions: "who was the president of the united states in 1928?"
}
const run = client.evaluate(t as any)
expect(run).toBeDefined()
expect(await run.text()).toContain("Calvin Coolidge")
})
test("evaluate executes and streams a prompt correctly", async () => {
let out = ""
let err = undefined
const t = {
instructions: "who was the president of the united states in 1928?"
}
const opts = {
disableCache: true,
}
try {
const run = client.evaluate(t as any, opts)
run.on(gptscript.RunEventType.CallProgress, data => {
out += `system: ${(data as any).content}`
})
await run.text()
err = run.err
} catch (e) {
console.error(e)
}
expect(out).toContain("Calvin Coolidge")
expect(err).toEqual("")
})
describe("run with test.gpt fixture", () => {
test("should execute test.gpt correctly", async () => {
const testGptPath = path.join(__dirname, "fixtures", "test.gpt")
try {
const result = await client.run(testGptPath).text()
expect(result).toBeDefined()
expect(result).toContain("Calvin Coolidge")
} catch (error) {
console.error(error)
fail("run threw an unexpected error.")
}
})
test("should execute test.gpt correctly when chdir is set", async () => {
const testGptPath = path.join(__dirname, "fixtures")
try {
// By changing the directory here, we should be able to find the test.gpt file without prepending the path.
const result = await client.run("test.gpt", {chdir: testGptPath}).text()
expect(result).toBeDefined()
expect(result).toContain("Calvin Coolidge")
} catch (error) {
console.error(error)
fail("run threw an unexpected error.")
}
})
})
test("run executes and stream a file correctly", async () => {
let out = ""
let err = undefined
const testGptPath = path.join(__dirname, "fixtures", "test.gpt")
const opts = {
disableCache: true,
}
try {
const run = client.run(testGptPath, opts)
run.on(gptscript.RunEventType.CallProgress, data => {
out += `system: ${(data as any).content}`
})
await run.text()
err = run.err
} catch (e) {
console.error(e)
}
expect(out).toContain("Calvin Coolidge")
expect(err).toEqual("")
})
test("run executes and streams a file with global tools correctly", async () => {
let out = ""
let err = undefined
const testGptPath = path.join(__dirname, "fixtures", "global-tools.gpt")
const opts = {
disableCache: true,
}
try {
const run = client.run(testGptPath, opts)
run.on(gptscript.RunEventType.CallProgress, data => {
out += `system: ${(data as any).content}`
})
await run.text()
err = run.err
} catch (e) {
console.error(e)
}
expect(out).toContain("Hello!")
expect(err).toEqual("")
}, 15000)
test("aborting a run is reported correctly", async () => {
let errMessage = ""
let err = undefined
const testGptPath = path.join(__dirname, "fixtures", "test.gpt")
const opts = {
disableCache: true,
}
try {
const run = client.run(testGptPath, opts)
run.on(gptscript.RunEventType.CallProgress, data => {
run.close()
})
await run.text()
err = run.err
} catch (error: any) {
errMessage = error
}
expect(errMessage).toContain("aborted")
expect(err).toBeUndefined()
})
describe("evaluate with multiple tools", () => {
test("multiple tools", async () => {
const t0 = {
tools: ["ask"],
instructions: "Only use the ask tool to ask who was the president of the united states in 1928?"
}
const t1 = {
name: "ask",
description: "This tool is used to ask a question",
arguments: {
type: "object",
question: "The question to ask"
},
instructions: "${question}"
}
const response = await client.evaluate([t0 as any, t1 as any]).text()
expect(response).toBeDefined()
expect(response).toContain("Calvin Coolidge")
}, 30000)
test("with sub tool", async () => {
const t0 = {
tools: ["ask"],
instructions: "Only use the ask tool to ask who was the president of the united states in 1928?"
} as any
const t1 = {
name: "other",
instructions: "Who was the president of the united states in 1986?"
} as any
const t2 = {
name: "ask",
description: "This tool is used to ask a question",
arguments: {
type: "object",
question: "The question to ask"
},
instructions: "${question}"
} as any
const response = await client.evaluate([t0, t1, t2], {subTool: "other"}).text()
expect(response).toBeDefined()
expect(response).toContain("Ronald Reagan")
}, 30000)
})
test("parse file", async () => {
const response = await client.parse(path.join(__dirname, "fixtures", "test.gpt"))
expect(response).toBeDefined()
expect(response).toHaveLength(1)
expect((response[0] as gptscript.Tool).instructions).toEqual("who was the president in 1928?")
}, 30000)
test("parse string tool", async () => {
const tool = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?"
const response = await client.parseTool(tool)
expect(response).toBeDefined()
expect(response).toHaveLength(1)
expect((response[0] as gptscript.Tool).instructions).toEqual(tool)
}, 30000)
test("parse string tool with text node", async () => {
const tool = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?\n---\n!markdown\nThis is a text node"
const response = await client.parseTool(tool)
expect(response).toBeDefined()
expect(response).toHaveLength(2)
expect((response[0] as gptscript.Tool).instructions).toEqual("How much wood would a woodchuck chuck if a woodchuck could chuck wood?")
expect((response[1] as gptscript.Text).content).toEqual("This is a text node")
}, 30000)
test("parse string tool global tools", async () => {
const tool = "Global Tools: acorn, do-work\nHow much wood would a woodchuck chuck if a woodchuck could chuck wood?"
const response = await client.parseTool(tool)
expect(response).toBeDefined()
expect(response).toHaveLength(1)
expect((response[0] as gptscript.Tool).instructions).toEqual("How much wood would a woodchuck chuck if a woodchuck could chuck wood?")
expect((response[0] as gptscript.Tool).globalTools).toEqual(["acorn", "do-work"])
}, 30000)
test("parse string tool first line shebang", async () => {
const tool = "\n#!/usr/bin/env python\nHow much wood would a woodchuck chuck if a woodchuck could chuck wood?"
const response = await client.parseTool(tool)
expect(response).toBeDefined()
expect(response).toHaveLength(1)
expect((response[0] as gptscript.Tool).instructions).toEqual("#!/usr/bin/env python\nHow much wood would a woodchuck chuck if a woodchuck could chuck wood?")
}, 30000)
test("format tool", async () => {
const tool = {
type: "tool",
tools: ["sys.write", "sys.read"],
instructions: "This is a test",
arguments: {
type: "object",
properties: {
text: {
type: "string",
description: "The text to write"
}
}
}
}
const response = await client.stringify([tool as any])
expect(response).toBeDefined()
expect(response).toContain("Tools: sys.write, sys.read")
expect(response).toContain("This is a test")
expect(response).toContain("Args: text: The text to write")
})
test("exec tool with chat", async () => {
let err = undefined
const t = {
chat: true,
instructions: "You are a chat bot. Don't finish the conversation until I say 'bye'.",
tools: ["sys.chat.finish"]
}
const opts = {
disableCache: true,
}
let run = client.evaluate(t as any, opts)
const inputs = [
"List the three largest states in the United States by area.",
"What is the capital of the third one?",
"What timezone is the first one in?"
]
const expectedOutputs = [
"California",
"Sacramento",
"Alaska Time Zone"
]
try {
await run.text()
for (let i: number = 0; i < inputs.length; i++) {
run = run.nextChat(inputs[i])
err = run.err
if (err) {
break
}
expect(await run.text()).toContain(expectedOutputs[i])
expect(run.state).toEqual(gptscript.RunState.Continue)
}
run = run.nextChat("bye")
await run.text()
} catch (e) {
console.error(e)
}
expect(run.state).toEqual(gptscript.RunState.Finished)
expect(err).toEqual("")
}, 60000)
test("exec file with chat", async () => {
let err = undefined
const opts = {
disableCache: true
}
let run = client.run(path.join(__dirname, "fixtures", "chat.gpt"), opts)
const inputs = [
"List the 3 largest of the Great Lakes by volume.",
"What is the volume of the second one in cubic miles?",
"What is the total area of the third one in square miles?"
]
const expectedOutputs = [
"Lake Superior",
"Lake Michigan",
"Lake Huron"
]
try {
await run.text()
for (let i: number = 0; i < inputs.length; i++) {
run = run.nextChat(inputs[i])
err = run.err
if (err) {
break
}
expect(await run.text()).toContain(expectedOutputs[i])
expect(run.state).toEqual(gptscript.RunState.Continue)
}
run = run.nextChat("bye")
await run.text()
} catch (e) {
console.error(e)
}
expect(run.state).toEqual(gptscript.RunState.Finished)
expect(err).toEqual("")
}, 60000)
})