#was wondering if anyone can take a look

38 messages · Page 1 of 1 (latest)

loud ermine
#

Are you using the right endpoint?

graceful slate
#

i have tried davinci endpoint also

#

and codex

loud ermine
#

have you tried https://api.openai.com/v1/completions

graceful slate
#

sec i will try

loud ermine
#

then adding the model in the promt?

#

model: "text-davinci-003"

#

for example

{
  model: "text-davinci-003",
  prompt: "Say this is a test",
  max_tokens: 7,
  temperature: 0,
}
graceful slate
#

okay so i built a GUI, that you enter your API key into, then it will bring up a secondary GUI if the api key passes that allows you to enter your text question and prompt question

#

let me try that

loud ermine
#

try printing the propt as well, might be some issues with how it generates the json?

graceful slate
#

so I built some IOexception handlers

#

and when i print the prompt or use:

{
model: "text-davinci-003",
prompt: "Say this is a test",
max_tokens: 7,
temperature: 0,
}

#

i get error 400 invalid exception from the api

loud ermine
#

let me try run the same query

#

same prompt as you gives me

#

is the issue how you send json in the first place?

graceful slate
#

im not using curl

loud ermine
#

im not too familiar with Java at the moment

#

I did the same with NodeJS

graceful slate
#

okay

#

so here is my method to call the API

#

public class GPT {

private String apiKey;

public GPT(String apiKey) {
    this.apiKey = apiKey;
}

public String generateText(String prompt) throws IOException {
    OkHttpClient client = new OkHttpClient().newBuilder()
            .build();
    MediaType mediaType = MediaType.parse("application/json");
    RequestBody body = RequestBody.create("{\"prompt\":\"" + prompt + "\"}",mediaType);
    Request request = new Request.Builder()
            .url("https://api.openai.com/v1/completions")
            .addHeader("Content-Type", "application/json")
            .addHeader("Authorization", "Bearer " + apiKey)
            .build();
    

    try (Response response = client.newCall(request).execute()) {
        if (response.code() == 401) {
            throw new IOException("Unauthorized: Invalid API key");
        }
        if (response.code() == 429) {
            throw new IOException("Too Many Requests: Usage limits reached");
        }
        if (response.code() != 200) {
            throw new IOException("Unexpected response code: " + response.code());
        }
        return response.body().string();
    }
}

}

#

this is my main method for the gui/input for question/completion:

public class Main {
private JFrame frame;
private JTextField inputField;
private JTextField outputField;
private JButton button;
private GPT gpt;

public Main() {
    frame = new JFrame("Ask GPT");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 200);
    frame.setLocationRelativeTo(null);

    inputField = new JTextField();
    inputField.setBounds(20, 20, 300, 25);
    frame.add(inputField);

    button = new JButton("Ask");
    button.setBounds(320, 20, 60, 25);
    frame.add(button);

    outputField = new JTextField();
    outputField.setBounds(20, 60, 360, 100);
    outputField.setEditable(false);
    frame.add(outputField);

    String apiKey = JOptionPane.showInputDialog(frame, "Enter your api key:");

    if (apiKey != null && !apiKey.isEmpty()) {
        gpt = new GPT(apiKey);
    }

    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String question = inputField.getText();
            try {
                String response = gpt.generateText(question);
                JsonObject json = JsonParser.parseString(response).getAsJsonObject();
                String output = json.get("choices").getAsJsonArray().get(0).getAsJsonObject().get("text").getAsString();
                outputField.setText(output);
            } catch (IOException ex) {
                System.out.println("Error: " + ex.getMessage());
            }
        }
    });

    frame.setLayout(null);
    frame.setVisible(true);
}

public static void main(String[] args) {
    Main main = new Main();
}

}

#

i think something in my try/catch statement may be off for string response and how the generateText is used?

loud ermine
#

Sorry, the Bot timed me out for 5 mins

#

Have you tried to validate the request you are sending? Make sure its actually a valid json you are sending

graceful slate
#

😦

loud ermine
#

Super strange, if you try using a request that isen't working and testing the same request using CURL does it work then?

graceful slate
loud ermine
graceful slate
#

okay, ill read up on this

loud ermine
#
curl https://api.openai.com/v1/completions \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer YOUR_API_KEY' \
  -d '{
  "model": "text-davinci-003",
  "prompt": "Say this is a test",
  "max_tokens": 7,
  "temperature": 0
}'
graceful slate
#

im new to java so i dont really understand the execution of curl commands, but ill figure it out

loud ermine
#

CURL is a terminal command, you can pretty much copy paste into a termial, just swap the YOUR_API_KEY with your key