|
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(); |
|
} |
|
} |
|
|