|
| 1 | +# type: ignore |
| 2 | +""" |
| 3 | +Tests for the TensorZero embeddings API using the OpenAI Python client |
| 4 | +
|
| 5 | +These tests cover the embeddings functionality of the TensorZero OpenAI-compatible interface. |
| 6 | +
|
| 7 | +To run: |
| 8 | +``` |
| 9 | +pytest tests/test_embeddings.py |
| 10 | +``` |
| 11 | +or |
| 12 | +``` |
| 13 | +uv run pytest tests/test_embeddings.py |
| 14 | +``` |
| 15 | +""" |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.asyncio |
| 21 | +async def test_basic_embeddings(async_client): |
| 22 | + """Test basic embeddings generation with a single input""" |
| 23 | + result = await async_client.embeddings.create( |
| 24 | + input="Hello, world!", |
| 25 | + model="text-embedding-3-small", |
| 26 | + ) |
| 27 | + |
| 28 | + # Verify the response structure |
| 29 | + assert result.model == "text-embedding-3-small" |
| 30 | + assert len(result.data) == 1 |
| 31 | + assert result.data[0].index == 0 |
| 32 | + assert result.data[0].object == "embedding" |
| 33 | + assert len(result.data[0].embedding) > 0 # Should have embedding vector |
| 34 | + assert result.usage.prompt_tokens > 0 |
| 35 | + assert result.usage.total_tokens > 0 |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.asyncio |
| 39 | +async def test_basic_embeddings_shorthand(async_client): |
| 40 | + """Test basic embeddings generation with a single input""" |
| 41 | + result = await async_client.embeddings.create( |
| 42 | + input="Hello, world!", |
| 43 | + model="openai::text-embedding-3-large", |
| 44 | + ) |
| 45 | + |
| 46 | + # Verify the response structure |
| 47 | + assert result.model == "openai::text-embedding-3-large" |
| 48 | + assert len(result.data) == 1 |
| 49 | + assert result.data[0].index == 0 |
| 50 | + assert result.data[0].object == "embedding" |
| 51 | + assert len(result.data[0].embedding) > 0 # Should have embedding vector |
| 52 | + assert result.usage.prompt_tokens > 0 |
| 53 | + assert result.usage.total_tokens > 0 |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.asyncio |
| 57 | +async def test_batch_embeddings(async_client): |
| 58 | + """Test embeddings generation with multiple inputs""" |
| 59 | + inputs = [ |
| 60 | + "Hello, world!", |
| 61 | + "How are you today?", |
| 62 | + "This is a test of batch embeddings.", |
| 63 | + ] |
| 64 | + |
| 65 | + result = await async_client.embeddings.create( |
| 66 | + input=inputs, |
| 67 | + model="text-embedding-3-small", |
| 68 | + ) |
| 69 | + |
| 70 | + # Verify the response structure |
| 71 | + assert result.model == "text-embedding-3-small" |
| 72 | + assert len(result.data) == len(inputs) |
| 73 | + |
| 74 | + for i, embedding_data in enumerate(result.data): |
| 75 | + assert embedding_data.index == i |
| 76 | + assert embedding_data.object == "embedding" |
| 77 | + assert len(embedding_data.embedding) > 0 |
| 78 | + |
| 79 | + assert result.usage.prompt_tokens > 0 |
| 80 | + assert result.usage.total_tokens > 0 |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.asyncio |
| 84 | +async def test_embeddings_with_dimensions(async_client): |
| 85 | + """Test embeddings with specified dimensions""" |
| 86 | + result = await async_client.embeddings.create( |
| 87 | + input="Test with specific dimensions", |
| 88 | + model="text-embedding-3-small", |
| 89 | + dimensions=512, |
| 90 | + ) |
| 91 | + |
| 92 | + # Verify the response structure |
| 93 | + assert result.model == "text-embedding-3-small" |
| 94 | + assert len(result.data) == 1 |
| 95 | + # Should match requested dimensions |
| 96 | + assert len(result.data[0].embedding) == 512 |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.asyncio |
| 100 | +async def test_embeddings_with_encoding_format_float(async_client): |
| 101 | + """Test embeddings with different encoding formats""" |
| 102 | + result = await async_client.embeddings.create( |
| 103 | + input="Test encoding format", |
| 104 | + model="text-embedding-3-small", |
| 105 | + encoding_format="float", |
| 106 | + ) |
| 107 | + |
| 108 | + # Verify the response structure |
| 109 | + assert result.model == "text-embedding-3-small" |
| 110 | + assert len(result.data) == 1 |
| 111 | + assert isinstance(result.data[0].embedding[0], float) |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.asyncio |
| 115 | +async def test_embeddings_with_encoding_format_base64(async_client): |
| 116 | + """Test embeddings with different encoding formats""" |
| 117 | + result = await async_client.embeddings.create( |
| 118 | + input="Test encoding format", |
| 119 | + model="text-embedding-3-small", |
| 120 | + encoding_format="base64", |
| 121 | + ) |
| 122 | + |
| 123 | + # Verify the response structure |
| 124 | + assert result.model == "text-embedding-3-small" |
| 125 | + assert len(result.data) == 1 |
| 126 | + assert isinstance(result.data[0].embedding, str) |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.asyncio |
| 130 | +async def test_embeddings_with_user_parameter(async_client): |
| 131 | + """Test embeddings with user parameter for tracking""" |
| 132 | + user_id = "test_user_123" |
| 133 | + result = await async_client.embeddings.create( |
| 134 | + input="Test with user parameter", |
| 135 | + model="text-embedding-3-small", |
| 136 | + user=user_id, |
| 137 | + ) |
| 138 | + |
| 139 | + # Verify the response structure |
| 140 | + assert result.model == "text-embedding-3-small" |
| 141 | + assert len(result.data) == 1 |
| 142 | + assert len(result.data[0].embedding) > 0 |
| 143 | + |
| 144 | + |
| 145 | +@pytest.mark.asyncio |
| 146 | +async def test_embeddings_invalid_model_error(async_client): |
| 147 | + """Test that invalid model name raises appropriate error""" |
| 148 | + with pytest.raises(Exception) as exc_info: |
| 149 | + await async_client.embeddings.create( |
| 150 | + input="Test invalid model", |
| 151 | + model="tensorzero::model_name::nonexistent_model", |
| 152 | + ) |
| 153 | + |
| 154 | + # Should get a 404 error for unknown model |
| 155 | + assert exc_info.value.status_code == 404 |
| 156 | + |
| 157 | + |
| 158 | +@pytest.mark.asyncio |
| 159 | +async def test_embeddings_large_batch(async_client): |
| 160 | + """Test embeddings with a larger batch of inputs""" |
| 161 | + # Create a batch of 10 different inputs |
| 162 | + inputs = [f"This is test input number {i + 1}" for i in range(10)] |
| 163 | + |
| 164 | + result = await async_client.embeddings.create( |
| 165 | + input=inputs, |
| 166 | + model="text-embedding-3-small", |
| 167 | + ) |
| 168 | + |
| 169 | + # Verify the response structure |
| 170 | + assert result.model == "text-embedding-3-small" |
| 171 | + assert len(result.data) == 10 |
| 172 | + |
| 173 | + # Verify each embedding |
| 174 | + for i, embedding_data in enumerate(result.data): |
| 175 | + assert embedding_data.index == i |
| 176 | + assert embedding_data.object == "embedding" |
| 177 | + assert len(embedding_data.embedding) > 0 |
| 178 | + |
| 179 | + assert result.usage.prompt_tokens > 0 |
| 180 | + assert result.usage.total_tokens > 0 |
| 181 | + |
| 182 | + |
| 183 | +@pytest.mark.asyncio |
| 184 | +async def test_embeddings_consistency(async_client): |
| 185 | + """Test that the same input produces consistent embeddings""" |
| 186 | + input_text = "This is a consistency test" |
| 187 | + |
| 188 | + # Generate embeddings twice with the same input |
| 189 | + result1 = await async_client.embeddings.create( |
| 190 | + input=input_text, |
| 191 | + model="text-embedding-3-small", |
| 192 | + ) |
| 193 | + |
| 194 | + result2 = await async_client.embeddings.create( |
| 195 | + input=input_text, |
| 196 | + model="text-embedding-3-small", |
| 197 | + ) |
| 198 | + |
| 199 | + # Both should have the same model and structure |
| 200 | + assert result1.model == result2.model |
| 201 | + assert len(result1.data) == len(result2.data) == 1 |
| 202 | + assert len(result1.data[0].embedding) == len(result2.data[0].embedding) |
| 203 | + |
| 204 | + # The embeddings should be identical for the same input |
| 205 | + # (assuming deterministic behavior or proper caching) |
| 206 | + embedding1 = result1.data[0].embedding |
| 207 | + embedding2 = result2.data[0].embedding |
| 208 | + |
| 209 | + # Check that embeddings are similar (allowing for small numerical differences) |
| 210 | + for i in range(min(10, len(embedding1))): # Check first 10 dimensions |
| 211 | + assert abs(embedding1[i] - embedding2[i]) < 0.01, ( |
| 212 | + f"Embeddings differ significantly at index {i}" |
| 213 | + ) |
0 commit comments