Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Programa em Java para consumir a API da Open AI. No código está fazendo um POST para o modelo 'text-davinci-003'

License

Notifications You must be signed in to change notification settings

nwrn/java-apache-openai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

8 Commits

Repository files navigation

Java-Apache-OpenAI

Programa em Java para consumir a API da Open AI com a biblioteca Apache HTTP.

No código está fazendo um POST para o modelo 'text-davinci-003' que é o modelo do Chat GPT-3

Uma maneira simples para fazer um POST na API da Open AI e receber seu output. Visando assim facilitar a integração com Chat GPT para projetos java.

Dependência MAVEN:

 <dependency>
 <groupId>org.apache.httpcomponents</groupId>
 <artifactId>httpclient</artifactId>
 <version>4.5.14</version>
 </dependency>

O sistema consiste em uma única classe:


public class OpenaiApplication {
 private static String KEY = "sk-";
 private static String PROMPT = "O que e o chat GPT?";
 private static long MAX_TOKENS = 100;
 private static float TEMPERATURE = 1;
 private static String MODEL = "text-davinci-003";
 public static void main(String[] args){
 try{
 DefaultHttpClient client = new DefaultHttpClient();
 HttpPost post = new HttpPost
 ("https://api.openai.com/v1/completions");
 StringEntity entity = new StringEntity
 ("{" +
 "\"model\" : " +
 "\"" +
 MODEL +
 "\"," +
 "\"prompt\" : \"" +
 PROMPT +
 "\"," +
 "\"max_tokens\" : " +
 MAX_TOKENS +
 "," +
 "\"temperature\" : " +
 TEMPERATURE +
 "}");
 entity.setContentType("application/json");
 post.setHeader("Content-Type","application/json");
 post.setHeader("Authorization","Bearer "+KEY);
 post.setEntity(entity);
 HttpResponse response = client.execute(post);
 if(response.getStatusLine().getStatusCode() != 201)
 System.out.println("HTTP Error: "+response.getStatusLine().getStatusCode());
 BufferedReader reader = new BufferedReader(
 new InputStreamReader(response.getEntity().getContent()));
 String output;
 System.out.println("\n\nGPT Resposta: \n");
 while ( (output = reader.readLine()) != null)
 System.out.println(output);
 client.getConnectionManager().shutdown();
 }catch (Exception exception){System.out.println( exception.getMessage() );}
 }
}

About

Programa em Java para consumir a API da Open AI. No código está fazendo um POST para o modelo 'text-davinci-003'

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

AltStyle によって変換されたページ (->オリジナル) /