* migrate to pyright * Refactor type checking to use Pyright, update dependencies, and clean up code. - Replaced MyPy with Pyright in configuration files and CI workflows. - Updated `pyproject.toml` and `uv.lock` to reflect new dependencies and versions. - Adjusted type hints and fixed minor code issues across various modules for better compatibility with Pyright. - Added new packages `backoff` and `posthog` to the project dependencies. * Update CI workflows to install all extra dependencies for type checking and unit tests * Update dependencies in uv.lock to replace MyPy with Pyright and add nodeenv package. Adjust type hinting in config.py for compatibility with Pyright.
65 lines
2.2 KiB
Python
65 lines
2.2 KiB
Python
"""
|
|
Copyright 2024, Zep Software, Inc.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
"""
|
|
|
|
from collections.abc import Iterable
|
|
|
|
import voyageai # type: ignore
|
|
from pydantic import Field
|
|
|
|
from .client import EmbedderClient, EmbedderConfig
|
|
|
|
DEFAULT_EMBEDDING_MODEL = 'voyage-3'
|
|
|
|
|
|
class VoyageAIEmbedderConfig(EmbedderConfig):
|
|
embedding_model: str = Field(default=DEFAULT_EMBEDDING_MODEL)
|
|
api_key: str | None = None
|
|
|
|
|
|
class VoyageAIEmbedder(EmbedderClient):
|
|
"""
|
|
VoyageAI Embedder Client
|
|
"""
|
|
|
|
def __init__(self, config: VoyageAIEmbedderConfig | None = None):
|
|
if config is None:
|
|
config = VoyageAIEmbedderConfig()
|
|
self.config = config
|
|
self.client = voyageai.AsyncClient(api_key=config.api_key) # type: ignore[reportUnknownMemberType]
|
|
|
|
async def create(
|
|
self, input_data: str | list[str] | Iterable[int] | Iterable[Iterable[int]]
|
|
) -> list[float]:
|
|
if isinstance(input_data, str):
|
|
input_list = [input_data]
|
|
elif isinstance(input_data, list):
|
|
input_list = [str(i) for i in input_data if i]
|
|
else:
|
|
input_list = [str(i) for i in input_data if i is not None]
|
|
|
|
input_list = [i for i in input_list if i]
|
|
if len(input_list) == 0:
|
|
return []
|
|
|
|
result = await self.client.embed(input_list, model=self.config.embedding_model)
|
|
return [float(x) for x in result.embeddings[0][: self.config.embedding_dim]]
|
|
|
|
async def create_batch(self, input_data_list: list[str]) -> list[list[float]]:
|
|
result = await self.client.embed(input_data_list, model=self.config.embedding_model)
|
|
return [
|
|
[float(x) for x in embedding[: self.config.embedding_dim]]
|
|
for embedding in result.embeddings
|
|
]
|