准备工作
ChatGPT账号(openai)
集成好网络框架(默认使用Retrofit)
接入
选择modele
这里使用的是 「https://api.openai.com/v1/chat/completions」
创建API Keys
运行效果
POST https://api.openai.com/v1/chat/completions
Content-Type: application/json
Content-Length: 79
Authorization: Bearer 你的API Keys(sk-xxxxxEUbR)
Body:{"messages":[{"content":"Who are you?","role":"user"}],"model":"gpt-3.5-turbo"}
返回
<-- 200 https://api.openai.com/v1/chat/completions (2576ms)
date: Mon, 04 Sep 2023 07:17:50 GMT
content-type: application/json
access-control-allow-origin: *
cache-control: no-cache, must-revalidate
openai-model: gpt-3.5-turbo-0613
openai-organization: user-pnritcadmm5y4rt9a1ebccdh
openai-processing-ms: 1357
openai-version: 2020-10-01
strict-transport-security: max-age=15724800; includeSubDomains
x-ratelimit-limit-requests: 200
x-ratelimit-limit-tokens: 40000
x-ratelimit-remaining-requests: 195
x-ratelimit-remaining-tokens: 39980
x-ratelimit-reset-requests: 31m9.648s
x-ratelimit-reset-tokens: 30ms
x-request-id: c87017722536f2676384cdfd02af455c
cf-cache-status: DYNAMIC
server: cloudflare
cf-ray: 801474734dba599d-IAD
alt-svc: h3=":443"; ma=86400
{
"id": "chatcmpl-7uy8Cml8r1GYP0DQkJB3KrzNo0vQl",
"object": "chat.completion",
"created": 1693811868,
"model": "gpt-3.5-turbo-0613",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "I am an AI language model created by OpenAI called GPT-3. I am here to assist and provide information to the best of my abilities."
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 11,
"completion_tokens": 31,
"total_tokens": 42
}
}
实现
2.1调用
ArrayList<ChatGPTUPBodyBean.MessageBean> list = new ArrayList<>();
list.add(new ChatGPTUPBodyBean.MessageBean("user","Who are you?"));
ChatGPTUPBodyBean bodyBean = new ChatGPTUPBodyBean(list,"gpt-3.5-turbo") ;
Map<String, String> headerMap = new HashMap<>();
headerMap.put("Content-Type", "application/json");
headerMap.put("Authorization", "Bearer 你的API Keys");
ChatGPTRetrofitManager.getInstance().create(IChatGPTService.class).completions(bodyBean,headerMap)
.subscribeOn(Schedulers.io())//切换到IO线程
.observeOn(AndroidSchedulers.mainThread())//切换到主线程
// 添加订阅
.subscribe(responseData -> {
}, throwable -> {
ToastUtil.showMessage(R.string.str_network_exception);
});
2.2 IChatGPTService
-
1.以Body的形式传递数据
-
2.设置请求头
public interface IChatGPTService {
@POST("v1/chat/completions")
Observable<ChatGPTBean> completions(@Body ChatGPTUPBodyBean bodyBean, @HeaderMap Map<String,String> headerMap);
}
2.3 上行参数(ChatGPTUPBodyBean)
public class ChatGPTUPBodyBean {
public List<MessageBean> messages;
public String model;
public static class MessageBean {
/**
* role : user
* content : Who are you?
*/
public String role;
public String content;
public MessageBean(String role, String content) {
this.role = role;
this.content = content;
}
}
public ChatGPTUPBodyBean(List<MessageBean> messages, String model) {
this.messages = messages;
this.model = model;
}
}
2.4 返回实体
按照上面返回的JSON生成即可,提取choices.message.content即可。
这只是个简单的Demo,接入起来十分简单。但是具体商用还是要结合自己的业务场景来做,这个仅供参考