MCPcopy Index your code
hub / github.com/abetlen/llama-cpp-python / create_embedding

Method create_embedding

llama_cpp/llama.py:1020–1058  ·  view source on GitHub ↗

Embed a string. Args: input: The utf-8 encoded string to embed. Returns: An embedding object.

(
        self, input: Union[str, List[str]], model: Optional[str] = None
    )

Source from the content-addressed store, hash-verified

1018 )
1019
1020 def create_embedding(
1021 self, input: Union[str, List[str]], model: Optional[str] = None
1022 ) -> CreateEmbeddingResponse:
1023 """Embed a string.
1024
1025 Args:
1026 input: The utf-8 encoded string to embed.
1027
1028 Returns:
1029 An embedding object.
1030 """
1031 model_name: str = model if model is not None else self.model_path
1032
1033 input = input if isinstance(input, list) else [input]
1034
1035 # get numeric embeddings
1036 embeds: Union[List[List[float]], List[List[List[float]]]]
1037 total_tokens: int
1038 embeds, total_tokens = self.embed(input, return_count=True) # type: ignore
1039
1040 # convert to CreateEmbeddingResponse
1041 data: List[Embedding] = [
1042 {
1043 "object": "embedding",
1044 "embedding": emb,
1045 "index": idx,
1046 }
1047 for idx, emb in enumerate(embeds)
1048 ]
1049
1050 return {
1051 "object": "list",
1052 "data": data,
1053 "model": model_name,
1054 "usage": {
1055 "prompt_tokens": total_tokens,
1056 "total_tokens": total_tokens,
1057 },
1058 }
1059
1060 def embed(
1061 self,

Callers

nothing calls this directly

Calls 1

embedMethod · 0.95

Tested by

no test coverage detected