2023-03-07 09:17:39 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
/*
|
|
|
|
This file is part of FairEmail.
|
|
|
|
|
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright 2018-2023 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.text.TextUtils;
|
2023-03-09 10:24:03 +00:00
|
|
|
import android.util.Pair;
|
2023-03-07 09:17:39 +00:00
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
2023-03-09 13:59:26 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
2023-03-07 09:17:39 +00:00
|
|
|
|
|
|
|
public class OpenAI {
|
|
|
|
static final String URI_ENDPOINT = "https://api.openai.com/";
|
|
|
|
static final String URI_PRIVACY = "https://openai.com/policies/privacy-policy";
|
|
|
|
|
2023-03-07 19:41:51 +00:00
|
|
|
private static final int TIMEOUT = 30; // seconds
|
2023-03-07 09:17:39 +00:00
|
|
|
|
|
|
|
static boolean isAvailable(Context context) {
|
|
|
|
if (BuildConfig.PLAY_STORE_RELEASE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean enabled = prefs.getBoolean("openai_enabled", false);
|
|
|
|
String apikey = prefs.getString("openai_apikey", null);
|
|
|
|
|
|
|
|
return (enabled && !TextUtils.isEmpty(apikey));
|
|
|
|
}
|
|
|
|
|
2023-03-09 10:24:03 +00:00
|
|
|
static Pair<Double, Double> getGrants(Context context) throws JSONException, IOException {
|
|
|
|
// dashboard/billing/credit_grants
|
|
|
|
// {
|
|
|
|
// "object": "credit_summary",
|
|
|
|
// "total_granted": <float>,
|
|
|
|
// "total_used": <float>,
|
|
|
|
// "total_available": <float>,
|
|
|
|
// "grants": {
|
|
|
|
// "object": "list",
|
|
|
|
// "data": [
|
|
|
|
// {
|
|
|
|
// "object": "credit_grant",
|
|
|
|
// "id": "<guid>>",
|
|
|
|
// "grant_amount": <float>,
|
|
|
|
// "used_amount": <float>>,
|
|
|
|
// "effective_at": <unixtime>,
|
|
|
|
// "expires_at": <unixtime>
|
|
|
|
// }
|
|
|
|
// ]
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
|
|
|
JSONObject grants = call(context, "GET", "dashboard/billing/credit_grants", null);
|
|
|
|
return new Pair<>(
|
|
|
|
grants.getDouble("total_used"),
|
|
|
|
grants.getDouble("total_granted"));
|
|
|
|
}
|
|
|
|
|
2023-03-09 13:59:26 +00:00
|
|
|
static void checkModeration(Context context, String text) throws JSONException, IOException {
|
|
|
|
// https://platform.openai.com/docs/api-reference/moderations/create
|
|
|
|
JSONObject jrequest = new JSONObject();
|
|
|
|
jrequest.put("input", text);
|
|
|
|
JSONObject jresponse = call(context, "POST", "v1/moderations", jrequest);
|
|
|
|
JSONArray jresults = jresponse.getJSONArray("results");
|
|
|
|
for (int i = 0; i < jresults.length(); i++) {
|
|
|
|
JSONObject jresult = jresults.getJSONObject(i);
|
|
|
|
if (jresult.getBoolean("flagged")) {
|
|
|
|
List<String> violations = new ArrayList<>();
|
|
|
|
JSONObject jcategories = jresult.getJSONObject("categories");
|
|
|
|
JSONObject jcategory_scores = jresult.getJSONObject("category_scores");
|
|
|
|
Iterator<String> keys = jcategories.keys();
|
|
|
|
while (keys.hasNext()) {
|
|
|
|
String key = keys.next();
|
|
|
|
Object value = jcategories.get(key);
|
|
|
|
if (Boolean.TRUE.equals(value)) {
|
|
|
|
Double score = (jcategories.has(key) ? jcategory_scores.getDouble(key) : null);
|
|
|
|
violations.add(key + (score == null ? "" : ":" + Math.round(score * 100) + "%"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException(TextUtils.join(", ", violations));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 06:43:39 +00:00
|
|
|
static Message[] completeChat(Context context, String model, Message[] messages, Float temperature, int n) throws JSONException, IOException {
|
2023-03-07 09:17:39 +00:00
|
|
|
// https://platform.openai.com/docs/guides/chat/introduction
|
|
|
|
// https://platform.openai.com/docs/api-reference/chat/create
|
|
|
|
|
|
|
|
JSONArray jmessages = new JSONArray();
|
|
|
|
for (Message message : messages) {
|
2023-03-09 13:59:26 +00:00
|
|
|
checkModeration(context, message.content);
|
2023-03-07 09:17:39 +00:00
|
|
|
JSONObject jmessage = new JSONObject();
|
|
|
|
jmessage.put("role", message.role);
|
|
|
|
jmessage.put("content", message.content);
|
|
|
|
jmessages.put(jmessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONObject jquestion = new JSONObject();
|
2023-03-08 19:34:39 +00:00
|
|
|
jquestion.put("model", model);
|
2023-03-07 09:17:39 +00:00
|
|
|
jquestion.put("messages", jmessages);
|
2023-03-09 06:43:39 +00:00
|
|
|
if (temperature != null)
|
|
|
|
jquestion.put("temperature", temperature);
|
2023-03-07 09:17:39 +00:00
|
|
|
jquestion.put("n", n);
|
2023-03-09 10:24:03 +00:00
|
|
|
JSONObject jresponse = call(context, "POST", "v1/chat/completions", jquestion);
|
2023-03-07 09:17:39 +00:00
|
|
|
|
|
|
|
JSONArray jchoices = jresponse.getJSONArray("choices");
|
|
|
|
Message[] choices = new Message[jchoices.length()];
|
|
|
|
for (int i = 0; i < jchoices.length(); i++) {
|
|
|
|
JSONObject jchoice = jchoices.getJSONObject(i);
|
|
|
|
JSONObject jmessage = jchoice.getJSONObject("message");
|
|
|
|
choices[i] = new Message(jmessage.getString("role"), jmessage.getString("content"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return choices;
|
|
|
|
}
|
|
|
|
|
2023-03-09 10:24:03 +00:00
|
|
|
private static JSONObject call(Context context, String method, String path, JSONObject args) throws JSONException, IOException {
|
2023-03-07 09:17:39 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
String apikey = prefs.getString("openai_apikey", null);
|
|
|
|
|
|
|
|
// https://platform.openai.com/docs/api-reference/introduction
|
2023-03-09 10:24:03 +00:00
|
|
|
Uri uri = Uri.parse(URI_ENDPOINT).buildUpon().appendEncodedPath(path).build();
|
2023-03-07 09:17:39 +00:00
|
|
|
Log.i("OpenAI uri=" + uri);
|
|
|
|
|
2023-03-09 13:59:26 +00:00
|
|
|
long start = new Date().getTime();
|
|
|
|
|
2023-03-07 09:17:39 +00:00
|
|
|
URL url = new URL(uri.toString());
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
2023-03-09 10:24:03 +00:00
|
|
|
connection.setRequestMethod(method);
|
|
|
|
connection.setDoOutput(args != null);
|
2023-03-07 09:17:39 +00:00
|
|
|
connection.setDoInput(true);
|
|
|
|
connection.setReadTimeout(TIMEOUT * 1000);
|
|
|
|
connection.setConnectTimeout(TIMEOUT * 1000);
|
|
|
|
ConnectionHelper.setUserAgent(context, connection);
|
|
|
|
connection.setRequestProperty("Accept", "application/json");
|
|
|
|
connection.setRequestProperty("Content-Type", "application/json");
|
|
|
|
connection.setRequestProperty("Authorization", "Bearer " + apikey);
|
|
|
|
connection.connect();
|
|
|
|
|
|
|
|
try {
|
2023-03-09 10:24:03 +00:00
|
|
|
if (args != null) {
|
|
|
|
String json = args.toString();
|
|
|
|
Log.i("OpenAI request=" + json);
|
|
|
|
connection.getOutputStream().write(json.getBytes());
|
|
|
|
}
|
2023-03-07 09:17:39 +00:00
|
|
|
|
|
|
|
int status = connection.getResponseCode();
|
|
|
|
if (status != HttpURLConnection.HTTP_OK) {
|
|
|
|
// https://platform.openai.com/docs/guides/error-codes/api-errors
|
|
|
|
String error = "Error " + status + ": " + connection.getResponseMessage();
|
|
|
|
try {
|
|
|
|
InputStream is = connection.getErrorStream();
|
2023-03-08 19:39:51 +00:00
|
|
|
if (is != null)
|
|
|
|
error += "\n" + Helper.readStream(is);
|
2023-03-07 09:17:39 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
throw new IOException(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
String response = Helper.readStream(connection.getInputStream());
|
|
|
|
Log.i("OpenAI response=" + response);
|
|
|
|
|
|
|
|
return new JSONObject(response);
|
|
|
|
} finally {
|
|
|
|
connection.disconnect();
|
2023-03-09 13:59:26 +00:00
|
|
|
long elapsed = new Date().getTime() - start;
|
|
|
|
Log.i("OpenAI elapsed=" + (elapsed / 1000f));
|
2023-03-07 09:17:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static class Message {
|
|
|
|
private final String role; // // system, user, assistant
|
|
|
|
private final String content;
|
|
|
|
|
|
|
|
public Message(String role, String content) {
|
|
|
|
this.role = role;
|
|
|
|
this.content = content;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getRole() {
|
|
|
|
return this.role;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getContent() {
|
|
|
|
return this.content;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return this.role + ": " + this.content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|