Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyUltra committed Mar 6, 2023
0 parents commit a52aa69
Show file tree
Hide file tree
Showing 5 changed files with 372 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) [2023] [Filip Zeman]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# TheChatGPT
![badge](https://img.shields.io/github/v/release/Fly_Ultra/TheChatGPT)
[![badge](https://jitpack.io/v/Fly_Ultra/TheChatGPT.svg)](https://jitpack.io/#Fly_Ultra/TheChatGPT)
![badge](https://img.shields.io/github/downloads/Fly_Ultra/TheChatGPT/total)
![badge](https://img.shields.io/github/last-commit/Fly_Ultra/TheChatGPT)
![badge](https://img.shields.io/badge/platform-spigot-lightgrey)
[![badge](https://img.shields.io/discord/896466173166747650?label=discord)](https://discord.gg/2PpdrfxhD4)
[![badge](https://img.shields.io/github/license/Fly_Ultra/TheChatGPT)](https://github.com/Fly_Ultra/TheChatGPT/blob/master/LICENSE.txt)

Very small API only about getting answer from ChatGPT from OpenAI!<br>
By using [**OpenAI-GPT3**](https://github.com/TheoKanning/openai-java)<br>
Only Spigot support!

## Table of contents

* [Getting started](#getting-started)
* [Depedencies](#dependencies)
* [Example](#example)
* [License](#license)

## Getting started

1. Add depedency into your maven/gradle
2. Create instance for ChatGPT
3. Use login, and register method
4. And have fun!

### Dependencies

Here you can see the configuration interface

<details>
<summary>Maven</summary>

```xml
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>

<dependency>
<groupId>com.github.Fly_Ultra</groupId>
<artifactId>TheChatGPT</artifactId>
<version>VERSION</version>
<scope>compile</scope>
</dependency>
```
</details>

<details>
<summary>Gradle</summary>

```gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.Fly_Ultra:TheChatGPT:VERSION'
}
```
</details>

### Example

```java
private ChatGPT chatGPT;

@Override
public void onEnable() {
instance = this;

// How to register ChatGPT
chatGPT = new ChatGPT(this);
chatGPT.login("your token", 0);
chatGPT.register("AI", "You", "text-davinci-003", 0.9D, 50, 1.0D, 0D, 0.6D);

}

// Getter for ChatGPT
public ChatGPT getChatGPT() {
return chatGPT;
}

// How to get answer from AI
getChatGPT().getAnswer(player, "How are you bro?");
```


## License
TheChatGPT is licensed under the permissive MIT license. Please see [`LICENSE.txt`](https://github.com/Fly_Ultra/TheChatGPT/blob/master/LICENSE.txt) for more information.
71 changes: 71 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>TheChatGPT</artifactId>
<version>1.0</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com.theokanning.openai-gpt3-java:service</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>maven-central</id>
<url>https://oss.sonatype.org/content/groups/public</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.theokanning.openai-gpt3-java</groupId>
<artifactId>service</artifactId>
<version>0.11.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.19-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
158 changes: 158 additions & 0 deletions src/main/java/cz/flyultra/api/ChatGPT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package cz.flyultra.api;

import com.theokanning.openai.OpenAiService;
import com.theokanning.openai.completion.CompletionRequest;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Arrays;
import java.util.HashMap;

public class ChatGPT {

private JavaPlugin plugin;

private OpenAiService service;

private String idForAI;
private String idForPlayer;

private String model;

private double temperature;
private int maxTokens;
private double topP;
private double frequency;
private double presence;

/**
*
* HashMap for save conversations
*
*/
private HashMap<String, String> conversations;

public ChatGPT(JavaPlugin plugin) {
this.plugin = plugin;
conversations = new HashMap<>();
}

/**
*
* Register method for specific parameters
*
*/
public void register(String idForAI, String idForPlayer, String model, double temperature, int maxTokens, double topP, double frequency, double presence) {
this.idForAI = idForAI;
this.idForPlayer = idForPlayer;
this.model = model;
this.temperature = temperature;
this.maxTokens = maxTokens;
this.topP = topP;
this.frequency = frequency;
this.presence = presence;
}

/**
*
* Login method for open connection
*
* @param token Dev API token from ChatGPT (OpenAI)
*/
public void login(String token, int timeout) {
service = new OpenAiService(token, timeout);
}

/**
*
* This create the space for answer by AI
*
*/
private void newLine(Player player, String message){
String conversation = getConversation(player.getName());

String context = conversation+"Human: " + message + "\nAI: ";
conversations.replace("Fly", context);
}

/**
*
* This save the AI response
*
*/
private void AIResponse(Player player, String AI) {
String conversation = getConversation(player.getName());
String context = conversation+ AI +"\n";
conversations.replace("Fly", context);
}

/**
*
* This method is the main part, here we can get answer from AI with conversation before
*
* @param player who asked
* @param message question
* @return answer from AI
*/
public String getAnswer(Player player, String message) {
if (!getConversations().containsKey(player.getName().toLowerCase())) {
createConversation(player);
}

newLine(player, message);
CompletionRequest AIRequest = CompletionRequest.builder()
.prompt(getConversation(player.getName()))
.model(model)
.temperature(temperature)
.maxTokens(maxTokens)
.topP(topP)
.frequencyPenalty(frequency)
.presencePenalty(presence)
.stop(Arrays.asList(idForPlayer+":", idForAI+":"))
.build();

String answerFromAI = service.createCompletion(AIRequest).getChoices().get(0).getText();
AIResponse(player, answerFromAI);
return answerFromAI;
}

/**
*
* This creates pattrn for conversation in hashmap
*
*/
public void createConversation(Player player) {
String playerName = player.getName().toLowerCase();
if (conversations.containsKey(playerName)) {
return;
}
conversations.put(playerName, "");
}

/**
*
* refresh conversation
*
*/
public void reCreateConversation(Player player) {
String playerName = player.getName().toLowerCase();
if (conversations.containsKey(playerName)) {
conversations.replace(playerName, "");
return;
}
createConversation(player);
}

private HashMap<String, String> getConversations() {
return conversations;
}

/**
*
* Get converastion by Player name
*
*/
public String getConversation(String name) {
return getConversations().get(name.toLowerCase());
}
}
28 changes: 28 additions & 0 deletions src/main/java/cz/flyultra/api/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cz.flyultra.api;

import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin {

private static Main instance;

private ChatGPT chatGPT;

@Override
public void onEnable() {
instance = this;

chatGPT = new ChatGPT(this);
chatGPT.login("your token", 0);
chatGPT.register("AI", "You", "text-davinci-003", 0.9D, 50, 1.0D, 0D, 0.6D);
}

@Override
public void onDisable() {

}

public ChatGPT getChatGPT() {
return chatGPT;
}
}

0 comments on commit a52aa69

Please sign in to comment.