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

Add: Include option to normalize & truncate embeddings in create_embe… #1864

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
26 changes: 20 additions & 6 deletions llama_cpp/llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,8 @@ def generate(

sample_idx += 1
if stopping_criteria is not None and stopping_criteria(
self._input_ids[: sample_idx], self._scores[sample_idx - self.n_tokens, :]
self._input_ids[:sample_idx],
self._scores[sample_idx - self.n_tokens, :],
):
return
tokens_or_none = yield token
Expand All @@ -955,7 +956,11 @@ def generate(
)

def create_embedding(
self, input: Union[str, List[str]], model: Optional[str] = None
self,
input: Union[str, List[str]],
model: Optional[str] = None,
normalize: bool = False,
truncate: bool = True,
) -> CreateEmbeddingResponse:
"""Embed a string.

Expand All @@ -972,7 +977,9 @@ def create_embedding(
# get numeric embeddings
embeds: Union[List[List[float]], List[List[List[float]]]]
total_tokens: int
embeds, total_tokens = self.embed(input, return_count=True) # type: ignore
embeds, total_tokens = self.embed(
input=input, normalize=normalize, truncate=truncate, return_count=True
)

# convert to CreateEmbeddingResponse
data: List[Embedding] = [
Expand Down Expand Up @@ -1310,7 +1317,7 @@ def logit_bias_processor(
if seed is not None:
self.set_seed(seed)
else:
self.set_seed(random.Random(self._seed).randint(0, 2 ** 32))
self.set_seed(random.Random(self._seed).randint(0, 2**32))

finish_reason = "length"
multibyte_fix = 0
Expand Down Expand Up @@ -2051,7 +2058,10 @@ def create_chat_completion_openai_v1(
stream = kwargs.get("stream", False) # type: ignore
assert isinstance(stream, bool)
if stream:
return (ChatCompletionChunk(**chunk) for chunk in self.create_chat_completion(*args, **kwargs)) # type: ignore
return (
ChatCompletionChunk(**chunk)
for chunk in self.create_chat_completion(*args, **kwargs)
) # type: ignore
else:
return ChatCompletion(**self.create_chat_completion(*args, **kwargs)) # type: ignore
except ImportError:
Expand Down Expand Up @@ -2310,7 +2320,11 @@ def from_pretrained(
if additional_files:
for additonal_file_name in additional_files:
# find the additional shard file:
matching_additional_files = [file for file in file_list if fnmatch.fnmatch(file, additonal_file_name)]
matching_additional_files = [
file
for file in file_list
if fnmatch.fnmatch(file, additonal_file_name)
]

if len(matching_additional_files) == 0:
raise ValueError(
Expand Down