together 1.2.11__py3-none-any.whl → 2.0.0a8__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.
- together/__init__.py +101 -63
- together/_base_client.py +1995 -0
- together/_client.py +1033 -0
- together/_compat.py +219 -0
- together/_constants.py +14 -0
- together/_exceptions.py +108 -0
- together/_files.py +123 -0
- together/_models.py +857 -0
- together/_qs.py +150 -0
- together/_resource.py +43 -0
- together/_response.py +830 -0
- together/_streaming.py +370 -0
- together/_types.py +260 -0
- together/_utils/__init__.py +64 -0
- together/_utils/_compat.py +45 -0
- together/_utils/_datetime_parse.py +136 -0
- together/_utils/_logs.py +25 -0
- together/_utils/_proxy.py +65 -0
- together/_utils/_reflection.py +42 -0
- together/_utils/_resources_proxy.py +24 -0
- together/_utils/_streams.py +12 -0
- together/_utils/_sync.py +58 -0
- together/_utils/_transform.py +457 -0
- together/_utils/_typing.py +156 -0
- together/_utils/_utils.py +421 -0
- together/_version.py +4 -0
- together/lib/.keep +4 -0
- together/lib/__init__.py +23 -0
- together/lib/cli/api/endpoints.py +467 -0
- together/lib/cli/api/evals.py +588 -0
- together/{cli → lib/cli}/api/files.py +20 -17
- together/lib/cli/api/fine_tuning.py +566 -0
- together/lib/cli/api/models.py +140 -0
- together/lib/cli/api/utils.py +50 -0
- together/{cli → lib/cli}/cli.py +17 -23
- together/lib/constants.py +61 -0
- together/lib/resources/__init__.py +11 -0
- together/lib/resources/files.py +999 -0
- together/lib/resources/fine_tuning.py +280 -0
- together/lib/resources/models.py +35 -0
- together/lib/types/__init__.py +13 -0
- together/lib/types/error.py +9 -0
- together/lib/types/fine_tuning.py +455 -0
- together/{utils → lib/utils}/__init__.py +7 -10
- together/{utils → lib/utils}/_log.py +18 -13
- together/lib/utils/files.py +628 -0
- together/lib/utils/serializer.py +10 -0
- together/{utils → lib/utils}/tools.py +17 -2
- together/resources/__init__.py +225 -24
- together/resources/audio/__init__.py +75 -0
- together/resources/audio/audio.py +198 -0
- together/resources/audio/speech.py +605 -0
- together/resources/audio/transcriptions.py +282 -0
- together/resources/audio/translations.py +256 -0
- together/resources/audio/voices.py +135 -0
- together/resources/batches.py +417 -0
- together/resources/chat/__init__.py +30 -21
- together/resources/chat/chat.py +102 -0
- together/resources/chat/completions.py +1063 -257
- together/resources/code_interpreter/__init__.py +33 -0
- together/resources/code_interpreter/code_interpreter.py +258 -0
- together/resources/code_interpreter/sessions.py +135 -0
- together/resources/completions.py +890 -225
- together/resources/embeddings.py +172 -68
- together/resources/endpoints.py +711 -0
- together/resources/evals.py +452 -0
- together/resources/files.py +397 -120
- together/resources/fine_tuning.py +1033 -0
- together/resources/hardware.py +181 -0
- together/resources/images.py +256 -108
- together/resources/jobs.py +214 -0
- together/resources/models.py +251 -44
- together/resources/rerank.py +190 -92
- together/resources/videos.py +374 -0
- together/types/__init__.py +66 -73
- together/types/audio/__init__.py +10 -0
- together/types/audio/speech_create_params.py +75 -0
- together/types/audio/transcription_create_params.py +54 -0
- together/types/audio/transcription_create_response.py +111 -0
- together/types/audio/translation_create_params.py +40 -0
- together/types/audio/translation_create_response.py +70 -0
- together/types/audio/voice_list_response.py +23 -0
- together/types/audio_speech_stream_chunk.py +16 -0
- together/types/autoscaling.py +13 -0
- together/types/autoscaling_param.py +15 -0
- together/types/batch_create_params.py +24 -0
- together/types/batch_create_response.py +14 -0
- together/types/batch_job.py +45 -0
- together/types/batch_list_response.py +10 -0
- together/types/chat/__init__.py +18 -0
- together/types/chat/chat_completion.py +60 -0
- together/types/chat/chat_completion_chunk.py +61 -0
- together/types/chat/chat_completion_structured_message_image_url_param.py +18 -0
- together/types/chat/chat_completion_structured_message_text_param.py +13 -0
- together/types/chat/chat_completion_structured_message_video_url_param.py +18 -0
- together/types/chat/chat_completion_usage.py +13 -0
- together/types/chat/chat_completion_warning.py +9 -0
- together/types/chat/completion_create_params.py +329 -0
- together/types/code_interpreter/__init__.py +5 -0
- together/types/code_interpreter/session_list_response.py +31 -0
- together/types/code_interpreter_execute_params.py +45 -0
- together/types/completion.py +42 -0
- together/types/completion_chunk.py +66 -0
- together/types/completion_create_params.py +138 -0
- together/types/dedicated_endpoint.py +44 -0
- together/types/embedding.py +24 -0
- together/types/embedding_create_params.py +31 -0
- together/types/endpoint_create_params.py +43 -0
- together/types/endpoint_list_avzones_response.py +11 -0
- together/types/endpoint_list_params.py +18 -0
- together/types/endpoint_list_response.py +41 -0
- together/types/endpoint_update_params.py +27 -0
- together/types/eval_create_params.py +263 -0
- together/types/eval_create_response.py +16 -0
- together/types/eval_list_params.py +21 -0
- together/types/eval_list_response.py +10 -0
- together/types/eval_status_response.py +100 -0
- together/types/evaluation_job.py +139 -0
- together/types/execute_response.py +108 -0
- together/types/file_delete_response.py +13 -0
- together/types/file_list.py +12 -0
- together/types/file_purpose.py +9 -0
- together/types/file_response.py +31 -0
- together/types/file_type.py +7 -0
- together/types/fine_tuning_cancel_response.py +194 -0
- together/types/fine_tuning_content_params.py +24 -0
- together/types/fine_tuning_delete_params.py +11 -0
- together/types/fine_tuning_delete_response.py +12 -0
- together/types/fine_tuning_list_checkpoints_response.py +21 -0
- together/types/fine_tuning_list_events_response.py +12 -0
- together/types/fine_tuning_list_response.py +199 -0
- together/types/finetune_event.py +41 -0
- together/types/finetune_event_type.py +33 -0
- together/types/finetune_response.py +177 -0
- together/types/hardware_list_params.py +16 -0
- together/types/hardware_list_response.py +58 -0
- together/types/image_data_b64.py +15 -0
- together/types/image_data_url.py +15 -0
- together/types/image_file.py +23 -0
- together/types/image_generate_params.py +85 -0
- together/types/job_list_response.py +47 -0
- together/types/job_retrieve_response.py +43 -0
- together/types/log_probs.py +18 -0
- together/types/model_list_response.py +10 -0
- together/types/model_object.py +42 -0
- together/types/model_upload_params.py +36 -0
- together/types/model_upload_response.py +23 -0
- together/types/rerank_create_params.py +36 -0
- together/types/rerank_create_response.py +36 -0
- together/types/tool_choice.py +23 -0
- together/types/tool_choice_param.py +23 -0
- together/types/tools_param.py +23 -0
- together/types/training_method_dpo.py +22 -0
- together/types/training_method_sft.py +18 -0
- together/types/video_create_params.py +86 -0
- together/types/video_job.py +57 -0
- together-2.0.0a8.dist-info/METADATA +680 -0
- together-2.0.0a8.dist-info/RECORD +164 -0
- {together-1.2.11.dist-info → together-2.0.0a8.dist-info}/WHEEL +1 -1
- together-2.0.0a8.dist-info/entry_points.txt +2 -0
- {together-1.2.11.dist-info → together-2.0.0a8.dist-info/licenses}/LICENSE +1 -1
- together/abstract/api_requestor.py +0 -723
- together/cli/api/chat.py +0 -276
- together/cli/api/completions.py +0 -119
- together/cli/api/finetune.py +0 -272
- together/cli/api/images.py +0 -82
- together/cli/api/models.py +0 -42
- together/client.py +0 -157
- together/constants.py +0 -31
- together/error.py +0 -191
- together/filemanager.py +0 -388
- together/legacy/__init__.py +0 -0
- together/legacy/base.py +0 -27
- together/legacy/complete.py +0 -93
- together/legacy/embeddings.py +0 -27
- together/legacy/files.py +0 -146
- together/legacy/finetune.py +0 -177
- together/legacy/images.py +0 -27
- together/legacy/models.py +0 -44
- together/resources/finetune.py +0 -489
- together/together_response.py +0 -50
- together/types/abstract.py +0 -26
- together/types/chat_completions.py +0 -171
- together/types/common.py +0 -65
- together/types/completions.py +0 -104
- together/types/embeddings.py +0 -35
- together/types/error.py +0 -16
- together/types/files.py +0 -89
- together/types/finetune.py +0 -265
- together/types/images.py +0 -42
- together/types/models.py +0 -44
- together/types/rerank.py +0 -43
- together/utils/api_helpers.py +0 -84
- together/utils/files.py +0 -204
- together/version.py +0 -6
- together-1.2.11.dist-info/METADATA +0 -408
- together-1.2.11.dist-info/RECORD +0 -58
- together-1.2.11.dist-info/entry_points.txt +0 -3
- /together/{abstract → lib/cli}/__init__.py +0 -0
- /together/{cli → lib/cli/api}/__init__.py +0 -0
- /together/{cli/api/__init__.py → py.typed} +0 -0
together/filemanager.py
DELETED
|
@@ -1,388 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
import os
|
|
4
|
-
import shutil
|
|
5
|
-
import stat
|
|
6
|
-
import tempfile
|
|
7
|
-
import uuid
|
|
8
|
-
from functools import partial
|
|
9
|
-
from pathlib import Path
|
|
10
|
-
from typing import Tuple
|
|
11
|
-
|
|
12
|
-
import requests
|
|
13
|
-
from filelock import FileLock
|
|
14
|
-
from requests.structures import CaseInsensitiveDict
|
|
15
|
-
from tqdm import tqdm
|
|
16
|
-
from tqdm.utils import CallbackIOWrapper
|
|
17
|
-
|
|
18
|
-
import together.utils
|
|
19
|
-
from together.abstract import api_requestor
|
|
20
|
-
from together.constants import DISABLE_TQDM, DOWNLOAD_BLOCK_SIZE, MAX_RETRIES
|
|
21
|
-
from together.error import (
|
|
22
|
-
APIError,
|
|
23
|
-
AuthenticationError,
|
|
24
|
-
DownloadError,
|
|
25
|
-
FileTypeError,
|
|
26
|
-
)
|
|
27
|
-
from together.together_response import TogetherResponse
|
|
28
|
-
from together.types import (
|
|
29
|
-
FilePurpose,
|
|
30
|
-
FileResponse,
|
|
31
|
-
FileType,
|
|
32
|
-
TogetherClient,
|
|
33
|
-
TogetherRequest,
|
|
34
|
-
)
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
def chmod_and_replace(src: Path, dst: Path) -> None:
|
|
38
|
-
"""Set correct permission before moving a blob from tmp directory to cache dir.
|
|
39
|
-
|
|
40
|
-
Do not take into account the `umask` from the process as there is no convenient way
|
|
41
|
-
to get it that is thread-safe.
|
|
42
|
-
"""
|
|
43
|
-
|
|
44
|
-
# Get umask by creating a temporary file in the cache folder.
|
|
45
|
-
tmp_file = dst.parent / f"tmp_{uuid.uuid4()}"
|
|
46
|
-
|
|
47
|
-
try:
|
|
48
|
-
tmp_file.touch()
|
|
49
|
-
|
|
50
|
-
cache_dir_mode = Path(tmp_file).stat().st_mode
|
|
51
|
-
|
|
52
|
-
os.chmod(src.as_posix(), stat.S_IMODE(cache_dir_mode))
|
|
53
|
-
|
|
54
|
-
finally:
|
|
55
|
-
tmp_file.unlink()
|
|
56
|
-
|
|
57
|
-
shutil.move(src.as_posix(), dst.as_posix())
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
def _get_file_size(
|
|
61
|
-
headers: CaseInsensitiveDict[str],
|
|
62
|
-
) -> int:
|
|
63
|
-
"""
|
|
64
|
-
Extracts file size from header
|
|
65
|
-
"""
|
|
66
|
-
total_size_in_bytes = 0
|
|
67
|
-
|
|
68
|
-
parts = headers.get("Content-Range", "").split(" ")
|
|
69
|
-
|
|
70
|
-
if len(parts) == 2:
|
|
71
|
-
range_parts = parts[1].split("/")
|
|
72
|
-
|
|
73
|
-
if len(range_parts) == 2:
|
|
74
|
-
total_size_in_bytes = int(range_parts[1])
|
|
75
|
-
|
|
76
|
-
assert total_size_in_bytes != 0, "Unable to retrieve remote file."
|
|
77
|
-
|
|
78
|
-
return total_size_in_bytes
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
def _prepare_output(
|
|
82
|
-
headers: CaseInsensitiveDict[str],
|
|
83
|
-
step: int = -1,
|
|
84
|
-
output: Path | None = None,
|
|
85
|
-
remote_name: str | None = None,
|
|
86
|
-
) -> Path:
|
|
87
|
-
"""
|
|
88
|
-
Generates output file name from remote name and headers
|
|
89
|
-
"""
|
|
90
|
-
if output:
|
|
91
|
-
return output
|
|
92
|
-
|
|
93
|
-
content_type = str(headers.get("content-type"))
|
|
94
|
-
|
|
95
|
-
assert remote_name, (
|
|
96
|
-
"No model name found in fine_tune object. "
|
|
97
|
-
"Please specify an `output` file name."
|
|
98
|
-
)
|
|
99
|
-
|
|
100
|
-
if step > 0:
|
|
101
|
-
remote_name += f"-checkpoint-{step}"
|
|
102
|
-
|
|
103
|
-
if "x-tar" in content_type.lower():
|
|
104
|
-
remote_name += ".tar.gz"
|
|
105
|
-
|
|
106
|
-
else:
|
|
107
|
-
remote_name += ".tar.zst"
|
|
108
|
-
|
|
109
|
-
return Path(remote_name)
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
class DownloadManager:
|
|
113
|
-
def __init__(self, client: TogetherClient) -> None:
|
|
114
|
-
self._client = client
|
|
115
|
-
|
|
116
|
-
def get_file_metadata(
|
|
117
|
-
self,
|
|
118
|
-
url: str,
|
|
119
|
-
output: Path | None = None,
|
|
120
|
-
remote_name: str | None = None,
|
|
121
|
-
fetch_metadata: bool = False,
|
|
122
|
-
) -> Tuple[Path, int]:
|
|
123
|
-
"""
|
|
124
|
-
gets remote file head and parses out file name and file size
|
|
125
|
-
"""
|
|
126
|
-
|
|
127
|
-
if not fetch_metadata:
|
|
128
|
-
if isinstance(output, Path):
|
|
129
|
-
file_path = output
|
|
130
|
-
else:
|
|
131
|
-
assert isinstance(remote_name, str)
|
|
132
|
-
file_path = Path(remote_name)
|
|
133
|
-
|
|
134
|
-
return file_path, 0
|
|
135
|
-
|
|
136
|
-
requestor = api_requestor.APIRequestor(
|
|
137
|
-
client=self._client,
|
|
138
|
-
)
|
|
139
|
-
|
|
140
|
-
response = requestor.request_raw(
|
|
141
|
-
options=TogetherRequest(
|
|
142
|
-
method="GET",
|
|
143
|
-
url=url,
|
|
144
|
-
headers={"Range": "bytes=0-1"},
|
|
145
|
-
),
|
|
146
|
-
remaining_retries=MAX_RETRIES,
|
|
147
|
-
stream=False,
|
|
148
|
-
)
|
|
149
|
-
|
|
150
|
-
try:
|
|
151
|
-
response.raise_for_status()
|
|
152
|
-
except requests.exceptions.HTTPError as e:
|
|
153
|
-
raise APIError(
|
|
154
|
-
"Error fetching file metadata", http_status=response.status_code
|
|
155
|
-
) from e
|
|
156
|
-
|
|
157
|
-
headers = response.headers
|
|
158
|
-
|
|
159
|
-
assert isinstance(headers, CaseInsensitiveDict)
|
|
160
|
-
|
|
161
|
-
file_path = _prepare_output(
|
|
162
|
-
headers=headers,
|
|
163
|
-
output=output,
|
|
164
|
-
remote_name=remote_name,
|
|
165
|
-
)
|
|
166
|
-
|
|
167
|
-
file_size = _get_file_size(headers)
|
|
168
|
-
|
|
169
|
-
return file_path, file_size
|
|
170
|
-
|
|
171
|
-
def download(
|
|
172
|
-
self,
|
|
173
|
-
url: str,
|
|
174
|
-
output: Path | None = None,
|
|
175
|
-
remote_name: str | None = None,
|
|
176
|
-
fetch_metadata: bool = False,
|
|
177
|
-
) -> Tuple[str, int]:
|
|
178
|
-
requestor = api_requestor.APIRequestor(
|
|
179
|
-
client=self._client,
|
|
180
|
-
)
|
|
181
|
-
|
|
182
|
-
# pre-fetch remote file name and file size
|
|
183
|
-
file_path, file_size = self.get_file_metadata(
|
|
184
|
-
url, output, remote_name, fetch_metadata
|
|
185
|
-
)
|
|
186
|
-
|
|
187
|
-
temp_file_manager = partial(
|
|
188
|
-
tempfile.NamedTemporaryFile, mode="wb", dir=file_path.parent, delete=False
|
|
189
|
-
)
|
|
190
|
-
|
|
191
|
-
# Prevent parallel downloads of the same file with a lock.
|
|
192
|
-
lock_path = Path(file_path.as_posix() + ".lock")
|
|
193
|
-
|
|
194
|
-
with FileLock(lock_path.as_posix()):
|
|
195
|
-
with temp_file_manager() as temp_file:
|
|
196
|
-
response = requestor.request_raw(
|
|
197
|
-
options=TogetherRequest(
|
|
198
|
-
method="GET",
|
|
199
|
-
url=url,
|
|
200
|
-
),
|
|
201
|
-
remaining_retries=MAX_RETRIES,
|
|
202
|
-
stream=True,
|
|
203
|
-
)
|
|
204
|
-
|
|
205
|
-
try:
|
|
206
|
-
response.raise_for_status()
|
|
207
|
-
except Exception as e:
|
|
208
|
-
os.remove(lock_path)
|
|
209
|
-
raise APIError(
|
|
210
|
-
"Error downloading file", http_status=response.status_code
|
|
211
|
-
) from e
|
|
212
|
-
|
|
213
|
-
if not fetch_metadata:
|
|
214
|
-
file_size = int(response.headers.get("content-length", 0))
|
|
215
|
-
|
|
216
|
-
assert file_size != 0, "Unable to retrieve remote file."
|
|
217
|
-
|
|
218
|
-
with tqdm(
|
|
219
|
-
total=file_size,
|
|
220
|
-
unit="B",
|
|
221
|
-
unit_scale=True,
|
|
222
|
-
desc=f"Downloading file {file_path.name}",
|
|
223
|
-
disable=bool(DISABLE_TQDM),
|
|
224
|
-
) as pbar:
|
|
225
|
-
for chunk in response.iter_content(DOWNLOAD_BLOCK_SIZE):
|
|
226
|
-
pbar.update(len(chunk))
|
|
227
|
-
temp_file.write(chunk)
|
|
228
|
-
|
|
229
|
-
# Raise exception if remote file size does not match downloaded file size
|
|
230
|
-
if os.stat(temp_file.name).st_size != file_size:
|
|
231
|
-
DownloadError(
|
|
232
|
-
f"Downloaded file size `{pbar.n}` bytes does not match "
|
|
233
|
-
f"remote file size `{file_size}` bytes."
|
|
234
|
-
)
|
|
235
|
-
|
|
236
|
-
# Moves temp file to output file path
|
|
237
|
-
chmod_and_replace(Path(temp_file.name), file_path)
|
|
238
|
-
|
|
239
|
-
os.remove(lock_path)
|
|
240
|
-
|
|
241
|
-
return str(file_path.resolve()), file_size
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
class UploadManager:
|
|
245
|
-
def __init__(self, client: TogetherClient) -> None:
|
|
246
|
-
self._client = client
|
|
247
|
-
|
|
248
|
-
@classmethod
|
|
249
|
-
def _redirect_error_handler(
|
|
250
|
-
cls, requestor: api_requestor.APIRequestor, response: requests.Response
|
|
251
|
-
) -> None:
|
|
252
|
-
if response.status_code == 401:
|
|
253
|
-
raise AuthenticationError(
|
|
254
|
-
"This job would exceed your free trial credits. "
|
|
255
|
-
"Please upgrade to a paid account through "
|
|
256
|
-
"Settings -> Billing on api.together.ai to continue.",
|
|
257
|
-
)
|
|
258
|
-
elif response.status_code != 302:
|
|
259
|
-
raise APIError(
|
|
260
|
-
f"Unexpected error raised by endpoint: {response.content.decode()}, headers: {response.headers}",
|
|
261
|
-
http_status=response.status_code,
|
|
262
|
-
)
|
|
263
|
-
|
|
264
|
-
def get_upload_url(
|
|
265
|
-
self,
|
|
266
|
-
url: str,
|
|
267
|
-
file: Path,
|
|
268
|
-
purpose: FilePurpose,
|
|
269
|
-
filetype: FileType,
|
|
270
|
-
) -> Tuple[str, str]:
|
|
271
|
-
data = {
|
|
272
|
-
"purpose": purpose.value,
|
|
273
|
-
"file_name": file.name,
|
|
274
|
-
"file_type": filetype.value,
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
requestor = api_requestor.APIRequestor(
|
|
278
|
-
client=self._client,
|
|
279
|
-
)
|
|
280
|
-
|
|
281
|
-
method = "POST"
|
|
282
|
-
|
|
283
|
-
headers = together.utils.get_headers(method, requestor.api_key)
|
|
284
|
-
|
|
285
|
-
response = requestor.request_raw(
|
|
286
|
-
options=TogetherRequest(
|
|
287
|
-
method=method,
|
|
288
|
-
url=url,
|
|
289
|
-
params=data,
|
|
290
|
-
allow_redirects=False,
|
|
291
|
-
override_headers=True,
|
|
292
|
-
headers=headers,
|
|
293
|
-
),
|
|
294
|
-
remaining_retries=MAX_RETRIES,
|
|
295
|
-
)
|
|
296
|
-
|
|
297
|
-
self._redirect_error_handler(requestor, response)
|
|
298
|
-
|
|
299
|
-
redirect_url = response.headers["Location"]
|
|
300
|
-
file_id = response.headers["X-Together-File-Id"]
|
|
301
|
-
|
|
302
|
-
return redirect_url, file_id
|
|
303
|
-
|
|
304
|
-
def callback(self, url: str) -> TogetherResponse:
|
|
305
|
-
requestor = api_requestor.APIRequestor(
|
|
306
|
-
client=self._client,
|
|
307
|
-
)
|
|
308
|
-
|
|
309
|
-
response, _, _ = requestor.request(
|
|
310
|
-
options=TogetherRequest(
|
|
311
|
-
method="POST",
|
|
312
|
-
url=url,
|
|
313
|
-
),
|
|
314
|
-
)
|
|
315
|
-
|
|
316
|
-
return response
|
|
317
|
-
|
|
318
|
-
def upload(
|
|
319
|
-
self,
|
|
320
|
-
url: str,
|
|
321
|
-
file: Path,
|
|
322
|
-
purpose: FilePurpose,
|
|
323
|
-
redirect: bool = False,
|
|
324
|
-
) -> FileResponse:
|
|
325
|
-
file_id = None
|
|
326
|
-
|
|
327
|
-
requestor = api_requestor.APIRequestor(
|
|
328
|
-
client=self._client,
|
|
329
|
-
)
|
|
330
|
-
|
|
331
|
-
redirect_url = None
|
|
332
|
-
if redirect:
|
|
333
|
-
if file.suffix == ".jsonl":
|
|
334
|
-
filetype = FileType.jsonl
|
|
335
|
-
elif file.suffix == ".parquet":
|
|
336
|
-
filetype = FileType.parquet
|
|
337
|
-
else:
|
|
338
|
-
raise FileTypeError(
|
|
339
|
-
f"Unknown extension of file {file}. "
|
|
340
|
-
"Only files with extensions .jsonl and .parquet are supported."
|
|
341
|
-
)
|
|
342
|
-
redirect_url, file_id = self.get_upload_url(url, file, purpose, filetype)
|
|
343
|
-
|
|
344
|
-
file_size = os.stat(file.as_posix()).st_size
|
|
345
|
-
|
|
346
|
-
with tqdm(
|
|
347
|
-
total=file_size,
|
|
348
|
-
unit="B",
|
|
349
|
-
unit_scale=True,
|
|
350
|
-
desc=f"Uploading file {file.name}",
|
|
351
|
-
disable=bool(DISABLE_TQDM),
|
|
352
|
-
) as pbar:
|
|
353
|
-
with file.open("rb") as f:
|
|
354
|
-
wrapped_file = CallbackIOWrapper(pbar.update, f, "read")
|
|
355
|
-
|
|
356
|
-
if redirect:
|
|
357
|
-
callback_response = requestor.request_raw(
|
|
358
|
-
options=TogetherRequest(
|
|
359
|
-
method="PUT",
|
|
360
|
-
url=redirect_url,
|
|
361
|
-
params=wrapped_file,
|
|
362
|
-
override_headers=True,
|
|
363
|
-
),
|
|
364
|
-
absolute=True,
|
|
365
|
-
remaining_retries=MAX_RETRIES,
|
|
366
|
-
)
|
|
367
|
-
else:
|
|
368
|
-
response, _, _ = requestor.request(
|
|
369
|
-
options=TogetherRequest(
|
|
370
|
-
method="PUT",
|
|
371
|
-
url=url,
|
|
372
|
-
params=wrapped_file,
|
|
373
|
-
),
|
|
374
|
-
)
|
|
375
|
-
|
|
376
|
-
if redirect:
|
|
377
|
-
assert isinstance(callback_response, requests.Response)
|
|
378
|
-
|
|
379
|
-
if not callback_response.status_code == 200:
|
|
380
|
-
raise APIError(
|
|
381
|
-
f"Error code: {callback_response.status_code} - Failed to process uploaded file"
|
|
382
|
-
)
|
|
383
|
-
|
|
384
|
-
response = self.callback(f"{url}/{file_id}/preprocess")
|
|
385
|
-
|
|
386
|
-
assert isinstance(response, TogetherResponse)
|
|
387
|
-
|
|
388
|
-
return FileResponse(**response.data)
|
together/legacy/__init__.py
DELETED
|
File without changes
|
together/legacy/base.py
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import functools
|
|
2
|
-
import warnings
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
API_KEY_WARNING = (
|
|
6
|
-
"The use of together.api_key is deprecated and will be removed in the next major release. "
|
|
7
|
-
"Please set the TOGETHER_API_KEY environment variable instead."
|
|
8
|
-
)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def deprecated(func): # type: ignore
|
|
12
|
-
"""
|
|
13
|
-
This is a decorator which can be used to mark functions
|
|
14
|
-
as deprecated. It will result in a warning being emitted
|
|
15
|
-
when the function is used.
|
|
16
|
-
"""
|
|
17
|
-
|
|
18
|
-
@functools.wraps(func)
|
|
19
|
-
def new_func(*args, **kwargs): # type: ignore
|
|
20
|
-
warnings.warn(
|
|
21
|
-
f"Call to deprecated function {func.__name__}.",
|
|
22
|
-
category=DeprecationWarning,
|
|
23
|
-
stacklevel=2,
|
|
24
|
-
)
|
|
25
|
-
return func(*args, **kwargs)
|
|
26
|
-
|
|
27
|
-
return new_func
|
together/legacy/complete.py
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
import warnings
|
|
4
|
-
from typing import Any, AsyncGenerator, Dict, Iterator
|
|
5
|
-
|
|
6
|
-
import together
|
|
7
|
-
from together.legacy.base import API_KEY_WARNING, deprecated
|
|
8
|
-
from together.types import CompletionChunk, CompletionResponse
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
class Complete:
|
|
12
|
-
@classmethod
|
|
13
|
-
@deprecated # type: ignore
|
|
14
|
-
def create(
|
|
15
|
-
cls,
|
|
16
|
-
prompt: str,
|
|
17
|
-
**kwargs: Any,
|
|
18
|
-
) -> Dict[str, Any]:
|
|
19
|
-
"""Legacy completion function."""
|
|
20
|
-
|
|
21
|
-
api_key = None
|
|
22
|
-
if together.api_key:
|
|
23
|
-
warnings.warn(API_KEY_WARNING)
|
|
24
|
-
api_key = together.api_key
|
|
25
|
-
|
|
26
|
-
client = together.Together(api_key=api_key)
|
|
27
|
-
|
|
28
|
-
result = client.completions.create(prompt=prompt, stream=False, **kwargs)
|
|
29
|
-
|
|
30
|
-
assert isinstance(result, CompletionResponse)
|
|
31
|
-
|
|
32
|
-
return result.model_dump(exclude_none=True)
|
|
33
|
-
|
|
34
|
-
@classmethod
|
|
35
|
-
@deprecated # type: ignore
|
|
36
|
-
def create_streaming(
|
|
37
|
-
cls,
|
|
38
|
-
prompt: str,
|
|
39
|
-
**kwargs: Any,
|
|
40
|
-
) -> Iterator[Dict[str, Any]]:
|
|
41
|
-
"""Legacy streaming completion function."""
|
|
42
|
-
|
|
43
|
-
api_key = None
|
|
44
|
-
if together.api_key:
|
|
45
|
-
warnings.warn(API_KEY_WARNING)
|
|
46
|
-
api_key = together.api_key
|
|
47
|
-
|
|
48
|
-
client = together.Together(api_key=api_key)
|
|
49
|
-
|
|
50
|
-
return (
|
|
51
|
-
token.model_dump(exclude_none=True) # type: ignore
|
|
52
|
-
for token in client.completions.create(prompt=prompt, stream=True, **kwargs)
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
class Completion:
|
|
57
|
-
@classmethod
|
|
58
|
-
@deprecated # type: ignore
|
|
59
|
-
def create(
|
|
60
|
-
cls,
|
|
61
|
-
prompt: str,
|
|
62
|
-
**kwargs: Any,
|
|
63
|
-
) -> CompletionResponse | Iterator[CompletionChunk]:
|
|
64
|
-
"""Completion function."""
|
|
65
|
-
|
|
66
|
-
api_key = None
|
|
67
|
-
if together.api_key:
|
|
68
|
-
warnings.warn(API_KEY_WARNING)
|
|
69
|
-
api_key = together.api_key
|
|
70
|
-
|
|
71
|
-
client = together.Together(api_key=api_key)
|
|
72
|
-
|
|
73
|
-
return client.completions.create(prompt=prompt, **kwargs)
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
class AsyncComplete:
|
|
77
|
-
@classmethod
|
|
78
|
-
@deprecated # type: ignore
|
|
79
|
-
async def create(
|
|
80
|
-
cls,
|
|
81
|
-
prompt: str,
|
|
82
|
-
**kwargs: Any,
|
|
83
|
-
) -> CompletionResponse | AsyncGenerator[CompletionChunk, None]:
|
|
84
|
-
"""Async completion function."""
|
|
85
|
-
|
|
86
|
-
api_key = None
|
|
87
|
-
if together.api_key:
|
|
88
|
-
warnings.warn(API_KEY_WARNING)
|
|
89
|
-
api_key = together.api_key
|
|
90
|
-
|
|
91
|
-
client = together.AsyncTogether(api_key=api_key)
|
|
92
|
-
|
|
93
|
-
return await client.completions.create(prompt=prompt, **kwargs)
|
together/legacy/embeddings.py
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import warnings
|
|
2
|
-
from typing import Any, Dict
|
|
3
|
-
|
|
4
|
-
import together
|
|
5
|
-
from together.legacy.base import API_KEY_WARNING, deprecated
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
class Embeddings:
|
|
9
|
-
@classmethod
|
|
10
|
-
@deprecated # type: ignore
|
|
11
|
-
def create(
|
|
12
|
-
cls,
|
|
13
|
-
input: str,
|
|
14
|
-
**kwargs: Any,
|
|
15
|
-
) -> Dict[str, Any]:
|
|
16
|
-
"""Legacy embeddings function."""
|
|
17
|
-
|
|
18
|
-
api_key = None
|
|
19
|
-
if together.api_key:
|
|
20
|
-
warnings.warn(API_KEY_WARNING)
|
|
21
|
-
api_key = together.api_key
|
|
22
|
-
|
|
23
|
-
client = together.Together(api_key=api_key)
|
|
24
|
-
|
|
25
|
-
return client.embeddings.create(input=input, **kwargs).model_dump(
|
|
26
|
-
exclude_none=True
|
|
27
|
-
)
|
together/legacy/files.py
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
import json
|
|
4
|
-
import warnings
|
|
5
|
-
from typing import Any, Dict, List
|
|
6
|
-
|
|
7
|
-
import together
|
|
8
|
-
from together.legacy.base import API_KEY_WARNING, deprecated
|
|
9
|
-
from together.utils.files import check_file as check_json
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class Files:
|
|
13
|
-
@classmethod
|
|
14
|
-
@deprecated # type: ignore
|
|
15
|
-
def list(
|
|
16
|
-
cls,
|
|
17
|
-
) -> Dict[str, Any]:
|
|
18
|
-
"""Legacy file list function."""
|
|
19
|
-
|
|
20
|
-
api_key = None
|
|
21
|
-
if together.api_key:
|
|
22
|
-
warnings.warn(API_KEY_WARNING)
|
|
23
|
-
api_key = together.api_key
|
|
24
|
-
|
|
25
|
-
client = together.Together(api_key=api_key)
|
|
26
|
-
|
|
27
|
-
return client.files.list().model_dump(exclude_none=True)
|
|
28
|
-
|
|
29
|
-
@classmethod
|
|
30
|
-
def check(self, file: str) -> Dict[str, object]:
|
|
31
|
-
return check_json(file)
|
|
32
|
-
|
|
33
|
-
@classmethod
|
|
34
|
-
@deprecated # type: ignore
|
|
35
|
-
def upload(
|
|
36
|
-
cls,
|
|
37
|
-
file: str,
|
|
38
|
-
check: bool = True,
|
|
39
|
-
) -> Dict[str, Any]:
|
|
40
|
-
"""Legacy file upload function."""
|
|
41
|
-
|
|
42
|
-
api_key = None
|
|
43
|
-
if together.api_key:
|
|
44
|
-
warnings.warn(API_KEY_WARNING)
|
|
45
|
-
api_key = together.api_key
|
|
46
|
-
|
|
47
|
-
if check:
|
|
48
|
-
report_dict = check_json(file)
|
|
49
|
-
if not report_dict["is_check_passed"]:
|
|
50
|
-
raise together.error.FileTypeError(
|
|
51
|
-
f"Invalid file supplied. Failed to upload.\nReport:\n {report_dict}"
|
|
52
|
-
)
|
|
53
|
-
|
|
54
|
-
client = together.Together(api_key=api_key)
|
|
55
|
-
|
|
56
|
-
# disabling the check, because it was run previously
|
|
57
|
-
response = client.files.upload(file=file, check=False).model_dump(
|
|
58
|
-
exclude_none=True
|
|
59
|
-
)
|
|
60
|
-
|
|
61
|
-
if check:
|
|
62
|
-
response["report_dict"] = report_dict
|
|
63
|
-
|
|
64
|
-
return response
|
|
65
|
-
|
|
66
|
-
@classmethod
|
|
67
|
-
@deprecated # type: ignore
|
|
68
|
-
def delete(
|
|
69
|
-
cls,
|
|
70
|
-
file_id: str,
|
|
71
|
-
) -> Dict[str, Any]:
|
|
72
|
-
"""Legacy file delete function."""
|
|
73
|
-
|
|
74
|
-
api_key = None
|
|
75
|
-
if together.api_key:
|
|
76
|
-
warnings.warn(API_KEY_WARNING)
|
|
77
|
-
api_key = together.api_key
|
|
78
|
-
|
|
79
|
-
client = together.Together(api_key=api_key)
|
|
80
|
-
|
|
81
|
-
return client.files.delete(id=file_id).model_dump(exclude_none=True)
|
|
82
|
-
|
|
83
|
-
@classmethod
|
|
84
|
-
@deprecated # type: ignore
|
|
85
|
-
def retrieve(
|
|
86
|
-
cls,
|
|
87
|
-
file_id: str,
|
|
88
|
-
) -> Dict[str, Any]:
|
|
89
|
-
"""Legacy file retrieve function."""
|
|
90
|
-
|
|
91
|
-
api_key = None
|
|
92
|
-
if together.api_key:
|
|
93
|
-
warnings.warn(API_KEY_WARNING)
|
|
94
|
-
api_key = together.api_key
|
|
95
|
-
|
|
96
|
-
client = together.Together(api_key=api_key)
|
|
97
|
-
|
|
98
|
-
return client.files.retrieve(id=file_id).model_dump(exclude_none=True)
|
|
99
|
-
|
|
100
|
-
@classmethod
|
|
101
|
-
@deprecated # type: ignore
|
|
102
|
-
def retrieve_content(
|
|
103
|
-
cls,
|
|
104
|
-
file_id: str,
|
|
105
|
-
output: str | None = None,
|
|
106
|
-
) -> Dict[str, Any]:
|
|
107
|
-
"""Legacy file retrieve content function."""
|
|
108
|
-
|
|
109
|
-
api_key = None
|
|
110
|
-
if together.api_key:
|
|
111
|
-
warnings.warn(API_KEY_WARNING)
|
|
112
|
-
api_key = together.api_key
|
|
113
|
-
|
|
114
|
-
client = together.Together(api_key=api_key)
|
|
115
|
-
|
|
116
|
-
return client.files.retrieve_content(id=file_id, output=output).dict(
|
|
117
|
-
exclude_none=True
|
|
118
|
-
)
|
|
119
|
-
|
|
120
|
-
@classmethod
|
|
121
|
-
@deprecated # type: ignore
|
|
122
|
-
def save_jsonl(
|
|
123
|
-
self, data: Dict[str, str], output_path: str, append: bool = False
|
|
124
|
-
) -> None:
|
|
125
|
-
"""
|
|
126
|
-
Write list of objects to a JSON lines file.
|
|
127
|
-
"""
|
|
128
|
-
mode = "a+" if append else "w"
|
|
129
|
-
with open(output_path, mode, encoding="utf-8") as f:
|
|
130
|
-
for line in data:
|
|
131
|
-
json_record = json.dumps(line, ensure_ascii=False)
|
|
132
|
-
f.write(json_record + "\n")
|
|
133
|
-
print("Wrote {} records to {}".format(len(data), output_path))
|
|
134
|
-
|
|
135
|
-
@classmethod
|
|
136
|
-
@deprecated # type: ignore
|
|
137
|
-
def load_jsonl(self, input_path: str) -> List[Dict[str, str]]:
|
|
138
|
-
"""
|
|
139
|
-
Read list of objects from a JSON lines file.
|
|
140
|
-
"""
|
|
141
|
-
data = []
|
|
142
|
-
with open(input_path, "r", encoding="utf-8") as f:
|
|
143
|
-
for line in f:
|
|
144
|
-
data.append(json.loads(line.rstrip("\n|\r")))
|
|
145
|
-
print("Loaded {} records from {}".format(len(data), input_path))
|
|
146
|
-
return data
|