from openai import OpenAI from modules.llm.summarizer.models import ScriptureVerseSummary client = OpenAI() def summarize_scripture_verse( lyrics: str, translation: str, word_by_word_meaning: str, target_language: str ) -> ScriptureVerseSummary: """ Generates a simple and detailed meaning of the given lyrics in the specified target language using structured output. """ print("Summarizing ...") system_prompt = ( "You are a precise multilingual assistant that summarizes meanings of Sanatan scripture verses.\n" "Your job is to derive the verse’s simple and detailed meaning *strictly* from the provided inputs.\n\n" "=== MANDATORY RULES ===\n" "1. Use ONLY the information from: lyrics, translation, and word-by-word meaning.\n" # "2. Do NOT invent, guess, or infer anything not explicitly given.\n" "2. If the word-by-word meaning is not provided, try to derive it from the original lyrics based on the context of the verse and the translation if provided.\n" "3. Do NOT include any English words or transliterations unless they already appear in the input.\n" f"4. Every part of your output must be written completely in the target language: {target_language}.\n" " - If the target language lacks a direct word, explain it *in that language* (do not leave English placeholders).\n" " - Preserve transliterated proper nouns (e.g., ‘nanjIyar’, ‘rAmAnuja’) exactly as written — case-sensitive.\n" "5. NEVER mix scripts. Do not output Latin letters unless they occur verbatim in the input.\n" "6. If unsure of a word’s meaning, omit it or express uncertainty *in the target language*.\n" "7. Maintain a respectful, neutral tone. No opinions, commentary, or theological judgment.\n" "8. Output must follow the ScriptureVerseSummary schema exactly: only 'simple_meaning', 'detailed_meaning', 'word_by_word_meaning' and 'target_language'.\n" "9. Do NOT repeat the input text or provide explanations in any other language.\n" "10. Ensure all formatting is plain text — no quotes, brackets, or markdown around meanings.\n" ) user_prompt = ( f"Lyrics:\n{lyrics}\n\n" f"Translation:\n{translation}\n\n" f"Word-by-word meaning:\n{word_by_word_meaning}\n\n" f"Now generate the verse’s meaning entirely in **{target_language}**.\n" "Provide both a simple summary and a detailed explanation along with word by word meaning translating each word in original script to the target language.\n" "Do not include any English words unless they appear exactly as in the inputs." ) response = client.chat.completions.parse( model="gpt-4o-mini", messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt}, ], response_format=ScriptureVerseSummary, ) return response.choices[0].message.parsed