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

Method generate

app/src/main/java/eu/faircode/email/Gemini.java:68–148  ·  view source on GitHub ↗
(Context context, String model, Message[] messages, Float temperature, int n)

Source from the content-addressed store, hash-verified

66 }
67
68 static Message[] generate(Context context, String model, Message[] messages, Float temperature, int n) throws JSONException, IOException {
69 //https://ai.google.dev/api/rest/v1beta/models/generateContent
70 JSONArray jcontents = new JSONArray();
71 for (Message message : messages) {
72 JSONArray jparts = new JSONArray();
73 for (String text : message.getContent()) {
74 JSONObject jtext = new JSONObject();
75 jtext.put("text", text);
76 jparts.put(jtext);
77 }
78
79 JSONObject jcontent = new JSONObject();
80 jcontent.put("parts", jparts);
81 jcontent.put("role", message.role);
82
83 jcontents.put(jcontent);
84 }
85
86 // https://ai.google.dev/api/rest/v1beta/GenerationConfig
87 JSONObject jconfig = new JSONObject();
88 if (temperature != null)
89 jconfig.put("temperature", temperature);
90 jconfig.put("candidate_count", n);
91
92 // https://ai.google.dev/api/rest/v1beta/SafetySetting
93 JSONArray jsafety = new JSONArray();
94
95 JSONObject jsex = new JSONObject();
96 jsex.put("category", "HARM_CATEGORY_SEXUALLY_EXPLICIT");
97 jsex.put("threshold", "BLOCK_ONLY_HIGH");
98 jsafety.put(jsex);
99
100 JSONObject jhate = new JSONObject();
101 jhate.put("category", "HARM_CATEGORY_HATE_SPEECH");
102 jhate.put("threshold", "BLOCK_ONLY_HIGH");
103 jsafety.put(jhate);
104
105 JSONObject jharass = new JSONObject();
106 jharass.put("category", "HARM_CATEGORY_HARASSMENT");
107 jharass.put("threshold", "BLOCK_ONLY_HIGH");
108 jsafety.put(jharass);
109
110 JSONObject jdanger = new JSONObject();
111 jdanger.put("category", "HARM_CATEGORY_DANGEROUS_CONTENT");
112 jdanger.put("threshold", "BLOCK_ONLY_HIGH");
113 jsafety.put(jdanger);
114
115 JSONObject jrequest = new JSONObject();
116 jrequest.put("contents", jcontents);
117 jrequest.put("generationConfig", jconfig);
118 jrequest.put("safetySettings", jsafety);
119
120 String path = "models/" + Uri.encode(model) + ":generateContent";
121
122 JSONObject jresponse = call(context, "POST", path, jrequest);
123
124 List<Message> result = new ArrayList<>();
125

Callers 3

completeChatMethod · 0.95
getSummaryTextMethod · 0.95
onExecuteMethod · 0.80

Calls 9

callMethod · 0.95
toArrayMethod · 0.80
getContentMethod · 0.65
putMethod · 0.45
encodeMethod · 0.45
lengthMethod · 0.45
toStringMethod · 0.45
getStringMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected