Troubles using

#103
by Goncalocp - opened

Im trying to use the API on my java code, but when i try a simple test, the response is html code. something is wrong, maybe with the URL? i dont know, can someone help please? Heres the portion of the code:

private static final String API_URL = "https://api.together.ai/models/meta-llama/Llama-3.3-70B-Instruct-Turbo-Free";

public static String getLLMSuggestions (String prompt) throws IOException {

    // Corpo do JSON
    String jsonBody = "{"
            + "\"prompt\": \"" + prompt + "\","
            + "\"max_tokens\": 500"
            + "}";

    // Define o tipo de dados
    MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    // Cria o corpo do request
    RequestBody body = RequestBody.create(jsonBody, JSON);

    // configura o request HTTP
    Request request = new Request.Builder()
            .url(API_URL)
            .post(body)
            .addHeader("Content-Type", "application/json")
            .build();

    // Envia o request e processa a resposta
    OkHttpClient client = new OkHttpClient();
    try (Response response = client.newCall(request).execute()) {
        if (response.isSuccessful() && response.body() != null){
            return response.body().string();
        } else {
            throw  new IOException("Request error" + response.code());
        }
    }
}

public static void main(String[] args){
    try {
        String prompt = "Write a Java function that orders a list of ints";
        String response = getLLMSuggestions(prompt);
        System.out.println("LLM's response:\n" + response);
    } catch (IOException e){
        e.printStackTrace();
    }
}
Your need to confirm your account before you can post a new comment.

Sign up or log in to comment