npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

openai-api-mock

v0.1.19

Published

A Node.js module for mocking OpenAI API responses in a development environment

Downloads

138

Readme

OpenAI API Mock

This is a Node.js module for mocking OpenAI API responses in a development environment.

It's useful for testing and development purposes when you don't want to make actual API calls.

The module supports the following OpenAI API endpoints:

  • chat completions
  • chat completions with streaming
  • chat completions with functions
  • image generations

This module is powering the sandbox mode for Aipify.

Installation

You can install this module using npm as a dev dependency :

npm install -D openai-api-mock

Usage

const { mockOpenAIResponse } = require('openai-api-mock');

Then, call the mockOpenAIResponse function to set up the mock response:

mockOpenAIResponse();

This function intercepts HTTP calls to the OpenAI endpoint and returns a mock response. The mock response is generated based on the requestBody of the code , and it supports complex (function call) requestBody structures like arrays and nested objects.

mockOpenAIResponse(force = true);

The force parameter is a boolean that determines whether the mock response should be used regardless of the environment. If force is true, the mock response will be used regardless of the environment.

If force is false or not provided, the mock response will only be used if the NODE_ENV environment variable is set to development.

Example responces

// Call the mockOpenAIResponse function once to set up the mock
mockOpenAIResponse() 

// Now, when you call the OpenAI API, it will return a mock response
const response = await openai.chat.completions.create({
                model: "gpt-3.5",
                messages: [
                    { role: 'system', content: "You'r an expert chef" },
                    { role: 'user', content: "Suggest at least 5 recipes" },
                ]
});

In this example, the response constant will contain mock data, simulating a response from the OpenAI API:

{
    choices: [
        {
          finish_reason: 'stop',
          index: 0,
          message: [Object],
          logprobs: null
        }
      ],
      created: 1707040459,
      id: 'chatcmpl-tggOnwW8Lp2XiwQ8dmHHAcNYJ8CfzR',
      model: 'gpt-3.5-mock',
      object: 'chat.completion',
      usage: { completion_tokens: 17, prompt_tokens: 57, total_tokens: 74 }
}

The library also supports mocking stream responses

// Call the mockOpenAIResponse function once to set up the mock
mockOpenAIResponse() 
// Now, when you call the OpenAI API, it will return a mock response
const response = await openai.chat.completions.create({
                model: "gpt-3.5",
                stream : true,
                messages: [
                    { role: 'system', content: "You'r an expert chef" },
                    { role: 'user', content: "Suggest at least 5 recipes" },
                ]
});

// then read it 
for await (const part of response) {
    console.log(part.choices[0]?.delta?.content || '')
}

Intercepted URLs

This module uses the nock library to intercept HTTP calls to the following OpenAI API endpoints:

  • https://api.openai.com/v1/chat/completions: This endpoint is used for generating chat completions.
  • https://api.openai.com/v1/images/generations: This endpoint is used for generating images.

Dependencies

This module depends on the following npm packages:

  • nock : For intercepting HTTP calls.
  • @faker-js/faker : For generating fake data.

License

This project is licensed under the MIT License.