07-02 03:26
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- audio record
- JetPack
- Mac
- 청약
- ndkVersion
- ChatGPT
- 달러예금
- Android Studio
- EditText
- java
- 사전청약
- KB
- 3rd framework
- 새마을금고
- Andorid
- ios
- Data Binding
- kotlin
- 예금
- gradle
- CHAT GPT
- 용산 호반써밋 에이디션
- arm-linux-androideabi
- ndk r24
- BindingAdapter
- RETROFIT
- Android
- MG더뱅킹정기예금
- 고금리
- Swift
Archives
- Today
- Total
pear
[openai] chatgpt 2 - RestAPI with retrofit 본문
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 |
---|