|

Using Pydantic with OpenGVLab: InternVL3 78B on OpenRouter

This tutorial explains how to use a Pydantic schema to extract structured data using OpenGVLab: InternVL3 78B.

Please read this tutorial first to understand the basic setup

Code:

import os
import json
import requests
import time
from inputs import get_input_text
from system import system_instruction_1 as system_instruction
from schema import VAERSReport
from dotenv import load_dotenv

load_dotenv()
model_name = 'opengvlab/internvl3-78b'
api_key = os.getenv("OPENROUTER_API_KEY")

json_schema_text = json.dumps(VAERSReport.model_json_schema(), indent=2)
input_text, sentence_map = get_input_text()

prompt = f"""

Report: {input_text}

JSON Schema:
{json_schema_text}
"""
url = "https://openrouter.ai/api/v1/chat/completions"

headers = {
    "Authorization": f"Bearer {api_key}",
    "Content-Type": "application/json",
    "HTTP-Referer": "https://botflo.com/",  # Optional. Site URL for rankings on openrouter.ai.
    "X-Title": "LLM Accuracy Comparisons for Structured Outputs",  # Optional
}

payload = {
    "model": model_name,
    "messages": [
        {"role": "system", "content": f'{system_instruction}'},
        {"role": "user", "content": f'{prompt}'}
    ],
    "reasoning": {
        "effort": "high"
    }

}
before = time.time()
response_full = requests.post(url, headers=headers, data=json.dumps(payload))
elapsed = time.time() - before

response = response_full.json()
metadata = {
    "metrics": {
        "elapsed": elapsed,
        "usage": response['usage']
    },
    "text": {
        "system_instruction": system_instruction,
        "sentence_map": sentence_map,
        "json_schema_text": json_schema_text
    }
}
response["metadata"] = metadata

response_file_name = f"responses/response_{model_name.replace('/', '-')}.json"
with open(response_file_name, 'w+') as f:
    json.dump(response, f, indent=2)

output_text = response.get('choices')[0].get('message').get('content')
print(output_text)

input_file_name = f"inputs/input_{model_name.replace('/', '-')}.txt"
with open(input_file_name, 'w+') as f:
    f.write(input_text)

output_file_name = f"outputs/output_{model_name.replace('/', '-')}.txt"
with open(output_file_name, 'w+') as f:
    f.write(output_text)

LLM Response:

OK, let's break this down step by step so I can map the information from the VAERS report to the JSON schema provided. I need to carefully examine the report to extract the relevant data and fit it into the correct fields in the JSON structure.

1. Timeline: I'll start by identifying essential dates and the child's age at vaccination and diagnosis.
2. Developmental Milestones: I'll extract information on the milestones mentioned and their status.
3. Regression Record: Determine pre and post-regression levels for language, social communication, adaptive behavior, and whether there were losses or repetitive behaviors.
4. Early Vaccination Symptoms: Check for symptoms within the first 30 days after vaccination based on the VAERS data.
5. Diagnosis Record: Note the severity of ASD and any specific diagnoses with the professionals involved.
6. Intervention Record: Look for ongoing interventions and recovery status.
7. Birth Record: Verify if the child was developmentally normal before vaccination and find the APGAR score.
8. Behavior Record: List observed repetitive behaviors with explanations.
9. Heavy Metal Tests: Identify any tests and results regarding heavy metals.
10. Comorbidities: Identify any co-occurring conditions.
11. Environmental Exposures: Determine any potential environmental exposures.
12. Legal Matter: Check if the report is related to a lawsuit.
13. Diagnosis Reporting Delay Reason: Note if a delay in reporting is mentioned and the reason.

Let's proceed with each section using the report, mapping the extracted information to the appropriate JSON schema fields with citations and explanations where needed. I'll be meticulous to ensure the JSON adheres to the given schema and accurately represents the VAERS report.</think>

