openai-api-mock 0.1.31 → 0.1.33

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/LICENSE.md CHANGED
@@ -1,21 +1,21 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2024 Nabil Chiheb
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Nabil Chiheb
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,147 +1,173 @@
1
- # OpenAI API Mock
2
-
3
- This is a Node.js module for mocking OpenAI API responses in a development environment.
4
-
5
- [![Tests](https://github.com/chihebnabil/openai-api-mock/actions/workflows/test.yml/badge.svg)](https://github.com/chihebnabil/openai-api-mock/actions/workflows/test.yml)
6
-
7
- It's useful for testing and development purposes when you don't want to make actual API calls.
8
-
9
- The module supports the following OpenAI API endpoints:
10
- - chat completions
11
- - chat completions with streaming
12
- - chat completions with functions
13
- - image generations
14
-
15
- > This module is powering the sandbox mode for [Aipify](https://aipify.co).
16
-
17
- ## Installation
18
-
19
- You can install this module using npm as a dev dependency :
20
-
21
- ```sh
22
- npm install -D openai-api-mock
23
- ```
24
-
25
- ## Usage
26
-
27
- The module supports both ESM and CommonJS imports:
28
-
29
- ```js
30
- // ESM
31
- import { mockOpenAIResponse } from 'openai-api-mock';
32
-
33
- // CommonJS
34
- const { mockOpenAIResponse } = require('openai-api-mock');
35
- ```
36
-
37
- Then, call the mockOpenAIResponse function to set up the mock response:
38
-
39
- ```js
40
- // Basic usage
41
- mockOpenAIResponse();
42
-
43
- // Force mocking regardless of environment
44
- mockOpenAIResponse(true);
45
-
46
- // With configuration options
47
- mockOpenAIResponse(false, {
48
- includeErrors: true, // Simulate random API errors
49
- latency: 1000, // Add 1 second delay to responses
50
- logRequests: true // Log incoming requests to console
51
- });
52
- ```
53
-
54
- The function accepts two parameters:
55
- - `force` (boolean): Determines whether the mock response should be used regardless of the environment. If false or not provided, mocking only occurs in development environment.
56
- - `options` (object): Additional configuration options
57
- - `includeErrors` (boolean): When true, randomly simulates API errors
58
- - `latency` (number): Adds artificial delay to responses in milliseconds
59
- - `logRequests` (boolean): Logs incoming requests to console for debugging
60
-
61
- The function returns an object with control methods:
62
- ```js
63
- const mock = mockOpenAIResponse();
64
-
65
- // Check if mocking is active
66
- console.log(mock.isActive);
67
-
68
- // Stop all mocks
69
- mock.stopMocking();
70
-
71
- // Add custom endpoint mock (uses api.openai.com as base url)
72
- mock.addCustomEndpoint('POST', '/v1/custom', (uri, body) => {
73
- return [200, { custom: 'response' }];
74
- });
75
- ```
76
-
77
- ### Example responses
78
-
79
- ```js
80
- // Call the mockOpenAIResponse function once to set up the mock
81
- mockOpenAIResponse()
82
-
83
- // Now, when you call the OpenAI API, it will return a mock response
84
- const response = await openai.chat.completions.create({
85
- model: "gpt-3.5",
86
- messages: [
87
- { role: 'system', content: "You're an expert chef" },
88
- { role: 'user', content: "Suggest at least 5 recipes" },
89
- ]
90
- });
91
- ```
92
- In this example, the `response` constant will contain mock data, simulating a response from the OpenAI API:
93
-
94
- ```javascript
95
- {
96
- choices: [
97
- {
98
- finish_reason: 'stop',
99
- index: 0,
100
- message: [Object],
101
- logprobs: null
102
- }
103
- ],
104
- created: 1707040459,
105
- id: 'chatcmpl-tggOnwW8Lp2XiwQ8dmHHAcNYJ8CfzR',
106
- model: 'gpt-3.5-mock',
107
- object: 'chat.completion',
108
- usage: { completion_tokens: 17, prompt_tokens: 57, total_tokens: 74 }
109
- }
110
- ```
111
- The library also supports mocking `stream` responses
112
-
113
- ```js
114
- // Call the mockOpenAIResponse function once to set up the mock
115
- mockOpenAIResponse()
116
- // Now, when you call the OpenAI API, it will return a mock response
117
- const response = await openai.chat.completions.create({
118
- model: "gpt-3.5",
119
- stream : true,
120
- messages: [
121
- { role: 'system', content: "You're an expert chef" },
122
- { role: 'user', content: "Suggest at least 5 recipes" },
123
- ]
124
- });
125
-
126
- // then read it
127
- for await (const part of response) {
128
- console.log(part.choices[0]?.delta?.content || '')
129
- }
130
- ```
131
-
132
- ## Intercepted URLs
133
-
134
- This module uses the `nock` library to intercept HTTP calls to the following OpenAI API endpoints:
135
-
136
- - `https://api.openai.com/v1/chat/completions`: This endpoint is used for generating chat completions.
137
- - `https://api.openai.com/v1/images/generations`: This endpoint is used for generating images.
138
-
139
-
140
- ## Dependencies
141
- This module depends on the following npm packages:
142
-
143
- - nock : For intercepting HTTP calls.
144
- - @faker-js/faker : For generating fake data.
145
-
146
- ## License
147
- This project is licensed under the MIT License.
1
+ # OpenAI API Mock
2
+
3
+ This is a Node.js module for mocking OpenAI API responses in a development environment .
4
+
5
+ [![Tests](https://github.com/chihebnabil/openai-api-mock/actions/workflows/test.yml/badge.svg)](https://github.com/chihebnabil/openai-api-mock/actions/workflows/test.yml)
6
+
7
+ It's useful for testing and development purposes when you don't want to make actual API calls.
8
+
9
+ The module supports the following OpenAI API endpoints:
10
+ - chat completions
11
+ - chat completions with streaming
12
+ - chat completions with functions
13
+ - image generations
14
+
15
+ > This module is powering the sandbox mode for [Aipify](https://aipify.co).
16
+
17
+ ## Installation
18
+
19
+ You can install this module using npm as a dev dependency :
20
+
21
+ ```sh
22
+ npm install -D openai-api-mock
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ The module supports both ESM and CommonJS imports:
28
+
29
+ ```js
30
+ // ESM
31
+ import { mockOpenAIResponse } from 'openai-api-mock';
32
+
33
+ // CommonJS
34
+ const { mockOpenAIResponse } = require('openai-api-mock');
35
+ ```
36
+
37
+ Then, call the mockOpenAIResponse function to set up the mock response:
38
+
39
+ ```js
40
+ // Basic usage
41
+ mockOpenAIResponse();
42
+
43
+ // Force mocking regardless of environment
44
+ mockOpenAIResponse(true);
45
+
46
+ // With configuration options
47
+ mockOpenAIResponse(false, {
48
+ includeErrors: true, // Simulate random API errors
49
+ latency: 1000, // Add 1 second delay to responses
50
+ logRequests: true // Log incoming requests to console
51
+ });
52
+ ```
53
+
54
+ The function accepts two parameters:
55
+ - `force` (boolean): Determines whether the mock response should be used regardless of the environment. If false or not provided, mocking only occurs in development environment.
56
+ - `options` (object): Additional configuration options
57
+ - `includeErrors` (boolean): When true, randomly simulates API errors
58
+ - `latency` (number): Adds artificial delay to responses in milliseconds
59
+ - `logRequests` (boolean): Logs incoming requests to console for debugging
60
+
61
+ The function returns an object with control methods:
62
+ ```js
63
+ const mock = mockOpenAIResponse();
64
+
65
+ // Check if mocking is active
66
+ console.log(mock.isActive);
67
+
68
+ // Stop all mocks
69
+ mock.stopMocking();
70
+
71
+ // Add custom endpoint mock (uses api.openai.com as base url)
72
+ mock.addCustomEndpoint('POST', '/v1/custom', (uri, body) => {
73
+ return [200, { custom: 'response' }];
74
+ });
75
+ ```
76
+
77
+ ### Example responses
78
+
79
+ ```js
80
+ // Call the mockOpenAIResponse function once to set up the mock
81
+ mockOpenAIResponse()
82
+
83
+ // Now, when you call the OpenAI API, it will return a mock response
84
+ const response = await openai.chat.completions.create({
85
+ model: "gpt-3.5",
86
+ messages: [
87
+ { role: 'system', content: "You're an expert chef" },
88
+ { role: 'user', content: "Suggest at least 5 recipes" },
89
+ ]
90
+ });
91
+ ```
92
+ In this example, the `response` constant will contain mock data, simulating a response from the OpenAI API:
93
+
94
+ ```javascript
95
+ {
96
+ choices: [
97
+ {
98
+ finish_reason: 'stop',
99
+ index: 0,
100
+ message: [Object],
101
+ logprobs: null
102
+ }
103
+ ],
104
+ created: 1707040459,
105
+ id: 'chatcmpl-tggOnwW8Lp2XiwQ8dmHHAcNYJ8CfzR',
106
+ model: 'gpt-3.5-mock',
107
+ object: 'chat.completion',
108
+ usage: { completion_tokens: 17, prompt_tokens: 57, total_tokens: 74 }
109
+ }
110
+ ```
111
+ The library also supports mocking `stream` responses
112
+
113
+ ```js
114
+ // Call the mockOpenAIResponse function once to set up the mock
115
+ mockOpenAIResponse()
116
+ // Now, when you call the OpenAI API, it will return a mock response
117
+ const response = await openai.chat.completions.create({
118
+ model: "gpt-3.5",
119
+ stream : true,
120
+ messages: [
121
+ { role: 'system', content: "You're an expert chef" },
122
+ { role: 'user', content: "Suggest at least 5 recipes" },
123
+ ]
124
+ });
125
+
126
+ // then read it
127
+ for await (const part of response) {
128
+ console.log(part.choices[0]?.delta?.content || '')
129
+ }
130
+ ```
131
+
132
+ ## Intercepted URLs
133
+
134
+ This module uses the `nock` library to intercept HTTP calls to the following OpenAI API endpoints:
135
+
136
+ - `https://api.openai.com/v1/chat/completions`: This endpoint is used for generating chat completions.
137
+ - `https://api.openai.com/v1/images/generations`: This endpoint is used for generating images.
138
+
139
+
140
+ ## TypeScript Support
141
+
142
+ This package includes TypeScript definitions out of the box. After installing the package, you can use it with full type support:
143
+
144
+ ```typescript
145
+ import { mockOpenAIResponse, MockOptions } from 'openai-api-mock';
146
+
147
+ // Configure with TypeScript types
148
+ const options: MockOptions = {
149
+ includeErrors: true, // Optional: simulate random API errors
150
+ latency: 1000, // Optional: add 1 second delay
151
+ logRequests: true // Optional: log requests to console
152
+ };
153
+
154
+ const mock = mockOpenAIResponse(true, options);
155
+
156
+ // TypeScript provides full type checking and autocompletion
157
+ console.log(mock.isActive); // boolean
158
+ mock.stopMocking(); // function
159
+
160
+ // Custom endpoints with type safety
161
+ mock.addCustomEndpoint('POST', '/v1/custom', (uri, body) => {
162
+ return [200, { custom: 'response' }];
163
+ });
164
+ ```
165
+
166
+ ## Dependencies
167
+ This module depends on the following npm packages:
168
+
169
+ - nock : For intercepting HTTP calls.
170
+ - @faker-js/faker : For generating fake data.
171
+
172
+ ## License
173
+ This project is licensed under the MIT License.