Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Fast Embedder adapter using langchain class #571

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ core/cat/plugins/*
# tests plugin folder
plugin_folder

# cache embedder
core/local_cache/*

19 changes: 0 additions & 19 deletions core/cat/factory/custom_embedder.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,3 @@ def embed_query(self, text: str) -> List[float]:
ret.raise_for_status()
return ret.json()['data'][0]['embedding']

class CustomFastembedEmbeddings(Embeddings):
"""Use Fastembed for embedding.
"""
def __init__(self, url, model,max_length) -> None:
self.url = url
output = httpx.post(f"{url}/embeddings", json={"model": model, "max_length": max_length}, follow_redirects=True, timeout=None)
output.raise_for_status()


def embed_documents(self, texts: List[str]):
ret = httpx.post(f"{self.url}/embeddings/document", json={"document": texts}, timeout=None)
ret.raise_for_status()
return ret.json()

def embed_query(self, text: str) -> List[float]:
ret = httpx.post(f"{self.url}/embeddings/prompt", json={"prompt": text}, timeout=None)
ret.raise_for_status()
return ret.json()

23 changes: 12 additions & 11 deletions core/cat/factory/embedder.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from typing import Type
import langchain
from pydantic import BaseModel, ConfigDict

from cat.factory.custom_embedder import CustomFastembedEmbeddings, DumbEmbedder, CustomOpenAIEmbeddings
from langchain.embeddings.fastembed import FastEmbedEmbeddings
from cat.factory.custom_embedder import DumbEmbedder, CustomOpenAIEmbeddings


# Base class to manage LLM configuration.
Expand Down Expand Up @@ -107,19 +107,20 @@ class EmbedderCohereConfig(EmbedderSettings):
)


class EmbedderFastEmbedConfig(EmbedderSettings):
url: str
model: str = "intfloat/multilingual-e5-large"
max_length: int = 512

_pyclass: Type = CustomFastembedEmbeddings
class EmbedderQdrantFastEmbedConfig(EmbedderSettings):
model_name: str = "BAAI/bge-base-en"
max_length: int = 512 # Unknown behavior for values > 512.
doc_embed_type: str = "passage" # as suggest on fastembed documentation, "passage" is the best option for documents.
_pyclass: Type = FastEmbedEmbeddings

model_config = ConfigDict(
json_schema_extra = {
"humanReadableName": "Fast Embedder",
"description": "Configuration for Fast embeddings",
"humanReadableName": "Qdrant FastEmbed (Local)",
"description": "Configuration for Qdrant FastEmbed",
}
)



SUPPORTED_EMDEDDING_MODELS = [
Expand All @@ -129,7 +130,7 @@ class EmbedderFastEmbedConfig(EmbedderSettings):
EmbedderOpenAIConfig,
EmbedderAzureOpenAIConfig,
EmbedderCohereConfig,
EmbedderFastEmbedConfig
EmbedderQdrantFastEmbedConfig
]


Expand Down
5 changes: 3 additions & 2 deletions core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies = [
"pandas==1.5.3",
"scikit-learn==1.2.1",
"qdrant_client==1.6.6",
"langchain==0.0.315",
"langchain==0.0.336",
"openai==0.27.5",
"cohere==4.0.4",
"huggingface-hub==0.13.2",
Expand All @@ -37,7 +37,8 @@ dependencies = [
"perflint",
"pylint-actions",
"pytest",
"httpx"
"httpx",
"fastembed==0.1.1"
]

[tool.coverage.run]
Expand Down