from chromadb.api.types import EmbeddingFunction, Documents
import requests
class RemoteEmbeddingFunction(EmbeddingFunction[Documents]):
def __init__(self, api_url: str, api_key: str):
self._api_url = api_url
self._api_key = api_key
def __call__(self, input: Documents) -> List[List[float]]:
response = requests.post(
self._api_url,
headers={"Authorization": f"Bearer {self._api_key}"},
json={"texts": input}
)
return response.json()["embeddings"]
@staticmethod
def name() -> str:
return "remote_embedding_function"
@staticmethod
def build_from_config(config: dict) -> "RemoteEmbeddingFunction":
return RemoteEmbeddingFunction(
api_url=config["api_url"],
api_key=config["api_key"]
)
def get_config(self) -> dict:
return {
"api_url": self._api_url,
"api_key": self._api_key
}