「开源」Android Studio完美对接ChatGPT

支持多平台服务

  • OpenAI ChatGPT 系列模型
    - One Api 系列平台服务
    - API2D 平台服务
    - 其他代理服务

效果演示


部署教程

程序基于Android Studio开发,百度自行下载!

找到如下路径:app/src/main/java/com/example/gpt/MainActivity.java

搜索方法:callAPI

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
try {
// 创建messages数组,包含用户角色和问题内容
JSONArray messagesArray = new JSONArray();
JSONObject systemMessage = new JSONObject();
systemMessage.put("role", "system");
systemMessage.put("content", "You are ChatGPT, a large language model trained by OpenAI. Follow the user's instructions carefully. Respond using markdown.");
JSONObject userMessage = new JSONObject();
userMessage.put("role", "user");
userMessage.put("content", question);
messagesArray.put(systemMessage);
messagesArray.put(userMessage);
// 构建完整的JSON请求体
jsonBody.put("model", "gpt-3.5-turbo"); // 模型选择
jsonBody.put("messages", messagesArray);

}catch (JSONException e) {
throw new RuntimeException(e);
}

RequestBody body = RequestBody.create(jsonBody.toString(), JSON);
Request request = new Request.Builder()
.url("https://api.openai.com/v1/chat/completions") // 服务器地址
.header("Authorization", "Bearer sk-xxx") // 填写你的key
.header("Content-Type", "application/json")
.post(body)
.build();

下载

GitHub:https://github.com/PastKing/ChatGPT-Android
GitBee:https://gitee.com/past-dust/chat-gpt-android
123盘:https://www.123pan.com/s/7hX0Vv-hHkbv.html
提取码:BO5q