```json
{
  "timeline": {
    "date_of_vaccination": {
      "year": 1999,
      "month": 6,
      "citations": [4],
      "explanation": "Vaccination date listed as 1999-06-30"
    },
    "regression_onset_date": {
      "year": 2000,
      "month": null,
      "citations": [146],
      "explanation": "Parents noticed developmental delay at around 18 months"
    },
    "diagnosis_date": {
      "year": 2001,
      "month": 4,
      "citations": [147],
      "explanation": "Diagnosed with autism by January 2001"
    },
    "age_at_vaccination_months": {
      "value": 1.33,
      "citations": [3],
      "explanation": "Age listed as 1.33 months at the time of vaccination"
    },
    "age_at_diagnosis_months": {
      "value": 21.666,
      "citations": [146, 147],
      "explanation": "Age approximately 21 months at the time of diagnosis"
    }
  },
  "developmental_milestones": [
    {
      "milestone_name": {
        "value": "sitting_without_support",
        "citations": [41],
        "explanation": "Sitting without support achieved at 8 months"
      },
      "milestone_type": "adaptive_behavior",
      "milestone_age_in_months": {
        "value": 8,
        "citations": [41],
        "explanation": "Achieved at 8 months"
      },
      "milestone_has_regressed": {
        "value": false,
        "citations": [],
        "explanation": "No regression reported"
      },
      "regression_start_date_in_months": {
        "value": null,
        "citations": [],
        "explanation": "No regression reported"
      },
      "progressed_after_regression": {
        "value": null,
        "citations": [],
        "explanation": "No regression reported"
      }
    },
    {
      "milestone_name": {
        "value": "walking",
        "citations": [41],
        "explanation": "Walked by 12 months"
      },
      "milestone_type": "adaptive_behavior",
      "milestone_age_in_months": {
        "value": 12,
        "citations": [41],
        "explanation": "Achieved at 12 months"
      },
      "milestone_has_regressed": {
        "value": false,
        "citations": [],
        "explanation": "No regression reported"
      },
      "regression_start_date_in_months": {
        "value": null,
        "citations": [],
        "explanation": "No regression reported"
      },
      "progressed_after_regression": {
        "value": null,
        "citations": [],
        "explanation": "No regression reported"
      }
    },
    {
      "milestone_name": {
        "value": "single_words",
        "citations": [41],
        "explanation": "Started talking single words around 17 months"
      },
      "milestone_type": "language",
      "milestone_age_in_months": {
        "value": 17,
        "citations": [41],
        "explanation": "Achieved at 17 months"
      },
      "milestone_has_regressed": {
        "value": true,
        "citations": [49],
        "explanation": "Language delays noted by 9 months"
      },
      "regression_start_date_in_months": {
        "value": null,
        "citations": [],
        "explanation": "Regression start not specified"
      },
      "progressed_after_regression": {
        "value": null,
        "citations": [],
        "explanation": "No progression reported after regression"
      }
    }
  ],
  "regression_record": {
    "pre_regression_language_level": {
      "value": "serbia",
      "citations": [41],
      "explanation": "Language level before regression was single words"
    },
    "pre_regression_social_communication_level": {
      "value": "basic_engagement",
      "citations": [],
      "explanation": "Basic engagement before regression"
    },
    "pre_regression_adaptive_behavior_level": {
      "value": "age_appropriate",
      "citations": [33],
      "explanation": "No developmental delays noted at hospitalization"
    },
    "post_regression_language_level": {
      "value": "no_speech",
      "citations": [49],
      "explanation": "Language level after regression was delayed"
    },
    "post_regression_social_communication_level": {
      "value": "no_engagement",
      "citations": [114],
      "explanation": "Limited verbalizations post-regression"
    },
    "post_regression_adaptive_behavior_level": {
      "value": "no_independence",
      "citations": [137],
      "explanation": "Mild delays in fine motor development noted"
    },
    "has_language_loss": {
      "value": true,
      "citations": [115],
      "explanation": "Delayed speech development reported"
    },
    "has_social_loss": {
      "value": true,
      "citations": [113],
      "explanation": "Social interaction seemed limited post-regression"
    },
    "has_adaptive_loss": {
      "value": true,
      "citations": [137],
      "explanation": "Delays in fine motor skills"
    },
    "has_repetitive_behaviors": {
      "value": true,
      "citations": [126],
      "explanation": "Lining up toys mentioned as behavioral change"
    }
  },
  "early_vaccination_symptoms": {
    "had_autism_features": {
      "value": false,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "No immediate autism features"
    },
    "had_fever": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Fever not reported within 30 days"
    },
    "had_speech_loss": {
      "value": false,
      "ongoing": true,
      "days_to_symptom_onset": null,
      "citations": [41, 49],
      "explanation": "Speech delay noted later"
    },
    "had_irritability": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Irritability not specified"
    },
    "had_eye_contact_loss": {
      "value": false,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "No eye contact loss reported"
    },
    "had_diarrhea": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Diarrhea not reported within 30 days"
    },
    "had_seizures": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Seizures not reported"
    },
    "had_lethargy": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Lethargy not reported"
    },
    "had_screaming": {
      "value": false,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "No screaming reported"
    },
    "had_behavioral_changes": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Behavioral changes not within 30 days"
    },
    "had_rash": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Rash not reported"
    },
    "had_head_banging": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Head banging not mentioned"
    },
    "had_social_withdrawal": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Social withdrawal not within 30 days"
    },
    "had_injection_site_reaction": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Injection site reaction not reported"
    },
    "had_balance_loss": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Balance loss not reported"
    },
    "had_appetite_loss": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Appetite loss not within 30 days"
    },
    "had_staring_spells": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Staring spells not reported"
    },
    "had_hand_flapping": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Hand flapping not mentioned"
    },
    "had_toe_walking": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Toe walking not reported"
    },
    "had_gastrointestinal_problems": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Not within 30 days"
    },
    "had_hyperactivity": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Hyperactivity not reported"
    },
    "had_ear_infections": {
      "value": false,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [135],
      "explanation": "Ear infections mentioned later"
    },
    "had_vomiting": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Vomiting not reported"
    },
    "had_sleep_problems": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Sleep problems not within 30 days"
    },
    "had_encephalopathy": {
      "value": null,
      "ongoing": null,
      "days_to_symptom_onset": null,
      "citations": [],
      "explanation": "Encephalopathy not reported"
    }
  },
  "diagnosis_record": {
    "asd_severity": {
      "value": 2,
      "citations": [151],
      "explanation": "Rated in the 'moderate' autism category"
    },
    "diagnosis_name": [
      {
        "value": "autism",
        "citations": [148, 150],
        "explanation": "Diagnosed with autism"
      }
    ],
    "diagnosing_professional": [
      {
        "value": "psychologist",
        "citations": [130],
        "explanation": "Diagnosed by a psychologist"
      }
    ]
  },
  "intervention_record": {
    "interventions": [
      {
        "value": "speech_therapy",
        "citations": [76, 96, 115, 143],
        "explanation": "Speech therapy recommended"
      },
      {
        "value": "aba",
        "citations": [154],
        "explanation": "ABA recommended"
      },
      {
        "value": "occupational_therapy",
        "citations": [143],
        "explanation": "Occupational therapy recommended"
      }
    ],
    "is_intervention_ongoing": {
      "value": true,
      "citations": [],
      "explanation": "Ongoing therapies not explicitly stated"
    },
    "has_recovery": {
      "value": false,
      "citations": [160, 162],
      "explanation": "Autism persisted as of the follow-up"
    }
  },
  "birth_record": {
    "was_normal_pre_vaccination": {
      "value": null,
      "citations": [],
      "explanation": "Report does not specify if child was normal pre-vaccination"
    },
    "apgar_score": {
      "items": [
        {
          "value": "8, 9",
          "citations": [29],
          "explanation": "APGAR scores 8 and 9 at 1 and 5 minutes"
        }
      ]
    }
  },
  "behavior_record": {
    "repetitive_behaviors": [
      {
        "value": "excessive_shyness",
        "citations": [113],
        "explanation": "Reported shyness around other children"
      },
      {
        "value": "difficulty_with_speech",
        "citations": [115, 114],
        "explanation": "Speech issues noted"
      },
      {
        "value": "repetitive_language",
        "citations": [115],
        "explanation": "Used language repetitively"
```

Metrics

{
  "elapsed": 51.410799980163574,
  "usage": {
    "prompt_tokens": 13922,
    "completion_tokens": 3610,
    "total_tokens": 17532,
    "prompt_tokens_details": null
  }
}

Leave a Reply