Add Huggingface embeddings and Cohere embeddings (#63)
* Add huggingface embeddings and cohere embeddings * Update openai interface and the mock for newer OpenAI SDK --------- Co-authored-by: trducng <trungduc1992@gmail.com>
This commit is contained in:
@@ -2,6 +2,8 @@ import json
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from kotaemon.embeddings.cohere import CohereEmbdeddings
|
||||
from kotaemon.embeddings.huggingface import HuggingFaceEmbeddings
|
||||
from kotaemon.embeddings.openai import AzureOpenAIEmbeddings
|
||||
|
||||
with open(Path(__file__).parent / "resources" / "embedding_openai_batch.json") as f:
|
||||
@@ -12,7 +14,7 @@ with open(Path(__file__).parent / "resources" / "embedding_openai.json") as f:
|
||||
|
||||
|
||||
@patch(
|
||||
"openai.api_resources.embedding.Embedding.create",
|
||||
"openai.resources.embeddings.Embeddings.create",
|
||||
side_effect=lambda *args, **kwargs: openai_embedding,
|
||||
)
|
||||
def test_azureopenai_embeddings_raw(openai_embedding_call):
|
||||
@@ -29,7 +31,7 @@ def test_azureopenai_embeddings_raw(openai_embedding_call):
|
||||
|
||||
|
||||
@patch(
|
||||
"openai.api_resources.embedding.Embedding.create",
|
||||
"openai.resources.embeddings.Embeddings.create",
|
||||
side_effect=lambda *args, **kwargs: openai_embedding_batch,
|
||||
)
|
||||
def test_azureopenai_embeddings_batch_raw(openai_embedding_call):
|
||||
@@ -44,3 +46,42 @@ def test_azureopenai_embeddings_batch_raw(openai_embedding_call):
|
||||
assert isinstance(output[0], list)
|
||||
assert isinstance(output[0][0], float)
|
||||
openai_embedding_call.assert_called()
|
||||
|
||||
|
||||
@patch(
|
||||
"sentence_transformers.SentenceTransformer",
|
||||
side_effect=lambda *args, **kwargs: None,
|
||||
)
|
||||
@patch(
|
||||
"langchain.embeddings.huggingface.HuggingFaceBgeEmbeddings.embed_query",
|
||||
side_effect=lambda *args, **kwargs: [1.0, 2.1, 3.2],
|
||||
)
|
||||
def test_huggingface_embddings(
|
||||
langchain_huggingface_embedding_call, sentence_transformers_init
|
||||
):
|
||||
model = HuggingFaceEmbeddings(
|
||||
model_name="intfloat/multilingual-e5-large",
|
||||
model_kwargs={"device": "cpu"},
|
||||
encode_kwargs={"normalize_embeddings": False},
|
||||
)
|
||||
|
||||
output = model("Hello World")
|
||||
assert isinstance(output, list)
|
||||
assert isinstance(output[0], float)
|
||||
sentence_transformers_init.assert_called()
|
||||
langchain_huggingface_embedding_call.assert_called()
|
||||
|
||||
|
||||
@patch(
|
||||
"langchain.embeddings.cohere.CohereEmbeddings.embed_query",
|
||||
side_effect=lambda *args, **kwargs: [1.0, 2.1, 3.2],
|
||||
)
|
||||
def test_cohere_embddings(langchain_cohere_embedding_call):
|
||||
model = CohereEmbdeddings(
|
||||
model="embed-english-light-v2.0", cohere_api_key="my-api-key"
|
||||
)
|
||||
|
||||
output = model("Hello World")
|
||||
assert isinstance(output, list)
|
||||
assert isinstance(output[0], float)
|
||||
langchain_cohere_embedding_call.assert_called()
|
||||
|
Reference in New Issue
Block a user