Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

2.9 KB
Raw Blame
# pylint: disable=duplicate-code
"""
Azure OpenAI Test Suite

This module contains a suite of tests for OpenAI functionality using the OpenAI Python library.
It includes tests for various OpenAI API endpoints such as completions, chat completions,
embeddings creation, fine-tuning job creation, image generation, image variation creation,
and audio speech generation.

The tests are designed to cover different aspects of OpenAI's capabilities and serve as a
validation mechanism for the integration with the OpenLIT monitoring system.

Global client and initialization are set up for the OpenAI client and OpenLIT monitoring.

Environment Variables:
    - AZURE_OPENAI_API_TOKEN: OpenAI API key for authentication.

Note: Ensure the environment is properly configured for Azure OpenAI access and OpenLIT monitoring
prior to running these tests.
"""

import os
from openai import AzureOpenAI
import openlit

# Global client
client = AzureOpenAI(
    api_key=os.getenv("AZURE_OPENAI_API_TOKEN"),
    api_version = "2024-02-01",
    azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
)

azure_chat_model = os.getenv("AZURE_OPENAI_CHAT_MODEL")
azure_embedding_model = os.getenv("AZURE_OPENAI_EMBEDDING_MODEL")
azure_image_model = os.getenv("AZURE_OPENAI_IMAGE_MODEL")

# Global initialization
# pylint: disable=line-too-long
openlit.init(environment="openlit-testing", application_name="openlit-python-test")

def test_completion():
    """
    Test the completions.

    Raises:
        AssertionError: If the completion response object is not as expected.
    """

    completions_resp = client.completions.create(
        model=azure_chat_model,
        prompt="Hello world",
        max_tokens=100
    )
    assert completions_resp.object == 'text_completion'

def test_chat_completion():
    """
    Test chat completions.

    Raises:
        AssertionError: If the chat completion response object is not as expected.
    """

    chat_completions_resp = client.chat.completions.create(
        model=azure_chat_model,
        max_tokens=100,
        messages=[{"role": "user", "content": "What is Grafana?"}]
    )
    assert chat_completions_resp.object == 'chat.completion'

def test_embedding_creation():
    """
    Test embedding creation.

    Raises:
        AssertionError: If the embedding response object is not as expected.
    """

    embeddings_resp = client.embeddings.create(
        model=azure_embedding_model,
        input="The quick brown fox jumped over the lazy dog",
        encoding_format="float"
    )
    assert embeddings_resp.data[0].object == 'embedding'

def test_image_generation():
    """
    Test image generation.

    Raises:
        AssertionError: If the image generation response created timestamp is not present.
    """

    image_generation_resp = client.images.generate(
        model=azure_chat_model,
        prompt='Generate an image for LLM Observability Dashboard',
        n=1
    )
    assert image_generation_resp.created is not None