Skip to content

Commit

Permalink
use suspend, rename to model, update template
Browse files Browse the repository at this point in the history
  • Loading branch information
pld committed Dec 28, 2024
1 parent 320f599 commit beb8023
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import com.google.ai.client.generativeai.type.HarmCategory
import com.google.ai.client.generativeai.type.SafetySetting
import com.google.ai.client.generativeai.type.generationConfig

class GeminiClient(private val apiKey: String) {
class GeminiModel(private val apiKey: String) {
// model usage
// https://developer.android.com/ai/google-ai-client-sdk
private val model =
val model =
GenerativeModel(
modelName = "gemini-1.5-flash-001",
// todo actually add the API key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SpeechToForm(
* @param questionnaire The FHIR Questionnaire used to generate the response.
* @return The generated QuestionnaireResponse, or null if the process fails.
*/
fun processAudioToQuestionnaireResponse(
suspend fun processAudioToQuestionnaireResponse(
audioFile: File,
questionnaire: Questionnaire,
): QuestionnaireResponse? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
package org.smartregister.fhircore.quest.ui.speechtoform

import ca.uhn.fhir.interceptor.model.RequestPartitionId.fromJson
import com.google.ai.client.generativeai.GenerativeModel
import java.io.File
import java.util.logging.Logger
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.QuestionnaireResponse
import org.json.JSONObject

class TextToForm(private val geminiClient: GeminiClient) {
class TextToForm(private val generativeModel: GenerativeModel) {

private val logger = Logger.getLogger(TextToForm::class.java.name)

Expand All @@ -34,15 +35,15 @@ class TextToForm(private val geminiClient: GeminiClient) {
* @param questionnaire The FHIR Questionnaire to base the response on.
* @return The generated and validated QuestionnaireResponse or null if generation fails.
*/
fun generateQuestionnaireResponse(
suspend fun generateQuestionnaireResponse(
transcriptFile: File,
questionnaire: Questionnaire,
): QuestionnaireResponse? {
val transcript = transcriptFile.readText()
val prompt = buildPrompt(transcript, questionnaire)
val prompt = promptTemplate(transcript, questionnaire)

logger.info("Sending request to Gemini...")
val generatedText = geminiClient.generateContent(prompt)
val generatedText = generativeModel.generateContent(prompt).text

val questionnaireResponseJson = extractJsonBlock(generatedText) ?: return null

Expand All @@ -62,21 +63,23 @@ class TextToForm(private val geminiClient: GeminiClient) {
}

/** Builds the prompt for the Gemini model. */
private fun buildPrompt(transcript: String, questionnaire: Questionnaire): String {
private fun promptTemplate(transcript: String, questionnaire: Questionnaire): String {
return """
Using the following transcript of a conversation between a nurse and a patient:
$transcript
Generate an HL7 FHIR QuestionnaireResponse as if they had entered that information into the following FHIR Questionnaire:
$questionnaire
"""
You are a scribe created to turn conversational text into structure HL7 FHIR output. Below
you will see the text Transcript of a conversation between a nurse and a patient within
<transcript> XML tags and an HL7 FHIR Questionnaire within <questionnaire> XML tags. Your job
is to convert the text in Transcript into a new HL7 FHIR QuestionnaireResponse as if the
information in Transcript had been entered directly into the FHIR Questionniare. Only output
the FHIR QuestionnaireResponse as JSON and nothing else.
<transcript>$transcript</transcript>
<questionnaire>$questionnaire</questionnaire>
"""
.trimIndent()
}

/** Extracts the JSON block from the generated text. */
private fun extractJsonBlock(responseText: String): String? {
private fun extractJsonBlock(responseText: String?): String? {
if (responseText == null) return null
val start = responseText.indexOf("```json")
if (start == -1) return null
val end = responseText.indexOf("```", start + 7)
Expand Down

0 comments on commit beb8023

Please sign in to comment.