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

Method completeChat

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

Source from the content-addressed store, hash-verified

116 }
117
118 static Message[] completeChat(Context context, String model, Message[] messages, Float temperature, int n) throws JSONException, IOException {
119 // https://platform.openai.com/docs/guides/chat/introduction
120 // https://platform.openai.com/docs/api-reference/chat/create
121 JSONArray jmessages = new JSONArray();
122 for (Message message : messages) {
123 JSONObject jmessage = new JSONObject();
124 jmessage.put("role", message.role);
125
126 if (message.content.length == 1 && CONTENT_TEXT.equals(message.content[0].type))
127 jmessage.put("content", message.content[0].content);
128 else {
129 JSONArray jcontents = new JSONArray();
130 for (Content content : message.content) {
131 JSONObject jcontent = new JSONObject();
132 jcontent.put("type", content.type);
133 if (CONTENT_IMAGE.equals(content.type)) {
134 JSONObject jimage = new JSONObject();
135 jimage.put("url", content.content);
136 jcontent.put(content.type, jimage);
137 } else
138 jcontent.put(content.type, content.content);
139 jcontents.put(jcontent);
140 }
141 jmessage.put("content", jcontents);
142 }
143
144 jmessages.put(jmessage);
145 }
146
147 JSONObject jquestion = new JSONObject();
148 jquestion.put("model", model);
149 jquestion.put("messages", jmessages);
150 if (temperature != null)
151 jquestion.put("temperature", temperature);
152 jquestion.put("n", n);
153 JSONObject jresponse = call(context, "POST", "chat/completions", jquestion);
154
155 JSONArray jchoices = jresponse.getJSONArray("choices");
156 Message[] choices = new Message[jchoices.length()];
157 for (int i = 0; i < jchoices.length(); i++) {
158 JSONObject jchoice = jchoices.getJSONObject(i);
159 JSONObject jmessage = jchoice.getJSONObject("message");
160 choices[i] = new Message(jmessage.getString("role"),
161 new Content[]{new Content(CONTENT_TEXT, jmessage.getString("content"))});
162 }
163
164 return choices;
165 }
166
167 private static String getUri(Context context) {
168 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

Callers 2

completeChatMethod · 0.95
getSummaryTextMethod · 0.95

Calls 5

callMethod · 0.95
putMethod · 0.45
equalsMethod · 0.45
lengthMethod · 0.45
getStringMethod · 0.45

Tested by

no test coverage detected