#OpenAi
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
You can use </chatgpt:1108714622413963314> to ask ChatGPT about your question while you wait for a human to respond.
Please don't ask to ask, nor only say hello, just ask your actual question right away, thanks 
If you want to read about the details of why this is better, check out:
https://dontasktoask.com/
what do u need help on
package org.mycompany;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
public class OpenAIAPIClient {
public static void main(String[] args) {
try {
URL url = new URL("https://api.openai.com/v1/engines/davinci-codex/completions");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Bearer -------");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
// Gönderilecek veriyi oluştur
String inputJson = "{\"prompt\": \"Once upon a time\", \"max_tokens\": 100}";
try (OutputStream os = conn.getOutputStream()) {
byte[] input = inputJson.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
// Yanıtı oku
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}```
yeah and?
it is give IOException
java.io.FileNotFoundException: https://api.openai.com/v1/engines/davinci-codex/completions
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1994)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1599)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:223)
at org.mycompany.OpenAIAPIClient.main(OpenAIAPIClient.java:28)```
Detected code, here are some useful tools:
java.io.FileNotFoundException : https : //api.openai.com/v1/engines/davinci-codex/completions
at java.base / sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java : 1994) at java.base / sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java : 1599) at java.base / sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java : 223) at org.mycompany.OpenAIAPIClient.main(OpenAIAPIClient.java : 28)
Out of curiousity, did you ask chatGPT to generate you that code?
yes
bro knew
also isnt new URL dprecated
That's one "interesting" way to try to integrate with openAI.
And you want new URI("http://baeldung.com/a-guide-to-java-sockets").toURL().
what about URI#create?
well seems like they are the same
This convenience factory method works as if by invoking the URI(String) constructor; any URISyntaxException thrown by the constructor is caught and wrapped in a new IllegalArgumentException object, which is then thrown.
You can also use java.net.http.HttpClient
Before trying to integrate with openAI I would recommend first reading up a bit more on how to set up those connections. As it stands that code really is not the way to go.
I fixed the uri now it returns 403
or some openai java pie
check ur bearer token
?
403 = forbidden
They don't pass one in conn.setRequestProperty("Authorization", "Bearer -------");
------- = my api
Which is seemingly not valid.
I just created the API
cna you maybe share the full 403 error
java.io.IOException: Server returned HTTP response code: 403 for URL: https://platform.openai.com/api-keys
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1998)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1599)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:223)
at org.mycompany.OpenAIAPIClient.main(OpenAIAPIClient.java:28)
yeah but you didnt use it in the code
?
What?
formulate a full sentence
first which are you trying to use:
https://platform.openai.com/docs/api-reference
First of all, I wanted to check if I would get an error, but if I continue with the code, the number of errors will most likely increase
java.io.IOException: Server returned HTTP response code: 403 for URL: https://platform.openai.com/docs/api-reference
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1998)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1599)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:223)
at org.mycompany.OpenAIAPIClient.main(OpenAIAPIClient.java:28)```
thats not what I meant...
open the page
you are trying to use the api right?
what kind of endpoint do you want to use?
i open
yes
open the page with your browser
Opened
.
I want to move the questions and answers to the JSON file
Just answer the questions I ask
you mean you want to use chatgpt?
i trying to make chatgpt
Okay, so how can I do what I said?
java.io.IOException: Server returned HTTP response code: 403 for URL: https://platform.openai.com/docs/api-reference/chat
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1998)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1599)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:223)
at org.mycompany.OpenAIAPIClient.main(OpenAIAPIClient.java:28)
opened
its the documentation on how to use the api
read it
then apply these in your code
i cant translate
I can't read the page because I can't do it in Turkish :/
because I can't do it in Turkish
it is english?
yes
well and how are we supposed to help you with that then?
idk
i don’t think there’s an official page of that in turkish
there shouldnt be one imo, the api json will still be english
and english is kinda a requirement for programming
I translate it into Turkish with Java
it will cause issues if u use google translate maybe
but u can try
some technical stuff can’t be translated
I know I'm at A2 level in English
this is good practice to learn APIs if you want to do it manually, otherwise use a java library for openai api