Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🔮 Using ChatGPT4/3.5-turbo/Gemini-Pro/BlackBox and etc. unlimited and free

License

NotificationsYou must be signed in to change notification settings

zachey01/gpt4free.js

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.

Static BadgeGitHub top languageGitHub Repo starsGitHub issuesNPM Downloads

📚 Table of Contents

🛠️ Installation

Using NPM

npm install gpt4js

Using Yarn

yarn add gpt4js

Using Bun

bun add gpt4js

🧩 Examples

📤 Chat Completion

With thechatCompletion 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.

// CommonJSconstGPT4js=require("gpt4js");// ESMimportGPT4jsfrom"gpt4js";constmessages=[{role:"user",content:"hi!"}];constoptions={provider:"BlackBox",model:"BlackBox",};(async()=>{constprovider=GPT4js.createProvider(options.provider);try{consttext=awaitprovider.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 roleuser to provide a proper answer.

Give Your Instructions

You can provide your own instructions for the conversation before it starts using thesystem role.

constmessages=[{role:"system",content:"You're an expert bot in programming."},{role:"user",content:"Hi, write me something."},];constoptions={provider:"BlackBox",};(async()=>{constprovider=GPT4js.createProvider(options.provider);try{consttext=awaitprovider.chatCompletion(messages,options,(data)=>{console.log(data);});console.log(text);}catch(error){console.error("Error:",error);}})();

Conversation Roles

RoleDescription
systemUsed for providing instructions and context prior to the conversation.
userUsed to identify user messages
assistantUsed to identify AI messages

🔩 Configurable Options

OptionTypeDescription
providerstringChoose the provider to use for chat completions. Possible values includeNextway,BlackBox, etc. This determines which service will handle the request.
modelstringChoose 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.
streambooleanDetermine if the data should be streamed in parts or not. Iftrue, the response will be streamed in real-time as it's generated. Iffalse, the response will be sent all at once.
temperaturenumberSet 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.
webSearchbooleanEnable or disable web search functionality. Iftrue, the system can perform web searches to gather real-time information. Iffalse, it relies solely on pre-existing data.
codeModelModebooleanEnable or disable the code model mode. Iftrue, the system will use a model optimized for understanding and generating code. Iffalse, it uses the general-purpose language model.
isChromeExtbooleanSpecify whether the system is being used as a Chrome extension. Iftrue, it indicates integration with Chrome, possibly affecting certain functionalities and permissions.

🚀 Chat Completion Providers

WebsiteProviderGPT-3.5GPT-4StreamStatus
AryahcrAryahcr✔️✔️✔️Active
BlackBoxBlackBoxActive
NextwayNextway✔️✔️✔️Active
ChromeChrome✔️Active
OllamaOllama✔️Active
AlibabaAlibaba✔️✔️Active
ChatBotRuChatBotRu✔️✔️Inactive

📚 Chat Completion Models

ModelProviders that support it
All fromOllama libaryOllama
BlackBoxBlackBox

📷 Image Generation

📹 Example usage

constoptions={provider:"DALLE2",};(async()=>{constprovider=GPT4js.createProvider(options.provider);try{constbase64=awaitprovider.imageGeneration("wood",options);console.log(base64);}catch(error){console.error("Error:",error);}})();

With theimageGeneration 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

OptionTypeDescription
negativePromptstringIndicates the direction not to take in production.
heightnumberSpecifies the image height.
widthnumberSpecifies the image width.
samplingStepsnumberSpecifies the number of iterations. A higher number results in higher quality.
samplingMethodstringSelects a sampling method to control the diversity, quality, and coherence of images.
cfgScalenumberSpecifies the Classifier-Free Guidance to control how closely the generated image adheres to the given text prompt.

🧮 Number Type Options

ProviderSupported 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

ProviderStatusDefault Style
Dalle2ActiveSemi-realistic, detailed with vivid colors and natural lighting.
StableDiffusionActivePhotorealistic, 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

  1. chrome://flags/#prompt-api-for-gemini-nano Select 'Enabled'

  2. chrome://flags/#optimization-guide-on-device-model Select 'Enabled BypassPrefRequirement'

  3. 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

constmessages=[{role:"user",content:"hi!"}];constoptions={provider:"Chrome",};(async()=>{constprovider=GPT4js.createProvider(options.provider);try{consttext=awaitprovider.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 onGitHub. Additionally, if you encounter any errors that hinder your use of any project functionality, pleasereport them here. Your feedback helps our community access AI tools freely!


logo

Releases

No releases published

Sponsor this project

    Packages

    No packages published

    [8]ページ先頭

    ©2009-2025 Movatter.jp