hammad-python 0.0.14__py3-none-any.whl → 0.0.16__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.
Files changed (122) hide show
  1. hammad/__init__.py +177 -0
  2. hammad/{performance/imports.py → _internal.py} +7 -1
  3. hammad/cache/__init__.py +1 -1
  4. hammad/cli/__init__.py +3 -1
  5. hammad/cli/_runner.py +265 -0
  6. hammad/cli/animations.py +1 -1
  7. hammad/cli/plugins.py +133 -78
  8. hammad/cli/styles/__init__.py +1 -1
  9. hammad/cli/styles/utils.py +149 -3
  10. hammad/data/__init__.py +56 -29
  11. hammad/data/collections/__init__.py +27 -17
  12. hammad/data/collections/collection.py +205 -383
  13. hammad/data/collections/indexes/__init__.py +37 -0
  14. hammad/data/collections/indexes/qdrant/__init__.py +1 -0
  15. hammad/data/collections/indexes/qdrant/index.py +735 -0
  16. hammad/data/collections/indexes/qdrant/settings.py +94 -0
  17. hammad/data/collections/indexes/qdrant/utils.py +220 -0
  18. hammad/data/collections/indexes/tantivy/__init__.py +1 -0
  19. hammad/data/collections/indexes/tantivy/index.py +428 -0
  20. hammad/data/collections/indexes/tantivy/settings.py +51 -0
  21. hammad/data/collections/indexes/tantivy/utils.py +200 -0
  22. hammad/data/configurations/__init__.py +2 -2
  23. hammad/data/configurations/configuration.py +2 -2
  24. hammad/data/models/__init__.py +20 -9
  25. hammad/data/models/extensions/__init__.py +4 -0
  26. hammad/data/models/{pydantic → extensions/pydantic}/__init__.py +6 -19
  27. hammad/data/models/{pydantic → extensions/pydantic}/converters.py +143 -16
  28. hammad/data/models/{base/fields.py → fields.py} +1 -1
  29. hammad/data/models/{base/model.py → model.py} +1 -1
  30. hammad/data/models/{base/utils.py → utils.py} +1 -1
  31. hammad/data/sql/__init__.py +23 -0
  32. hammad/data/sql/database.py +578 -0
  33. hammad/data/sql/types.py +141 -0
  34. hammad/data/types/__init__.py +1 -3
  35. hammad/data/types/file.py +3 -3
  36. hammad/data/types/multimodal/__init__.py +2 -2
  37. hammad/data/types/multimodal/audio.py +2 -2
  38. hammad/data/types/multimodal/image.py +2 -2
  39. hammad/formatting/__init__.py +9 -27
  40. hammad/formatting/json/__init__.py +8 -2
  41. hammad/formatting/json/converters.py +7 -1
  42. hammad/formatting/text/__init__.py +1 -1
  43. hammad/formatting/yaml/__init__.py +1 -1
  44. hammad/genai/__init__.py +78 -0
  45. hammad/genai/agents/__init__.py +1 -0
  46. hammad/genai/agents/types/__init__.py +35 -0
  47. hammad/genai/agents/types/history.py +277 -0
  48. hammad/genai/agents/types/tool.py +490 -0
  49. hammad/genai/embedding_models/__init__.py +41 -0
  50. hammad/{ai/embeddings/client/litellm_embeddings_client.py → genai/embedding_models/embedding_model.py} +47 -142
  51. hammad/genai/embedding_models/embedding_model_name.py +77 -0
  52. hammad/genai/embedding_models/embedding_model_request.py +65 -0
  53. hammad/{ai/embeddings/types.py → genai/embedding_models/embedding_model_response.py} +3 -3
  54. hammad/genai/embedding_models/run.py +161 -0
  55. hammad/genai/language_models/__init__.py +35 -0
  56. hammad/genai/language_models/_streaming.py +622 -0
  57. hammad/genai/language_models/_types.py +276 -0
  58. hammad/genai/language_models/_utils/__init__.py +31 -0
  59. hammad/genai/language_models/_utils/_completions.py +131 -0
  60. hammad/genai/language_models/_utils/_messages.py +89 -0
  61. hammad/genai/language_models/_utils/_requests.py +202 -0
  62. hammad/genai/language_models/_utils/_structured_outputs.py +124 -0
  63. hammad/genai/language_models/language_model.py +734 -0
  64. hammad/genai/language_models/language_model_request.py +135 -0
  65. hammad/genai/language_models/language_model_response.py +219 -0
  66. hammad/genai/language_models/language_model_response_chunk.py +53 -0
  67. hammad/genai/language_models/run.py +530 -0
  68. hammad/genai/multimodal_models.py +48 -0
  69. hammad/genai/rerank_models.py +26 -0
  70. hammad/logging/__init__.py +1 -1
  71. hammad/logging/decorators.py +1 -1
  72. hammad/logging/logger.py +2 -2
  73. hammad/mcp/__init__.py +1 -1
  74. hammad/mcp/client/__init__.py +35 -0
  75. hammad/mcp/client/client.py +105 -4
  76. hammad/mcp/client/client_service.py +10 -3
  77. hammad/mcp/servers/__init__.py +24 -0
  78. hammad/{performance/runtime → runtime}/__init__.py +2 -2
  79. hammad/{performance/runtime → runtime}/decorators.py +1 -1
  80. hammad/{performance/runtime → runtime}/run.py +1 -1
  81. hammad/service/__init__.py +1 -1
  82. hammad/service/create.py +3 -8
  83. hammad/service/decorators.py +8 -8
  84. hammad/typing/__init__.py +28 -0
  85. hammad/web/__init__.py +3 -3
  86. hammad/web/http/client.py +1 -1
  87. hammad/web/models.py +53 -21
  88. hammad/web/search/client.py +99 -52
  89. hammad/web/utils.py +13 -13
  90. hammad_python-0.0.16.dist-info/METADATA +191 -0
  91. hammad_python-0.0.16.dist-info/RECORD +110 -0
  92. hammad/ai/__init__.py +0 -1
  93. hammad/ai/_utils.py +0 -142
  94. hammad/ai/completions/__init__.py +0 -45
  95. hammad/ai/completions/client.py +0 -684
  96. hammad/ai/completions/create.py +0 -710
  97. hammad/ai/completions/settings.py +0 -100
  98. hammad/ai/completions/types.py +0 -792
  99. hammad/ai/completions/utils.py +0 -486
  100. hammad/ai/embeddings/__init__.py +0 -35
  101. hammad/ai/embeddings/client/__init__.py +0 -1
  102. hammad/ai/embeddings/client/base_embeddings_client.py +0 -26
  103. hammad/ai/embeddings/client/fastembed_text_embeddings_client.py +0 -200
  104. hammad/ai/embeddings/create.py +0 -159
  105. hammad/data/collections/base_collection.py +0 -58
  106. hammad/data/collections/searchable_collection.py +0 -556
  107. hammad/data/collections/vector_collection.py +0 -596
  108. hammad/data/databases/__init__.py +0 -21
  109. hammad/data/databases/database.py +0 -902
  110. hammad/data/models/base/__init__.py +0 -35
  111. hammad/data/models/pydantic/models/__init__.py +0 -28
  112. hammad/data/models/pydantic/models/arbitrary_model.py +0 -46
  113. hammad/data/models/pydantic/models/cacheable_model.py +0 -79
  114. hammad/data/models/pydantic/models/fast_model.py +0 -318
  115. hammad/data/models/pydantic/models/function_model.py +0 -176
  116. hammad/data/models/pydantic/models/subscriptable_model.py +0 -63
  117. hammad/performance/__init__.py +0 -36
  118. hammad/py.typed +0 -0
  119. hammad_python-0.0.14.dist-info/METADATA +0 -70
  120. hammad_python-0.0.14.dist-info/RECORD +0 -99
  121. {hammad_python-0.0.14.dist-info → hammad_python-0.0.16.dist-info}/WHEEL +0 -0
  122. {hammad_python-0.0.14.dist-info → hammad_python-0.0.16.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,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
- )
@@ -1,58 +0,0 @@
1
- """hammad.data.collections.base_collection"""
2
-
3
- from typing import Any, Dict, Optional, List, TypeVar, Union, Type, Generic
4
- from abc import ABC, abstractmethod
5
-
6
- __all__ = (
7
- "BaseCollection",
8
- "Object",
9
- "Filters",
10
- "Schema",
11
- )
12
-
13
-
14
- Object = TypeVar("Object")
15
- """Represents an object that can be stored within a collection."""
16
-
17
-
18
- Filters = Dict[str, object]
19
- """Represents a dictionary of filters that can be applied to objects within
20
- a collection."""
21
-
22
-
23
- Schema = Union[Type[Any], Dict[str, Any], None]
24
- """Represents a strict schema that a collection can optionally enforce."""
25
-
26
-
27
- class BaseCollection(ABC, Generic[Object]):
28
- """Base class defining the interface for collections. This
29
- class does not provide any functionality.
30
- """
31
-
32
- @abstractmethod
33
- def get(self, id: str, *, filters: Optional[Filters] = None) -> Optional[Any]:
34
- """Get an item by ID."""
35
- pass
36
-
37
- @abstractmethod
38
- def add(
39
- self,
40
- entry: Any,
41
- *,
42
- id: Optional[str] = None,
43
- filters: Optional[Filters] = None,
44
- ttl: Optional[int] = None,
45
- ) -> None:
46
- """Add an item to the collection."""
47
- pass
48
-
49
- @abstractmethod
50
- def query(
51
- self,
52
- *,
53
- filters: Optional[Filters] = None,
54
- search: Optional[str] = None,
55
- limit: Optional[int] = None,
56
- ) -> List[Any]:
57
- """Query items from the collection."""
58
- pass