接下來我們要實作gpt的API Client及Service,並接到gpt返回的結果
我們可以先使用postman來測試API請求是否成功,成功後就可以在專案中實作各種方法
這裡我們存放所需的base_url、key,以及其他啟動服務所需的方法
public class GPTApiClient {
private static final String API_TOKEN = "sk-proj-2kKxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
private static final GPTApiClient GPTApiInstance = new GPTApiClient();
private final GPTApiService gptApiService;
public GPTApiClient(){
// 創建一個OkHttpClient實例,並設置一個Interceptor,用於在每個請求中添加Authorization header
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.addInterceptor(new Interceptor() {
@NotNull
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
Request originalRequest = chain.request();
// 使用新的Request.Builder創建一個請求,將token添加到header中
Request newRequest = originalRequest.newBuilder()
.header("Authorization", "Bearer " + API_TOKEN)
.build();
return chain.proceed(newRequest);
}
})
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.openai.com/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
.client(okHttpClient)
.build();
gptApiService = retrofit.create(GPTApiService.class);
}
public static GPTApiClient getGPTApiInstance() {
return GPTApiInstance;
}
public GPTApiService getGPTApiService(){
return gptApiService;
}
}
這邊可以透過request及路徑來請求
public interface GPTApiService {
@POST("v1/chat/completions")
Observable<Response<GPTApiRespond>> getChatGPTRespond(@Body GPTApiRequest gptRequest);
}
這裡存放請求的資料,包括一些gpt常用的參數,可以至官方的說明文件查看
public class GPTApiRequest {
private final String model = "gpt-4o-mini";
private List<ChatMessage> messages;
private final Double temperature = 0.1;
private final Integer max_tokens = 1000;
private final Integer top_p = 1;
private final Integer frequency_penalty = 0;
private final Integer presence_penalty = 0;
public void setMessages(List<ChatMessage> messages) {
this.messages = messages;
}
public List<ChatMessage> getMessages() {
return messages;
}
public static class ChatMessage {
private String role;
private String content;
public ChatMessage(String role, String content) {
this.role = role;
this.content = content;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
}
這裡處理請求成功後回傳的資料,主要使用到getContent()部分取得內容
public class GPTApiRespond {
public Error error;
public String id;
public String object;
public int created;
public String model;
public ArrayList<Choice> choices;
public Usage usage;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getObject() {
return object;
}
public void setObject(String object) {
this.object = object;
}
public int getCreated() {
return created;
}
public void setCreated(int created) {
this.created = created;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public ArrayList<Choice> getChoices() {
return choices;
}
public void setChoices(ArrayList<Choice> choices) {
this.choices = choices;
}
public Usage getUsage() {
return usage;
}
public void setUsage(Usage usage) {
this.usage = usage;
}
public static class Error{
public String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
public static class Choice{
public int index;
public String text;
public Message message;
public Object logprobs;
public String finish_reason;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public Object getLogprobs() {
return logprobs;
}
public void setLogprobs(Object logprobs) {
this.logprobs = logprobs;
}
public String getFinish_reason() {
return finish_reason;
}
public void setFinish_reason(String finish_reason) {
this.finish_reason = finish_reason;
}
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
public static class Message {
private String role;
private String content;
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
}
public static class Usage{
public int prompt_tokens;
public int completion_tokens;
public int total_tokens;
}
}
這樣在專案中onNext()方法就會收到資料了,下篇我們再來介紹提示詞的寫法,以及測試的成果