File size: 1,321 Bytes
1c90314 |
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 |
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpRequest.BodyPublishers;
public class MistralChatbot {
private static final String API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1";
private static final String API_TOKEN = "YOUR_HF_API_TOKEN";
public static void main(String[] args) throws Exception {
String prompt = "Explain the difference between Java and Python.";
String response = sendPromptToModel(prompt);
System.out.println("AI Response:\n" + response);
}
public static String sendPromptToModel(String prompt) throws Exception {
HttpClient client = HttpClient.newHttpClient();
String requestBody = "{ \"inputs\": \"" + prompt.replace("\"", "\\\"") + "\" }";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(API_URL))
.header("Authorization", "Bearer " + API_TOKEN)
.header("Content-Type", "application/json")
.POST(BodyPublishers.ofString(requestBody))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.body();
}
}
|