Skip to content

Commit

Permalink
Gemini client
Browse files Browse the repository at this point in the history
  • Loading branch information
lincmba committed Dec 20, 2024
1 parent 1edcc9b commit b27d2ee
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ class SpeechToText {
}
return tempFile
}
}
}

0 comments on commit b27d2ee

Please sign in to comment.