Jump to content

ChatGPT enabled chatbot


Recommended Posts

This is a silly script just to see if I can make a chat bot in RS using this client.
I'm sure this can be improved upon. Feel free to let me know how!
I just started learning java recently and have made a few simple scripts, but wanted to see if I could put chatGPT into a script. Here it is: 

package scripts;

import org.jetbrains.annotations.NotNull;
import org.tribot.script.sdk.*;
import org.tribot.script.sdk.input.Keyboard;
import org.tribot.script.sdk.interfaces.PlayerMessageListener;
import org.tribot.script.sdk.query.PlayerQuery;
import org.tribot.script.sdk.script.TribotScript;
import org.tribot.script.sdk.script.TribotScriptManifest;
import org.tribot.script.sdk.query.Query;
import org.tribot.script.sdk.types.Player;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.Key;


@TribotScriptManifest(name = "GPT Test Script", author = "Me", category = "Practice", description = "GPT Testing")
public class GPTTestScript implements TribotScript {
	boolean talkingToSomeone;
	String lastMessage;

	private static final String API_URL = "https://api.openai.com/v1/chat/completions";

	String player;
	String message;
	PlayerMessageListener playerMessage = new PlayerMessageListener() {
		@Override
		public void onPlayerMessage(@NotNull String s, @NotNull String s1) {
			player = s;
			message = s1;

		}
	};

	@Override
	public void execute(final String args) {
		while (true) {
			MessageListening.addPlayerMessageListener(playerMessage);
			playerMessage.onPlayerMessage(player,message);
			if (!talkingToSomeone)
			{
				// can enable these to chat in the game and ask people to chat
				//Keyboard.typeString("Talk to me plz! Put '..' in the message to chat with me!");
				//Keyboard.pressEnter();
			}
				//looking for a message with ".." so we aren't responding to every message that we get. This can be configured
			if (message != null && message.contains("..") && message != lastMessage) {
				talkingToSomeone = true;
				Log.debug("Player: " + player);
				Log.debug("Sent message: " + message);
				Keyboard.typeString(player + " " + chatGPT("Respond with a few words" + message));
				Keyboard.pressEnter();
				//Log.debug("Response: " + chatGPT("Respond with a few words" + message));
				lastMessage = message;
			}
			else {
				talkingToSomeone = false;
				//Waiting.waitNormal(10000,1500);
			}
			Waiting.waitUniform(500,800);
		}
	}

	public String chatGPT(String message) {
		String url = "https://api.openai.com/v1/chat/completions";
		String apiKey = "APIKEYHERE"; // YOUR API key goes here
		String model = "gpt-3.5-turbo"; // current model of chatgpt api

		try {
			// Create the HTTP POST request
			URL obj = new URL(url);
			HttpURLConnection con = (HttpURLConnection) obj.openConnection();
			con.setRequestMethod("POST");
			con.setRequestProperty("Authorization", "Bearer " + apiKey);
			con.setRequestProperty("Content-Type", "application/json");

			// Build the request body
			String body = "{\"model\": \"" + model + "\", \"messages\": [{\"role\": \"user\", \"content\": \"" + message + "\"}]}";
			con.setDoOutput(true);
			OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
			writer.write(body);
			writer.flush();
			writer.close();

			// Get the response
			BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;
			StringBuffer response = new StringBuffer();
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();

			// returns the extracted contents of the response.
			return extractContentFromResponse(response.toString());

		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	// This method extracts the response expected from chatgpt and returns it.
	public String extractContentFromResponse(String response) {
		int startMarker = response.indexOf("content") + 11; // Marker for where the content starts.
		int endMarker = response.indexOf("\"", startMarker); // Marker for where the content ends.
		return response.substring(startMarker, endMarker); // Returns the substring containing only the response.
	}
}

 

Edited by Thecoldwolf52
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...