-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcAI.java
More file actions
53 lines (47 loc) · 1.92 KB
/
cAI.java
File metadata and controls
53 lines (47 loc) · 1.92 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
import java.io.*;
import java.util.Scanner;
import org.jsoup.Jsoup;
import com.google.genai.Client; // Ensure you have the Gemini SDK JAR
public class AIChatBot {
private static final String FILE_PATH = "logs.txt";
private static final String API_KEY = "YOUR_GEMINI_API_KEY"; // Replace this!
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("AI Online. Commands: 'read [url]' or just ask a question.");
while (true) {
System.out.print("\nYou: ");
String input = scanner.nextLine().trim();
if (input.equalsIgnoreCase("exit")) break;
if (input.startsWith("read ")) {
// Feature: Read a website
String url = input.substring(5);
System.out.println("Reading " + url + "...");
String webContent = scrapeWebsite(url);
String aiResponse = askAI("Summarize this: " + webContent);
System.out.println("AI Summary: " + aiResponse);
} else {
// Feature: Normal Chat
String aiResponse = askAI(input);
System.out.println("AI: " + aiResponse);
}
}
scanner.close();
}
public static String scrapeWebsite(String url) {
try {
return Jsoup.connect(url).get().text().substring(0, 2000); // Limit text for AI
} catch (Exception e) {
return "Error reading site: " + e.getMessage();
}
}
public static String askAI(String prompt) {
try {
Client client = new Client();
// In a real setup, you'd use: client.setApiKey(API_KEY);
var response = client.models.generateContent("gemini-1.5-flash", prompt, null);
return response.text();
} catch (Exception e) {
return "AI Error: Check your API key or connection.";
}
}
}