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.
Files changed (208) hide show
  1. together/__init__.py +101 -114
  2. together/_base_client.py +1995 -0
  3. together/_client.py +1033 -0
  4. together/_compat.py +219 -0
  5. together/_constants.py +14 -0
  6. together/_exceptions.py +108 -0
  7. together/_files.py +123 -0
  8. together/_models.py +857 -0
  9. together/_qs.py +150 -0
  10. together/_resource.py +43 -0
  11. together/_response.py +830 -0
  12. together/_streaming.py +370 -0
  13. together/_types.py +260 -0
  14. together/_utils/__init__.py +64 -0
  15. together/_utils/_compat.py +45 -0
  16. together/_utils/_datetime_parse.py +136 -0
  17. together/_utils/_logs.py +25 -0
  18. together/_utils/_proxy.py +65 -0
  19. together/_utils/_reflection.py +42 -0
  20. together/_utils/_resources_proxy.py +24 -0
  21. together/_utils/_streams.py +12 -0
  22. together/_utils/_sync.py +58 -0
  23. together/_utils/_transform.py +457 -0
  24. together/_utils/_typing.py +156 -0
  25. together/_utils/_utils.py +421 -0
  26. together/_version.py +4 -0
  27. together/lib/.keep +4 -0
  28. together/lib/__init__.py +23 -0
  29. together/{cli → lib/cli}/api/endpoints.py +65 -81
  30. together/{cli/api/evaluation.py → lib/cli/api/evals.py} +152 -43
  31. together/{cli → lib/cli}/api/files.py +20 -17
  32. together/{cli/api/finetune.py → lib/cli/api/fine_tuning.py} +116 -172
  33. together/{cli → lib/cli}/api/models.py +34 -27
  34. together/lib/cli/api/utils.py +50 -0
  35. together/{cli → lib/cli}/cli.py +16 -26
  36. together/{constants.py → lib/constants.py} +11 -24
  37. together/lib/resources/__init__.py +11 -0
  38. together/lib/resources/files.py +999 -0
  39. together/lib/resources/fine_tuning.py +280 -0
  40. together/lib/resources/models.py +35 -0
  41. together/lib/types/__init__.py +13 -0
  42. together/lib/types/error.py +9 -0
  43. together/lib/types/fine_tuning.py +397 -0
  44. together/{utils → lib/utils}/__init__.py +6 -14
  45. together/{utils → lib/utils}/_log.py +11 -16
  46. together/{utils → lib/utils}/files.py +90 -288
  47. together/lib/utils/serializer.py +10 -0
  48. together/{utils → lib/utils}/tools.py +19 -55
  49. together/resources/__init__.py +225 -39
  50. together/resources/audio/__init__.py +72 -48
  51. together/resources/audio/audio.py +198 -0
  52. together/resources/audio/speech.py +574 -128
  53. together/resources/audio/transcriptions.py +247 -261
  54. together/resources/audio/translations.py +221 -241
  55. together/resources/audio/voices.py +111 -41
  56. together/resources/batches.py +417 -0
  57. together/resources/chat/__init__.py +30 -21
  58. together/resources/chat/chat.py +102 -0
  59. together/resources/chat/completions.py +1063 -263
  60. together/resources/code_interpreter/__init__.py +33 -0
  61. together/resources/code_interpreter/code_interpreter.py +258 -0
  62. together/resources/code_interpreter/sessions.py +135 -0
  63. together/resources/completions.py +884 -225
  64. together/resources/embeddings.py +172 -68
  65. together/resources/endpoints.py +589 -477
  66. together/resources/evals.py +452 -0
  67. together/resources/files.py +397 -129
  68. together/resources/fine_tuning.py +1033 -0
  69. together/resources/hardware.py +181 -0
  70. together/resources/images.py +258 -104
  71. together/resources/jobs.py +214 -0
  72. together/resources/models.py +223 -193
  73. together/resources/rerank.py +190 -92
  74. together/resources/videos.py +286 -214
  75. together/types/__init__.py +66 -167
  76. together/types/audio/__init__.py +10 -0
  77. together/types/audio/speech_create_params.py +75 -0
  78. together/types/audio/transcription_create_params.py +54 -0
  79. together/types/audio/transcription_create_response.py +111 -0
  80. together/types/audio/translation_create_params.py +40 -0
  81. together/types/audio/translation_create_response.py +70 -0
  82. together/types/audio/voice_list_response.py +23 -0
  83. together/types/audio_speech_stream_chunk.py +16 -0
  84. together/types/autoscaling.py +13 -0
  85. together/types/autoscaling_param.py +15 -0
  86. together/types/batch_create_params.py +24 -0
  87. together/types/batch_create_response.py +14 -0
  88. together/types/batch_job.py +45 -0
  89. together/types/batch_list_response.py +10 -0
  90. together/types/chat/__init__.py +18 -0
  91. together/types/chat/chat_completion.py +60 -0
  92. together/types/chat/chat_completion_chunk.py +61 -0
  93. together/types/chat/chat_completion_structured_message_image_url_param.py +18 -0
  94. together/types/chat/chat_completion_structured_message_text_param.py +13 -0
  95. together/types/chat/chat_completion_structured_message_video_url_param.py +18 -0
  96. together/types/chat/chat_completion_usage.py +13 -0
  97. together/types/chat/chat_completion_warning.py +9 -0
  98. together/types/chat/completion_create_params.py +329 -0
  99. together/types/code_interpreter/__init__.py +5 -0
  100. together/types/code_interpreter/session_list_response.py +31 -0
  101. together/types/code_interpreter_execute_params.py +45 -0
  102. together/types/completion.py +42 -0
  103. together/types/completion_chunk.py +66 -0
  104. together/types/completion_create_params.py +138 -0
  105. together/types/dedicated_endpoint.py +44 -0
  106. together/types/embedding.py +24 -0
  107. together/types/embedding_create_params.py +31 -0
  108. together/types/endpoint_create_params.py +43 -0
  109. together/types/endpoint_list_avzones_response.py +11 -0
  110. together/types/endpoint_list_params.py +18 -0
  111. together/types/endpoint_list_response.py +41 -0
  112. together/types/endpoint_update_params.py +27 -0
  113. together/types/eval_create_params.py +263 -0
  114. together/types/eval_create_response.py +16 -0
  115. together/types/eval_list_params.py +21 -0
  116. together/types/eval_list_response.py +10 -0
  117. together/types/eval_status_response.py +100 -0
  118. together/types/evaluation_job.py +139 -0
  119. together/types/execute_response.py +108 -0
  120. together/types/file_delete_response.py +13 -0
  121. together/types/file_list.py +12 -0
  122. together/types/file_purpose.py +9 -0
  123. together/types/file_response.py +31 -0
  124. together/types/file_type.py +7 -0
  125. together/types/fine_tuning_cancel_response.py +194 -0
  126. together/types/fine_tuning_content_params.py +24 -0
  127. together/types/fine_tuning_delete_params.py +11 -0
  128. together/types/fine_tuning_delete_response.py +12 -0
  129. together/types/fine_tuning_list_checkpoints_response.py +21 -0
  130. together/types/fine_tuning_list_events_response.py +12 -0
  131. together/types/fine_tuning_list_response.py +199 -0
  132. together/types/finetune_event.py +41 -0
  133. together/types/finetune_event_type.py +33 -0
  134. together/types/finetune_response.py +177 -0
  135. together/types/hardware_list_params.py +16 -0
  136. together/types/hardware_list_response.py +58 -0
  137. together/types/image_data_b64.py +15 -0
  138. together/types/image_data_url.py +15 -0
  139. together/types/image_file.py +23 -0
  140. together/types/image_generate_params.py +85 -0
  141. together/types/job_list_response.py +47 -0
  142. together/types/job_retrieve_response.py +43 -0
  143. together/types/log_probs.py +18 -0
  144. together/types/model_list_response.py +10 -0
  145. together/types/model_object.py +42 -0
  146. together/types/model_upload_params.py +36 -0
  147. together/types/model_upload_response.py +23 -0
  148. together/types/rerank_create_params.py +36 -0
  149. together/types/rerank_create_response.py +36 -0
  150. together/types/tool_choice.py +23 -0
  151. together/types/tool_choice_param.py +23 -0
  152. together/types/tools_param.py +23 -0
  153. together/types/training_method_dpo.py +22 -0
  154. together/types/training_method_sft.py +18 -0
  155. together/types/video_create_params.py +86 -0
  156. together/types/video_create_response.py +10 -0
  157. together/types/video_job.py +57 -0
  158. together-2.0.0a6.dist-info/METADATA +729 -0
  159. together-2.0.0a6.dist-info/RECORD +165 -0
  160. {together-1.5.34.dist-info → together-2.0.0a6.dist-info}/WHEEL +1 -1
  161. together-2.0.0a6.dist-info/entry_points.txt +2 -0
  162. {together-1.5.34.dist-info → together-2.0.0a6.dist-info}/licenses/LICENSE +1 -1
  163. together/abstract/api_requestor.py +0 -770
  164. together/cli/api/chat.py +0 -298
  165. together/cli/api/completions.py +0 -119
  166. together/cli/api/images.py +0 -93
  167. together/cli/api/utils.py +0 -139
  168. together/client.py +0 -186
  169. together/error.py +0 -194
  170. together/filemanager.py +0 -635
  171. together/legacy/__init__.py +0 -0
  172. together/legacy/base.py +0 -27
  173. together/legacy/complete.py +0 -93
  174. together/legacy/embeddings.py +0 -27
  175. together/legacy/files.py +0 -146
  176. together/legacy/finetune.py +0 -177
  177. together/legacy/images.py +0 -27
  178. together/legacy/models.py +0 -44
  179. together/resources/batch.py +0 -165
  180. together/resources/code_interpreter.py +0 -82
  181. together/resources/evaluation.py +0 -808
  182. together/resources/finetune.py +0 -1388
  183. together/together_response.py +0 -50
  184. together/types/abstract.py +0 -26
  185. together/types/audio_speech.py +0 -311
  186. together/types/batch.py +0 -54
  187. together/types/chat_completions.py +0 -210
  188. together/types/code_interpreter.py +0 -57
  189. together/types/common.py +0 -67
  190. together/types/completions.py +0 -107
  191. together/types/embeddings.py +0 -35
  192. together/types/endpoints.py +0 -123
  193. together/types/error.py +0 -16
  194. together/types/evaluation.py +0 -93
  195. together/types/files.py +0 -93
  196. together/types/finetune.py +0 -464
  197. together/types/images.py +0 -42
  198. together/types/models.py +0 -96
  199. together/types/rerank.py +0 -43
  200. together/types/videos.py +0 -69
  201. together/utils/api_helpers.py +0 -124
  202. together/version.py +0 -6
  203. together-1.5.34.dist-info/METADATA +0 -583
  204. together-1.5.34.dist-info/RECORD +0 -77
  205. together-1.5.34.dist-info/entry_points.txt +0 -3
  206. /together/{abstract → lib/cli}/__init__.py +0 -0
  207. /together/{cli → lib/cli/api}/__init__.py +0 -0
  208. /together/{cli/api/__init__.py → py.typed} +0 -0
@@ -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,6 +0,0 @@
1
- import importlib.metadata
2
-
3
-
4
- VERSION = importlib.metadata.version(
5
- "together"
6
- ) # gets version number from pyproject.toml
@@ -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
- [![PyPI version](https://img.shields.io/pypi/v/together.svg)](https://pypi.org/project/together/)
76
- [![Discord](https://dcbadge.limes.pink/api/server/https://discord.gg/9Rk6sSeWEG?style=flat&theme=discord-inverted)](https://discord.com/invite/9Rk6sSeWEG)
77
- [![Twitter](https://img.shields.io/twitter/url/https/twitter.com/togethercompute.svg?style=social&label=Follow%20%40togethercompute)](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
-