ChatGPT API in JAVA | (simple & easy)

preview_player
Показать описание
ChatGPT API in JAVA | (simple & easy)
In today's video, I will be showing you how to use ChatGPT's API in Java.

► Code Used:
● Check the Description.

► ChatGPT API KEY:

► Software Used:

► Helpful Learning Sites:

► Song Used:

► Support or Follow me:
Рекомендации по теме
Комментарии
Автор

(updated june 2023)

API KEY: no longer works! get your own! link in desc!

Code Used:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {

public static void main(String[] args) {
System.out.println(chatGPT("hello, how are you?"));
// Prints out a response to the question.
}

public static String chatGPT(String message) {
String apiKey = "KEY GOES // API key goes here
String model = "gpt-3.5-turbo"; // current model of chatgpt api

try {
// Create the HTTP POST request
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Authorization", "Bearer " + apiKey);
con.setRequestProperty("Content-Type", "application/json");

// Build the request body
String body = "{\"model\": \"" + model + "\", \"messages\": [{\"role\": \"user\", \"content\": \"" + message + "\"}]}";
con.setDoOutput(true);
OutputStreamWriter writer = new
writer.write(body);
writer.flush();
writer.close();

// Get the response
BufferedReader in = new BufferedReader(new
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

// returns the extracted contents of the response.
return

} catch (IOException e) {
throw new RuntimeException(e);
}
}

// This method extracts the response expected from chatgpt and returns it.
public static String response) {
int startMarker = // Marker for where the content starts.
int endMarker = response.indexOf("\"", startMarker); // Marker for where the content ends.
return response.substring(startMarker, endMarker); // Returns the substring containing only the response.
}
}

mintype
Автор

I get an "Server returned HTTP response code: 429" Error. I believer the BufferedReader in is what's causing the issue. Any ideas on what might be going wrong?

mathsbigsecrets
Автор

Could you please make a video explaining how to use Ollama API with java?
Ollama is a command-line based program that runs locally and runs models like llama2, llama3, etc...

BrennerEraFan
Автор

Hey, how could I get the Java codes? You mentioned that check description, but I failed to find it...

pubmcjw
Автор

Yes I use my api key but it why the link cgatgpt link is not working

hosteldays
Автор

bro i have getting 3 errors near regex: beginIndex: it's saying : is not corroct in vs code

TrendingView
Автор

hello bro can u plz make more video's like this only diffrent i love this type of consept's

TrendingView
Автор

Requires GPT-4, because when reading an error message after asking, getting insufficient credits

giveaway
Автор

This doesn't work for me. Did open ai change anything?

samshasti
Автор

when I ask for a java program it is not giving the complete output. Could you help me in this?

debalinaghosh
Автор

I don't know why it always refuses the connection when i try to execute it

PokeMichele
Автор

I think API keys are expired now, so we must pay to run the code?
Plz. reply me as fast as you can, i must finish my project in next two weeks

txqdfsh
Автор

i got server 429 error for first time why is it happening

hudashaikh
Автор

I can't get the code to work. Error 429. I have created another user with another key

joseangelnavarro
join shbcf.ru