-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
250 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
45 changes: 45 additions & 0 deletions
45
src/main/java/link/locutus/discord/gpt/imps/GPTText2Text.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package link.locutus.discord.gpt.imps; | ||
|
||
import com.knuddels.jtokkit.api.Encoding; | ||
import com.knuddels.jtokkit.api.EncodingRegistry; | ||
import com.knuddels.jtokkit.api.ModelType; | ||
import com.theokanning.openai.OpenAiService; | ||
import com.theokanning.openai.completion.CompletionChoice; | ||
import com.theokanning.openai.completion.CompletionRequest; | ||
import com.theokanning.openai.completion.CompletionResult; | ||
import link.locutus.discord.gpt.GPTUtil; | ||
import link.locutus.discord.gpt.IEmbeddingDatabase; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class GPTText2Text implements IText2Text{ | ||
private final EncodingRegistry registry; | ||
private final OpenAiService service; | ||
private final Encoding chatEncoder; | ||
private final ModelType model; | ||
private final IEmbeddingDatabase embeddings; | ||
|
||
public GPTText2Text(EncodingRegistry registry, OpenAiService service, IEmbeddingDatabase embeddings) { | ||
this.registry = registry; | ||
this.service = service; | ||
this.model = ModelType.GPT_3_5_TURBO; | ||
this.chatEncoder = registry.getEncodingForModel(model); | ||
this.embeddings = embeddings; | ||
} | ||
|
||
@Override | ||
public String generate(String text) { | ||
CompletionRequest completionRequest = CompletionRequest.builder() | ||
.prompt(text) | ||
.model(this.model.getName()) | ||
.echo(false) | ||
.build(); | ||
CompletionResult completion = service.createCompletion(completionRequest); | ||
List<String> results = new ArrayList<>(); | ||
for (CompletionChoice choice : completion.getChoices()) { | ||
results.add(choice.getText()); | ||
} | ||
return String.join("\n", results); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package link.locutus.discord.gpt.imps; | ||
|
||
public interface IText2Text { | ||
String generate(String text); | ||
} |
This file contains 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
55 changes: 55 additions & 0 deletions
55
src/main/java/link/locutus/discord/gpt/imps/ProcessText2Text.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package link.locutus.discord.gpt.imps; | ||
|
||
import com.knuddels.jtokkit.api.Encoding; | ||
import com.knuddels.jtokkit.api.EncodingRegistry; | ||
import com.knuddels.jtokkit.api.ModelType; | ||
import com.theokanning.openai.OpenAiService; | ||
import com.theokanning.openai.completion.CompletionChoice; | ||
import com.theokanning.openai.completion.CompletionRequest; | ||
import com.theokanning.openai.completion.CompletionResult; | ||
import link.locutus.discord.gpt.IEmbeddingDatabase; | ||
import link.locutus.discord.util.StringMan; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.Base64; | ||
import java.util.List; | ||
|
||
public class ProcessText2Text implements IText2Text{ | ||
private final File file; | ||
private final File venvExe; | ||
|
||
public ProcessText2Text(File venvExe, File file) { | ||
this.venvExe = venvExe; | ||
this.file = file; | ||
} | ||
|
||
@Override | ||
public String generate(String text) { | ||
String encodedString = Base64.getEncoder().encodeToString(text.getBytes()); | ||
List<String> lines = new ArrayList<>(); | ||
String command = venvExe == null ? "python" : venvExe.getAbsolutePath(); | ||
ProcessBuilder pb = new ProcessBuilder(command, file.getAbsolutePath(), encodedString).redirectErrorStream(true); | ||
try { | ||
Process p = pb.start(); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
lines.add(line); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
String result = StringMan.join(lines, "\n"); | ||
if (result.contains("result:")) { | ||
result = result.substring(result.indexOf("result:") + 7); | ||
return result; | ||
} else { | ||
System.err.println(result); | ||
throw new IllegalArgumentException("Unknown process result (see console)"); | ||
} | ||
} | ||
} |
Oops, something went wrong.