1

GitHub - sandaru1/openai-web: Javascript library for the OpenAI API

 1 year ago
source link: https://github.com/sandaru1/openai-web
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

OpenAI Javascript Library

The OpenAI javascript library provides a browser compatible javascript library for convenient access to the OpenAI API. This is an unofficial fork of the OpenAI library with modifications for browser and extension compatibility. The library is using "fetch" instead of XHR requests to make API calls, making it compatible with browser extensions.

This library is intended to be used when you want to give the user ability to use their own API keys and directly make requests from the browser. This can be used for HTML apps that run in the browser or browser extensions.

You can look at ChatGPT Rewriter extension to see how it can be used in a Chrome extension.

Important note: Don't embed your API keys in the browser. Always ask the user for their API keys.

Installation

npm install openai-web

Usage

The library needs to be configured with an OpenAI account's secret key, which is available on the website. You should request the users to provide the API key.

<script src="openai.min.js"></script>
<script type="module">
    const configuration = new OpenAI.Configuration({
        apiKey: $("#key").val(),  // Read the API key from user input
    });
    const openai = new OpenAI.OpenAIApi(configuration);

    const response = openai.createCompletion({
        model: "text-davinci-003",
        prompt: "Say Hello World in javascript",
        temperature: 0,
        max_tokens: 7,
    }).then((res) => {
        console.log(res.data.choices[0].text);
    },(error) => {
        const message = error.response.data.error.message ?? (error.message ?? "An error occured");
        console.log("Error",message);
    });
</script>

This library is a fork of the official OpenAI Node.js library. Check out the full API documentation for examples of all the available functions.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK