Using Pydantic with Arcee AI: AFM 4.5B on OpenRouter
This tutorial explains how to use a Pydantic schema to extract structured data using Arcee AI: AFM 4.5B.
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 = 'arcee-ai/afm-4.5b'
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:
<think>
{
"timeline": {
"date_of_vaccination": "1999-06-30",
"regression_onset_date": "2000-10-01",
"diagnosis_date": "2003-03-15",
"age_at_vaccination_months": 0.93,
"age_at_diagnosis_months": 5.55
},
"developmental_milestones": [
{
"milestone_name": "babbling",
"milestone_type": "language",
"milestone_age_in_months": 13
},
{
"milestone_name": "single_words",
"milestone_type": "language",
"milestone_age_in_months": 15
},
{
"milestone_name": "two_word_phrases",
"milestone_type": "language",
"milestone_age_in_months": 24
},
{
"milestone_name": "naming_objects",
"milestone_type": "language",
"milestone_age_in_months": 29
},
{
"milestone_name": "conversation_turning",
"milestone_type": "social_communication",
"milestone_age_in_months": 31
},
{
"milestone_name": "self_feeding",
"milestone_type": "adaptive_behavior",
"milestone_age_in_months": 33
}
],
"regression_record": {
"pre_regression_language_level": "18-20 months",
"pre_regression_social_communication_level": "18-20 months",
"pre_regression_adaptive_behavior_level": "22 months",
"post_regression_language_level": "8-15 months",
"post_regression SOCIAL_COMMUNICATION": "15-18 months",
"post_regression_ADAPTIVE_BEHAVIOR": "22-22 months",
"has_language_loss": true,
"has_social_loss": true,
"has_adaptive_loss": true,
"has_repetitive_behaviors": false
},
"early_vaccination_symptoms": {
"had_autism_features": true,
"had_fever": true,
"had_speech_loss": true,
"had_ear_infections": true,
"had_vomiting": true,
"had_screaming": true,
"had_behavioral_changes": true,
"had_rash": true,
"had_head_banging": true,
"had_social_withdrawal": true,
"had_injection_site_reaction": true,
"had_balance_loss": true,
"had_appetite_loss": true,
"had_staring_spells": true,
"had_hand_flapping": true,
"had_toe_walking": true,
"had_gastrointestinal_problems": true,
"had_hyperactivity": true,
"had_ear_infections": true,
"had_vomiting": true,
"had_sleep_problems": true,
"had_encephalopathy": true
},
"diagnosis_record": {
"asd_severity": "moderate",
"diagnosis_name": [
"autism",
"PDD-NOS"
],
"diagnosing_professional": [
"pediatrician",
"specialist"
]
},
"intervention_record": {
"is_intervention_ongoing": true,
"has_recovery": true
},
"birth_record": {
"was_normal_pre_vaccination": null,
"apgar_score": [
{
"impression": "normal"
},
{
"impression": "normal"
}
]
},
"behavior_record": {
"has_repetitive_behaviors": true,
"repetitive_behaviors": [
{
"explanation": "Limited eye contact and reaching while making a vocalization",
"ongoing": true,
"value": false,
"citations": []
},
{
"explanation": "Screaming in frustration when unable to do something",
"ongoing": true,
"value": false,
"citations": []
},
{
"explanation": "Grunting, screaming, and occasional single words to get needs met",
"ongoing": true,
"value": true,
"citations": []
},
{
"explanation": "Prognosis for improved communication skills is excellent with initiation of intervention",
"ongoing": true,
"value": true,
"citations": []
},
{
"explanation": "Speech therapy is strongly recommended to provide extra stimulation of communication skills",
"ongoing": true,
"value": true,
"citations": []
},
{
"explanation": "Behavior modifications are recommended to help decrease aggressive behaviors",
"ongoing": true,
"value": true,
"citations": []
},
{
"explanation": "Special instruction is recommended to work on cognitive, language, and social stimulation",
"ongoing": true,
"value": true,
"citations": []
},
{
"explanation": "Behavioral training is recommended to work on responsive behaviors and social interaction",
"ongoing": true,
"value": true,
"citations": []
},
{
"explanation": "Occupational therapy is recommended to address developmental delays",
"ongoing": true,
"value": true,
"citations": []
},
{
"explanation": "The subject may be currently experiencing a developmental regression",
"ongoing": true,
"value": true,
"citations": []
},
{
"explanation": "Cognitive, language, and social stimulation are needed",
"ongoing": true,
"value": true,
"citations": []
},
{
"explanation": "Speech therapy is recommended to address communication needs",
"ongoing": true,
"value": true,
"citations": []
},
{
"explanation": "Interaction with typical peers is recommended",
"ongoing": true,
"value": true,
"citations": []
}
]
},
"heavy_metal_tests": null,
"comorbidities": [
{
"comorbidity_type": "colitis",
"citations": []
},
{
"comorbidity_type": "anxiety",
"citations": []
}
],
"environmental_exposures": [
{
"exposure_type": "other_mercury",
"citations": []
},
{
"exposure_type": "unknown",
"citations": []
}
],
"legal_matter": {
"is_related_to_litigation": true,
"citations": []
},
"diagnosis_reporting_delay_reason": {
"reason": "Delay in diagnosis due to initial condition not being recognized",
"citations": []
}
}
Metrics
{
"elapsed": 8.735158920288086,
"usage": {
"prompt_tokens": 13496,
"completion_tokens": 1523,
"total_tokens": 15019
}
}