🔍 Code Extractor

function test_llm_connectivity

Maturity: 44

Tests the connectivity and functionality of an OpenAI LLM integration by analyzing a mock email with vendor information extraction.

File:
/tf/active/vicechatdev/find_email/test_vendor_extractor.py
Lines:
170 - 203
Complexity:
simple

Purpose

This function serves as a diagnostic tool to verify that the OpenAI API integration is working correctly within a vendor email extraction system. It creates a mock email with known vendor information, sends it to the LLM for analysis, and validates that the response contains expected fields (vendor emails, confidence, reasoning). This is useful for troubleshooting API connectivity issues, validating API key configuration, and ensuring the LLM analysis pipeline is functioning before processing real emails.

Source Code

def test_llm_connectivity(extractor):
    """Test OpenAI LLM connectivity"""
    print("\n" + "="*60)
    print("TESTING LLM CONNECTIVITY")
    print("="*60)
    
    try:
        # Create a mock email
        mock_email = {
            'subject': 'Order confirmation from Acme Corp',
            'bodyPreview': 'Thank you for your order. Contact us at sales@acme.com',
            'from': {'emailAddress': {'address': 'sales@acme.com'}},
            'toRecipients': [],
            'ccRecipients': [],
            'body': {'content': 'Contact: sales@acme.com or support@acme.com'}
        }
        
        result = extractor.analyze_email_with_llm(mock_email, "Acme Corp")
        
        print(f"✅ LLM analysis successful!")
        print(f"\nTest result:")
        print(f"  Vendor emails: {result.get('vendor_emails', [])}")
        print(f"  Confidence: {result.get('confidence', 'N/A')}")
        print(f"  Reasoning: {result.get('reasoning', 'N/A')}")
        
        return True
        
    except Exception as e:
        print(f"❌ LLM connectivity failed: {str(e)}")
        print("\nTroubleshooting:")
        print("- Verify OPENAI_API_KEY is correct")
        print("- Check OpenAI API quota/billing")
        print("- Ensure internet connectivity")
        return False

Parameters

Name Type Default Kind
extractor - - positional_or_keyword

Parameter Details

extractor: An instance of VendorEmailExtractor class that contains the analyze_email_with_llm method. This object should be properly initialized with OpenAI API credentials and configured to perform LLM-based email analysis. The extractor is expected to have methods for analyzing email content and extracting vendor contact information.

Return Value

Returns a boolean value: True if the LLM connectivity test succeeds (the analyze_email_with_llm method executes without errors and returns a result), False if any exception occurs during the test (such as API authentication failures, network issues, or quota exceeded errors).

Dependencies

  • sys
  • pathlib
  • vendor_email_extractor
  • vendor_email_config
  • openai

Required Imports

from vendor_email_extractor import VendorEmailExtractor
from vendor_email_config import OPENAI_API_KEY

Usage Example

from vendor_email_extractor import VendorEmailExtractor
from vendor_email_config import OPENAI_API_KEY, TENANT_ID, CLIENT_ID, CLIENT_SECRET, DOMAIN

# Initialize the extractor with required credentials
extractor = VendorEmailExtractor(
    tenant_id=TENANT_ID,
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET,
    openai_api_key=OPENAI_API_KEY,
    domain=DOMAIN
)

# Run the connectivity test
success = test_llm_connectivity(extractor)

if success:
    print("LLM is ready for production use")
else:
    print("Fix LLM connectivity issues before proceeding")

Best Practices

  • Run this test function before processing real emails to ensure API connectivity is working
  • Check the console output for detailed error messages and troubleshooting steps if the test fails
  • Ensure the extractor object is properly initialized with valid credentials before calling this function
  • Use this function as part of a health check or startup validation routine
  • The mock email structure should match the expected format from Microsoft Graph API email objects
  • Monitor OpenAI API quota and billing status if tests consistently fail
  • Consider implementing retry logic or exponential backoff if using this in automated testing pipelines

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function test_llm_client 72.9% similar

    Tests the LLM client functionality by analyzing a sample contract text and verifying the extraction of key contract metadata such as third parties, dates, and status.

    From: /tf/active/vicechatdev/contract_validity_analyzer/test_implementation.py
  • function test_new_fields 71.9% similar

    A test function that validates an LLM client's ability to extract third-party email addresses and tax identification numbers from contract documents.

    From: /tf/active/vicechatdev/contract_validity_analyzer/test_new_fields.py
  • function test_llm_extraction 70.9% similar

    A test function that validates LLM-based contract data extraction by processing a sample contract and verifying the extracted fields against expected values.

    From: /tf/active/vicechatdev/contract_validity_analyzer/test_extractor.py
  • function run_all_tests 70.2% similar

    Orchestrates a comprehensive test suite for the Vendor Email Extractor system, verifying configuration, authentication, mailbox access, email search, and LLM connectivity.

    From: /tf/active/vicechatdev/find_email/test_vendor_extractor.py
  • function test_international_tax_ids 61.1% similar

    A test function that validates an LLM client's ability to extract tax identification numbers and business registration numbers from a multi-party international contract document across 8 different countries.

    From: /tf/active/vicechatdev/contract_validity_analyzer/test_international_tax_ids.py
← Back to Browse