together 1.5.34__py3-none-any.whl → 2.0.0a6__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 -114
- 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/{cli → lib/cli}/api/endpoints.py +65 -81
- together/{cli/api/evaluation.py → lib/cli/api/evals.py} +152 -43
- together/{cli → lib/cli}/api/files.py +20 -17
- together/{cli/api/finetune.py → lib/cli/api/fine_tuning.py} +116 -172
- together/{cli → lib/cli}/api/models.py +34 -27
- together/lib/cli/api/utils.py +50 -0
- together/{cli → lib/cli}/cli.py +16 -26
- together/{constants.py → lib/constants.py} +11 -24
- 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 +397 -0
- together/{utils → lib/utils}/__init__.py +6 -14
- together/{utils → lib/utils}/_log.py +11 -16
- together/{utils → lib/utils}/files.py +90 -288
- together/lib/utils/serializer.py +10 -0
- together/{utils → lib/utils}/tools.py +19 -55
- together/resources/__init__.py +225 -39
- together/resources/audio/__init__.py +72 -48
- together/resources/audio/audio.py +198 -0
- together/resources/audio/speech.py +574 -128
- together/resources/audio/transcriptions.py +247 -261
- together/resources/audio/translations.py +221 -241
- together/resources/audio/voices.py +111 -41
- 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 -263
- 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 +884 -225
- together/resources/embeddings.py +172 -68
- together/resources/endpoints.py +589 -477
- together/resources/evals.py +452 -0
- together/resources/files.py +397 -129
- together/resources/fine_tuning.py +1033 -0
- together/resources/hardware.py +181 -0
- together/resources/images.py +258 -104
- together/resources/jobs.py +214 -0
- together/resources/models.py +223 -193
- together/resources/rerank.py +190 -92
- together/resources/videos.py +286 -214
- together/types/__init__.py +66 -167
- 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_create_response.py +10 -0
- together/types/video_job.py +57 -0
- together-2.0.0a6.dist-info/METADATA +729 -0
- together-2.0.0a6.dist-info/RECORD +165 -0
- {together-1.5.34.dist-info → together-2.0.0a6.dist-info}/WHEEL +1 -1
- together-2.0.0a6.dist-info/entry_points.txt +2 -0
- {together-1.5.34.dist-info → together-2.0.0a6.dist-info}/licenses/LICENSE +1 -1
- together/abstract/api_requestor.py +0 -770
- together/cli/api/chat.py +0 -298
- together/cli/api/completions.py +0 -119
- together/cli/api/images.py +0 -93
- together/cli/api/utils.py +0 -139
- together/client.py +0 -186
- together/error.py +0 -194
- together/filemanager.py +0 -635
- 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/batch.py +0 -165
- together/resources/code_interpreter.py +0 -82
- together/resources/evaluation.py +0 -808
- together/resources/finetune.py +0 -1388
- together/together_response.py +0 -50
- together/types/abstract.py +0 -26
- together/types/audio_speech.py +0 -311
- together/types/batch.py +0 -54
- together/types/chat_completions.py +0 -210
- together/types/code_interpreter.py +0 -57
- together/types/common.py +0 -67
- together/types/completions.py +0 -107
- together/types/embeddings.py +0 -35
- together/types/endpoints.py +0 -123
- together/types/error.py +0 -16
- together/types/evaluation.py +0 -93
- together/types/files.py +0 -93
- together/types/finetune.py +0 -464
- together/types/images.py +0 -42
- together/types/models.py +0 -96
- together/types/rerank.py +0 -43
- together/types/videos.py +0 -69
- together/utils/api_helpers.py +0 -124
- together/version.py +0 -6
- together-1.5.34.dist-info/METADATA +0 -583
- together-1.5.34.dist-info/RECORD +0 -77
- together-1.5.34.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/utils/api_helpers.py
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
import json
|
|
4
|
-
import os
|
|
5
|
-
import sys
|
|
6
|
-
import platform
|
|
7
|
-
from typing import TYPE_CHECKING, Any, Dict
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if TYPE_CHECKING:
|
|
11
|
-
from _typeshed import SupportsKeysAndGetItem
|
|
12
|
-
|
|
13
|
-
import together
|
|
14
|
-
from together import error
|
|
15
|
-
from together.utils._log import _console_log_level
|
|
16
|
-
from together.utils import log_info
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
def get_headers(
|
|
20
|
-
method: str | None = None,
|
|
21
|
-
api_key: str | None = None,
|
|
22
|
-
extra: "SupportsKeysAndGetItem[str, Any] | None" = None,
|
|
23
|
-
) -> Dict[str, str]:
|
|
24
|
-
"""
|
|
25
|
-
Generates request headers with API key, metadata, and supplied headers
|
|
26
|
-
|
|
27
|
-
Args:
|
|
28
|
-
method (str, optional): HTTP request type (POST, GET, etc.)
|
|
29
|
-
Defaults to None.
|
|
30
|
-
api_key (str, optional): API key to add as an Authorization header.
|
|
31
|
-
Defaults to None.
|
|
32
|
-
extra (SupportsKeysAndGetItem[str, Any], optional): Additional headers to add to request.
|
|
33
|
-
Defaults to None.
|
|
34
|
-
|
|
35
|
-
Returns:
|
|
36
|
-
headers (Dict[str, str]): Compiled headers from data
|
|
37
|
-
"""
|
|
38
|
-
|
|
39
|
-
user_agent = "Together/v1 PythonBindings/%s" % (together.version,)
|
|
40
|
-
|
|
41
|
-
uname_without_node = " ".join(
|
|
42
|
-
v for k, v in platform.uname()._asdict().items() if k != "node"
|
|
43
|
-
)
|
|
44
|
-
ua = {
|
|
45
|
-
"bindings_version": together.version,
|
|
46
|
-
"httplib": "requests",
|
|
47
|
-
"lang": "python",
|
|
48
|
-
"lang_version": platform.python_version(),
|
|
49
|
-
"platform": platform.platform(),
|
|
50
|
-
"publisher": "together",
|
|
51
|
-
"uname": uname_without_node,
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
headers: Dict[str, Any] = {
|
|
55
|
-
"X-Together-Client-User-Agent": json.dumps(ua),
|
|
56
|
-
"Authorization": f"Bearer {default_api_key(api_key)}",
|
|
57
|
-
"User-Agent": user_agent,
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if _console_log_level():
|
|
61
|
-
headers["Together-Debug"] = _console_log_level()
|
|
62
|
-
if extra:
|
|
63
|
-
headers.update(extra)
|
|
64
|
-
|
|
65
|
-
return headers
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
def default_api_key(api_key: str | None = None) -> str | None:
|
|
69
|
-
"""
|
|
70
|
-
API key fallback logic from input argument and environment variable
|
|
71
|
-
|
|
72
|
-
Args:
|
|
73
|
-
api_key (str, optional): Supplied API key. This argument takes priority over env var
|
|
74
|
-
|
|
75
|
-
Returns:
|
|
76
|
-
together_api_key (str): Returns API key from supplied input or env var
|
|
77
|
-
|
|
78
|
-
Raises:
|
|
79
|
-
together.error.AuthenticationError: if API key not found
|
|
80
|
-
"""
|
|
81
|
-
if api_key:
|
|
82
|
-
return api_key
|
|
83
|
-
if os.environ.get("TOGETHER_API_KEY"):
|
|
84
|
-
return os.environ.get("TOGETHER_API_KEY")
|
|
85
|
-
|
|
86
|
-
raise error.AuthenticationError(together.constants.MISSING_API_KEY_MESSAGE)
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
def get_google_colab_secret(secret_name: str = "TOGETHER_API_KEY") -> str | None:
|
|
90
|
-
"""
|
|
91
|
-
Checks to see if the user is running in Google Colab, and looks for the Together API Key secret.
|
|
92
|
-
|
|
93
|
-
Args:
|
|
94
|
-
secret_name (str, optional). Defaults to TOGETHER_API_KEY
|
|
95
|
-
|
|
96
|
-
Returns:
|
|
97
|
-
str: if the API key is found; None if an error occurred or the secret was not found.
|
|
98
|
-
"""
|
|
99
|
-
# If running in Google Colab, check for Together in notebook secrets
|
|
100
|
-
if "google.colab" in sys.modules:
|
|
101
|
-
if TYPE_CHECKING:
|
|
102
|
-
from google.colab import userdata # type: ignore
|
|
103
|
-
else:
|
|
104
|
-
from google.colab import userdata
|
|
105
|
-
|
|
106
|
-
try:
|
|
107
|
-
api_key = userdata.get(secret_name)
|
|
108
|
-
if not isinstance(api_key, str):
|
|
109
|
-
return None
|
|
110
|
-
else:
|
|
111
|
-
return str(api_key)
|
|
112
|
-
except userdata.NotebookAccessError:
|
|
113
|
-
log_info(
|
|
114
|
-
"The TOGETHER_API_KEY Colab secret was found, but notebook access is disabled. Please enable notebook "
|
|
115
|
-
"access for the secret."
|
|
116
|
-
)
|
|
117
|
-
except userdata.SecretNotFoundError:
|
|
118
|
-
# warn and carry on
|
|
119
|
-
log_info("Colab: No Google Colab secret named TOGETHER_API_KEY was found.")
|
|
120
|
-
|
|
121
|
-
return None
|
|
122
|
-
|
|
123
|
-
else:
|
|
124
|
-
return None
|
together/version.py
DELETED
|
@@ -1,583 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: together
|
|
3
|
-
Version: 1.5.34
|
|
4
|
-
Summary: Python client for Together's Cloud Platform! Note: SDK 2.0 is now available at https://github.com/togethercomputer/together-py
|
|
5
|
-
License: Apache-2.0
|
|
6
|
-
License-File: LICENSE
|
|
7
|
-
Author: Together AI
|
|
8
|
-
Author-email: support@together.ai
|
|
9
|
-
Requires-Python: >=3.10,<4.0
|
|
10
|
-
Classifier: License :: OSI Approved :: Apache Software License
|
|
11
|
-
Classifier: Operating System :: POSIX :: Linux
|
|
12
|
-
Classifier: Programming Language :: Python :: 3
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
-
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
-
Provides-Extra: pyarrow
|
|
19
|
-
Requires-Dist: aiohttp (>=3.9.3,<4.0.0)
|
|
20
|
-
Requires-Dist: black (>=25.9.0,<26.0.0)
|
|
21
|
-
Requires-Dist: click (>=8.1.7,<9.0.0)
|
|
22
|
-
Requires-Dist: eval-type-backport (>=0.1.3,<0.3.0)
|
|
23
|
-
Requires-Dist: filelock (>=3.13.1,<4.0.0)
|
|
24
|
-
Requires-Dist: numpy (>=1.23.5) ; python_version < "3.12"
|
|
25
|
-
Requires-Dist: numpy (>=1.26.0) ; python_version >= "3.12"
|
|
26
|
-
Requires-Dist: pillow (>=11.1.0,<12.0.0)
|
|
27
|
-
Requires-Dist: pyarrow (>=10.0.1) ; extra == "pyarrow"
|
|
28
|
-
Requires-Dist: pydantic (>=2.6.3,<3.0.0)
|
|
29
|
-
Requires-Dist: requests (>=2.31.0,<3.0.0)
|
|
30
|
-
Requires-Dist: rich (>=13.8.1,<15.0.0)
|
|
31
|
-
Requires-Dist: tabulate (>=0.9.0,<0.10.0)
|
|
32
|
-
Requires-Dist: tqdm (>=4.66.2,<5.0.0)
|
|
33
|
-
Requires-Dist: typer (>=0.9,<0.20)
|
|
34
|
-
Project-URL: Bug Tracker, https://github.com/togethercomputer/together-python/issues
|
|
35
|
-
Project-URL: Homepage, https://github.com/togethercomputer/together-python
|
|
36
|
-
Project-URL: Repository, https://github.com/togethercomputer/together-python
|
|
37
|
-
Description-Content-Type: text/markdown
|
|
38
|
-
|
|
39
|
-
<div align="center">
|
|
40
|
-
<a href="https://www.together.ai/">
|
|
41
|
-
<img alt="together.ai" height="100px" src="https://assets-global.website-files.com/64f6f2c0e3f4c5a91c1e823a/654693d569494912cfc0c0d4_favicon.svg">
|
|
42
|
-
</a>
|
|
43
|
-
</div>
|
|
44
|
-
|
|
45
|
-
> [!NOTE]
|
|
46
|
-
> ## 🚀 Together Python SDK 2.0 is now available!
|
|
47
|
-
>
|
|
48
|
-
> Check out the new SDK: **[together-py](https://github.com/togethercomputer/together-py)**
|
|
49
|
-
>
|
|
50
|
-
> 📖 **Migration Guide:** [https://docs.together.ai/docs/pythonv2-migration-guide](https://docs.together.ai/docs/pythonv2-migration-guide)
|
|
51
|
-
>
|
|
52
|
-
> ### Install the Beta
|
|
53
|
-
>
|
|
54
|
-
> **Using uv (Recommended):**
|
|
55
|
-
> ```bash
|
|
56
|
-
> # Install uv if you haven't already
|
|
57
|
-
> curl -LsSf https://astral.sh/uv/install.sh | sh
|
|
58
|
-
>
|
|
59
|
-
> # Install together python SDK
|
|
60
|
-
> uv add together --prerelease allow
|
|
61
|
-
>
|
|
62
|
-
> # Or upgrade an existing installation
|
|
63
|
-
> uv sync --upgrade-package together --prerelease allow
|
|
64
|
-
> ```
|
|
65
|
-
>
|
|
66
|
-
> **Using pip:**
|
|
67
|
-
> ```bash
|
|
68
|
-
> pip install --pre together
|
|
69
|
-
> ```
|
|
70
|
-
>
|
|
71
|
-
> This package will be maintained until January 2026.
|
|
72
|
-
|
|
73
|
-
# Together Python API library
|
|
74
|
-
|
|
75
|
-
[](https://pypi.org/project/together/)
|
|
76
|
-
[](https://discord.com/invite/9Rk6sSeWEG)
|
|
77
|
-
[](https://twitter.com/togethercompute)
|
|
78
|
-
|
|
79
|
-
The [Together Python API Library](https://pypi.org/project/together/) is the official Python client for Together's API platform, providing a convenient way for interacting with the REST APIs and enables easy integrations with Python 3.10+ applications with easy to use synchronous and asynchronous clients.
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
## Installation
|
|
84
|
-
|
|
85
|
-
> 🚧
|
|
86
|
-
> The Library was rewritten in v1.0.0 released in April of 2024. There were significant changes made.
|
|
87
|
-
|
|
88
|
-
To install Together Python Library from PyPI, simply run:
|
|
89
|
-
|
|
90
|
-
```shell Shell
|
|
91
|
-
pip install --upgrade together
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
### Setting up API Key
|
|
95
|
-
|
|
96
|
-
> 🚧 You will need to create an account with [Together.ai](https://api.together.xyz/) to obtain a Together API Key.
|
|
97
|
-
|
|
98
|
-
Once logged in to the Together Playground, you can find available API keys in [this settings page](https://api.together.xyz/settings/api-keys).
|
|
99
|
-
|
|
100
|
-
#### Setting environment variable
|
|
101
|
-
|
|
102
|
-
```shell
|
|
103
|
-
export TOGETHER_API_KEY=xxxxx
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
#### Using the client
|
|
107
|
-
|
|
108
|
-
```python
|
|
109
|
-
from together import Together
|
|
110
|
-
|
|
111
|
-
client = Together(api_key="xxxxx")
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
This repo contains both a Python Library and a CLI. We'll demonstrate how to use both below.
|
|
115
|
-
|
|
116
|
-
## Usage – Python Client
|
|
117
|
-
|
|
118
|
-
### Chat Completions
|
|
119
|
-
|
|
120
|
-
```python
|
|
121
|
-
from together import Together
|
|
122
|
-
|
|
123
|
-
client = Together()
|
|
124
|
-
|
|
125
|
-
# Simple text message
|
|
126
|
-
response = client.chat.completions.create(
|
|
127
|
-
model="meta-llama/Llama-4-Scout-17B-16E-Instruct",
|
|
128
|
-
messages=[{"role": "user", "content": "tell me about new york"}],
|
|
129
|
-
)
|
|
130
|
-
print(response.choices[0].message.content)
|
|
131
|
-
|
|
132
|
-
# Multi-modal message with text and image
|
|
133
|
-
response = client.chat.completions.create(
|
|
134
|
-
model="meta-llama/Llama-3.2-11B-Vision-Instruct-Turbo",
|
|
135
|
-
messages=[{
|
|
136
|
-
"role": "user",
|
|
137
|
-
"content": [
|
|
138
|
-
{
|
|
139
|
-
"type": "text",
|
|
140
|
-
"text": "What's in this image?"
|
|
141
|
-
},
|
|
142
|
-
{
|
|
143
|
-
"type": "image_url",
|
|
144
|
-
"image_url": {
|
|
145
|
-
"url": "https://huggingface.co/datasets/patrickvonplaten/random_img/resolve/main/yosemite.png"
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
]
|
|
149
|
-
}]
|
|
150
|
-
)
|
|
151
|
-
print(response.choices[0].message.content)
|
|
152
|
-
|
|
153
|
-
# Multi-modal message with multiple images
|
|
154
|
-
response = client.chat.completions.create(
|
|
155
|
-
model="Qwen/Qwen2.5-VL-72B-Instruct",
|
|
156
|
-
messages=[{
|
|
157
|
-
"role": "user",
|
|
158
|
-
"content": [
|
|
159
|
-
{
|
|
160
|
-
"type": "text",
|
|
161
|
-
"text": "Compare these two images."
|
|
162
|
-
},
|
|
163
|
-
{
|
|
164
|
-
"type": "image_url",
|
|
165
|
-
"image_url": {
|
|
166
|
-
"url": "https://huggingface.co/datasets/patrickvonplaten/random_img/resolve/main/yosemite.png"
|
|
167
|
-
}
|
|
168
|
-
},
|
|
169
|
-
{
|
|
170
|
-
"type": "image_url",
|
|
171
|
-
"image_url": {
|
|
172
|
-
"url": "https://huggingface.co/datasets/patrickvonplaten/random_img/resolve/main/slack.png"
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
]
|
|
176
|
-
}]
|
|
177
|
-
)
|
|
178
|
-
print(response.choices[0].message.content)
|
|
179
|
-
|
|
180
|
-
# Multi-modal message with text and video
|
|
181
|
-
response = client.chat.completions.create(
|
|
182
|
-
model="Qwen/Qwen2.5-VL-72B-Instruct",
|
|
183
|
-
messages=[{
|
|
184
|
-
"role": "user",
|
|
185
|
-
"content": [
|
|
186
|
-
{
|
|
187
|
-
"type": "text",
|
|
188
|
-
"text": "What's happening in this video?"
|
|
189
|
-
},
|
|
190
|
-
{
|
|
191
|
-
"type": "video_url",
|
|
192
|
-
"video_url": {
|
|
193
|
-
"url": "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerFun.mp4"
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
]
|
|
197
|
-
}]
|
|
198
|
-
)
|
|
199
|
-
print(response.choices[0].message.content)
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
The chat completions API supports three types of content:
|
|
203
|
-
- Plain text messages using the `content` field directly
|
|
204
|
-
- Multi-modal messages with images using `type: "image_url"`
|
|
205
|
-
- Multi-modal messages with videos using `type: "video_url"`
|
|
206
|
-
|
|
207
|
-
When using multi-modal content, the `content` field becomes an array of content objects, each with its own type and corresponding data.
|
|
208
|
-
|
|
209
|
-
#### Streaming
|
|
210
|
-
|
|
211
|
-
```python
|
|
212
|
-
import os
|
|
213
|
-
from together import Together
|
|
214
|
-
|
|
215
|
-
client = Together()
|
|
216
|
-
stream = client.chat.completions.create(
|
|
217
|
-
model="meta-llama/Llama-4-Scout-17B-16E-Instruct",
|
|
218
|
-
messages=[{"role": "user", "content": "tell me about new york"}],
|
|
219
|
-
stream=True,
|
|
220
|
-
)
|
|
221
|
-
|
|
222
|
-
for chunk in stream:
|
|
223
|
-
print(chunk.choices[0].delta.content or "", end="", flush=True)
|
|
224
|
-
```
|
|
225
|
-
|
|
226
|
-
#### Async usage
|
|
227
|
-
|
|
228
|
-
```python
|
|
229
|
-
import asyncio
|
|
230
|
-
from together import AsyncTogether
|
|
231
|
-
|
|
232
|
-
async_client = AsyncTogether()
|
|
233
|
-
messages = [
|
|
234
|
-
"What are the top things to do in San Francisco?",
|
|
235
|
-
"What country is Paris in?",
|
|
236
|
-
]
|
|
237
|
-
|
|
238
|
-
async def async_chat_completion(messages):
|
|
239
|
-
async_client = AsyncTogether()
|
|
240
|
-
tasks = [
|
|
241
|
-
async_client.chat.completions.create(
|
|
242
|
-
model="meta-llama/Llama-4-Scout-17B-16E-Instruct",
|
|
243
|
-
messages=[{"role": "user", "content": message}],
|
|
244
|
-
)
|
|
245
|
-
for message in messages
|
|
246
|
-
]
|
|
247
|
-
responses = await asyncio.gather(*tasks)
|
|
248
|
-
|
|
249
|
-
for response in responses:
|
|
250
|
-
print(response.choices[0].message.content)
|
|
251
|
-
|
|
252
|
-
asyncio.run(async_chat_completion(messages))
|
|
253
|
-
```
|
|
254
|
-
|
|
255
|
-
#### Fetching logprobs
|
|
256
|
-
|
|
257
|
-
Logprobs are logarithms of token-level generation probabilities that indicate the likelihood of the generated token based on the previous tokens in the context. Logprobs allow us to estimate the model's confidence in its outputs, which can be used to decide how to optimally consume the model's output (e.g. rejecting low confidence outputs, retrying or ensembling model outputs etc).
|
|
258
|
-
|
|
259
|
-
```python
|
|
260
|
-
from together import Together
|
|
261
|
-
|
|
262
|
-
client = Together()
|
|
263
|
-
|
|
264
|
-
response = client.chat.completions.create(
|
|
265
|
-
model="meta-llama/Llama-3.2-3B-Instruct-Turbo",
|
|
266
|
-
messages=[{"role": "user", "content": "tell me about new york"}],
|
|
267
|
-
logprobs=1
|
|
268
|
-
)
|
|
269
|
-
|
|
270
|
-
response_lobprobs = response.choices[0].logprobs
|
|
271
|
-
|
|
272
|
-
print(dict(zip(response_lobprobs.tokens, response_lobprobs.token_logprobs)))
|
|
273
|
-
# {'New': -2.384e-07, ' York': 0.0, ',': 0.0, ' also': -0.20703125, ' known': -0.20214844, ' as': -8.34465e-07, ... }
|
|
274
|
-
```
|
|
275
|
-
|
|
276
|
-
More details about using logprobs in Together's API can be found [here](https://docs.together.ai/docs/logprobs).
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
### Completions
|
|
280
|
-
|
|
281
|
-
Completions are for code and language models shown [here](https://docs.together.ai/docs/inference-models). Below, a code model example is shown.
|
|
282
|
-
|
|
283
|
-
```python
|
|
284
|
-
from together import Together
|
|
285
|
-
|
|
286
|
-
client = Together()
|
|
287
|
-
|
|
288
|
-
response = client.completions.create(
|
|
289
|
-
model="codellama/CodeLlama-34b-Python-hf",
|
|
290
|
-
prompt="Write a Next.js component with TailwindCSS for a header component.",
|
|
291
|
-
max_tokens=200,
|
|
292
|
-
)
|
|
293
|
-
print(response.choices[0].text)
|
|
294
|
-
```
|
|
295
|
-
|
|
296
|
-
#### Streaming
|
|
297
|
-
|
|
298
|
-
```python
|
|
299
|
-
from together import Together
|
|
300
|
-
|
|
301
|
-
client = Together()
|
|
302
|
-
stream = client.completions.create(
|
|
303
|
-
model="codellama/CodeLlama-34b-Python-hf",
|
|
304
|
-
prompt="Write a Next.js component with TailwindCSS for a header component.",
|
|
305
|
-
stream=True,
|
|
306
|
-
)
|
|
307
|
-
|
|
308
|
-
for chunk in stream:
|
|
309
|
-
print(chunk.choices[0].delta.content or "", end="", flush=True)
|
|
310
|
-
```
|
|
311
|
-
|
|
312
|
-
#### Async usage
|
|
313
|
-
|
|
314
|
-
```python
|
|
315
|
-
import asyncio
|
|
316
|
-
from together import AsyncTogether
|
|
317
|
-
|
|
318
|
-
async_client = AsyncTogether()
|
|
319
|
-
prompts = [
|
|
320
|
-
"Write a Next.js component with TailwindCSS for a header component.",
|
|
321
|
-
"Write a python function for the fibonacci sequence",
|
|
322
|
-
]
|
|
323
|
-
|
|
324
|
-
async def async_chat_completion(prompts):
|
|
325
|
-
tasks = [
|
|
326
|
-
async_client.completions.create(
|
|
327
|
-
model="codellama/CodeLlama-34b-Python-hf",
|
|
328
|
-
prompt=prompt,
|
|
329
|
-
)
|
|
330
|
-
for prompt in prompts
|
|
331
|
-
]
|
|
332
|
-
responses = await asyncio.gather(*tasks)
|
|
333
|
-
|
|
334
|
-
for response in responses:
|
|
335
|
-
print(response.choices[0].text)
|
|
336
|
-
|
|
337
|
-
asyncio.run(async_chat_completion(prompts))
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
### Image generation
|
|
341
|
-
|
|
342
|
-
```python
|
|
343
|
-
from together import Together
|
|
344
|
-
|
|
345
|
-
client = Together()
|
|
346
|
-
|
|
347
|
-
response = client.images.generate(
|
|
348
|
-
prompt="space robots",
|
|
349
|
-
model="stabilityai/stable-diffusion-xl-base-1.0",
|
|
350
|
-
steps=10,
|
|
351
|
-
n=4,
|
|
352
|
-
)
|
|
353
|
-
print(response.data[0].b64_json)
|
|
354
|
-
```
|
|
355
|
-
|
|
356
|
-
### Embeddings
|
|
357
|
-
|
|
358
|
-
```python
|
|
359
|
-
from typing import List
|
|
360
|
-
from together import Together
|
|
361
|
-
|
|
362
|
-
client = Together()
|
|
363
|
-
|
|
364
|
-
def get_embeddings(texts: List[str], model: str) -> List[List[float]]:
|
|
365
|
-
texts = [text.replace("\n", " ") for text in texts]
|
|
366
|
-
outputs = client.embeddings.create(model=model, input = texts)
|
|
367
|
-
return [outputs.data[i].embedding for i in range(len(texts))]
|
|
368
|
-
|
|
369
|
-
input_texts = ['Our solar system orbits the Milky Way galaxy at about 515,000 mph']
|
|
370
|
-
embeddings = get_embeddings(input_texts, model='togethercomputer/m2-bert-80M-8k-retrieval')
|
|
371
|
-
|
|
372
|
-
print(embeddings)
|
|
373
|
-
```
|
|
374
|
-
|
|
375
|
-
### Reranking
|
|
376
|
-
|
|
377
|
-
```python
|
|
378
|
-
from typing import List
|
|
379
|
-
from together import Together
|
|
380
|
-
|
|
381
|
-
client = Together()
|
|
382
|
-
|
|
383
|
-
def get_reranked_documents(query: str, documents: List[str], model: str, top_n: int = 3) -> List[str]:
|
|
384
|
-
outputs = client.rerank.create(model=model, query=query, documents=documents, top_n=top_n)
|
|
385
|
-
# sort by relevance score and returns the original docs
|
|
386
|
-
return [documents[i] for i in [x.index for x in sorted(outputs.results, key=lambda x: x.relevance_score, reverse=True)]]
|
|
387
|
-
|
|
388
|
-
query = "What is the capital of the United States?"
|
|
389
|
-
documents = ["New York","Washington, D.C.", "Los Angeles"]
|
|
390
|
-
|
|
391
|
-
reranked_documents = get_reranked_documents(query, documents, model='Salesforce/Llama-Rank-V1', top_n=1)
|
|
392
|
-
|
|
393
|
-
print(reranked_documents)
|
|
394
|
-
```
|
|
395
|
-
|
|
396
|
-
Read more about Reranking [here](https://docs.together.ai/docs/rerank-overview).
|
|
397
|
-
|
|
398
|
-
### Files
|
|
399
|
-
|
|
400
|
-
The files API is used for fine-tuning and allows developers to upload data to fine-tune on. It also has several methods to list all files, retrive files, and delete files. Please refer to our fine-tuning docs [here](https://docs.together.ai/docs/fine-tuning-python).
|
|
401
|
-
|
|
402
|
-
```python
|
|
403
|
-
from together import Together
|
|
404
|
-
|
|
405
|
-
client = Together()
|
|
406
|
-
|
|
407
|
-
client.files.upload(file="somedata.jsonl") # uploads a file
|
|
408
|
-
client.files.list() # lists all uploaded files
|
|
409
|
-
client.files.retrieve(id="file-d0d318cb-b7d9-493a-bd70-1cfe089d3815") # retrieves a specific file
|
|
410
|
-
client.files.retrieve_content(id="file-d0d318cb-b7d9-493a-bd70-1cfe089d3815") # retrieves content of a specific file
|
|
411
|
-
client.files.delete(id="file-d0d318cb-b7d9-493a-bd70-1cfe089d3815") # deletes a file
|
|
412
|
-
```
|
|
413
|
-
|
|
414
|
-
### Fine-tunes
|
|
415
|
-
|
|
416
|
-
The finetune API is used for fine-tuning and allows developers to create finetuning jobs. It also has several methods to list all jobs, retrive statuses and get checkpoints. Please refer to our fine-tuning docs [here](https://docs.together.ai/docs/fine-tuning-quickstart).
|
|
417
|
-
|
|
418
|
-
```python
|
|
419
|
-
from together import Together
|
|
420
|
-
|
|
421
|
-
client = Together()
|
|
422
|
-
|
|
423
|
-
client.fine_tuning.create(
|
|
424
|
-
training_file = 'file-d0d318cb-b7d9-493a-bd70-1cfe089d3815',
|
|
425
|
-
model = 'meta-llama/Llama-3.2-3B-Instruct',
|
|
426
|
-
n_epochs = 3,
|
|
427
|
-
n_checkpoints = 1,
|
|
428
|
-
batch_size = "max",
|
|
429
|
-
learning_rate = 1e-5,
|
|
430
|
-
suffix = 'my-demo-finetune',
|
|
431
|
-
wandb_api_key = '1a2b3c4d5e.......',
|
|
432
|
-
)
|
|
433
|
-
client.fine_tuning.list() # lists all fine-tuned jobs
|
|
434
|
-
client.fine_tuning.retrieve(id="ft-c66a5c18-1d6d-43c9-94bd-32d756425b4b") # retrieves information on finetune event
|
|
435
|
-
client.fine_tuning.cancel(id="ft-c66a5c18-1d6d-43c9-94bd-32d756425b4b") # Cancels a fine-tuning job
|
|
436
|
-
client.fine_tuning.list_events(id="ft-c66a5c18-1d6d-43c9-94bd-32d756425b4b") # Lists events of a fine-tune job
|
|
437
|
-
client.fine_tuning.download(id="ft-c66a5c18-1d6d-43c9-94bd-32d756425b4b") # downloads compressed fine-tuned model or checkpoint to local disk
|
|
438
|
-
```
|
|
439
|
-
|
|
440
|
-
### Models
|
|
441
|
-
|
|
442
|
-
This lists all the models that Together supports.
|
|
443
|
-
|
|
444
|
-
```python
|
|
445
|
-
from together import Together
|
|
446
|
-
|
|
447
|
-
client = Together()
|
|
448
|
-
|
|
449
|
-
models = client.models.list()
|
|
450
|
-
|
|
451
|
-
for model in models:
|
|
452
|
-
print(model)
|
|
453
|
-
```
|
|
454
|
-
|
|
455
|
-
### Batch Inference
|
|
456
|
-
|
|
457
|
-
The batch API allows you to submit larger inference jobs for completion with a 24 hour turn-around time, below is an example. To learn more refer to the [docs here](https://docs.together.ai/docs/batch-inference).
|
|
458
|
-
|
|
459
|
-
```python
|
|
460
|
-
from together import Together
|
|
461
|
-
|
|
462
|
-
client = Together()
|
|
463
|
-
|
|
464
|
-
# Upload the batch file
|
|
465
|
-
batch_file = client.files.upload(file="simpleqa_batch_student.jsonl", purpose="batch-api")
|
|
466
|
-
|
|
467
|
-
# Create the batch job
|
|
468
|
-
batch = client.batches.create_batch(file_id=batch_file.id, endpoint="/v1/chat/completions")
|
|
469
|
-
|
|
470
|
-
# Monitor the batch status
|
|
471
|
-
batch_stat = client.batches.get_batch(batch.id)
|
|
472
|
-
|
|
473
|
-
# List all batches - contains other batches as well
|
|
474
|
-
client.batches.list_batches()
|
|
475
|
-
|
|
476
|
-
# Download the file content if job completed
|
|
477
|
-
if batch_stat.status == 'COMPLETED':
|
|
478
|
-
output_response = client.files.retrieve_content(id=batch_stat.output_file_id,
|
|
479
|
-
output="simpleqa_v3_output.jsonl")
|
|
480
|
-
```
|
|
481
|
-
|
|
482
|
-
## Usage – CLI
|
|
483
|
-
|
|
484
|
-
### Chat Completions
|
|
485
|
-
|
|
486
|
-
```bash
|
|
487
|
-
together chat.completions \
|
|
488
|
-
--message "system" "You are a helpful assistant named Together" \
|
|
489
|
-
--message "user" "What is your name?" \
|
|
490
|
-
--model meta-llama/Llama-4-Scout-17B-16E-Instruct
|
|
491
|
-
```
|
|
492
|
-
|
|
493
|
-
The Chat Completions CLI enables streaming tokens to stdout by default. To disable streaming, use `--no-stream`.
|
|
494
|
-
|
|
495
|
-
### Completions
|
|
496
|
-
|
|
497
|
-
```bash
|
|
498
|
-
together completions \
|
|
499
|
-
"Large language models are " \
|
|
500
|
-
--model meta-llama/Llama-4-Scout-17B-16E-Instruct \
|
|
501
|
-
--max-tokens 512 \
|
|
502
|
-
--stop "."
|
|
503
|
-
```
|
|
504
|
-
|
|
505
|
-
The Completions CLI enables streaming tokens to stdout by default. To disable streaming, use `--no-stream`.
|
|
506
|
-
|
|
507
|
-
### Image Generations
|
|
508
|
-
|
|
509
|
-
```bash
|
|
510
|
-
together images generate \
|
|
511
|
-
"space robots" \
|
|
512
|
-
--model stabilityai/stable-diffusion-xl-base-1.0 \
|
|
513
|
-
--n 4
|
|
514
|
-
```
|
|
515
|
-
|
|
516
|
-
The image is opened in the default image viewer by default. To disable this, use `--no-show`.
|
|
517
|
-
|
|
518
|
-
### Files
|
|
519
|
-
|
|
520
|
-
```bash
|
|
521
|
-
# Help
|
|
522
|
-
together files --help
|
|
523
|
-
|
|
524
|
-
# Check file
|
|
525
|
-
together files check example.jsonl
|
|
526
|
-
|
|
527
|
-
# Upload file
|
|
528
|
-
together files upload example.jsonl
|
|
529
|
-
|
|
530
|
-
# List files
|
|
531
|
-
together files list
|
|
532
|
-
|
|
533
|
-
# Retrieve file metadata
|
|
534
|
-
together files retrieve file-6f50f9d1-5b95-416c-9040-0799b2b4b894
|
|
535
|
-
|
|
536
|
-
# Retrieve file content
|
|
537
|
-
together files retrieve-content file-6f50f9d1-5b95-416c-9040-0799b2b4b894
|
|
538
|
-
|
|
539
|
-
# Delete remote file
|
|
540
|
-
together files delete file-6f50f9d1-5b95-416c-9040-0799b2b4b894
|
|
541
|
-
```
|
|
542
|
-
|
|
543
|
-
### Fine-tuning
|
|
544
|
-
|
|
545
|
-
```bash
|
|
546
|
-
# Help
|
|
547
|
-
together fine-tuning --help
|
|
548
|
-
|
|
549
|
-
# Create fine-tune job
|
|
550
|
-
together fine-tuning create \
|
|
551
|
-
--model togethercomputer/llama-2-7b-chat \
|
|
552
|
-
--training-file file-711d8724-b3e3-4ae2-b516-94841958117d
|
|
553
|
-
|
|
554
|
-
# List fine-tune jobs
|
|
555
|
-
together fine-tuning list
|
|
556
|
-
|
|
557
|
-
# Retrieve fine-tune job details
|
|
558
|
-
together fine-tuning retrieve ft-c66a5c18-1d6d-43c9-94bd-32d756425b4b
|
|
559
|
-
|
|
560
|
-
# List fine-tune job events
|
|
561
|
-
together fine-tuning list-events ft-c66a5c18-1d6d-43c9-94bd-32d756425b4b
|
|
562
|
-
|
|
563
|
-
# Cancel running job
|
|
564
|
-
together fine-tuning cancel ft-c66a5c18-1d6d-43c9-94bd-32d756425b4b
|
|
565
|
-
|
|
566
|
-
# Download fine-tuned model weights
|
|
567
|
-
together fine-tuning download ft-c66a5c18-1d6d-43c9-94bd-32d756425b4b
|
|
568
|
-
```
|
|
569
|
-
|
|
570
|
-
### Models
|
|
571
|
-
|
|
572
|
-
```bash
|
|
573
|
-
# Help
|
|
574
|
-
together models --help
|
|
575
|
-
|
|
576
|
-
# List models
|
|
577
|
-
together models list
|
|
578
|
-
```
|
|
579
|
-
|
|
580
|
-
## Contributing
|
|
581
|
-
|
|
582
|
-
Refer to the [Contributing Guide](CONTRIBUTING.md)
|
|
583
|
-
|