MCPcopy Create free account
hub / github.com/M66B/FairEmail / completeChat

Method completeChat

app/src/main/java/eu/faircode/email/AI.java:47–132  ·  view source on GitHub ↗
(Context context, long id, boolean system, CharSequence body, String reply, String prompt)

Source from the content-addressed store, hash-verified

45 }
46
47 @NonNull
48 static Spanned completeChat(Context context, long id, boolean system, CharSequence body, String reply, String prompt) throws JSONException, IOException {
49 StringBuilder sb = new StringBuilder();
50 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
51 if (OpenAI.isAvailable(context)) {
52 String model = prefs.getString("openai_model", OpenAI.DEFAULT_MODEL);
53 float temperature = prefs.getFloat("openai_temperature", OpenAI.DEFAULT_TEMPERATURE);
54 boolean multimodal = prefs.getBoolean("openai_multimodal", false);
55 String defaultPrompt = prefs.getString("openai_answer", OpenAI.DEFAULT_ANSWER_PROMPT);
56 String systemPrompt = prefs.getString("openai_system", null);
57
58 List<OpenAI.Message> messages = new ArrayList<>();
59
60 if (system && !TextUtils.isEmpty(systemPrompt))
61 messages.add(new OpenAI.Message(OpenAI.SYSTEM, new OpenAI.Content[]{
62 new OpenAI.Content(OpenAI.CONTENT_TEXT, systemPrompt)}));
63
64 if (body instanceof Spannable && multimodal) {
65 messages.add(new OpenAI.Message(OpenAI.USER, new OpenAI.Content[]{
66 new OpenAI.Content(OpenAI.CONTENT_TEXT, prompt == null ? defaultPrompt : prompt)}));
67
68 if (!TextUtils.isEmpty(body))
69 messages.add(new OpenAI.Message(OpenAI.USER,
70 OpenAI.Content.get((Spannable) body, id, context)));
71
72 if (!TextUtils.isEmpty(reply))
73 messages.add(new OpenAI.Message(OpenAI.USER, new OpenAI.Content[]{
74 new OpenAI.Content(OpenAI.CONTENT_TEXT, reply)}));
75 } else {
76 List<String> contents = new ArrayList<>();
77 contents.add(prompt == null ? defaultPrompt : prompt);
78 if (!TextUtils.isEmpty(body))
79 contents.add(body.toString());
80 if (!TextUtils.isEmpty(reply))
81 contents.add(reply);
82 messages.add(new OpenAI.Message(OpenAI.USER, new OpenAI.Content[]{
83 new OpenAI.Content(OpenAI.CONTENT_TEXT, TextUtils.join("\n", contents))}));
84 }
85
86 OpenAI.Message[] completions = OpenAI.completeChat(context,
87 model, messages.toArray(new OpenAI.Message[0]), temperature, 1);
88
89 for (OpenAI.Message completion : completions)
90 for (OpenAI.Content content : completion.getContent())
91 if (OpenAI.CONTENT_TEXT.equals(content.getType())) {
92 if (sb.length() > 0)
93 sb.append('\n');
94 sb.append(content.getContent()
95 .replaceAll("^\\n+", "")
96 .replaceAll("\\n+$", ""));
97 }
98 } else if (Gemini.isAvailable(context)) {
99 String model = prefs.getString("gemini_model", Gemini.DEFAULT_MODEL);
100 float temperature = prefs.getFloat("gemini_temperature", Gemini.DEFAULT_TEMPERATURE);
101 String defaultPrompt = prefs.getString("gemini_answer", Gemini.DEFAULT_ANSWER_PROMPT);
102
103 List<Gemini.Message> messages = new ArrayList<>();
104

Callers 2

onExecuteMethod · 0.95
evaluateMethod · 0.95

Calls 15

isAvailableMethod · 0.95
completeChatMethod · 0.95
isAvailableMethod · 0.95
truncateParagraphsMethod · 0.95
generateMethod · 0.95
toHtmlMethod · 0.95
sanitizeComposeMethod · 0.95
fromDocumentMethod · 0.95
getFloatMethod · 0.80
getBooleanMethod · 0.80
joinMethod · 0.80
toArrayMethod · 0.80

Tested by

no test coverage detected