synapsai-python 0.1.0__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.
- synapsai/__init__.py +16 -0
- synapsai/client.py +628 -0
- synapsai/exceptions.py +64 -0
- synapsai/logging.py +70 -0
- synapsai/processing.py +100 -0
- synapsai/resources/__init__.py +59 -0
- synapsai/resources/audio.py +495 -0
- synapsai/resources/chat.py +199 -0
- synapsai/resources/classifications.py +454 -0
- synapsai/resources/completions.py +162 -0
- synapsai/resources/embeddings.py +207 -0
- synapsai/resources/feature_extraction.py +80 -0
- synapsai/resources/fill_mask.py +88 -0
- synapsai/resources/images.py +560 -0
- synapsai/resources/models.py +80 -0
- synapsai/resources/question_answering.py +257 -0
- synapsai/resources/rerank.py +90 -0
- synapsai/resources/videos.py +303 -0
- synapsai/types/__init__.py +144 -0
- synapsai/types/audio.py +177 -0
- synapsai/types/classifications.py +228 -0
- synapsai/types/common.py +56 -0
- synapsai/types/completion.py +244 -0
- synapsai/types/embeddings.py +68 -0
- synapsai/types/feature_extraction.py +31 -0
- synapsai/types/fill_mask.py +38 -0
- synapsai/types/images.py +172 -0
- synapsai/types/models.py +28 -0
- synapsai/types/question_answering.py +127 -0
- synapsai/types/rerank.py +40 -0
- synapsai/types/videos.py +92 -0
- synapsai/utils.py +32 -0
- synapsai_python-0.1.0.dist-info/METADATA +312 -0
- synapsai_python-0.1.0.dist-info/RECORD +37 -0
- synapsai_python-0.1.0.dist-info/WHEEL +5 -0
- synapsai_python-0.1.0.dist-info/licenses/LICENSE +201 -0
- synapsai_python-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# Copyright 2026 SynapsAI Technologies Inc.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
Embeddings resource handlers
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from typing import Union, List, TYPE_CHECKING, Literal, Optional
|
|
20
|
+
|
|
21
|
+
from ..types.embeddings import (
|
|
22
|
+
EmbeddingResponse,
|
|
23
|
+
SimilarityResponse,
|
|
24
|
+
)
|
|
25
|
+
from ..exceptions import APIError
|
|
26
|
+
import math
|
|
27
|
+
|
|
28
|
+
if TYPE_CHECKING:
|
|
29
|
+
from ..client import SynapsAI, AsyncSynapsAI
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _dot_product(a: List[float], b: List[float]) -> float:
|
|
33
|
+
return sum(x * y for x, y in zip(a, b))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _vector_norm(a: List[float]) -> float:
|
|
37
|
+
return math.sqrt(sum(x * x for x in a))
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _cosine_similarity(a: List[float], b: List[float]) -> float:
|
|
41
|
+
denom = _vector_norm(a) * _vector_norm(b)
|
|
42
|
+
if denom == 0:
|
|
43
|
+
return 0.0
|
|
44
|
+
return _dot_product(a, b) / denom
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class EmbeddingsResource:
|
|
48
|
+
"""Embeddings resource handler"""
|
|
49
|
+
|
|
50
|
+
def __init__(self, client: "SynapsAI"):
|
|
51
|
+
self._client = client
|
|
52
|
+
|
|
53
|
+
def create(
|
|
54
|
+
self,
|
|
55
|
+
model: str,
|
|
56
|
+
input: Union[str, List[str], List[int], List[List[int]]],
|
|
57
|
+
encoding_format: Optional[Literal["float", "base64"]] = "float",
|
|
58
|
+
**kwargs
|
|
59
|
+
) -> EmbeddingResponse:
|
|
60
|
+
"""Create embeddings for the given input"""
|
|
61
|
+
|
|
62
|
+
# Build request
|
|
63
|
+
request_data = self._client._build_request(
|
|
64
|
+
model=model,
|
|
65
|
+
input=input,
|
|
66
|
+
encoding_format=encoding_format,
|
|
67
|
+
**kwargs
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
endpoint = "embeddings"
|
|
71
|
+
|
|
72
|
+
# Make request
|
|
73
|
+
response = self._client._post(endpoint, json_data=request_data)
|
|
74
|
+
response_data = response.json()
|
|
75
|
+
return EmbeddingResponse.model_validate(response_data)
|
|
76
|
+
|
|
77
|
+
def similarity(
|
|
78
|
+
self,
|
|
79
|
+
model: str,
|
|
80
|
+
source_sentence: str,
|
|
81
|
+
sentences: List[str],
|
|
82
|
+
return_embeddings: Optional[bool] = False,
|
|
83
|
+
encoding_format: Optional[Literal["float", "base64"]] = "float",
|
|
84
|
+
**kwargs
|
|
85
|
+
) -> SimilarityResponse:
|
|
86
|
+
"""Calculate similarity between a source sentence and other sentences. It basically is a wrapper around the create function and returns the similarity score."""
|
|
87
|
+
# Validate inputs
|
|
88
|
+
if not sentences:
|
|
89
|
+
raise APIError("`sentences` must be a non-empty list")
|
|
90
|
+
|
|
91
|
+
# Build inputs: first element is source_sentence followed by other sentences
|
|
92
|
+
inputs = [source_sentence] + sentences
|
|
93
|
+
|
|
94
|
+
# Call create to get embeddings
|
|
95
|
+
emb_response = self.create(
|
|
96
|
+
model=model,
|
|
97
|
+
input=inputs,
|
|
98
|
+
encoding_format=encoding_format,
|
|
99
|
+
**kwargs,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
# Validate response
|
|
103
|
+
if not emb_response or not getattr(emb_response, "data", None):
|
|
104
|
+
raise APIError("Failed to obtain embeddings")
|
|
105
|
+
|
|
106
|
+
# Sort by index to guarantee alignment with input order
|
|
107
|
+
sorted_data = sorted(emb_response.data, key=lambda x: x.index)
|
|
108
|
+
|
|
109
|
+
source_emb = sorted_data[0].embedding # now guaranteed to be source_sentence
|
|
110
|
+
|
|
111
|
+
results = []
|
|
112
|
+
for item in sorted_data[1:]:
|
|
113
|
+
sim = _cosine_similarity(source_emb, item.embedding)
|
|
114
|
+
result_obj = {
|
|
115
|
+
"object": "similarity",
|
|
116
|
+
"similarity": float(sim),
|
|
117
|
+
"embedding": item.embedding if return_embeddings else None,
|
|
118
|
+
"index": int(item.index - 1),
|
|
119
|
+
}
|
|
120
|
+
results.append(result_obj)
|
|
121
|
+
|
|
122
|
+
response_obj = {
|
|
123
|
+
"object": "list",
|
|
124
|
+
"data": results,
|
|
125
|
+
"model": emb_response.model,
|
|
126
|
+
"usage": getattr(emb_response, "usage", None),
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return SimilarityResponse.model_validate(response_obj)
|
|
130
|
+
|
|
131
|
+
class AsyncEmbeddingsResource:
|
|
132
|
+
"""Async embeddings resource handler"""
|
|
133
|
+
|
|
134
|
+
def __init__(self, client: "AsyncSynapsAI"):
|
|
135
|
+
self._client = client
|
|
136
|
+
|
|
137
|
+
async def create(
|
|
138
|
+
self,
|
|
139
|
+
model: str,
|
|
140
|
+
input: Union[str, List[str], List[int], List[List[int]]],
|
|
141
|
+
encoding_format: Optional[Literal["float", "base64"]] = "float",
|
|
142
|
+
**kwargs
|
|
143
|
+
) -> EmbeddingResponse:
|
|
144
|
+
"""Create embeddings for the given input asynchronously"""
|
|
145
|
+
|
|
146
|
+
# Build request
|
|
147
|
+
request_data = self._client._build_request(
|
|
148
|
+
model=model,
|
|
149
|
+
input=input,
|
|
150
|
+
encoding_format=encoding_format,
|
|
151
|
+
**kwargs
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
endpoint = "embeddings"
|
|
155
|
+
|
|
156
|
+
# Make request
|
|
157
|
+
response = await self._client._post(endpoint, json_data=request_data)
|
|
158
|
+
response_data = response.json()
|
|
159
|
+
return EmbeddingResponse.model_validate(response_data)
|
|
160
|
+
|
|
161
|
+
async def similarity(
|
|
162
|
+
self,
|
|
163
|
+
model: str,
|
|
164
|
+
source_sentence: str,
|
|
165
|
+
sentences: List[str],
|
|
166
|
+
return_embeddings: Optional[bool] = False,
|
|
167
|
+
encoding_format: Optional[Literal["float", "base64"]] = "float",
|
|
168
|
+
**kwargs
|
|
169
|
+
) -> SimilarityResponse:
|
|
170
|
+
"""Calculate similarity between a source sentence and other sentences. It basically is a wrapper around the create function and returns the similarity score."""
|
|
171
|
+
# Validate inputs
|
|
172
|
+
if not sentences:
|
|
173
|
+
raise APIError("`sentences` must be a non-empty list")
|
|
174
|
+
|
|
175
|
+
inputs = [source_sentence] + sentences
|
|
176
|
+
|
|
177
|
+
emb_response = await self.create(
|
|
178
|
+
model=model,
|
|
179
|
+
input=inputs,
|
|
180
|
+
encoding_format=encoding_format,
|
|
181
|
+
**kwargs,
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
if not emb_response or not getattr(emb_response, "data", None):
|
|
185
|
+
raise APIError("Failed to obtain embeddings")
|
|
186
|
+
|
|
187
|
+
source_emb = emb_response.data[0].embedding
|
|
188
|
+
|
|
189
|
+
results = []
|
|
190
|
+
for item in emb_response.data[1:]:
|
|
191
|
+
sim = _cosine_similarity(source_emb, item.embedding)
|
|
192
|
+
result_obj = {
|
|
193
|
+
"object": "similarity",
|
|
194
|
+
"similarity": float(sim),
|
|
195
|
+
"embedding": item.embedding if return_embeddings else None,
|
|
196
|
+
"index": int(item.index - 1),
|
|
197
|
+
}
|
|
198
|
+
results.append(result_obj)
|
|
199
|
+
|
|
200
|
+
response_obj = {
|
|
201
|
+
"object": "list",
|
|
202
|
+
"data": results,
|
|
203
|
+
"model": emb_response.model,
|
|
204
|
+
"usage": getattr(emb_response, "usage", None),
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return SimilarityResponse.model_validate(response_obj)
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# Copyright 2026 SynapsAI Technologies Inc.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
Feature extraction resource handlers
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from typing import TYPE_CHECKING, Union
|
|
20
|
+
|
|
21
|
+
from ..types.feature_extraction import FeatureExtractionResponse
|
|
22
|
+
|
|
23
|
+
if TYPE_CHECKING:
|
|
24
|
+
from ..client import SynapsAI, AsyncSynapsAI
|
|
25
|
+
|
|
26
|
+
class FeatureExtractionResource:
|
|
27
|
+
"""Feature extraction resource handler"""
|
|
28
|
+
|
|
29
|
+
def __init__(self, client: "SynapsAI"):
|
|
30
|
+
self._client = client
|
|
31
|
+
|
|
32
|
+
def create(
|
|
33
|
+
self,
|
|
34
|
+
model: str,
|
|
35
|
+
inputs: Union[str, list[str]],
|
|
36
|
+
**kwargs
|
|
37
|
+
) -> FeatureExtractionResponse:
|
|
38
|
+
"""Generate images from text prompts"""
|
|
39
|
+
|
|
40
|
+
# Build request
|
|
41
|
+
request_data = self._client._build_request(
|
|
42
|
+
model=model,
|
|
43
|
+
inputs=inputs,
|
|
44
|
+
**kwargs
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
# Make request
|
|
48
|
+
endpoint = "feature-extraction"
|
|
49
|
+
|
|
50
|
+
response = self._client._post(endpoint, json_data=request_data)
|
|
51
|
+
response_data = response.json()
|
|
52
|
+
return FeatureExtractionResponse.model_validate(response_data)
|
|
53
|
+
|
|
54
|
+
class AsyncFeatureExtractionResource:
|
|
55
|
+
"""Feature extraction resource handler"""
|
|
56
|
+
|
|
57
|
+
def __init__(self, client: "AsyncSynapsAI"):
|
|
58
|
+
self._client = client
|
|
59
|
+
|
|
60
|
+
async def create(
|
|
61
|
+
self,
|
|
62
|
+
model: str,
|
|
63
|
+
inputs: Union[str, list[str]],
|
|
64
|
+
**kwargs
|
|
65
|
+
) -> FeatureExtractionResponse:
|
|
66
|
+
"""Generate images from text prompts"""
|
|
67
|
+
|
|
68
|
+
# Build request
|
|
69
|
+
request_data = self._client._build_request(
|
|
70
|
+
model=model,
|
|
71
|
+
inputs=inputs,
|
|
72
|
+
**kwargs
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
# Make request
|
|
76
|
+
endpoint = "feature-extraction"
|
|
77
|
+
|
|
78
|
+
response = await self._client._post(endpoint, json_data=request_data)
|
|
79
|
+
response_data = response.json()
|
|
80
|
+
return FeatureExtractionResponse.model_validate(response_data)
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# Copyright 2026 SynapsAI Technologies Inc.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
Fill mask resource handlers
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from typing import TYPE_CHECKING, Optional, Union
|
|
20
|
+
|
|
21
|
+
from ..types.fill_mask import FillMaskResponse
|
|
22
|
+
|
|
23
|
+
if TYPE_CHECKING:
|
|
24
|
+
from ..client import SynapsAI, AsyncSynapsAI
|
|
25
|
+
|
|
26
|
+
class FillMaskResource:
|
|
27
|
+
"""Fill mask resource handler"""
|
|
28
|
+
|
|
29
|
+
def __init__(self, client: "SynapsAI"):
|
|
30
|
+
self._client = client
|
|
31
|
+
|
|
32
|
+
def create(
|
|
33
|
+
self,
|
|
34
|
+
model: str,
|
|
35
|
+
inputs: Union[str, list[str]],
|
|
36
|
+
targets: Optional[Union[str, list[str]]] = None,
|
|
37
|
+
top_k: int = 5,
|
|
38
|
+
**kwargs
|
|
39
|
+
) -> FillMaskResponse:
|
|
40
|
+
"""Generate images from text prompts"""
|
|
41
|
+
|
|
42
|
+
# Build request
|
|
43
|
+
request_data = self._client._build_request(
|
|
44
|
+
model=model,
|
|
45
|
+
inputs=inputs,
|
|
46
|
+
targets=targets,
|
|
47
|
+
top_k=top_k,
|
|
48
|
+
**kwargs
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
# Make request
|
|
52
|
+
endpoint = "fill-mask"
|
|
53
|
+
|
|
54
|
+
response = self._client._post(endpoint, json_data=request_data)
|
|
55
|
+
response_data = response.json()
|
|
56
|
+
return FillMaskResponse.model_validate(response_data)
|
|
57
|
+
|
|
58
|
+
class AsyncFillMaskResource:
|
|
59
|
+
"""Fill mask resource handler"""
|
|
60
|
+
|
|
61
|
+
def __init__(self, client: "AsyncSynapsAI"):
|
|
62
|
+
self._client = client
|
|
63
|
+
|
|
64
|
+
async def create(
|
|
65
|
+
self,
|
|
66
|
+
model: str,
|
|
67
|
+
inputs: Union[str, list[str]],
|
|
68
|
+
targets: Optional[Union[str, list[str]]] = None,
|
|
69
|
+
top_k: int = 5,
|
|
70
|
+
**kwargs
|
|
71
|
+
) -> FillMaskResponse:
|
|
72
|
+
"""Generate images from text prompts"""
|
|
73
|
+
|
|
74
|
+
# Build request
|
|
75
|
+
request_data = self._client._build_request(
|
|
76
|
+
model=model,
|
|
77
|
+
inputs=inputs,
|
|
78
|
+
targets=targets,
|
|
79
|
+
top_k=top_k,
|
|
80
|
+
**kwargs
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
# Make request
|
|
84
|
+
endpoint = "fill-mask"
|
|
85
|
+
|
|
86
|
+
response = await self._client._post(endpoint, json_data=request_data)
|
|
87
|
+
response_data = response.json()
|
|
88
|
+
return FillMaskResponse.model_validate(response_data)
|