| 102 | } |
| 103 | |
| 104 | static double[] getEmbedding(Context context, String text, String model) throws JSONException, IOException { |
| 105 | // https://platform.openai.com/docs/api-reference/embeddings |
| 106 | JSONObject jrequest = new JSONObject(); |
| 107 | jrequest.put("input", text); |
| 108 | jrequest.put("model", model == null ? "text-embedding-ada-002" : model); |
| 109 | JSONObject jresponse = call(context, "POST", "embeddings", jrequest); |
| 110 | JSONObject jdata = jresponse.getJSONArray("data").getJSONObject(0); |
| 111 | JSONArray jembedding = jdata.getJSONArray("embedding"); |
| 112 | double[] result = new double[jembedding.length()]; |
| 113 | for (int i = 0; i < jembedding.length(); i++) |
| 114 | result[i] = jembedding.getDouble(i); |
| 115 | return result; |
| 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 |