GPT4js 🔮
GPT4js is a package that simplifies interaction with various AI models, eliminating the need for an API Key or any other authorization method to access these chat completions and image generation models.
This package can be used in Node.js or Browser environments.
📚 Table of Contents
🛠️ Installation
Using NPM
npm install gpt4js
Using Yarn
yarn add gpt4js
Using Bun
bun add gpt4js
🧩 Examples
📤 Chat Completion
With the chatCompletion
function, you can obtain a textual response to a conversation with some context, using providers and models designed for this task. Additionally, you can manipulate the answer before converting it to a stream or force the AI to give you a certain answer by generating several retries.
⚙️ Basic Usage
Simple Fetch
It will capture the messages and the context, and any provider will respond with a string.
const getGPT4js = require("gpt4js");
const GPT4js = await getGPT4js();
import GPT4js from "gpt4js";
const messages = [{ role: "user", content: "hi!" }];
const options = {
provider: "Nextway",
model: "gpt-4o-free",
};
(async () => {
const provider = GPT4js.createProvider(options.provider);
try {
const text = await provider.chatCompletion(messages, options, (data) => {
console.log(data);
});
console.log(text);
} catch (error) {
console.error("Error:", error);
}
})();
Note: The conversation needs to include at least one message with the role user to provide a proper answer.
Give Your Instructions
You can provide your own instructions for the conversation before it starts using the system role.
const messages = [
{ role: "system", content: "You're an expert bot in programming." },
{ role: "user", content: "Hi, write me something." },
];
const options = {
provider: "Nextway",
model: "gpt-4o-free",
};
(async () => {
const provider = GPT4js.createProvider(options.provider);
try {
const text = await provider.chatCompletion(messages, options, (data) => {
console.log(data);
});
console.log(text);
} catch (error) {
console.error("Error:", error);
}
})();
Conversation Roles
Role | Description |
---|
system | Used for providing instructions and context prior to the conversation. |
user | Used to identify user messages |
assistant | Used to identify AI messages |
🔩 Configurable Options
Option | Type | Description |
---|
provider | string | Choose the provider to use for chat completions. Possible values include Nextway , BlackBox , etc. This determines which service will handle the request. |
model | string | Choose the model to use by a provider that supports it. For example, gpt-4 , gpt-3.5-turbo , etc. This specifies the particular language model for generating completions. |
stream | boolean | Determine if the data should be streamed in parts or not. If true , the response will be streamed in real-time as it's generated. If false , the response will be sent all at once. |
temperature | number | Set the temperature to control the randomness of the output. A value between 0 and 1 where higher values (closer to 1) make the output more random, and lower values (closer to 0) make it more deterministic. |
webSearch | boolean | Enable or disable web search functionality. If true , the system can perform web searches to gather real-time information. If false , it relies solely on pre-existing data. |
codeModelMode | boolean | Enable or disable the code model mode. If true , the system will use a model optimized for understanding and generating code. If false , it uses the general-purpose language model. |
isChromeExt | boolean | Specify whether the system is being used as a Chrome extension. If true , it indicates integration with Chrome, possibly affecting certain functionalities and permissions. |
🚀 Chat Completion Providers
📚 Chat Completion Models
Model | Providers that support it |
---|
gpt-4 | Aryahcr , Nextway , ChatBotRu |
gpt-4-0613 | Aryahcr |
gpt-4-32k | Aryahcr |
gpt-4-0314 | Aryahcr |
gpt-4o-free | Nextway |
gpt-4o | ChatBotRu |
gpt-4-32k-0314 | Aryahcr |
gpt-4-turbo | ChatBotRu |
gpt-3.5-turbo | Aryahcr , Nextway , Alibaba |
gpt-3.5-turbo-16k | Aryahcr |
gpt-3.5-turbo-0613 | Aryahcr |
gpt-3.5-turbo-16k-0613 | Aryahcr |
gpt-3.5-turbo-0301 | Aryahcr |
text-davinci-003 | Aryahcr |
text-davinci-002 | Aryahcr |
code-davinci-002 | Aryahcr |
gpt-3 | Aryahcr |
text-curie-001 | Aryahcr |
text-babbage-001 | Aryahcr |
text-ada-001 | Aryahcr |
davinci | Aryahcr |
curie | Aryahcr |
babbage | Aryahcr |
ada | Aryahcr |
babbage-002 | Aryahcr |
davinci-002 | Aryahcr |
gemini-pro | Nextway |
gemini-nano | Chrome |
All Ollama models | Ollama |
SparkDesk-v1.1 | Alibaba |
deepseek-coder | Alibaba |
deepseek-chat | Alibaba |
Qwen2-7B-Instruct | Alibaba |
glm4-9B-chat | Alibaba |
chatglm3-6B | Alibaba |
Yi-1.5-9B-Chat | Alibaba |
llama-3.1-405b-instruct-free | Nextway |
📷 Image Generation
📹 Example usage
const options = {
provider: "DALLE2",
};
(async () => {
const provider = GPT4js.createProvider(options.provider);
try {
const base64 = await provider.imageGeneration("wood", options);
console.log(base64);
} catch (error) {
console.error("Error:", error);
}
})();
With the imageGeneration
function, you can generate images from textual input along with optional parameters to customize and stylize the images in various artistic styles.
🌐 Image Generation Provider Options
Option | Type | Description |
---|
negativePrompt | string | Indicates the direction not to take in production. |
height | number | Specifies the image height. |
width | number | Specifies the image width. |
samplingSteps | number | Specifies the number of iterations. A higher number results in higher quality. |
samplingMethod | string | Selects a sampling method to control the diversity, quality, and coherence of images. |
cfgScale | number | Specifies the Classifier-Free Guidance to control how closely the generated image adheres to the given text prompt. |
🧮 Number Type Options
Provider | Supported Number Type Options and Values |
---|
StableDiffusion | - height : Default 512, Min 50, Max 1024 - width : Default 512, Min 50, Max 1024 - samplingSteps : Default 25, Min 1, Max 30 - cfgScale : Default 7, Min 1, Max 20 |
🖼️ Image Generation Providers
Provider | Status | Default Style |
---|
Dalle2 | | Semi-realistic, detailed with vivid colors and natural lighting. |
StableDiffusion | | Photorealistic, capturing fine details and textures to simulate real-life scenes. |
🧠 Google Chrome AI
Warning: This is an experimental feature and may not work correctly, it only works in Google Chrome 127 or higher (Chrome Dev). Also history is not supported
Setting Browser
-
chrome://flags/#prompt-api-for-gemini-nano Select 'Enabled'
-
chrome://flags/#optimization-guide-on-device-model Select 'Enabled BypassPrefRequirement'
-
chrome://components Click 'Check for Update' on Optimization Guide On Device Model to download the model. If you don't see Optimization Guide, ensure you have set the flags correctly above, relaunch your browser, and refresh the page.
Simple Usage
const messages = [{ role: "user", content: "hi!" }];
const options = {
provider: "Chrome",
};
(async () => {
const provider = GPT4js.createProvider(options.provider);
try {
const text = await provider.chatCompletion(messages, options, (data) => {
console.log(data);
});
console.log(text);
} catch (error) {
console.error("Error:", error);
}
})();
🧪 Testing
Running: npm test
🚧 Building
Webpack
npm run build
- Build using Webpack.npm run dev
- Live development build with Webpack.
Bun
npm run build:bun
- Build using Bun.npm run dev:bun
- Live development build with Bun.
🤝 Contribute
If you'd like to contribute to this project, you can do so directly on GitHub. Additionally, if you encounter any errors that hinder your use of any project functionality, please report them here. Your feedback helps our community access AI tools freely!