Skip to content

Commit cac8e36

Browse files
committed
merged main
2 parents a13efcc + 82fa060 commit cac8e36

File tree

4 files changed

+178
-30
lines changed

4 files changed

+178
-30
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ pyproject.toml
3030
uv.lock
3131

3232
.ruff_cache/
33-
local_tests/
33+
local_tests/*
34+

jigsawstack/__init__.py

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from .image_generation import ImageGeneration, AsyncImageGeneration
1717
from .classification import Classification, AsyncClassification
1818
from .prompt_engine import PromptEngine, AsyncPromptEngine
19+
from .embeddingV2 import EmbeddingV2, AsyncEmbeddingV2
1920

2021

2122
class JigsawStack:
@@ -50,7 +51,7 @@ def __init__(
5051
if api_url is None:
5152
api_url = os.environ.get("JIGSAWSTACK_API_URL")
5253
if api_url is None:
53-
api_url = f"https://api.jigsawstack.com/v1"
54+
api_url = f"https://api.jigsawstack.com/"
5455

5556
self.api_key = api_key
5657
self.api_url = api_url
@@ -61,69 +62,76 @@ def __init__(
6162

6263
self.audio = Audio(
6364
api_key=api_key,
64-
api_url=api_url,
65+
api_url=api_url + "/v1",
6566
disable_request_logging=disable_request_logging,
6667
)
6768
self.web = Web(
6869
api_key=api_key,
69-
api_url=api_url,
70+
api_url=api_url + "/v1",
7071
disable_request_logging=disable_request_logging,
7172
)
7273
self.sentiment = Sentiment(
7374
api_key=api_key,
74-
api_url=api_url,
75+
api_url=api_url + "/v1",
7576
disable_request_logging=disable_request_logging,
7677
).analyze
7778
self.validate = Validate(
7879
api_key=api_key,
79-
api_url=api_url,
80+
api_url=api_url + "/v1",
8081
disable_request_logging=disable_request_logging,
8182
)
8283
self.summary = Summary(
8384
api_key=api_key,
84-
api_url=api_url,
85+
api_url=api_url + "/v1",
8586
disable_request_logging=disable_request_logging,
8687
).summarize
8788
self.vision = Vision(
8889
api_key=api_key,
89-
api_url=api_url,
90+
api_url=api_url + "/v1",
9091
disable_request_logging=disable_request_logging,
9192
)
9293
self.prediction = Prediction(
9394
api_key=api_key,
94-
api_url=api_url,
95+
api_url=api_url + "/v1",
9596
disable_request_logging=disable_request_logging,
9697
).predict
9798
self.text_to_sql = SQL(
9899
api_key=api_key,
99-
api_url=api_url,
100+
api_url=api_url + "/v1",
100101
disable_request_logging=disable_request_logging,
101102
).text_to_sql
102103
self.store = Store(
103104
api_key=api_key,
104-
api_url=api_url,
105+
api_url=api_url + "/v1",
105106
disable_request_logging=disable_request_logging,
106107
)
107108
self.translate = Translate(
108109
api_key=api_key,
109-
api_url=api_url,
110+
api_url=api_url + "/v1",
110111
disable_request_logging=disable_request_logging,
111112
)
112113

113114
self.embedding = Embedding(
114115
api_key=api_key,
115-
api_url=api_url,
116+
api_url=api_url + "/v1",
116117
disable_request_logging=disable_request_logging,
117118
).execute
119+
120+
self.embeddingV2 = EmbeddingV2(
121+
api_key=api_key,
122+
api_url=api_url + "/v2",
123+
disable_request_logging=disable_request_logging,
124+
).execute
125+
118126
self.image_generation = ImageGeneration(
119127
api_key=api_key,
120-
api_url=api_url,
128+
api_url=api_url + "/v1",
121129
disable_request_logging=disable_request_logging,
122130
).image_generation
123131

124132
self.classification = Classification(
125133
api_key=api_key,
126-
api_url=api_url,
134+
api_url=api_url + "/v1",
127135
disable_request_logging=disable_request_logging,
128136
).classify
129137

@@ -163,84 +171,90 @@ def __init__(
163171
if api_url is None:
164172
api_url = os.environ.get("JIGSAWSTACK_API_URL")
165173
if api_url is None:
166-
api_url = f"https://api.jigsawstack.com/v1"
174+
api_url = f"https://api.jigsawstack.com/"
167175

168176
self.api_key = api_key
169177
self.api_url = api_url
170178

171179
self.web = AsyncWeb(
172180
api_key=api_key,
173-
api_url=api_url,
181+
api_url=api_url + "/v1",
174182
disable_request_logging=disable_request_logging,
175183
)
176184

177185
self.validate = AsyncValidate(
178186
api_key=api_key,
179-
api_url=api_url,
187+
api_url=api_url + "/v1",
180188
disable_request_logging=disable_request_logging,
181189
)
182190
self.audio = AsyncAudio(
183191
api_key=api_key,
184-
api_url=api_url,
192+
api_url=api_url + "/v1",
185193
disable_request_logging=disable_request_logging,
186194
)
187195

188196
self.vision = AsyncVision(
189197
api_key=api_key,
190-
api_url=api_url,
198+
api_url=api_url + "/v1",
191199
disable_request_logging=disable_request_logging,
192200
)
193201

194202
self.store = AsyncStore(
195203
api_key=api_key,
196-
api_url=api_url,
204+
api_url=api_url + "/v1",
197205
disable_request_logging=disable_request_logging,
198206
)
199207

200208
self.summary = AsyncSummary(
201209
api_key=api_key,
202-
api_url=api_url,
210+
api_url=api_url + "/v1",
203211
disable_request_logging=disable_request_logging,
204212
).summarize
205213

206214
self.prediction = AsyncPrediction(
207215
api_key=api_key,
208-
api_url=api_url,
216+
api_url=api_url + "/v1",
209217
disable_request_logging=disable_request_logging,
210218
).predict
211219
self.text_to_sql = AsyncSQL(
212220
api_key=api_key,
213-
api_url=api_url,
221+
api_url=api_url + "/v1",
214222
disable_request_logging=disable_request_logging,
215223
).text_to_sql
216224

217225
self.sentiment = AsyncSentiment(
218226
api_key=api_key,
219-
api_url=api_url,
227+
api_url=api_url + "/v1",
220228
disable_request_logging=disable_request_logging,
221229
).analyze
222230

223231
self.translate = AsyncTranslate(
224232
api_key=api_key,
225-
api_url=api_url,
233+
api_url=api_url + "/v1",
226234
disable_request_logging=disable_request_logging,
227235
)
228236

229237
self.embedding = AsyncEmbedding(
230238
api_key=api_key,
231-
api_url=api_url,
239+
api_url=api_url + "/v1",
240+
disable_request_logging=disable_request_logging,
241+
).execute
242+
243+
self.embeddingV2 = AsyncEmbeddingV2(
244+
api_key=api_key,
245+
api_url=api_url + "/v2",
232246
disable_request_logging=disable_request_logging,
233247
).execute
234248

235249
self.image_generation = AsyncImageGeneration(
236250
api_key=api_key,
237-
api_url=api_url,
251+
api_url=api_url + "/v1",
238252
disable_request_logging=disable_request_logging,
239253
).image_generation
240254

241255
self.classification = AsyncClassification(
242256
api_key=api_key,
243-
api_url=api_url,
257+
api_url=api_url + "/v1",
244258
disable_request_logging=disable_request_logging,
245259
).classify
246260

jigsawstack/embedding.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Chunk(TypedDict):
2424

2525
class EmbeddingResponse(BaseResponse):
2626
embeddings: List[List[float]]
27-
chunks: List[Chunk]
27+
chunks: Union[List[Chunk], List[str]]
2828

2929

3030
class Embedding(ClientConfig):

jigsawstack/embeddingV2.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
from typing import Any, Dict, List, Union, cast, Literal, overload
2+
from typing_extensions import NotRequired, TypedDict
3+
from .request import Request, RequestConfig
4+
from .async_request import AsyncRequest
5+
from typing import List, Union
6+
from ._config import ClientConfig
7+
from .helpers import build_path
8+
from .embedding import Chunk
9+
10+
11+
class EmbeddingV2Params(TypedDict):
12+
text: NotRequired[str]
13+
file_content: NotRequired[Any]
14+
type: Literal["text", "text-other", "image", "audio", "pdf"]
15+
url: NotRequired[str]
16+
file_store_key: NotRequired[str]
17+
token_overflow_mode: NotRequired[Literal["truncate", "chunk", "error"]] = "chunk"
18+
speaker_fingerprint: NotRequired[bool]
19+
20+
21+
class EmbeddingV2Response(TypedDict):
22+
success: bool
23+
embeddings: List[List[float]]
24+
chunks: Union[List[str], List[Chunk]]
25+
speaker_embeddings: List[List[float]]
26+
27+
28+
class EmbeddingV2(ClientConfig):
29+
config: RequestConfig
30+
31+
def __init__(
32+
self,
33+
api_key: str,
34+
api_url: str,
35+
disable_request_logging: Union[bool, None] = False,
36+
):
37+
super().__init__(api_key, api_url, disable_request_logging)
38+
self.config = RequestConfig(
39+
api_url=api_url,
40+
api_key=api_key,
41+
disable_request_logging=disable_request_logging,
42+
)
43+
44+
@overload
45+
def execute(self, params: EmbeddingV2Params) -> EmbeddingV2Response: ...
46+
@overload
47+
def execute(
48+
self, blob: bytes, options: EmbeddingV2Params = None
49+
) -> EmbeddingV2Response: ...
50+
51+
def execute(
52+
self,
53+
blob: Union[EmbeddingV2Params, bytes],
54+
options: EmbeddingV2Params = None,
55+
) -> EmbeddingV2Response:
56+
path = "/embedding"
57+
if isinstance(blob, dict):
58+
resp = Request(
59+
config=self.config,
60+
path=path,
61+
params=cast(Dict[Any, Any], blob),
62+
verb="post",
63+
).perform_with_content()
64+
return resp
65+
66+
options = options or {}
67+
path = build_path(base_path=path, params=options)
68+
content_type = options.get("content_type", "application/octet-stream")
69+
_headers = {"Content-Type": content_type}
70+
71+
resp = Request(
72+
config=self.config,
73+
path=path,
74+
params=options,
75+
data=blob,
76+
headers=_headers,
77+
verb="post",
78+
).perform_with_content()
79+
return resp
80+
81+
82+
class AsyncEmbeddingV2(ClientConfig):
83+
config: RequestConfig
84+
85+
def __init__(
86+
self,
87+
api_key: str,
88+
api_url: str,
89+
disable_request_logging: Union[bool, None] = False,
90+
):
91+
super().__init__(api_key, api_url, disable_request_logging)
92+
self.config = RequestConfig(
93+
api_url=api_url,
94+
api_key=api_key,
95+
disable_request_logging=disable_request_logging,
96+
)
97+
98+
@overload
99+
async def execute(self, params: EmbeddingV2Params) -> EmbeddingV2Response: ...
100+
@overload
101+
async def execute(
102+
self, blob: bytes, options: EmbeddingV2Params = None
103+
) -> EmbeddingV2Response: ...
104+
105+
async def execute(
106+
self,
107+
blob: Union[EmbeddingV2Params, bytes],
108+
options: EmbeddingV2Params = None,
109+
) -> EmbeddingV2Response:
110+
path = "/embedding"
111+
if isinstance(blob, dict):
112+
resp = await AsyncRequest(
113+
config=self.config,
114+
path=path,
115+
params=cast(Dict[Any, Any], blob),
116+
verb="post",
117+
).perform_with_content()
118+
return resp
119+
120+
options = options or {}
121+
path = build_path(base_path=path, params=options)
122+
content_type = options.get("content_type", "application/octet-stream")
123+
_headers = {"Content-Type": content_type}
124+
125+
resp = await AsyncRequest(
126+
config=self.config,
127+
path=path,
128+
params=options,
129+
data=blob,
130+
headers=_headers,
131+
verb="post",
132+
).perform_with_content()
133+
return resp

0 commit comments

Comments
 (0)