How to use Gemini response_schema
You can use Gemini response_schema to get structured output from your text input.
How to extract numerical values
Sample code
import google.generativeai as genai
import os
import json
from dotenv import load_dotenv
load_dotenv()
api_key = os.getenv('GEMINI_API_KEY')
genai.configure(api_key=api_key)
import typing_extensions as typing
class Explanation(typing.TypedDict):
matching_sentence_if_any: str
matching_phrase_if_any: str
explanation: str
class PatientInfo(typing.TypedDict):
patient_age: int
patient_age_explanation: Explanation
symptom_text = '''
After the third Comirnaty vaccination, the patient had a attack (asthma) in the waiting area during follow-up; This is a spontaneous report received from a contactable reporter(s) (Physician) from Regulatory Authority. Regulatory number: v21133143. A 49 year-old female patient received bnt162b2 (COMIRNATY), administration date 22Jan2022 12:50 (Lot number: FJ5929, Expiration Date: 30Apr2022) at the age of 49 years as dose 3 (booster), single for covid-19 immunisation. The patient's relevant medical history and concomitant medications were not reported. Vaccination history included: Covid-19 vaccine (dose1, MANUFACTURER UNKNOWN), for COVID-19 immunisation; Comirnaty (dose2, Asthmatic attack, appeared 60 minutes after the second comirnaty vaccination.), for COVID-19 immunisation, reaction(s): ""Asthmatic attack"". The following information was reported: ASTHMA (medically significant) with onset 22Jan2022 13:20, outcome ""recovered"" (22Jan2022), described as ""After the third Comirnaty vaccination, the patient had a attack (asthma) in the waiting area during follow-up"". The patient underwent the following laboratory tests and procedures: body temperature: (22Jan2022) 36.1 Centigrade, notes: Before vaccination. Clinical course: On 22Jan2022 at 13:20 (30minutes after the vaccination), the event was onset. On 22Jan2022 (same day of the vaccination), the outcome of the event was recovering(as reported).The course of the event was as follows: After the third Comirnaty vaccination, the patient had a attack (asthma) in the waiting area during follow-up. Therapeutic measures were taken as a result of asthma.Correspondence with vascular securement including 0.5 mL subcutaneous Adrenaline and 3.3 mg IV Dexamethasone. After about 1 hour, the attack disappeared. The reporting physician classified the event as serious (Medically significant). The causality between the event and the vaccine was not provided. Other possible cause of the event such as any other diseases was not provided. The reporting physician commented as follows: The patient originally had a history of asthma and needed to reconsider the fourth and subsequent vaccination. No follow-up attempts are needed. No further information is expected."
'''
model = genai.GenerativeModel(
model_name="gemini-1.5-pro-latest",
system_instruction="You are a biomedical expert. If the value is not available, use 'Unknown' for string and -1 for int. If there is no matching sentence, leave the field empty.")
result = model.generate_content(
f'''
Writeup:
{symptom_text}
''',
generation_config=genai.GenerationConfig(
response_mime_type="application/json", response_schema=PatientInfo
),
)
response = json.loads(result.text)
print(json.dumps(response, indent=2))
Response from Gemini:
{
"patient_age": 49,
"patient_age_explanation": {
"explanation": "Patient's age is extracted from \"A 49 year-old female patient received\"",
"matching_phrase_if_any": "49 year-old",
"matching_sentence_if_any": "A 49 year-old female patient received bnt162b2 (COMIRNATY), administration date 22Jan2022 12:50 (Lot number: FJ5929, Expiration Date: 30Apr2022) at the age of 49 years as dose 3 (booster), single for covid-19 immunisation."
}
}
You might have noticed that I use an Explanation class along with the field I want to extract.