hammad-python 0.0.14__py3-none-any.whl → 0.0.15__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hammad_python-0.0.15.dist-info/METADATA +184 -0
- hammad_python-0.0.15.dist-info/RECORD +4 -0
- hammad/__init__.py +0 -1
- hammad/ai/__init__.py +0 -1
- hammad/ai/_utils.py +0 -142
- hammad/ai/completions/__init__.py +0 -45
- hammad/ai/completions/client.py +0 -684
- hammad/ai/completions/create.py +0 -710
- hammad/ai/completions/settings.py +0 -100
- hammad/ai/completions/types.py +0 -792
- hammad/ai/completions/utils.py +0 -486
- hammad/ai/embeddings/__init__.py +0 -35
- hammad/ai/embeddings/client/__init__.py +0 -1
- hammad/ai/embeddings/client/base_embeddings_client.py +0 -26
- hammad/ai/embeddings/client/fastembed_text_embeddings_client.py +0 -200
- hammad/ai/embeddings/client/litellm_embeddings_client.py +0 -288
- hammad/ai/embeddings/create.py +0 -159
- hammad/ai/embeddings/types.py +0 -69
- hammad/cache/__init__.py +0 -40
- hammad/cache/base_cache.py +0 -181
- hammad/cache/cache.py +0 -169
- hammad/cache/decorators.py +0 -261
- hammad/cache/file_cache.py +0 -80
- hammad/cache/ttl_cache.py +0 -74
- hammad/cli/__init__.py +0 -33
- hammad/cli/animations.py +0 -573
- hammad/cli/plugins.py +0 -781
- hammad/cli/styles/__init__.py +0 -55
- hammad/cli/styles/settings.py +0 -139
- hammad/cli/styles/types.py +0 -358
- hammad/cli/styles/utils.py +0 -480
- hammad/data/__init__.py +0 -56
- hammad/data/collections/__init__.py +0 -34
- hammad/data/collections/base_collection.py +0 -58
- hammad/data/collections/collection.py +0 -452
- hammad/data/collections/searchable_collection.py +0 -556
- hammad/data/collections/vector_collection.py +0 -596
- hammad/data/configurations/__init__.py +0 -35
- hammad/data/configurations/configuration.py +0 -564
- hammad/data/databases/__init__.py +0 -21
- hammad/data/databases/database.py +0 -902
- hammad/data/models/__init__.py +0 -44
- hammad/data/models/base/__init__.py +0 -35
- hammad/data/models/base/fields.py +0 -546
- hammad/data/models/base/model.py +0 -1078
- hammad/data/models/base/utils.py +0 -280
- hammad/data/models/pydantic/__init__.py +0 -55
- hammad/data/models/pydantic/converters.py +0 -632
- hammad/data/models/pydantic/models/__init__.py +0 -28
- hammad/data/models/pydantic/models/arbitrary_model.py +0 -46
- hammad/data/models/pydantic/models/cacheable_model.py +0 -79
- hammad/data/models/pydantic/models/fast_model.py +0 -318
- hammad/data/models/pydantic/models/function_model.py +0 -176
- hammad/data/models/pydantic/models/subscriptable_model.py +0 -63
- hammad/data/types/__init__.py +0 -41
- hammad/data/types/file.py +0 -358
- hammad/data/types/multimodal/__init__.py +0 -24
- hammad/data/types/multimodal/audio.py +0 -96
- hammad/data/types/multimodal/image.py +0 -80
- hammad/data/types/text.py +0 -1066
- hammad/formatting/__init__.py +0 -38
- hammad/formatting/json/__init__.py +0 -21
- hammad/formatting/json/converters.py +0 -152
- hammad/formatting/text/__init__.py +0 -63
- hammad/formatting/text/converters.py +0 -723
- hammad/formatting/text/markdown.py +0 -131
- hammad/formatting/yaml/__init__.py +0 -26
- hammad/formatting/yaml/converters.py +0 -5
- hammad/logging/__init__.py +0 -35
- hammad/logging/decorators.py +0 -834
- hammad/logging/logger.py +0 -954
- hammad/mcp/__init__.py +0 -50
- hammad/mcp/client/__init__.py +0 -1
- hammad/mcp/client/client.py +0 -523
- hammad/mcp/client/client_service.py +0 -393
- hammad/mcp/client/settings.py +0 -178
- hammad/mcp/servers/__init__.py +0 -1
- hammad/mcp/servers/launcher.py +0 -1161
- hammad/performance/__init__.py +0 -36
- hammad/performance/imports.py +0 -231
- hammad/performance/runtime/__init__.py +0 -32
- hammad/performance/runtime/decorators.py +0 -142
- hammad/performance/runtime/run.py +0 -299
- hammad/py.typed +0 -0
- hammad/service/__init__.py +0 -49
- hammad/service/create.py +0 -532
- hammad/service/decorators.py +0 -285
- hammad/typing/__init__.py +0 -407
- hammad/web/__init__.py +0 -43
- hammad/web/http/__init__.py +0 -1
- hammad/web/http/client.py +0 -944
- hammad/web/models.py +0 -245
- hammad/web/openapi/__init__.py +0 -1
- hammad/web/openapi/client.py +0 -740
- hammad/web/search/__init__.py +0 -1
- hammad/web/search/client.py +0 -988
- hammad/web/utils.py +0 -472
- hammad_python-0.0.14.dist-info/METADATA +0 -70
- hammad_python-0.0.14.dist-info/RECORD +0 -99
- {hammad_python-0.0.14.dist-info → hammad_python-0.0.15.dist-info}/WHEEL +0 -0
- {hammad_python-0.0.14.dist-info → hammad_python-0.0.15.dist-info}/licenses/LICENSE +0 -0
@@ -1,200 +0,0 @@
|
|
1
|
-
"""hammad.ai.embeddings.client.fastembed_text_embeddings_client"""
|
2
|
-
|
3
|
-
from typing import Any, List, Optional, Union, Literal
|
4
|
-
import sys
|
5
|
-
|
6
|
-
if sys.version_info >= (3, 12):
|
7
|
-
from typing import TypedDict
|
8
|
-
else:
|
9
|
-
from typing_extensions import TypedDict
|
10
|
-
|
11
|
-
from .base_embeddings_client import BaseEmbeddingsClient
|
12
|
-
from ..types import (
|
13
|
-
Embedding,
|
14
|
-
EmbeddingUsage,
|
15
|
-
EmbeddingResponse,
|
16
|
-
)
|
17
|
-
from ....formatting.text.converters import convert_to_text
|
18
|
-
from ..._utils import (
|
19
|
-
get_fastembed_text_embedding_model,
|
20
|
-
)
|
21
|
-
|
22
|
-
|
23
|
-
__all__ = (
|
24
|
-
"FastEmbedTextEmbeddingsClient",
|
25
|
-
"FastEmbedTextEmbeddingModel",
|
26
|
-
"FastEmbedTextEmbeddingModelSettings",
|
27
|
-
)
|
28
|
-
|
29
|
-
|
30
|
-
FastEmbedTextEmbeddingModel = Literal[
|
31
|
-
"BAAI/bge-small-en-v1.5",
|
32
|
-
"BAAI/bge-small-zh-v1.5",
|
33
|
-
"snowflake/snowflake-arctic-embed-xs",
|
34
|
-
"sentence-transformers/all-MiniLM-L6-v2",
|
35
|
-
"jinaai/jina-embeddings-v2-small-en",
|
36
|
-
"BAAI/bge-small-en",
|
37
|
-
"snowflake/snowflake-arctic-embed-s",
|
38
|
-
"nomic-ai/nomic-embed-text-v1.5-Q",
|
39
|
-
"BAAI/bge-base-en-v1.5",
|
40
|
-
"sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2",
|
41
|
-
"Qdrant/clip-ViT-B-32-text",
|
42
|
-
"jinaai/jina-embeddings-v2-base-de",
|
43
|
-
"BAAI/bge-base-en",
|
44
|
-
"snowflake/snowflake-arctic-embed-m",
|
45
|
-
"nomic-ai/nomic-embed-text-v1.5",
|
46
|
-
"jinaai/jina-embeddings-v2-base-en",
|
47
|
-
"nomic-ai/nomic-embed-text-v1",
|
48
|
-
"snowflake/snowflake-arctic-embed-m-long",
|
49
|
-
"mixedbread-ai/mxbai-embed-large-v1",
|
50
|
-
"jinaai/jina-embeddings-v2-base-code",
|
51
|
-
"sentence-transformers/paraphrase-multilingual-mpnet-base-v2",
|
52
|
-
"snowflake/snowflake-arctic-embed-l",
|
53
|
-
"thenlper/gte-large",
|
54
|
-
"BAAI/bge-large-en-v1.5",
|
55
|
-
"intfloat/multilingual-e5-large",
|
56
|
-
]
|
57
|
-
"""All supported text embedding models supported by `fastembed`."""
|
58
|
-
|
59
|
-
|
60
|
-
class FastEmbedTextEmbeddingModelSettings(TypedDict):
|
61
|
-
"""Valid settings for the `fastembed` text embedding models."""
|
62
|
-
|
63
|
-
model: FastEmbedTextEmbeddingModel | str
|
64
|
-
parallel: Optional[int]
|
65
|
-
batch_size: Optional[int]
|
66
|
-
format: bool
|
67
|
-
|
68
|
-
|
69
|
-
class FastEmbedTextEmbeddingError(Exception):
|
70
|
-
"""Exception raised when an error occurs while generating embeddings
|
71
|
-
using `fastembed` text embedding models."""
|
72
|
-
|
73
|
-
def __init__(self, message: str, response: Any):
|
74
|
-
self.message = message
|
75
|
-
self.response = response
|
76
|
-
|
77
|
-
|
78
|
-
def _parse_fastembed_response_to_embedding_response(
|
79
|
-
response: Any,
|
80
|
-
model: FastEmbedTextEmbeddingModel | str,
|
81
|
-
) -> EmbeddingResponse:
|
82
|
-
"""Parse the response from the `fastembed` text embedding model to an `EmbeddingResponse` object."""
|
83
|
-
try:
|
84
|
-
embedding_data: List[Embedding] = []
|
85
|
-
|
86
|
-
# Convert generator to list if needed
|
87
|
-
if hasattr(response, "__iter__") and not isinstance(response, (list, tuple)):
|
88
|
-
response = list(response)
|
89
|
-
|
90
|
-
for i, item in enumerate(response):
|
91
|
-
# Ensure item is a list of floats
|
92
|
-
if hasattr(item, "tolist"):
|
93
|
-
item = item.tolist()
|
94
|
-
elif not isinstance(item, list):
|
95
|
-
item = list(item)
|
96
|
-
|
97
|
-
embedding_data.append(
|
98
|
-
Embedding(embedding=item, index=i, object="embedding")
|
99
|
-
)
|
100
|
-
|
101
|
-
return EmbeddingResponse(
|
102
|
-
data=embedding_data,
|
103
|
-
model=str(model),
|
104
|
-
object="list",
|
105
|
-
usage=EmbeddingUsage(prompt_tokens=0, total_tokens=0),
|
106
|
-
)
|
107
|
-
except Exception as e:
|
108
|
-
raise FastEmbedTextEmbeddingError(
|
109
|
-
f"Failed to parse fastembed response to embedding response: {e}",
|
110
|
-
response,
|
111
|
-
)
|
112
|
-
|
113
|
-
|
114
|
-
class FastEmbedTextEmbeddingsClient(BaseEmbeddingsClient):
|
115
|
-
"""Client for the `fastembed` text embedding models."""
|
116
|
-
|
117
|
-
@staticmethod
|
118
|
-
def embed(
|
119
|
-
input: List[Any] | Any,
|
120
|
-
model: FastEmbedTextEmbeddingModel | str,
|
121
|
-
parallel: Optional[int] = None,
|
122
|
-
batch_size: Optional[int] = None,
|
123
|
-
format: bool = False,
|
124
|
-
**kwargs: Any,
|
125
|
-
) -> EmbeddingResponse:
|
126
|
-
"""Generate embeddings for the given input using
|
127
|
-
a valid `fastembed` text embedding model.
|
128
|
-
|
129
|
-
Args:
|
130
|
-
input (List[Any] | Any) : The input text / content to generate embeddings for.
|
131
|
-
model (FastEmbedTextEmbeddingModel | str) : The model to use for generating embeddings.
|
132
|
-
parallel (Optional[int]) : The number of parallel processes to use for the embedding.
|
133
|
-
batch_size (Optional[int]) : The batch size to use for the embedding.
|
134
|
-
format (bool) : Whether to format each non-string input as a markdown string.
|
135
|
-
**kwargs : Any : Additional keyword arguments to pass to the `fastembed` text embedding model.
|
136
|
-
|
137
|
-
Returns:
|
138
|
-
EmbeddingResponse : The embedding response from the `fastembed` text embedding model.
|
139
|
-
"""
|
140
|
-
if not isinstance(input, list):
|
141
|
-
input = [input]
|
142
|
-
|
143
|
-
if format:
|
144
|
-
for i in input:
|
145
|
-
try:
|
146
|
-
i = convert_to_text(i)
|
147
|
-
except Exception as e:
|
148
|
-
raise FastEmbedTextEmbeddingError(
|
149
|
-
f"Failed to format input to text: {e}",
|
150
|
-
i,
|
151
|
-
)
|
152
|
-
|
153
|
-
model = get_fastembed_text_embedding_model(model)
|
154
|
-
|
155
|
-
try:
|
156
|
-
response = model.embed(
|
157
|
-
documents=input,
|
158
|
-
parallel=parallel,
|
159
|
-
batch_size=batch_size,
|
160
|
-
**kwargs,
|
161
|
-
)
|
162
|
-
except Exception as e:
|
163
|
-
raise FastEmbedTextEmbeddingError(
|
164
|
-
f"Failed to generate embeddings: {e}",
|
165
|
-
input,
|
166
|
-
)
|
167
|
-
|
168
|
-
return _parse_fastembed_response_to_embedding_response(response, str(model))
|
169
|
-
|
170
|
-
@staticmethod
|
171
|
-
async def async_embed(
|
172
|
-
input: List[Any] | Any,
|
173
|
-
model: FastEmbedTextEmbeddingModel | str,
|
174
|
-
parallel: Optional[int] = None,
|
175
|
-
batch_size: Optional[int] = None,
|
176
|
-
format: bool = False,
|
177
|
-
**kwargs: Any,
|
178
|
-
) -> EmbeddingResponse:
|
179
|
-
"""Async generate embeddings for the given input using
|
180
|
-
a valid `fastembed` text embedding model.
|
181
|
-
|
182
|
-
Args:
|
183
|
-
input (List[Any] | Any) : The input text / content to generate embeddings for.
|
184
|
-
model (FastEmbedTextEmbeddingModel | str) : The model to use for generating embeddings.
|
185
|
-
parallel (Optional[int]) : The number of parallel processes to use for the embedding.
|
186
|
-
batch_size (Optional[int]) : The batch size to use for the embedding.
|
187
|
-
format (bool) : Whether to format each non-string input as a markdown string.
|
188
|
-
**kwargs : Any : Additional keyword arguments to pass to the `fastembed` text embedding model.
|
189
|
-
|
190
|
-
Returns:
|
191
|
-
EmbeddingResponse : The embedding response from the `fastembed` text embedding model.
|
192
|
-
"""
|
193
|
-
return FastEmbedTextEmbeddingsClient.embed(
|
194
|
-
input=input,
|
195
|
-
model=model,
|
196
|
-
parallel=parallel,
|
197
|
-
batch_size=batch_size,
|
198
|
-
format=format,
|
199
|
-
**kwargs,
|
200
|
-
)
|
@@ -1,288 +0,0 @@
|
|
1
|
-
"""hammad.ai.embeddings.client.litellm_embeddings_client"""
|
2
|
-
|
3
|
-
from typing import Any, List, Literal, Optional
|
4
|
-
import sys
|
5
|
-
|
6
|
-
if sys.version_info >= (3, 12):
|
7
|
-
from typing import TypedDict
|
8
|
-
else:
|
9
|
-
from typing_extensions import TypedDict
|
10
|
-
|
11
|
-
|
12
|
-
from .base_embeddings_client import BaseEmbeddingsClient
|
13
|
-
from ..types import (
|
14
|
-
Embedding,
|
15
|
-
EmbeddingUsage,
|
16
|
-
EmbeddingResponse,
|
17
|
-
)
|
18
|
-
from ....formatting.text.converters import convert_to_text
|
19
|
-
from ..._utils import get_litellm
|
20
|
-
|
21
|
-
__all__ = (
|
22
|
-
"LiteLlmEmbeddingsClient",
|
23
|
-
"LiteLlmEmbeddingModel",
|
24
|
-
"LiteLlmEmbeddingModelSettings",
|
25
|
-
)
|
26
|
-
|
27
|
-
|
28
|
-
LiteLlmEmbeddingModel = Literal[
|
29
|
-
# OpenAI Embedding Models
|
30
|
-
"text-embedding-3-small",
|
31
|
-
"text-embedding-3-large",
|
32
|
-
"text-embedding-ada-002",
|
33
|
-
# OpenAI Compatible Embedding Models
|
34
|
-
"openai/text-embedding-3-small",
|
35
|
-
"openai/text-embedding-3-large",
|
36
|
-
"openai/text-embedding-ada-002",
|
37
|
-
# Bedrock Embedding Models
|
38
|
-
"amazon.titan-embed-text-v1",
|
39
|
-
"cohere.embed-english-v3",
|
40
|
-
"cohere.embed-multilingual-v3",
|
41
|
-
# Cohere Embedding Models
|
42
|
-
"embed-english-v3.0",
|
43
|
-
"embed-english-light-v3.0",
|
44
|
-
"embed-multilingual-v3.0",
|
45
|
-
"embed-multilingual-light-v3.0",
|
46
|
-
"embed-english-v2.0",
|
47
|
-
"embed-english-light-v2.0",
|
48
|
-
"embed-multilingual-v2.0",
|
49
|
-
# NVIDIA NIM Embedding Models
|
50
|
-
"nvidia_nim/NV-Embed-QA",
|
51
|
-
"nvidia_nim/nvidia/nv-embed-v1",
|
52
|
-
"nvidia_nim/nvidia/nv-embedqa-mistral-7b-v2",
|
53
|
-
"nvidia_nim/nvidia/nv-embedqa-e5-v5",
|
54
|
-
"nvidia_nim/nvidia/embed-qa-4",
|
55
|
-
"nvidia_nim/nvidia/llama-3.2-nv-embedqa-1b-v1",
|
56
|
-
"nvidia_nim/nvidia/llama-3.2-nv-embedqa-1b-v2",
|
57
|
-
"nvidia_nim/snowflake/arctic-embed-l",
|
58
|
-
"nvidia_nim/baai/bge-m3",
|
59
|
-
# HuggingFace Embedding Models
|
60
|
-
"huggingface/microsoft/codebert-base",
|
61
|
-
"huggingface/BAAI/bge-large-zh",
|
62
|
-
# Mistral AI Embedding Models
|
63
|
-
"mistral/mistral-embed",
|
64
|
-
# Gemini AI Embedding Models
|
65
|
-
"gemini/text-embedding-004",
|
66
|
-
# Vertex AI Embedding Models
|
67
|
-
"vertex_ai/textembedding-gecko",
|
68
|
-
"vertex_ai/textembedding-gecko-multilingual",
|
69
|
-
"vertex_ai/textembedding-gecko-multilingual@001",
|
70
|
-
"vertex_ai/textembedding-gecko@001",
|
71
|
-
"vertex_ai/textembedding-gecko@003",
|
72
|
-
"vertex_ai/text-embedding-preview-0409",
|
73
|
-
"vertex_ai/text-multilingual-embedding-preview-0409",
|
74
|
-
# Voyage AI Embedding Models
|
75
|
-
"voyage/voyage-01",
|
76
|
-
"voyage/voyage-lite-01",
|
77
|
-
"voyage/voyage-lite-01-instruct",
|
78
|
-
# Nebius AI Studio Embedding Models
|
79
|
-
"nebius/BAAI/bge-en-icl",
|
80
|
-
"nebius/BAAI/bge-multilingual-gemma2",
|
81
|
-
"nebius/intfloat/e5-mistral-7b-instruct",
|
82
|
-
# Ollama Embedding Models
|
83
|
-
"ollama/granite-embedding:30m",
|
84
|
-
"ollama/granite-embedding:278m",
|
85
|
-
"ollama/snowflake-arctic-embed2",
|
86
|
-
"ollama/bge-large",
|
87
|
-
"ollama/paraphrase-multilingual",
|
88
|
-
"ollama/bge-m3",
|
89
|
-
"ollama/snowflake-arctic-embed",
|
90
|
-
"ollama/mxbai-embed-large",
|
91
|
-
"ollama/all-minilm",
|
92
|
-
"ollama/nomic-embed-text",
|
93
|
-
]
|
94
|
-
"""Common embedding models supported by `litellm`."""
|
95
|
-
|
96
|
-
|
97
|
-
class LiteLlmEmbeddingModelSettings(TypedDict):
|
98
|
-
"""Valid settings for the `litellm` embedding models."""
|
99
|
-
|
100
|
-
model: LiteLlmEmbeddingModel | str
|
101
|
-
dimensions: Optional[int]
|
102
|
-
encoding_format: Optional[str]
|
103
|
-
timeout: Optional[int]
|
104
|
-
api_base: Optional[str]
|
105
|
-
api_version: Optional[str]
|
106
|
-
api_key: Optional[str]
|
107
|
-
api_type: Optional[str]
|
108
|
-
caching: bool
|
109
|
-
user: Optional[str]
|
110
|
-
|
111
|
-
|
112
|
-
class LiteLlmEmbeddingError(Exception):
|
113
|
-
"""Exception raised when an error occurs while generating embeddings
|
114
|
-
using `litellm`."""
|
115
|
-
|
116
|
-
def __init__(self, message: str, response: Any):
|
117
|
-
self.message = message
|
118
|
-
self.response = response
|
119
|
-
super().__init__(self.message)
|
120
|
-
|
121
|
-
|
122
|
-
def _parse_litellm_response_to_embedding_response(response: Any) -> EmbeddingResponse:
|
123
|
-
"""Parse the response from `litellm` to an `EmbeddingResponse` object."""
|
124
|
-
try:
|
125
|
-
embedding_data: List[Embedding] = []
|
126
|
-
|
127
|
-
for i, item in enumerate(response.data):
|
128
|
-
embedding_data.append(
|
129
|
-
Embedding(embedding=item["embedding"], index=i, object="embedding")
|
130
|
-
)
|
131
|
-
usage = EmbeddingUsage(
|
132
|
-
prompt_tokens=response.usage.prompt_tokens,
|
133
|
-
total_tokens=response.usage.total_tokens,
|
134
|
-
)
|
135
|
-
return EmbeddingResponse(
|
136
|
-
data=embedding_data,
|
137
|
-
model=response.model,
|
138
|
-
object="list",
|
139
|
-
usage=usage,
|
140
|
-
)
|
141
|
-
except Exception as e:
|
142
|
-
raise LiteLlmEmbeddingError(
|
143
|
-
f"Failed to parse litellm response to embedding response: {e}",
|
144
|
-
response,
|
145
|
-
)
|
146
|
-
|
147
|
-
|
148
|
-
class LiteLlmEmbeddingsClient(BaseEmbeddingsClient):
|
149
|
-
"""Embeddings provider client that utilizes the `litellm` module
|
150
|
-
when generating embeddings."""
|
151
|
-
|
152
|
-
@staticmethod
|
153
|
-
async def async_embed(
|
154
|
-
input: List[Any] | Any,
|
155
|
-
model: LiteLlmEmbeddingModel | str,
|
156
|
-
dimensions: Optional[int] = None,
|
157
|
-
encoding_format: Optional[str] = None,
|
158
|
-
timeout=600,
|
159
|
-
api_base: Optional[str] = None,
|
160
|
-
api_version: Optional[str] = None,
|
161
|
-
api_key: Optional[str] = None,
|
162
|
-
api_type: Optional[str] = None,
|
163
|
-
caching: bool = False,
|
164
|
-
user: Optional[str] = None,
|
165
|
-
format: bool = False,
|
166
|
-
) -> Embedding:
|
167
|
-
"""Asynchronously generate embeddings for the given input using
|
168
|
-
a valid `litellm` model.
|
169
|
-
|
170
|
-
Args:
|
171
|
-
input (List[Any] | Any) : The input text / content to generate embeddings for.
|
172
|
-
model (LiteLlmEmbeddingModel | str) : The model to use for generating embeddings.
|
173
|
-
dimensions (Optional[int]) : The number of dimensions for the embedding.
|
174
|
-
encoding_format (Optional[str]) : The format to return the embeddings in. (e.g. "float", "base64")
|
175
|
-
timeout (int) : The timeout for the request.
|
176
|
-
api_base (Optional[str]) : The base URL for the API.
|
177
|
-
api_version (Optional[str]) : The version of the API.
|
178
|
-
api_key (Optional[str]) : The API key to use for the request.
|
179
|
-
api_type (Optional[str]) : The API type to use for the request.
|
180
|
-
caching (bool) : Whether to cache the request.
|
181
|
-
user (Optional[str]) : The user to use for the request.
|
182
|
-
format (bool) : Whether to format each non-string input as a markdown string.
|
183
|
-
|
184
|
-
Returns:
|
185
|
-
Embedding : The embedding generated for the given input.
|
186
|
-
"""
|
187
|
-
if not isinstance(input, list):
|
188
|
-
input = [input]
|
189
|
-
|
190
|
-
if format:
|
191
|
-
for i in input:
|
192
|
-
try:
|
193
|
-
i = convert_to_text(i)
|
194
|
-
except Exception as e:
|
195
|
-
raise LiteLlmEmbeddingError(
|
196
|
-
f"Failed to format input to text: {e}",
|
197
|
-
i,
|
198
|
-
)
|
199
|
-
|
200
|
-
async_embedding_fn = get_litellm().aembedding
|
201
|
-
|
202
|
-
try:
|
203
|
-
response = await async_embedding_fn(
|
204
|
-
model=model,
|
205
|
-
input=input,
|
206
|
-
dimensions=dimensions,
|
207
|
-
encoding_format=encoding_format,
|
208
|
-
timeout=timeout,
|
209
|
-
api_base=api_base,
|
210
|
-
api_version=api_version,
|
211
|
-
api_key=api_key,
|
212
|
-
api_type=api_type,
|
213
|
-
caching=caching,
|
214
|
-
user=user,
|
215
|
-
)
|
216
|
-
except Exception as e:
|
217
|
-
raise e
|
218
|
-
|
219
|
-
return _parse_litellm_response_to_embedding_response(response)
|
220
|
-
|
221
|
-
@staticmethod
|
222
|
-
def embed(
|
223
|
-
input: List[Any] | Any,
|
224
|
-
model: LiteLlmEmbeddingModel | str,
|
225
|
-
dimensions: Optional[int] = None,
|
226
|
-
encoding_format: Optional[str] = None,
|
227
|
-
timeout=600,
|
228
|
-
api_base: Optional[str] = None,
|
229
|
-
api_version: Optional[str] = None,
|
230
|
-
api_key: Optional[str] = None,
|
231
|
-
api_type: Optional[str] = None,
|
232
|
-
caching: bool = False,
|
233
|
-
user: Optional[str] = None,
|
234
|
-
format: bool = False,
|
235
|
-
) -> Embedding:
|
236
|
-
"""Generate embeddings for the given input using
|
237
|
-
a valid `litellm` model.
|
238
|
-
|
239
|
-
Args:
|
240
|
-
input (List[Any] | Any) : The input text / content to generate embeddings for.
|
241
|
-
model (LiteLlmEmbeddingModel | str) : The model to use for generating embeddings.
|
242
|
-
dimensions (Optional[int]) : The number of dimensions for the embedding.
|
243
|
-
encoding_format (Optional[str]) : The format to return the embeddings in. (e.g. "float", "base64")
|
244
|
-
timeout (int) : The timeout for the request.
|
245
|
-
api_base (Optional[str]) : The base URL for the API.
|
246
|
-
api_version (Optional[str]) : The version of the API.
|
247
|
-
api_key (Optional[str]) : The API key to use for the request.
|
248
|
-
api_type (Optional[str]) : The API type to use for the request.
|
249
|
-
caching (bool) : Whether to cache the request.
|
250
|
-
user (Optional[str]) : The user to use for the request.
|
251
|
-
format (bool) : Whether to format each non-string input as a markdown string.
|
252
|
-
|
253
|
-
Returns:
|
254
|
-
Embedding : The embedding generated for the given input.
|
255
|
-
"""
|
256
|
-
if not isinstance(input, list):
|
257
|
-
input = [input]
|
258
|
-
|
259
|
-
if format:
|
260
|
-
for i in input:
|
261
|
-
try:
|
262
|
-
i = convert_to_text(i)
|
263
|
-
except Exception as e:
|
264
|
-
raise LiteLlmEmbeddingError(
|
265
|
-
f"Failed to format input to text: {e}",
|
266
|
-
i,
|
267
|
-
)
|
268
|
-
|
269
|
-
sync_embedding_fn = get_litellm().embedding
|
270
|
-
|
271
|
-
try:
|
272
|
-
response = sync_embedding_fn(
|
273
|
-
model=model,
|
274
|
-
input=input,
|
275
|
-
dimensions=dimensions,
|
276
|
-
encoding_format=encoding_format,
|
277
|
-
timeout=timeout,
|
278
|
-
api_base=api_base,
|
279
|
-
api_version=api_version,
|
280
|
-
api_key=api_key,
|
281
|
-
api_type=api_type,
|
282
|
-
caching=caching,
|
283
|
-
user=user,
|
284
|
-
)
|
285
|
-
except Exception as e:
|
286
|
-
raise e
|
287
|
-
|
288
|
-
return _parse_litellm_response_to_embedding_response(response)
|
hammad/ai/embeddings/create.py
DELETED
@@ -1,159 +0,0 @@
|
|
1
|
-
"""hammad.ai.embeddings.create"""
|
2
|
-
|
3
|
-
from typing import Any, List, Optional
|
4
|
-
|
5
|
-
from .types import (
|
6
|
-
EmbeddingResponse,
|
7
|
-
)
|
8
|
-
from .client.fastembed_text_embeddings_client import (
|
9
|
-
FastEmbedTextEmbeddingsClient,
|
10
|
-
FastEmbedTextEmbeddingModel,
|
11
|
-
FastEmbedTextEmbeddingModelSettings,
|
12
|
-
)
|
13
|
-
from .client.litellm_embeddings_client import (
|
14
|
-
LiteLlmEmbeddingsClient,
|
15
|
-
LiteLlmEmbeddingModel,
|
16
|
-
)
|
17
|
-
|
18
|
-
|
19
|
-
__all__ = ("async_create_embeddings", "create_embeddings")
|
20
|
-
|
21
|
-
|
22
|
-
async def async_create_embeddings(
|
23
|
-
input: List[Any] | Any,
|
24
|
-
model: FastEmbedTextEmbeddingModel | LiteLlmEmbeddingModel | str,
|
25
|
-
format: bool = False,
|
26
|
-
# LiteLLM Settings
|
27
|
-
dimensions: Optional[int] = None,
|
28
|
-
encoding_format: Optional[str] = None,
|
29
|
-
timeout: Optional[int] = None,
|
30
|
-
api_base: Optional[str] = None,
|
31
|
-
api_version: Optional[str] = None,
|
32
|
-
api_key: Optional[str] = None,
|
33
|
-
api_type: Optional[str] = None,
|
34
|
-
caching: bool = False,
|
35
|
-
user: Optional[str] = None,
|
36
|
-
# FastEmbed Settings
|
37
|
-
parallel: Optional[int] = None,
|
38
|
-
batch_size: Optional[int] = None,
|
39
|
-
**kwargs: Any,
|
40
|
-
) -> EmbeddingResponse:
|
41
|
-
"""Asynchronously create embeddings for the given input using the specified model.
|
42
|
-
|
43
|
-
Args:
|
44
|
-
input (List[Any] | Any) : The input text / content to generate embeddings for.
|
45
|
-
model (FastEmbedTextEmbeddingModel | LiteLlmEmbeddingModel | str) : The model to use for generating embeddings.
|
46
|
-
format (bool) : Whether to format each non-string input as a markdown string.
|
47
|
-
dimensions (Optional[int]) : The dimensions of the embedding. NOTE: LiteLLM models only
|
48
|
-
encoding_format (Optional[str]) : The encoding format of the embedding. NOTE: LiteLLM models only
|
49
|
-
timeout (Optional[int]) : The timeout for the embedding. NOTE: LiteLLM models only
|
50
|
-
api_base (Optional[str]) : The base URL for the embedding API. NOTE: LiteLLM models only
|
51
|
-
api_version (Optional[str]) : The version of the embedding API. NOTE: LiteLLM models only
|
52
|
-
api_key (Optional[str]) : The API key for the embedding API. NOTE: LiteLLM models only
|
53
|
-
api_type (Optional[str]) : The type of the embedding API. NOTE: LiteLLM models only
|
54
|
-
caching (bool) : Whether to cache the embedding. NOTE: LiteLLM models only
|
55
|
-
user (Optional[str]) : The user for the embedding. NOTE: LiteLLM models only
|
56
|
-
parallel (Optional[int]) : The number of parallel processes to use for the embedding. NOTE: FastEmbed models only
|
57
|
-
batch_size (Optional[int]) : The batch size to use for the embedding. NOTE: FastEmbed models only
|
58
|
-
**kwargs : Any : Additional keyword arguments to pass to the embedding client.
|
59
|
-
|
60
|
-
Returns:
|
61
|
-
EmbeddingResponse : The embedding response from the embedding client.
|
62
|
-
"""
|
63
|
-
|
64
|
-
if model.startswith("fastembed/"):
|
65
|
-
model = model.split("fastembed/")[1]
|
66
|
-
return await FastEmbedTextEmbeddingsClient.async_embed(
|
67
|
-
input=input,
|
68
|
-
model=model,
|
69
|
-
parallel=parallel,
|
70
|
-
batch_size=batch_size,
|
71
|
-
format=format,
|
72
|
-
**kwargs,
|
73
|
-
)
|
74
|
-
else:
|
75
|
-
return await LiteLlmEmbeddingsClient.async_embed(
|
76
|
-
input=input,
|
77
|
-
model=model,
|
78
|
-
dimensions=dimensions,
|
79
|
-
encoding_format=encoding_format,
|
80
|
-
timeout=timeout,
|
81
|
-
api_base=api_base,
|
82
|
-
api_version=api_version,
|
83
|
-
api_key=api_key,
|
84
|
-
api_type=api_type,
|
85
|
-
caching=caching,
|
86
|
-
user=user,
|
87
|
-
format=format,
|
88
|
-
**kwargs,
|
89
|
-
)
|
90
|
-
|
91
|
-
|
92
|
-
def create_embeddings(
|
93
|
-
input: List[Any] | Any,
|
94
|
-
model: FastEmbedTextEmbeddingModel | LiteLlmEmbeddingModel | str,
|
95
|
-
format: bool = False,
|
96
|
-
# LiteLLM Settings
|
97
|
-
dimensions: Optional[int] = None,
|
98
|
-
encoding_format: Optional[str] = None,
|
99
|
-
timeout: Optional[int] = None,
|
100
|
-
api_base: Optional[str] = None,
|
101
|
-
api_version: Optional[str] = None,
|
102
|
-
api_key: Optional[str] = None,
|
103
|
-
api_type: Optional[str] = None,
|
104
|
-
caching: bool = False,
|
105
|
-
user: Optional[str] = None,
|
106
|
-
# FastEmbed Settings
|
107
|
-
parallel: Optional[int] = None,
|
108
|
-
batch_size: Optional[int] = None,
|
109
|
-
**kwargs: Any,
|
110
|
-
) -> EmbeddingResponse:
|
111
|
-
"""Asynchronously create embeddings for the given input using the specified model.
|
112
|
-
|
113
|
-
Args:
|
114
|
-
input (List[Any] | Any) : The input text / content to generate embeddings for.
|
115
|
-
model (FastEmbedTextEmbeddingModel | LiteLlmEmbeddingModel | str) : The model to use for generating embeddings.
|
116
|
-
format (bool) : Whether to format each non-string input as a markdown string.
|
117
|
-
dimensions (Optional[int]) : The dimensions of the embedding. NOTE: LiteLLM models only
|
118
|
-
encoding_format (Optional[str]) : The encoding format of the embedding. NOTE: LiteLLM models only
|
119
|
-
timeout (Optional[int]) : The timeout for the embedding. NOTE: LiteLLM models only
|
120
|
-
api_base (Optional[str]) : The base URL for the embedding API. NOTE: LiteLLM models only
|
121
|
-
api_version (Optional[str]) : The version of the embedding API. NOTE: LiteLLM models only
|
122
|
-
api_key (Optional[str]) : The API key for the embedding API. NOTE: LiteLLM models only
|
123
|
-
api_type (Optional[str]) : The type of the embedding API. NOTE: LiteLLM models only
|
124
|
-
caching (bool) : Whether to cache the embedding. NOTE: LiteLLM models only
|
125
|
-
user (Optional[str]) : The user for the embedding. NOTE: LiteLLM models only
|
126
|
-
parallel (Optional[int]) : The number of parallel processes to use for the embedding. NOTE: FastEmbed models only
|
127
|
-
batch_size (Optional[int]) : The batch size to use for the embedding. NOTE: FastEmbed models only
|
128
|
-
**kwargs : Any : Additional keyword arguments to pass to the embedding client.
|
129
|
-
|
130
|
-
Returns:
|
131
|
-
EmbeddingResponse : The embedding response from the embedding client.
|
132
|
-
"""
|
133
|
-
|
134
|
-
if model.startswith("fastembed/"):
|
135
|
-
model = model.split("fastembed/")[1]
|
136
|
-
return FastEmbedTextEmbeddingsClient.embed(
|
137
|
-
input=input,
|
138
|
-
model=model,
|
139
|
-
parallel=parallel,
|
140
|
-
batch_size=batch_size,
|
141
|
-
format=format,
|
142
|
-
**kwargs,
|
143
|
-
)
|
144
|
-
else:
|
145
|
-
return LiteLlmEmbeddingsClient.embed(
|
146
|
-
input=input,
|
147
|
-
model=model,
|
148
|
-
dimensions=dimensions,
|
149
|
-
encoding_format=encoding_format,
|
150
|
-
timeout=timeout,
|
151
|
-
api_base=api_base,
|
152
|
-
api_version=api_version,
|
153
|
-
api_key=api_key,
|
154
|
-
api_type=api_type,
|
155
|
-
caching=caching,
|
156
|
-
user=user,
|
157
|
-
format=format,
|
158
|
-
**kwargs,
|
159
|
-
)
|