mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
"""Test openai embeddings."""
|
|
import numpy as np
|
|
import openai
|
|
|
|
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
|
|
assert len(output[0]) == 1536
|
|
|
|
|
|
def test_openai_embedding_documents_multiple() -> None:
|
|
"""Test openai embeddings."""
|
|
documents = ["foo bar", "bar foo", "foo"]
|
|
embedding = OpenAIEmbeddings(chunk_size=2)
|
|
embedding.embedding_ctx_length = 8191
|
|
output = embedding.embed_documents(documents)
|
|
assert len(output) == 3
|
|
assert len(output[0]) == 1536
|
|
assert len(output[1]) == 1536
|
|
assert len(output[2]) == 1536
|
|
|
|
|
|
def test_openai_embedding_query() -> None:
|
|
"""Test openai embeddings."""
|
|
document = "foo bar"
|
|
embedding = OpenAIEmbeddings()
|
|
output = embedding.embed_query(document)
|
|
assert len(output) == 1536
|
|
|
|
|
|
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
|