repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | from collections.abc import Callable |
| #2 | from typing import Any, Optional |
| #3 | |
| #4 | from embedchain.config.embedder.base import BaseEmbedderConfig |
| #5 | |
| #6 | try: |
| #7 | from chromadb.api.types import Embeddable, EmbeddingFunction, Embeddings |
| #8 | except RuntimeError: |
| #9 | from embedchain.utils.misc import use_pysqlite3 |
| #10 | |
| #11 | use_pysqlite3() |
| #12 | from chromadb.api.types import Embeddable, EmbeddingFunction, Embeddings |
| #13 | |
| #14 | |
| #15 | class EmbeddingFunc(EmbeddingFunction): |
| #16 | def __init__(self, embedding_fn: Callable[[list[str]], list[str]]): |
| #17 | self.embedding_fn = embedding_fn |
| #18 | |
| #19 | def __call__(self, input: Embeddable) -> Embeddings: |
| #20 | return self.embedding_fn(input) |
| #21 | |
| #22 | |
| #23 | class BaseEmbedder: |
| #24 | """ |
| #25 | Class that manages everything regarding embeddings. Including embedding function, loaders and chunkers. |
| #26 | |
| #27 | Embedding functions and vector dimensions are set based on the child class you choose. |
| #28 | To manually overwrite you can use this classes `set_...` methods. |
| #29 | """ |
| #30 | |
| #31 | def __init__(self, config: Optional[BaseEmbedderConfig] = None): |
| #32 | """ |
| #33 | Initialize the embedder class. |
| #34 | |
| #35 | :param config: embedder configuration option class, defaults to None |
| #36 | :type config: Optional[BaseEmbedderConfig], optional |
| #37 | """ |
| #38 | if config is None: |
| #39 | self.config = BaseEmbedderConfig() |
| #40 | else: |
| #41 | self.config = config |
| #42 | self.vector_dimension: int |
| #43 | |
| #44 | def set_embedding_fn(self, embedding_fn: Callable[[list[str]], list[str]]): |
| #45 | """ |
| #46 | Set or overwrite the embedding function to be used by the database to store and retrieve documents. |
| #47 | |
| #48 | :param embedding_fn: Function to be used to generate embeddings. |
| #49 | :type embedding_fn: Callable[[list[str]], list[str]] |
| #50 | :raises ValueError: Embedding function is not callable. |
| #51 | """ |
| #52 | if not hasattr(embedding_fn, "__call__"): |
| #53 | raise ValueError("Embedding function is not a function") |
| #54 | self.embedding_fn = embedding_fn |
| #55 | |
| #56 | def set_vector_dimension(self, vector_dimension: int): |
| #57 | """ |
| #58 | Set or overwrite the vector dimension size |
| #59 | |
| #60 | :param vector_dimension: vector dimension size |
| #61 | :type vector_dimension: int |
| #62 | """ |
| #63 | if not isinstance(vector_dimension, int): |
| #64 | raise TypeError("vector dimension must be int") |
| #65 | self.vector_dimension = vector_dimension |
| #66 | |
| #67 | @staticmethod |
| #68 | def _langchain_default_concept(embeddings: Any): |
| #69 | """ |
| #70 | Langchains default function layout for embeddings. |
| #71 | |
| #72 | :param embeddings: Langchain embeddings |
| #73 | :type embeddings: Any |
| #74 | :return: embedding function |
| #75 | :rtype: Callable |
| #76 | """ |
| #77 | |
| #78 | return EmbeddingFunc(embeddings.embed_documents) |
| #79 | |
| #80 | def to_embeddings(self, data: str, **_): |
| #81 | """ |
| #82 | Convert data to embeddings |
| #83 | |
| #84 | :param data: data to convert to embeddings |
| #85 | :type data: str |
| #86 | :return: embeddings |
| #87 | :rtype: list[float] |
| #88 | """ |
| #89 | embeddings = self.embedding_fn([data]) |
| #90 | return embeddings[0] |
| #91 |