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

Method getSummaryText

app/src/main/java/eu/faircode/email/AI.java:154–245  ·  view source on GitHub ↗
(Context context, EntityMessage message, long template, String prompt)

Source from the content-addressed store, hash-verified

152 }
153
154 static Spanned getSummaryText(Context context, EntityMessage message, long template, String prompt) throws JSONException, IOException {
155 File file = message.getFile(context);
156 if (!file.exists())
157 return null;
158
159 Document d = JsoupEx.parse(file);
160
161 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
162 boolean remove_signatures = prefs.getBoolean("remove_signatures", false);
163 if (remove_signatures)
164 HtmlHelper.removeSignatures(d);
165
166 d = HtmlHelper.sanitizeView(context, d, false);
167
168 HtmlHelper.truncate(d, MAX_SUMMARIZE_TEXT_SIZE);
169
170 String body = d.body().text().trim();
171 if (TextUtils.isEmpty(body))
172 return null;
173
174 String templatePrompt = prompt;
175 if (templatePrompt == null && template > 0L) {
176 DB db = DB.getInstance(context);
177 EntityAnswer t = db.answer().getAnswer(template);
178 if (t != null) {
179 String html = t.getData(context, null).getHtml();
180 templatePrompt = JsoupEx.parse(html).body().text();
181 }
182 }
183
184 StringBuilder sb = new StringBuilder();
185 if (OpenAI.isAvailable(context)) {
186 String model = prefs.getString("openai_model", OpenAI.DEFAULT_MODEL);
187 float temperature = prefs.getFloat("openai_temperature", OpenAI.DEFAULT_TEMPERATURE);
188 String defaultPrompt = prefs.getString("openai_summarize", OpenAI.DEFAULT_SUMMARY_PROMPT);
189 boolean multimodal = prefs.getBoolean("openai_multimodal", false);
190
191 List<OpenAI.Message> input = new ArrayList<>();
192
193 if (multimodal) {
194 input.add(new OpenAI.Message(OpenAI.USER,
195 new OpenAI.Content[]{new OpenAI.Content(OpenAI.CONTENT_TEXT,
196 templatePrompt == null ? defaultPrompt : templatePrompt)}));
197
198 SpannableStringBuilder ssb = HtmlHelper.fromDocument(context, d, null, null);
199 input.add(new OpenAI.Message(OpenAI.USER,
200 OpenAI.Content.get(ssb, message.id, context)));
201 } else {
202 List<String> contents = new ArrayList<>();
203 contents.add(templatePrompt == null ? defaultPrompt : templatePrompt);
204 contents.add(body);
205 input.add(new OpenAI.Message(OpenAI.USER, new OpenAI.Content[]{
206 new OpenAI.Content(OpenAI.CONTENT_TEXT, TextUtils.join("\n", contents))}));
207 }
208
209 OpenAI.Message[] completions =
210 OpenAI.completeChat(context, model, input.toArray(new OpenAI.Message[0]), temperature, 1);
211

Callers 2

onExecuteMethod · 0.95
onActionSummarizeMethod · 0.95

Calls 15

parseMethod · 0.95
removeSignaturesMethod · 0.95
sanitizeViewMethod · 0.95
truncateMethod · 0.95
getInstanceMethod · 0.95
answerMethod · 0.95
getDataMethod · 0.95
isAvailableMethod · 0.95
fromDocumentMethod · 0.95
completeChatMethod · 0.95
getContentMethod · 0.95
isAvailableMethod · 0.95

Tested by

no test coverage detected