# =====================================================
# Victor Evidence SDK — Corpus-Based Integration Example
#
# example_corpus_only_v1_0.py
# 
# Architecture:
#   Corpus Retrieval → LLM → Victor → Application
#
# This example demonstrates how Victor Evidence validates
# responses generated from existing medical corpus data.
#
# The client may use:
# - SQL tables
# - PostgreSQL
# - FAISS
# - SBERT
# - Vector databases
# - Custom indexing systems
#
# Victor Evidence does NOT require any specific
# retrieval or embedding framework.
#
# Dependencies:
# pip install requests openai psycopg2
# =====================================================


# =====================================================
# CONFIGURATION
# =====================================================

OPENAI_API_KEY = "YOUR_OPENAI_API_KEY"
VICTOR_API_KEY = "YOUR_VICTOR_API_KEY"
MODEL = "gpt-4o-mini"


# =====================================================
# STEP 1 — CORPUS RETRIEVAL (Example: Medical Records)
# =====================================================

def retrieve_patient_data(patient_id):

    import psycopg2

    connection = psycopg2.connect(
        host="localhost",
        database="medical_records",
        user="postgres",
        password="your_password"
    )

    cursor = connection.cursor()

    cursor.execute(
        "SELECT diagnosis, medications, notes FROM patients WHERE id = %s;",
        (patient_id,)
    )

    row = cursor.fetchone()

    cursor.close()
    connection.close()

    if row is None:
        return ""

    diagnosis = row[0]
    medications = row[1]
    notes = row[2]

    context = ""
    context += "Diagnosis: " + str(diagnosis) + "\n"
    context += "Medications: " + str(medications) + "\n"
    context += "Notes: " + str(notes) + "\n"

    return context


# =====================================================
# STEP 2 — GENERATE RESPONSE FROM CORPUS
# =====================================================

from openai import OpenAI

llm_client = OpenAI(api_key=OPENAI_API_KEY)

patient_id = 101
question = (
    "Summarize the diabetes-related information "
    "contained in this patient record."
)

corpus_context = retrieve_patient_data(patient_id)

prompt = (
    "Based on the following patient record:\n\n"
    + corpus_context
    + "\nAnswer the question:\n"
    + question
)

response = llm_client.responses.create(
    model=MODEL,
    input=prompt
)

answer_text = response.output_text.strip()


# =====================================================
# STEP 3 — EVALUATE EVIDENCE SUFFICIENCY
# =====================================================

from victor_ai_sdk_v1_0 import VictorAI

victor = VictorAI(victor_api_key=VICTOR_API_KEY)

result = victor.validate(
    question=question,
    answer=answer_text
)


# =====================================================
# STEP 4 — APPLICATION GATING
# =====================================================

print("\nGenerated Corpus-Based Response:\n")
print(answer_text)

print("\nVictor Gate:", result.get("gate"))
print("Retrieval Strength:", result.get("retrieval_pct"))

if result.get("gate") == "PASS":
    print("\nSufficient admissible evidence detected.")
else:
    print("\nSufficient admissible evidence not established.")