|
1 |
| -import { test } from "node:test"; |
| 1 | +import { test, suite } from "node:test"; |
2 | 2 |
|
3 | 3 | import { MockAgent } from "undici";
|
4 | 4 |
|
5 | 5 | import { prompt } from "../index.js";
|
6 | 6 |
|
7 |
| -test("smoke", (t) => { |
8 |
| - t.assert.equal(typeof prompt, "function"); |
9 |
| -}); |
| 7 | +suite("prompt", () => { |
| 8 | + test("smoke", (t) => { |
| 9 | + t.assert.equal(typeof prompt, "function"); |
| 10 | + }); |
10 | 11 |
|
11 |
| -test("minimal usage", async (t) => { |
12 |
| - const mockAgent = new MockAgent(); |
13 |
| - function fetchMock(url, opts) { |
14 |
| - opts ||= {}; |
15 |
| - opts.dispatcher = mockAgent; |
16 |
| - return fetch(url, opts); |
17 |
| - } |
| 12 | + test("minimal usage", async (t) => { |
| 13 | + const mockAgent = new MockAgent(); |
| 14 | + function fetchMock(url, opts) { |
| 15 | + opts ||= {}; |
| 16 | + opts.dispatcher = mockAgent; |
| 17 | + return fetch(url, opts); |
| 18 | + } |
18 | 19 |
|
19 |
| - mockAgent.disableNetConnect(); |
20 |
| - const mockPool = mockAgent.get("https://api.githubcopilot.com"); |
21 |
| - mockPool |
22 |
| - .intercept({ |
23 |
| - method: "post", |
24 |
| - path: `/chat/completions`, |
25 |
| - body: JSON.stringify({ |
26 |
| - messages: [ |
27 |
| - { |
28 |
| - role: "system", |
29 |
| - content: "You are a helpful assistant.", |
30 |
| - }, |
31 |
| - { |
32 |
| - role: "user", |
33 |
| - content: "What is the capital of France?", |
34 |
| - }, |
35 |
| - ], |
36 |
| - model: "gpt-4", |
37 |
| - }), |
38 |
| - }) |
39 |
| - .reply( |
40 |
| - 200, |
41 |
| - { |
42 |
| - choices: [ |
43 |
| - { |
44 |
| - message: { |
45 |
| - content: "<response text>", |
| 20 | + mockAgent.disableNetConnect(); |
| 21 | + const mockPool = mockAgent.get("https://api.githubcopilot.com"); |
| 22 | + mockPool |
| 23 | + .intercept({ |
| 24 | + method: "post", |
| 25 | + path: `/chat/completions`, |
| 26 | + body: JSON.stringify({ |
| 27 | + messages: [ |
| 28 | + { |
| 29 | + role: "system", |
| 30 | + content: "You are a helpful assistant.", |
46 | 31 | },
|
47 |
| - }, |
48 |
| - ], |
49 |
| - }, |
50 |
| - { |
51 |
| - headers: { |
52 |
| - "content-type": "application/json", |
53 |
| - "x-request-id": "<request-id>", |
| 32 | + { |
| 33 | + role: "user", |
| 34 | + content: "What is the capital of France?", |
| 35 | + }, |
| 36 | + ], |
| 37 | + model: "gpt-4", |
| 38 | + }), |
| 39 | + }) |
| 40 | + .reply( |
| 41 | + 200, |
| 42 | + { |
| 43 | + choices: [ |
| 44 | + { |
| 45 | + message: { |
| 46 | + content: "<response text>", |
| 47 | + }, |
| 48 | + }, |
| 49 | + ], |
54 | 50 | },
|
55 |
| - } |
56 |
| - ); |
| 51 | + { |
| 52 | + headers: { |
| 53 | + "content-type": "application/json", |
| 54 | + "x-request-id": "<request-id>", |
| 55 | + }, |
| 56 | + } |
| 57 | + ); |
57 | 58 |
|
58 |
| - const result = await prompt("What is the capital of France?", { |
59 |
| - token: "secret", |
60 |
| - model: "gpt-4", |
61 |
| - request: { fetch: fetchMock }, |
62 |
| - }); |
| 59 | + const result = await prompt("What is the capital of France?", { |
| 60 | + token: "secret", |
| 61 | + model: "gpt-4", |
| 62 | + request: { fetch: fetchMock }, |
| 63 | + }); |
63 | 64 |
|
64 |
| - t.assert.deepEqual(result, { |
65 |
| - requestId: "<request-id>", |
66 |
| - message: { |
67 |
| - content: "<response text>", |
68 |
| - }, |
| 65 | + t.assert.deepEqual(result, { |
| 66 | + requestId: "<request-id>", |
| 67 | + message: { |
| 68 | + content: "<response text>", |
| 69 | + }, |
| 70 | + }); |
69 | 71 | });
|
70 |
| -}); |
71 | 72 |
|
72 |
| -test("function calling", async (t) => { |
73 |
| - const mockAgent = new MockAgent(); |
74 |
| - function fetchMock(url, opts) { |
75 |
| - opts ||= {}; |
76 |
| - opts.dispatcher = mockAgent; |
77 |
| - return fetch(url, opts); |
78 |
| - } |
| 73 | + test("function calling", async (t) => { |
| 74 | + const mockAgent = new MockAgent(); |
| 75 | + function fetchMock(url, opts) { |
| 76 | + opts ||= {}; |
| 77 | + opts.dispatcher = mockAgent; |
| 78 | + return fetch(url, opts); |
| 79 | + } |
79 | 80 |
|
80 |
| - mockAgent.disableNetConnect(); |
81 |
| - const mockPool = mockAgent.get("https://api.githubcopilot.com"); |
82 |
| - mockPool |
83 |
| - .intercept({ |
84 |
| - method: "post", |
85 |
| - path: `/chat/completions`, |
86 |
| - body: JSON.stringify({ |
87 |
| - messages: [ |
88 |
| - { |
89 |
| - role: "system", |
90 |
| - content: |
91 |
| - "You are a helpful assistant. Use the supplied tools to assist the user.", |
92 |
| - }, |
93 |
| - { role: "user", content: "Call the function" }, |
94 |
| - ], |
95 |
| - model: "gpt-4", |
96 |
| - toolChoice: "auto", |
97 |
| - tools: [ |
98 |
| - { |
99 |
| - type: "function", |
100 |
| - function: { name: "the_function", description: "The function" }, |
101 |
| - }, |
102 |
| - ], |
103 |
| - }), |
104 |
| - }) |
105 |
| - .reply( |
106 |
| - 200, |
107 |
| - { |
108 |
| - choices: [ |
109 |
| - { |
110 |
| - message: { |
111 |
| - content: "<response text>", |
| 81 | + mockAgent.disableNetConnect(); |
| 82 | + const mockPool = mockAgent.get("https://api.githubcopilot.com"); |
| 83 | + mockPool |
| 84 | + .intercept({ |
| 85 | + method: "post", |
| 86 | + path: `/chat/completions`, |
| 87 | + body: JSON.stringify({ |
| 88 | + messages: [ |
| 89 | + { |
| 90 | + role: "system", |
| 91 | + content: |
| 92 | + "You are a helpful assistant. Use the supplied tools to assist the user.", |
112 | 93 | },
|
113 |
| - }, |
114 |
| - ], |
115 |
| - }, |
116 |
| - { |
117 |
| - headers: { |
118 |
| - "content-type": "application/json", |
119 |
| - "x-request-id": "<request-id>", |
| 94 | + { role: "user", content: "Call the function" }, |
| 95 | + ], |
| 96 | + model: "gpt-4", |
| 97 | + toolChoice: "auto", |
| 98 | + tools: [ |
| 99 | + { |
| 100 | + type: "function", |
| 101 | + function: { name: "the_function", description: "The function" }, |
| 102 | + }, |
| 103 | + ], |
| 104 | + }), |
| 105 | + }) |
| 106 | + .reply( |
| 107 | + 200, |
| 108 | + { |
| 109 | + choices: [ |
| 110 | + { |
| 111 | + message: { |
| 112 | + content: "<response text>", |
| 113 | + }, |
| 114 | + }, |
| 115 | + ], |
120 | 116 | },
|
121 |
| - } |
122 |
| - ); |
| 117 | + { |
| 118 | + headers: { |
| 119 | + "content-type": "application/json", |
| 120 | + "x-request-id": "<request-id>", |
| 121 | + }, |
| 122 | + } |
| 123 | + ); |
123 | 124 |
|
124 |
| - const result = await prompt("Call the function", { |
125 |
| - token: "secret", |
126 |
| - model: "gpt-4", |
127 |
| - tools: [ |
128 |
| - { |
129 |
| - type: "function", |
130 |
| - function: { |
131 |
| - name: "the_function", |
132 |
| - description: "The function", |
| 125 | + const result = await prompt("Call the function", { |
| 126 | + token: "secret", |
| 127 | + model: "gpt-4", |
| 128 | + tools: [ |
| 129 | + { |
| 130 | + type: "function", |
| 131 | + function: { |
| 132 | + name: "the_function", |
| 133 | + description: "The function", |
| 134 | + }, |
133 | 135 | },
|
134 |
| - }, |
135 |
| - ], |
136 |
| - request: { fetch: fetchMock }, |
137 |
| - }); |
| 136 | + ], |
| 137 | + request: { fetch: fetchMock }, |
| 138 | + }); |
138 | 139 |
|
139 |
| - t.assert.deepEqual(result, { |
140 |
| - requestId: "<request-id>", |
141 |
| - message: { |
142 |
| - content: "<response text>", |
143 |
| - }, |
| 140 | + t.assert.deepEqual(result, { |
| 141 | + requestId: "<request-id>", |
| 142 | + message: { |
| 143 | + content: "<response text>", |
| 144 | + }, |
| 145 | + }); |
144 | 146 | });
|
145 | 147 | });
|
0 commit comments