Skip to content

Commit

Permalink
Merge pull request #571 from valentimarco/develop
Browse files Browse the repository at this point in the history
Change Fast Embedder adapter using langchain class
  • Loading branch information
pieroit authored Nov 23, 2023
2 parents 0c79621 + b443528 commit 7d07401
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,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

0 comments on commit 7d07401

Please sign in to comment.