-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatBot.java
More file actions
116 lines (94 loc) · 4.26 KB
/
ChatBot.java
File metadata and controls
116 lines (94 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.io.*;
import java.util.*;
import java.nio.file.*;
import java.nio.charset.StandardCharsets;
public class ChatBot {
private static String CHAT_HISTORY_FILE = "chat_history.txt";
private static String BOT_MODEL_CMD = "./your-bot-model/chat";
public static List<String> loadChatHistory() {
if (!Files.exists(Paths.get(CHAT_HISTORY_FILE))) {
return new ArrayList<>();
}
try {
return Files.readAllLines(Paths.get(CHAT_HISTORY_FILE), StandardCharsets.UTF_8);
} catch (IOException e) {
System.err.println("Error loading history: " + e.getMessage());
return new ArrayList<>();
}
}
public static void saveChatHistory(List<String> chatHistory) {
try {
Files.write(Paths.get(CHAT_HISTORY_FILE), chatHistory, StandardCharsets.UTF_8);
} catch (IOException e) {
System.err.println("Error saving history: " + e.getMessage());
}
}
public static String callBotModel(String prompt) {
try {
// Split command by space to get executable and initial args.
// This is a simple implementation and might not handle quoted arguments in the command string correctly.
List<String> command = new ArrayList<>();
String[] parts = BOT_MODEL_CMD.trim().split("\\s+");
Collections.addAll(command, parts);
command.add(prompt);
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(true); // Merge stderr into stdout
Process process = pb.start();
StringBuilder output = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append(System.lineSeparator());
}
}
int exitCode = process.waitFor();
if (exitCode != 0) {
// If the process failed but produced output (which we captured), we still return it,
// but usually stderr is more useful here. Since we merged streams, output contains error too.
}
return output.toString().trim();
} catch (IOException | InterruptedException e) {
System.err.println("Error calling bot model: " + e.getMessage());
return "";
}
}
public static void main(String[] args) {
if (args.length > 0) {
BOT_MODEL_CMD = args[0];
}
if (args.length > 1) {
CHAT_HISTORY_FILE = args[1];
}
System.out.println("Using model command: " + BOT_MODEL_CMD);
System.out.println("Using history file: " + CHAT_HISTORY_FILE);
List<String> chatHistory = loadChatHistory();
System.out.println("Loaded " + chatHistory.size() + " lines of history.");
System.out.println("Type 'quit' or 'exit' to end the session.");
try (Scanner scanner = new Scanner(System.in)) {
while (true) {
System.out.print("You: ");
if (!scanner.hasNextLine()) break;
String userInput = scanner.nextLine();
if (userInput.equalsIgnoreCase("quit") || userInput.equalsIgnoreCase("exit")) {
break;
}
if (userInput.trim().isEmpty()) continue;
chatHistory.add("You: " + userInput);
StringBuilder promptBuilder = new StringBuilder();
int startIdx = Math.max(0, chatHistory.size() - 1000);
for (int i = startIdx; i < chatHistory.size(); i++) {
promptBuilder.append(chatHistory.get(i)).append("\n");
}
String prompt = promptBuilder.toString();
String aiResponse = callBotModel(prompt);
if (!aiResponse.isEmpty()) {
System.out.println("Bot: " + aiResponse);
chatHistory.add("Bot: " + aiResponse);
saveChatHistory(chatHistory);
} else {
System.out.println("Bot: [No response]");
}
}
}
}
}