mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-01 02:50:47 +00:00
Add embeddings for AwaEmbedding (#8353)
- Description: Adds AwaEmbeddings class for embeddings, which provides users with a convenient way to do fine-tuning, as well as the potential need for multimodality - Tag maintainer: @baskaryan Create `Awa.ipynb`: an example notebook for AwaEmbeddings class Modify `embeddings/__init__.py`: Import the class Create `embeddings/awa.py`: The embedding class Create `embeddings/test_awa.py`: The test file. --------- Co-authored-by: taozhiwang <taozhiwa@gmail.com>
This commit is contained in:
@@ -6,6 +6,7 @@ from langchain.embeddings.aleph_alpha import (
|
||||
AlephAlphaAsymmetricSemanticEmbedding,
|
||||
AlephAlphaSymmetricSemanticEmbedding,
|
||||
)
|
||||
from langchain.embeddings.awa import AwaEmbeddings
|
||||
from langchain.embeddings.bedrock import BedrockEmbeddings
|
||||
from langchain.embeddings.clarifai import ClarifaiEmbeddings
|
||||
from langchain.embeddings.cohere import CohereEmbeddings
|
||||
@@ -78,6 +79,7 @@ __all__ = [
|
||||
"NLPCloudEmbeddings",
|
||||
"GPT4AllEmbeddings",
|
||||
"LocalAIEmbeddings",
|
||||
"AwaEmbeddings",
|
||||
]
|
||||
|
||||
|
||||
|
56
libs/langchain/langchain/embeddings/awa.py
Normal file
56
libs/langchain/langchain/embeddings/awa.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from pydantic import BaseModel, root_validator
|
||||
|
||||
from langchain.embeddings.base import Embeddings
|
||||
|
||||
|
||||
class AwaEmbeddings(BaseModel, Embeddings):
|
||||
client: Any #: :meta private:
|
||||
model: str = "all-mpnet-base-v2"
|
||||
|
||||
@root_validator()
|
||||
def validate_environment(cls, values: Dict) -> Dict:
|
||||
"""Validate that awadb library is installed."""
|
||||
|
||||
try:
|
||||
from awadb import AwaEmbedding
|
||||
except ImportError as exc:
|
||||
raise ImportError(
|
||||
"Could not import awadb library. "
|
||||
"Please install it with `pip install awadb`"
|
||||
) from exc
|
||||
values["client"] = AwaEmbedding()
|
||||
return values
|
||||
|
||||
def set_model(self, model_name: str) -> None:
|
||||
"""Set the model used for embedding.
|
||||
The default model used is all-mpnet-base-v2
|
||||
|
||||
Args:
|
||||
model_name: A string which represents the name of model.
|
||||
"""
|
||||
self.model = model_name
|
||||
self.client.model_name = model_name
|
||||
|
||||
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
||||
"""Embed a list of documents using AwaEmbedding.
|
||||
|
||||
Args:
|
||||
texts: The list of texts need to be embedded
|
||||
|
||||
Returns:
|
||||
List of embeddings, one for each text.
|
||||
"""
|
||||
return self.client.EmbeddingBatch(texts)
|
||||
|
||||
def embed_query(self, text: str) -> List[float]:
|
||||
"""Compute query embeddings using AwaEmbedding.
|
||||
|
||||
Args:
|
||||
text: The text to embed.
|
||||
|
||||
Returns:
|
||||
Embeddings for the text.
|
||||
"""
|
||||
return self.client.Embedding(text)
|
@@ -0,0 +1,19 @@
|
||||
"""Test Awa Embedding"""
|
||||
from langchain.embeddings.awa import AwaEmbeddings
|
||||
|
||||
|
||||
def test_awa_embedding_documents() -> None:
|
||||
"""Test Awa embeddings for documents."""
|
||||
documents = ["foo bar", "test document"]
|
||||
embedding = AwaEmbeddings()
|
||||
output = embedding.embed_documents(documents)
|
||||
assert len(output) == 2
|
||||
assert len(output[0]) == 768
|
||||
|
||||
|
||||
def test_awa_embedding_query() -> None:
|
||||
"""Test Awa embeddings for query."""
|
||||
document = "foo bar"
|
||||
embedding = AwaEmbeddings()
|
||||
output = embedding.embed_query(document)
|
||||
assert len(output) == 768
|
Reference in New Issue
Block a user