Created
January 11, 2022 10:08
-
-
Save azazar/e1bc87468db5ed96babe4bb2395dbdba to your computer and use it in GitHub Desktop.
Vicgalle.net GPT-3 Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.OutputStream; | |
| import java.net.HttpURLConnection; | |
| import java.net.URL; | |
| import com.google.gson.JsonObject; | |
| import com.google.gson.JsonParser; | |
| import org.apache.commons.io.IOUtils; | |
| import static java.net.URLEncoder.encode; | |
| import static java.nio.charset.StandardCharsets.UTF_8; | |
| public class GPT3Api { | |
| public String generate(String context, int maxTokenLength, float temperature, float topP, String stopSequence) throws IOException { | |
| URL url = new URL("http://api.vicgalle.net:5000/generate"); | |
| HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
| conn.setConnectTimeout(30000); | |
| conn.setReadTimeout(60000); | |
| conn.setDoInput(true); | |
| conn.setDoOutput(true); | |
| try (OutputStream out = conn.getOutputStream()) { | |
| out.write(("context=" + encode(context, UTF_8) + "&token_max_length=" + maxTokenLength + "&temperature=" + temperature + "&top_p=" + topP + "&stop_sequence=" + encode(stopSequence, UTF_8)).getBytes(UTF_8)); | |
| } | |
| String responseJson; | |
| try (InputStream in = conn.getInputStream()) { | |
| responseJson = IOUtils.toString(in, UTF_8); | |
| } | |
| JsonObject response = new JsonParser().parse(responseJson).getAsJsonObject(); | |
| return response.get("text").getAsString(); | |
| } | |
| public String generate(String context, int maxTokenLength, String stopSequence) throws IOException { | |
| return generate(context, maxTokenLength, 1f, 0.9f, stopSequence); | |
| } | |
| public String generate(String context, String stopSequence) throws IOException { | |
| return generate(context, 512, stopSequence); | |
| } | |
| public String generate(String context) throws IOException { | |
| return generate(context, ""); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment