This change remove `BaseComponent`'s: - run_raw - run_batch_raw - run_document - run_batch_document - is_document - is_batch Each component is expected to support multiple types of inputs and a single type of output. Since we want the component to work out-of-the-box with both standardized and customized use cases, supporting multiple types of inputs are expected. At the same time, to reduce the complexity of understanding how to use a component, we restrict a component to only have a single output type. To accommodate these changes, we also refactor some components to remove their run_raw, run_batch_raw... methods, and to decide the common output interface for those components. Tests are updated accordingly. Commit changes: * Add kwargs to vector store's query * Simplify the BaseComponent * Update tests * Remove support for Python 3.8 and 3.9 * Bump version 0.3.0 * Fix github PR caching still use old environment after bumping version --------- Co-authored-by: ian <ian@cinnamon.is>
63 lines
1.8 KiB
Python
63 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
from abc import abstractmethod
|
|
from typing import Type
|
|
|
|
from langchain.schema.embeddings import Embeddings as LCEmbeddings
|
|
from theflow import Param
|
|
|
|
from ..base import BaseComponent
|
|
from ..documents.base import Document
|
|
|
|
|
|
class BaseEmbeddings(BaseComponent):
|
|
@abstractmethod
|
|
def run(
|
|
self, text: str | list[str] | Document | list[Document]
|
|
) -> list[list[float]]:
|
|
...
|
|
|
|
|
|
class LangchainEmbeddings(BaseEmbeddings):
|
|
_lc_class: Type[LCEmbeddings]
|
|
|
|
def __init__(self, **params):
|
|
if self._lc_class is None:
|
|
raise AttributeError(
|
|
"Should set _lc_class attribute to the LLM class from Langchain "
|
|
"if using LLM from Langchain"
|
|
)
|
|
|
|
self._kwargs: dict = {}
|
|
for param in list(params.keys()):
|
|
if param in self._lc_class.__fields__: # type: ignore
|
|
self._kwargs[param] = params.pop(param)
|
|
super().__init__(**params)
|
|
|
|
def __setattr__(self, name, value):
|
|
if name in self._lc_class.__fields__:
|
|
self._kwargs[name] = value
|
|
else:
|
|
super().__setattr__(name, value)
|
|
|
|
@Param.auto(cache=False)
|
|
def agent(self):
|
|
return self._lc_class(**self._kwargs)
|
|
|
|
def run(self, text) -> list[list[float]]:
|
|
input_: list[str] = []
|
|
if not isinstance(text, list):
|
|
text = [text]
|
|
|
|
for item in text:
|
|
if isinstance(item, str):
|
|
input_.append(item)
|
|
elif isinstance(item, Document):
|
|
input_.append(item.text)
|
|
else:
|
|
raise ValueError(
|
|
f"Invalid input type {type(item)}, should be str or Document"
|
|
)
|
|
|
|
return self.agent.embed_documents(input_)
|