Created
December 20, 2023 03:56
-
-
Save aelaguiz/d5800d5019494776d53ebe33753c5b1f to your computer and use it in GitHub Desktop.
Langchain + CTransformers embedding function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from langchain_community.llms.ctransformers import CTransformers | |
from langchain_core.embeddings import Embeddings | |
from typing import List | |
import asyncio | |
class CTransformersEmbeddings(Embeddings): | |
def __init__(self, llm): | |
self.llm = llm | |
def embed_documents(self, texts: List[str]) -> List[List[float]]: | |
res = [] | |
for t in texts: | |
embedding = self.llm.client.embed(t) | |
res.append(embedding) | |
return res | |
def embed_query(self, text: str) -> List[float]: | |
return self.llm.client.embed(text) | |
async def aembed_documents(self, texts: List[str]) -> List[List[float]]: | |
"""Asynchronous Embed search docs.""" | |
return await asyncio.get_running_loop().run_in_executor( | |
None, self.embed_documents, texts | |
) | |
async def aembed_query(self, text: str) -> List[float]: | |
"""Asynchronous Embed query text.""" | |
return await asyncio.get_running_loop().run_in_executor( | |
None, self.embed_query, text | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment