Created
April 18, 2023 12:16
-
-
Save punkmonday/407eb5ffbfa98f476c40a5e29a657083 to your computer and use it in GitHub Desktop.
chatgpt写的chatgpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.entity.StringEntity; | |
import org.apache.http.impl.client.HttpClientBuilder; | |
import org.apache.http.util.EntityUtils; | |
import org.json.JSONObject; | |
public class ChatGPT { | |
// Hugging Face API URL | |
private static final String HF_API_URL = "https://api-inference.huggingface.co/models/..."; | |
// Hugging Face API Token | |
private static final String HF_API_TOKEN = "..."; | |
public static void main(String[] args) throws Exception { | |
// Input text to send to ChatGPT | |
String inputText = "Hello, how are you?"; | |
// Send a request to Hugging Face API | |
HttpClient httpClient = HttpClientBuilder.create().build(); | |
HttpPost postRequest = new HttpPost(HF_API_URL); | |
postRequest.setHeader("Authorization", "Bearer " + HF_API_TOKEN); | |
postRequest.setHeader("Content-type", "application/json"); | |
JSONObject requestBody = new JSONObject().put("inputs", inputText); | |
postRequest.setEntity(new StringEntity(requestBody.toString())); | |
String response = EntityUtils.toString(httpClient.execute(postRequest).getEntity()); | |
// Parse the response and extract ChatGPT's output text | |
JSONObject responseJson = new JSONObject(response); | |
String outputText = responseJson.getJSONArray("generated_text").getString(0); | |
// Display the output text in the console | |
System.out.println(outputText); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment