2022-11-02 04:29:39 +00:00
|
|
|
"""Test openai embeddings."""
|
2023-04-25 05:19:47 +00:00
|
|
|
import numpy as np
|
|
|
|
import openai
|
2023-06-22 06:16:33 +00:00
|
|
|
import pytest
|
2023-04-25 05:19:47 +00:00
|
|
|
|
2022-11-02 04:29:39 +00:00
|
|
|
from langchain.embeddings.openai import OpenAIEmbeddings
|
|
|
|
|
|
|
|
|
|
|
|
def test_openai_embedding_documents() -> None:
|
|
|
|
"""Test openai embeddings."""
|
|
|
|
documents = ["foo bar"]
|
|
|
|
embedding = OpenAIEmbeddings()
|
|
|
|
output = embedding.embed_documents(documents)
|
|
|
|
assert len(output) == 1
|
2023-02-10 14:59:50 +00:00
|
|
|
assert len(output[0]) == 1536
|
|
|
|
|
|
|
|
|
|
|
|
def test_openai_embedding_documents_multiple() -> None:
|
|
|
|
"""Test openai embeddings."""
|
|
|
|
documents = ["foo bar", "bar foo", "foo"]
|
2023-03-09 05:24:18 +00:00
|
|
|
embedding = OpenAIEmbeddings(chunk_size=2)
|
2023-02-16 07:02:32 +00:00
|
|
|
embedding.embedding_ctx_length = 8191
|
2023-03-09 05:24:18 +00:00
|
|
|
output = embedding.embed_documents(documents)
|
2023-02-10 14:59:50 +00:00
|
|
|
assert len(output) == 3
|
|
|
|
assert len(output[0]) == 1536
|
|
|
|
assert len(output[1]) == 1536
|
|
|
|
assert len(output[2]) == 1536
|
2022-11-02 04:29:39 +00:00
|
|
|
|
|
|
|
|
2023-06-22 06:16:33 +00:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_openai_embedding_documents_async_multiple() -> None:
|
|
|
|
"""Test openai embeddings."""
|
|
|
|
documents = ["foo bar", "bar foo", "foo"]
|
|
|
|
embedding = OpenAIEmbeddings(chunk_size=2)
|
|
|
|
embedding.embedding_ctx_length = 8191
|
|
|
|
output = await embedding.aembed_documents(documents)
|
|
|
|
assert len(output) == 3
|
|
|
|
assert len(output[0]) == 1536
|
|
|
|
assert len(output[1]) == 1536
|
|
|
|
assert len(output[2]) == 1536
|
|
|
|
|
|
|
|
|
2022-11-02 04:29:39 +00:00
|
|
|
def test_openai_embedding_query() -> None:
|
|
|
|
"""Test openai embeddings."""
|
|
|
|
document = "foo bar"
|
|
|
|
embedding = OpenAIEmbeddings()
|
|
|
|
output = embedding.embed_query(document)
|
2023-02-10 14:59:50 +00:00
|
|
|
assert len(output) == 1536
|
2023-04-25 05:19:47 +00:00
|
|
|
|
|
|
|
|
2023-06-22 06:16:33 +00:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_openai_embedding_async_query() -> None:
|
|
|
|
"""Test openai embeddings."""
|
|
|
|
document = "foo bar"
|
|
|
|
embedding = OpenAIEmbeddings()
|
|
|
|
output = await embedding.aembed_query(document)
|
|
|
|
assert len(output) == 1536
|
|
|
|
|
|
|
|
|
2023-04-25 05:19:47 +00:00
|
|
|
def test_openai_embedding_with_empty_string() -> None:
|
|
|
|
"""Test openai embeddings with empty string."""
|
|
|
|
document = ["", "abc"]
|
|
|
|
embedding = OpenAIEmbeddings()
|
|
|
|
output = embedding.embed_documents(document)
|
|
|
|
assert len(output) == 2
|
|
|
|
assert len(output[0]) == 1536
|
|
|
|
expected_output = openai.Embedding.create(input="", model="text-embedding-ada-002")[
|
|
|
|
"data"
|
|
|
|
][0]["embedding"]
|
|
|
|
assert np.allclose(output[0], expected_output)
|
|
|
|
assert len(output[1]) == 1536
|