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

support TEI #1553

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
53 changes: 53 additions & 0 deletions dsp/modules/sentence_vectorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import numpy as np
import openai
import math
import requests


class BaseSentenceVectorizer(abc.ABC):
Expand Down Expand Up @@ -306,3 +308,54 @@ def __call__(self, inp_examples: List["Example"]) -> np.ndarray:

embeddings = np.array(embedding_list, dtype=np.float32)
return embeddings


class TEIVectorizer(BaseSentenceVectorizer):
"""The TEIVectorizer class utilizes the TEI(Text Embeddings Inference) Embeddings API to
convert text into embeddings.

For detailed information on the supported models, visit: https://github.com/huggingface/text-embeddings-inference.

`model` is embedding model name.
`embed_batch_size` is the maximum batch size for a single request.
`api_key` request authorization.
`api_url` custom inference endpoint url.

To learn more about getting started with TEI, visit: https://github.com/huggingface/text-embeddings-inference.
"""

def __init__(
self,
model: Optional[str] = "bge-base-en-v1.5",
embed_batch_size: int = 256,
api_key: Optional[str] = None,
api_url: str = "",
):
self.model = model
self.embed_batch_size = embed_batch_size
self.api_key = api_key
self.api_url = api_url

@property
def _headers(self) -> dict:
return {"Authorization": f"Bearer {self.api_key}"}

def __call__(self, inp_examples: List["Example"]) -> np.ndarray:
text_to_vectorize = self._extract_text_from_examples(inp_examples)
embeddings_list = []

n = math.ceil(len(text_to_vectorize) / self.embed_batch_size)
for i in range(n):
response = requests.post(
self.api_url,
headers=self._headers,
json={
"inputs": text_to_vectorize[i * self.embed_batch_size:(i + 1) * self.embed_batch_size],
"normalize": True,
"truncate": True
},
)
embeddings_list.extend(response.json())

embeddings = np.array(embeddings_list, dtype=np.float32)
return embeddings
6 changes: 6 additions & 0 deletions dspy/clients/lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ def __call__(self, prompt=None, messages=None, **kwargs):
def inspect_history(self, n: int = 1):
_inspect_history(self, n)

def copy(self, **kwargs):
"""Returns a copy of the language model with the same parameters."""
kwargs = {**self.kwargs, **kwargs}

return self.__class__(model=self.model, **kwargs)


@functools.lru_cache(maxsize=None)
def cached_litellm_completion(request):
Expand Down