07-02 03:26
Notice
Recent Posts
Recent Comments
07-02 03:26
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

pear

[openai] chatgpt 2 - RestAPI with retrofit 본문

dev/etc

[openai] chatgpt 2 - RestAPI with retrofit

pearlab 2023. 3. 20. 17:42

 

Call Retrofit API

GPTRetrofitInterface mRetrofitInterface = RetrofitGPT
        .getClient(domain).create(GPTRetrofitInterface.class);
try {


    gptBody body = new gptBody();
    body.model = "gpt-3.5-turbo";
    body.messages[0].role = "user";
    body.messages[0].content = "contents";

    Gson gson = new Gson();
    String json = gson.toJson(body);
    JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
    Call<String> call = mRetrofitInterface.completions(jsonObject);

    Response<String> response =  call.execute();

Make Retrofit (Setting Header Authorization - Bearer($SECERET_KEY)

Authorization using bearer scheme

ScalarsConverterFactory for String body

public class RetrofitGPT {

    static Retrofit retrofit = null;
    public static OkHttpClient client = null;

    public static Retrofit getClient(String aServer) {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        try {

            client = new OkHttpClient.Builder()
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .writeTimeout(30, TimeUnit.SECONDS)
                    .addInterceptor(interceptor)
                    .addInterceptor(new Interceptor() {

                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            // TODO Auto-generated method stub
                            String token = "Bearer $SECERET_KEY";
                            Request request = chain.request().newBuilder()
                                    .addHeader("Authorization", token)
                                    .build();
                            return chain.proceed(request);
                        }
                    }).build();

            retrofit = new Retrofit.Builder().baseUrl(aServer)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(client).build();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return retrofit;
    }
}

 

 

Rest API Request Retrofit Interface

public interface GPTRetrofitInterface {
   @Headers("Content-Type: application/json")
   @POST("completions ")
   Call<String> completions(@Body JsonObject sendJSONObject);
}

 

Rest API Request Body

public class gptBody {
   public gptBody(){
      messages = new messages[1];
      messages[0] = new messages();
   }
   public String model;
   public messages messages[];
   public class messages{
      public String role;
      public String content;
   }
}

'dev > etc' 카테고리의 다른 글

[openai] chatgpt 1 - Setting & RestAPI  (0) 2023.03.20