Coder Social home page Coder Social logo

rice668 / chatgpt-java Goto Github PK

View Code? Open in Web Editor NEW

This project forked from acaisoftware/chatgpt-java

0.0 1.0 1.0 10.58 MB

Lightweight Java library for OpenAI's ChatGPT using the reverse-engineered API.

Home Page: https://acai.gg

Java 100.00%

chatgpt-java's Introduction

ChatGPT-Java

A Java wrapper for the unofficial reverse-engineered ChatGPT API.

Important Update

OpenAI have recently implemented Cloudflare's Under Attack mode on ChatGPT, meaning you will need to supply a valid cf_clearance cookie and user agent to use the API.

This may be temporary, however we do not know at the moment.

Refer to the Registering ChatGPT section for more information on how to set these values.

Add ChatGPT-Java to your own build

Maven

<repository>
  <id>chatgpt-java</id>
  <url>https://raw.github.com/AcaiSoftware/chatgpt-java/repository/</url>
</repository>

<dependency>
  <groupId>gg.acai</groupId>
  <artifactId>chatgpt-java</artifactId>
  <version>1.0.2</version>
</dependency>

Usage Examples

Registering ChatGPT

All builder fields are optional except for sessionToken.

ChatGPT chatGpt = ChatGPT.newBuilder()
  .sessionToken("token_here") // required field: get from cookies
  .cfClearance("cf_clearance_here") // required to bypass Cloudflare: get from cookies
  .userAgent("user_agent_here") // required to bypass Cloudflare: google 'what is my user agent'
  .addExceptionAttribute(new ParsedExceptionEntry("exception keyword", Exception.class)) // optional: adds an exception attribute
  .connectTimeout(60L) // optional: specify custom connection timeout limit
  .readTimeout(30L) // optional: specify custom read timeout limit
  .writeTimeout(30L) // optional: specify custom write timeout limit
  .build(); // builds the ChatGPT client

Session Token

Not required, the ChatGPT client checks & verifies the session token upon the build procedure. The client will throw a TokenExpiredException if the token has expired.

 chatGpt.getComplexAccessCache().refreshAccessToken() // refreshing cache and verifies session token
    .whenComplete((accessToken) -> { // called when the promise is completed, not required
        System.out.println("Access token: " + accessToken);
    });

ChatGPT Conversation

Supports both asynchronous & synchronous handling.

Create a conversation with a promise completing the response:

Conversation conversation = chatGpt.createConversation();
conversation.sendMessageAsync("Hello!")
    .whenComplete((response) -> { // called when the promise is completed with its response
        System.out.println("Response: " + response.getMessage());
    });

Create a conversation with an event stream listener:

Conversation streamConversation = chatGpt.createStreamConversation(new StreamResponseListener() {
    @Override
    public void onResponse(StreamResponse response) {
        System.out.println(response.getMessage()); // the response from the event stream
    }
});
streamConversation.sendMessageAsync("Hello!"); // does not support promise callbacks

NOTE: A Stream Conversation does not support promise callbacks with its response

Documentation

// Link to docs

Features

  • Functional Style
  • Sending messages with responses
  • Event Stream Response
  • Asynchronous & Synchronous methods
  • Token Access Cache
  • Conversations
  • Simple Builders
  • Easy to use, but powerful
  • Optimal performance

Important Notes

Because this is developed using the unofficial reverse-engineered API, be aware that it could break at any time. However, we will work to fix any issues as they may arise.

Contributing

Contributions are highly appreciated! If you feel your pull request is useful, go ahead! Before creating a pull request, make sure your changes work as they should and give a description of what it provides.

Authors

Kaiser Clouke

chatgpt-java's People

Contributors

clouke avatar kaiserbloo avatar jonathanhallstrom avatar

Watchers

 avatar

Forkers

osscm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.