1.1.0 • Published 1 year ago

openai-edge-streaming v1.1.0

Weekly downloads
-
License
Unlicense
Repository
github
Last release
1 year ago

openai-edge-streaming@1.1.0

This unofficial OpenAI library provides convenient access to the OpenAI API. This library can be used in browser (not recommended as you would expose your API key), edge, and server environments. It improves upon the official library by:

  1. Adding support for usage in Next.js Edge Runtime by passing useFetch: true
  2. Adds streaming support for the /chat/completions endpoint with openai.chat.createCompletionStream method. See example usage in a ChatGPT clone here
  3. Reorganizing OpenAI's endpoints into namespaces
  4. Simplifying usage with less imports

Authored by: https://www.konfigthis.com/

Installing

npm

npm install openai-edge-streaming --save

yarn

yarn add openai-edge-streaming

Usage in Next.js Edge Runtime

If you are using openai-edge-streaming in a Next.js Edge runtime make sure to set useFetch: true like so:

import { Openai } from "openai-edge-streaming"

export const config = {
    runtime: "edge",
};

export default async function handler(req: Request): Promise<Response> {
    const openai = new Openai({
        useFetch: true, // useFetch must be "true" for openai-edge-streaming to work in Next.js edge runtime
    })
    return new Response(JSON.stringify({ message: "This is from openai-edge-streaming" }), { status: 200 })
}

Getting Started

import { Openai } from "openai-edge-streaming"

const openai = new Openai({
    // Defining the base path is optional and defaults to https://api.openai.com/v1
    // basePath: "https://api.openai.com/v1",
    apiKey: process.env.OPENAI_API_KEY,
})

const createResponse = await openai.answers.create({
        "model": "model_example",
        "question": "What is the capital of Japan?",
        "examples": [["What is the capital of Canada?","Ottawa"],["Which province is Ottawa in?","Ontario"]],
        "examples_context": "Ottawa, Canada's capital, is located in the east of southern Ontario, near the city of Montréal and the U.S. border.",
        "documents": ["Japan is an island country in East Asia, located in the northwest Pacific Ocean.","Tokyo is the capital and most populous prefecture of Japan."],
        "search_model": "ada",
        "max_rerank": 200,
        "temperature": 0,
        "max_tokens": 16,
        "n": 1,
        "return_metadata": false,
        "return_prompt": false,
        "expand": [],
        "user": "user-1234",
    })

console.log(createResponse)

Documentation for API Endpoints

All URIs are relative to https://api.openai.com/v1

ClassMethodHTTP requestDescription
AnswersApicreatePOST /answersAnswers the specified question using the provided documents and examples. The endpoint first searches over provided documents or files to find relevant context. The relevant context is combined with the provided examples and question to create the prompt for completion.
AudioApicreateTranscriptionPOST /audio/transcriptionsTranscribes audio into the input language.
AudioApicreateTranslationPOST /audio/translationsTranslates audio into into English.
ChatApicreateCompletionPOST /chat/completionsCreates a completion for the chat message
ClassificationsApicreatePOST /classificationsClassifies the specified `query` using provided examples. The endpoint first searches over the labeled examples to select the ones most relevant for the particular query. Then, the relevant examples are combined with the query to construct a prompt to produce the final label via the completions endpoint. Labeled examples can be provided via an uploaded `file`, or explicitly listed in the request using the `examples` parameter for quick tests and small scale use cases.
CompletionsApicreatePOST /completionsCreates a completion for the provided prompt and parameters
EditsApicreatePOST /editsCreates a new edit for the provided input, instruction, and parameters.
EmbeddingsApicreatePOST /embeddingsCreates an embedding vector representing the input text.
EnginesApicreateSearchPOST /engines/{engine_id}/searchThe search endpoint computes similarity scores between provided query and documents. Documents can be passed directly to the API if there are no more than 200 of them. To go beyond the 200 document limit, documents can be processed offline and then used for efficient retrieval at query time. When `file` is set, the search endpoint searches over all the documents in the given file and returns up to the `max_rerank` number of documents. These documents will be returned along with their search scores. The similarity score is a positive score that usually ranges from 0 to 300 (but can sometimes go higher), where a score above 200 usually means the document is semantically similar to the query.
EnginesApilistGET /enginesLists the currently available (non-finetuned) models, and provides basic information about each one such as the owner and availability.
EnginesApiretrieveGET /engines/{engine_id}Retrieves a model instance, providing basic information about it such as the owner and availability.
FilesApicreatePOST /filesUpload a file that contains document(s) to be used across various endpoints/features. Currently, the size of all the files uploaded by one organization can be up to 1 GB. Please contact us if you need to increase the storage limit.
FilesApideleteDELETE /files/{file_id}Delete a file.
FilesApidownloadGET /files/{file_id}/contentReturns the contents of the specified file
FilesApilistGET /filesReturns a list of files that belong to the user\'s organization.
FilesApiretrieveGET /files/{file_id}Returns information about a specific file.
FinetunesApicancelPOST /fine-tunes/{fine_tune_id}/cancelImmediately cancel a fine-tune job.
FinetunesApicreatePOST /fine-tunesCreates a job that fine-tunes a specified model from a given dataset. Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete. Learn more about Fine-tuning
FinetunesApilistGET /fine-tunesList your organization\'s fine-tuning jobs
FinetunesApilistEventsGET /fine-tunes/{fine_tune_id}/eventsGet fine-grained status updates for a fine-tune job.
FinetunesApiretrieveGET /fine-tunes/{fine_tune_id}Gets info about the fine-tune job. Learn more about Fine-tuning
ImagesApicreatePOST /images/generationsCreates an image given a prompt.
ImagesApicreateEditPOST /images/editsCreates an edited or extended image given an original image and a prompt.
ImagesApicreateVariationPOST /images/variationsCreates a variation of a given image.
ModelsApideleteDELETE /models/{model}Delete a fine-tuned model. You must have the Owner role in your organization.
ModelsApilistGET /modelsLists the currently available models, and provides basic information about each one such as the owner and availability.
ModelsApiretrieveGET /models/{model}Retrieves a model instance, providing basic information about the model such as the owner and permissioning.
ModerationsApicreatePOST /moderationsClassifies if text violates OpenAI\'s Content Policy