{"id":20363960,"url":"https://github.com/chihebnabil/openai-api-mock","last_synced_at":"2025-04-12T04:33:15.876Z","repository":{"id":219745772,"uuid":"749783936","full_name":"chihebnabil/openai-api-mock","owner":"chihebnabil","description":"A Node.js module for mocking OpenAI API responses in a development environment","archived":false,"fork":false,"pushed_at":"2025-04-04T21:57:43.000Z","size":292,"stargazers_count":16,"open_issues_count":2,"forks_count":3,"subscribers_count":2,"default_branch":"dev","last_synced_at":"2025-04-04T22:31:18.243Z","etag":null,"topics":["api","apitesting","chatgpt-api","mock","nodejs","npm","npm-package","openai"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/openai-api-mock?activeTab=readme","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chihebnabil.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-29T11:46:53.000Z","updated_at":"2025-04-04T21:57:26.000Z","dependencies_parsed_at":"2024-06-29T09:30:49.324Z","dependency_job_id":"ed55901a-c846-4a1e-bc26-784fa6bc628b","html_url":"https://github.com/chihebnabil/openai-api-mock","commit_stats":null,"previous_names":["chihebnabil/openai-api-mock"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chihebnabil%2Fopenai-api-mock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chihebnabil%2Fopenai-api-mock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chihebnabil%2Fopenai-api-mock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chihebnabil%2Fopenai-api-mock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chihebnabil","download_url":"https://codeload.github.com/chihebnabil/openai-api-mock/tar.gz/refs/heads/dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248517423,"owners_count":21117449,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","apitesting","chatgpt-api","mock","nodejs","npm","npm-package","openai"],"created_at":"2024-11-15T00:09:05.694Z","updated_at":"2025-04-12T04:33:15.869Z","avatar_url":"https://github.com/chihebnabil.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# OpenAI API Mock\n\nThis is a Node.js module for mocking OpenAI API responses in a development environment .\n\n[![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)\n\nIt's useful for testing and development purposes when you don't want to make actual API calls.\n\nThe module supports the following OpenAI API endpoints:\n- chat completions\n- chat completions with streaming\n- chat completions with functions\n- image generations\n\n\u003e This module is powering the sandbox mode for [Aipify](https://aipify.co).\n\n## Installation\n\nYou can install this module using npm as a dev dependency :\n\n```sh\nnpm install -D openai-api-mock\n```\n\n## Usage\n\nThe module supports both ESM and CommonJS imports:\n\n```js\n// ESM\nimport { mockOpenAIResponse } from 'openai-api-mock';\n\n// CommonJS\nconst { mockOpenAIResponse } = require('openai-api-mock');\n```\n\nThen, call the mockOpenAIResponse function to set up the mock response:\n\n```js\n// Basic usage\nmockOpenAIResponse();\n\n// Force mocking regardless of environment\nmockOpenAIResponse(true);\n\n// With configuration options\nmockOpenAIResponse(false, {\n    includeErrors: true,    // Simulate random API errors\n    latency: 1000,         // Add 1 second delay to responses\n    logRequests: true      // Log incoming requests to console\n});\n```\n\nThe function accepts two parameters:\n- `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.\n- `options` (object): Additional configuration options\n  - `includeErrors` (boolean): When true, randomly simulates API errors\n  - `latency` (number): Adds artificial delay to responses in milliseconds\n  - `logRequests` (boolean): Logs incoming requests to console for debugging\n\nThe function returns an object with control methods:\n```js\nconst mock = mockOpenAIResponse();\n\n// Check if mocking is active\nconsole.log(mock.isActive);\n\n// Stop all mocks\nmock.stopMocking();\n\n// Add custom endpoint mock (uses api.openai.com as base url)\nmock.addCustomEndpoint('POST', '/v1/custom', (uri, body) =\u003e {\n    return [200, { custom: 'response' }];\n});\n```\n\n### Example responses\n\n```js\n// Call the mockOpenAIResponse function once to set up the mock\nmockOpenAIResponse() \n\n// Now, when you call the OpenAI API, it will return a mock response\nconst response = await openai.chat.completions.create({\n                model: \"gpt-3.5\",\n                messages: [\n                    { role: 'system', content: \"You're an expert chef\" },\n                    { role: 'user', content: \"Suggest at least 5 recipes\" },\n                ]\n});\n ```\nIn this example, the `response` constant will contain mock data, simulating a response from the OpenAI API:\n\n```javascript\n{\n    choices: [\n        {\n          finish_reason: 'stop',\n          index: 0,\n          message: [Object],\n          logprobs: null\n        }\n      ],\n      created: 1707040459,\n      id: 'chatcmpl-tggOnwW8Lp2XiwQ8dmHHAcNYJ8CfzR',\n      model: 'gpt-3.5-mock',\n      object: 'chat.completion',\n      usage: { completion_tokens: 17, prompt_tokens: 57, total_tokens: 74 }\n}\n```\nThe library also supports mocking `stream` responses\n\n```js\n// Call the mockOpenAIResponse function once to set up the mock\nmockOpenAIResponse() \n// Now, when you call the OpenAI API, it will return a mock response\nconst response = await openai.chat.completions.create({\n                model: \"gpt-3.5\",\n                stream : true,\n                messages: [\n                    { role: 'system', content: \"You're an expert chef\" },\n                    { role: 'user', content: \"Suggest at least 5 recipes\" },\n                ]\n});\n\n// then read it \nfor await (const part of response) {\n    console.log(part.choices[0]?.delta?.content || '')\n}\n```\n\n## Intercepted URLs\n\nThis module uses the `nock` library to intercept HTTP calls to the following OpenAI API endpoints:\n\n- `https://api.openai.com/v1/chat/completions`: This endpoint is used for generating chat completions.\n- `https://api.openai.com/v1/images/generations`: This endpoint is used for generating images.\n\n\n## Dependencies\nThis module depends on the following npm packages:\n\n- nock : For intercepting HTTP calls.\n- @faker-js/faker : For generating fake data.\n\n## License\nThis project is licensed under the MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchihebnabil%2Fopenai-api-mock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchihebnabil%2Fopenai-api-mock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchihebnabil%2Fopenai-api-mock/lists"}