-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
29 additions
and
1 deletion.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
android/quest/src/main/java/org/smartregister/fhircore/quest/ui/speechtoform/GeminiClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.smartregister.fhircore.quest.ui.speechtoform | ||
|
||
import okhttp3.* | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import org.json.JSONObject | ||
import java.io.IOException | ||
|
||
class GeminiApiClient(private val apiKey: String) { | ||
private val client = OkHttpClient() | ||
private val baseUrl = "https://generativeai.googleapis.com/v1/models/gemini-1.5-flash:generateContent" | ||
|
||
fun generateContent(prompt: String): String { | ||
val requestBody = JSONObject().put("prompt", prompt).toString() | ||
|
||
val request = Request.Builder() | ||
.url(baseUrl) | ||
.addHeader("Authorization", "Bearer $apiKey") | ||
.post(RequestBody.create("application/json".toMediaType(), requestBody)) | ||
.build() | ||
|
||
client.newCall(request).execute().use { response -> | ||
if (!response.isSuccessful) throw IOException("Unexpected code $response") | ||
|
||
val jsonResponse = JSONObject(response.body?.string() ?: "") | ||
return jsonResponse.getString("text") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,4 +54,4 @@ class SpeechToText { | |
} | ||
return tempFile | ||
} | ||
} | ||
} |