Skip to content

Commit

Permalink
Code clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
m0ver committed Apr 8, 2024
1 parent 50ad766 commit 086910a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 23 deletions.
20 changes: 7 additions & 13 deletions src/main/java/custom/application/v1/smalltalk.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import org.tinystruct.system.util.Matrix;
import org.tinystruct.transfer.DistributedMessageQueue;

import jakarta.activation.MimetypesFileTypeMap;

import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.charset.StandardCharsets;
Expand All @@ -40,7 +38,7 @@ public class smalltalk extends DistributedMessageQueue implements SessionListene

public static final String CHAT_GPT = "ChatGPT";
private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-M-d h:m:s");
private boolean cli_mode;
private boolean cliMode;
private boolean chatGPT;

public void init() {
Expand All @@ -49,7 +47,6 @@ public void init() {
this.setVariable("message", "");
this.setVariable("topic", "");

// Set env with LANG=en_US.UTF-8
System.setProperty("LANG", "en_US.UTF-8");

SessionManager.getInstance().addListener(this);
Expand Down Expand Up @@ -262,7 +259,7 @@ public void run() {

@Action("chat")
public void chat() {
this.cli_mode = true;
this.cliMode = true;
if (this.config.get("openai.api_key") == null || this.config.get("openai.api_key").isEmpty()) {
String url = "https://platform.openai.com/account/api-keys";

Expand Down Expand Up @@ -294,12 +291,11 @@ public void chat() {
String input = scanner.nextLine();

if (input.equals("exit")) {
System.out.println("Exiting...");
System.exit(-1);
System.out.println("Bye!");
break;
} else {
try {
if (input.trim().length() > 0) {
if (!input.trim().isEmpty()) {
String message = this.chat(sessionId, input.replaceAll("\n", " ") + "\n");
System.out.print(String.format("%s %s >: ", format.format(new Date()), CHAT_GPT));
message = message.replaceAll("\\\\n", "\n").replaceAll("\\\\\"", "\"");
Expand All @@ -326,6 +322,7 @@ public void chat() {
}

scanner.close();
System.exit(-1);
}

private String chat(String sessionId, String message) throws ApplicationException {
Expand All @@ -345,16 +342,13 @@ private String chatGPT(String sessionId, String message, String image) throws Ap
// Replace YOUR_API_KEY with your actual API key
String API_URL = this.config.get("openai.api_endpoint") + "/v1/chat/completions";

if (!cli_mode)
if (!cliMode)
message = message.replaceAll("<br>|<br />", "");

// Try to get some information from internet
String payload = "{\n" +
" \"model\": \"gpt-3.5-turbo\"}";

// " \"messages\":[\n" +
// " {\"role\": \"system\", \"content\": \"I am an AI assistant specialized in IT. If you enter any Linux command, I will execute it and display the result as you would see in a terminal. I can also engage in normal conversations but will consider the context of the conversation to provide the best answers. If you ask me a question that I am not knowledgeable enough to answer, I will ask if you have any reference content, you can provide the content or a url can be referenced. If you provide an URL to me, I will output the url strictly to you as I'm not able to access the internet. However, I don't have the capability to create images, so I will redirect such requests to image-generation APIs. If you want to generate an image, please provide clear and concise instructions, and I will use the OpenAI API and strictly follow the instructions below as I do not have the capability. so if it's about to create images, I'll output the OpenAI api in response simply: https://api.openai.com/v1/images/generations. If it's about image edit, then simply to output: https://api.openai.com/v1/images/edits. and if it's about image variations, then output the api simply: https://api.openai.com/v1/images/variations.\n\"},\n";

Builder payloadBuilder = new Builder();
payloadBuilder.parse(payload);
Builders messages = new Builders();
Expand Down Expand Up @@ -438,7 +432,7 @@ private String chat(String sessionId, String message, String image) throws Appli
// Replace YOUR_API_KEY with your actual API key
String API_URL = this.config.get("openai.api_endpoint") + "/v1/completions";

if (!cli_mode)
if (!cliMode)
message = message.replaceAll("<br>|<br />", "");

String payload = "{\n" +
Expand Down
10 changes: 0 additions & 10 deletions src/main/resources/themes/hello.view

This file was deleted.

0 comments on commit 086910a

Please sign in to comment.