|
12 | 12 | public class OpenaiApplication { |
13 | 13 |
|
14 | 14 | private static String KEY = "sk-"; |
| 15 | + private static String PROMPT = "O que e o chat GPT?"; |
| 16 | + private static long MAX_TOKENS = 100; |
| 17 | + private static float TEMPERATURE = 1; |
| 18 | + private static String MODEL = "text-davinci-003"; |
15 | 19 |
|
16 | | - public static void main(String[] args) throws Exception{ |
| 20 | + |
| 21 | + public static void main(String[] args){ |
17 | 22 |
|
18 | 23 | try{ |
19 | 24 | DefaultHttpClient client = new DefaultHttpClient(); |
20 | 25 | HttpPost post = new HttpPost |
21 | 26 | ("https://api.openai.com/v1/completions"); |
22 | 27 |
|
23 | | - StringEntity entity = new StringEntity("{\"model\" : \"text-davinci-003\",\"prompt\" : \"" + |
24 | | - "O que e a nova rota da seda\"}"); |
| 28 | + StringEntity entity = new StringEntity |
| 29 | + ("{" + |
| 30 | + "\"model\" : " + |
| 31 | + "\"" + |
| 32 | + MODEL + |
| 33 | + "\"," + |
| 34 | + "\"prompt\" : \"" + |
| 35 | + PROMPT + |
| 36 | + "\"," + |
| 37 | + "\"max_tokens\" : " + |
| 38 | + MAX_TOKENS + |
| 39 | + "," + |
| 40 | + "\"temperature\" : " + |
| 41 | + TEMPERATURE + |
| 42 | + "}"); |
| 43 | + |
25 | 44 | entity.setContentType("application/json"); |
26 | 45 | post.setHeader("Content-Type","application/json"); |
27 | 46 | post.setHeader("Authorization","Bearer "+KEY); |
28 | 47 | post.setEntity(entity); |
| 48 | + |
29 | 49 | HttpResponse response = client.execute(post); |
| 50 | + |
30 | 51 | if(response.getStatusLine().getStatusCode() != 201) |
31 | 52 | System.out.println("HTTP Error: "+response.getStatusLine().getStatusCode()); |
| 53 | + |
32 | 54 | BufferedReader reader = new BufferedReader( |
33 | 55 | new InputStreamReader(response.getEntity().getContent())); |
| 56 | + |
34 | 57 | String output; |
35 | 58 | System.out.println("\n\nGPT Resposta: \n"); |
36 | 59 | while ( (output = reader.readLine()) != null) |
37 | 60 | System.out.println(output); |
| 61 | + |
38 | 62 | client.getConnectionManager().shutdown(); |
39 | 63 |
|
40 | 64 | }catch (Exception exception){System.out.println( exception.getMessage() );} |
|
0 commit comments