Skip to content

Commit d8c4db6

Browse files
authored
Legacy compatibility (#87)
* Add legacy compatibility code for all API classes * Bump version to v1.0.1 and update github link * Remove migration guide link * Delete MIGRATION_GUIDE_v1.md
1 parent cc80645 commit d8c4db6

File tree

12 files changed

+543
-7
lines changed

12 files changed

+543
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ The [Together Python Library](https://pypi.org/project/together/) is the officia
33
# Installation
44

55
> 🚧
6-
> The library was rewritten in v1.0.0 released in April of 2024. There were significant changes made. Find the complete migration guide [here](docs/MIGRATION_GUIDE_v1.md).
6+
> The library was rewritten in v1.0.0 released in April of 2024. There were significant changes made.
77
88
To install Together Python Library from PyPi, simply run:
99

pyproject.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api"
44

55
[tool.poetry]
66
name = "together"
7-
version = "1.0.0"
7+
version = "1.0.1"
88
authors = [
99
"Together AI <[email protected]>"
1010
]
@@ -16,8 +16,8 @@ classifiers = [
1616
"License :: OSI Approved :: Apache Software License",
1717
"Operating System :: POSIX :: Linux",
1818
]
19-
repository = "https://github.com/togethercomputer/together"
20-
homepage = "https://github.com/togethercomputer/together"
19+
repository = "https://github.com/togethercomputer/together-python"
20+
homepage = "https://github.com/togethercomputer/together-python"
2121

2222
[tool.poetry.dependencies]
2323
python = "^3.8"
@@ -54,8 +54,8 @@ tox = "^4.14.1"
5454

5555

5656
[tool.poetry.urls]
57-
"Homepage" = "https://github.com/togethercomputer/together"
58-
"Bug Tracker" = "https://github.com/togethercomputer/together/issues"
57+
"Homepage" = "https://github.com/togethercomputer/together-python"
58+
"Bug Tracker" = "https://github.com/togethercomputer/together-python/issues"
5959

6060
[tool.poetry.scripts]
6161
together = "together.cli.cli:main"

src/together/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@
3434
from together.client import AsyncClient, AsyncTogether, Client, Together
3535

3636

37+
api_key: str | None = None # To be deprecated in the next major release
38+
39+
# Legacy functions
40+
from together.legacy.complete import AsyncComplete, Complete, Completion
41+
from together.legacy.embeddings import Embeddings
42+
from together.legacy.files import Files
43+
from together.legacy.finetune import Finetune
44+
from together.legacy.images import Image
45+
from together.legacy.models import Models
46+
47+
3748
__all__ = [
3849
"aiosession",
3950
"constants",
File renamed without changes.

src/together/legacy/base.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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

src/together/legacy/complete.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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,
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+
return client.completions.create(
29+
prompt=prompt, stream=False, **kwargs
30+
).model_dump() # type: ignore
31+
32+
@classmethod
33+
@deprecated # type: ignore
34+
def create_streaming(
35+
cls,
36+
prompt: str,
37+
**kwargs,
38+
) -> Iterator[Dict[str, Any]]:
39+
"""Legacy streaming completion function."""
40+
41+
api_key = None
42+
if together.api_key:
43+
warnings.warn(API_KEY_WARNING)
44+
api_key = together.api_key
45+
46+
client = together.Together(api_key=api_key)
47+
48+
return (
49+
token.model_dump() # type: ignore
50+
for token in client.completions.create(prompt=prompt, stream=True, **kwargs)
51+
)
52+
53+
54+
class Completion:
55+
@classmethod
56+
@deprecated # type: ignore
57+
def create(
58+
cls,
59+
prompt: str,
60+
**kwargs,
61+
) -> CompletionResponse | Iterator[CompletionChunk]:
62+
"""Completion function."""
63+
64+
api_key = None
65+
if together.api_key:
66+
warnings.warn(API_KEY_WARNING)
67+
api_key = together.api_key
68+
69+
client = together.Together(api_key=api_key)
70+
71+
return client.completions.create(prompt=prompt, **kwargs)
72+
73+
74+
class AsyncComplete:
75+
@classmethod
76+
@deprecated # type: ignore
77+
async def create(
78+
cls,
79+
prompt: str,
80+
**kwargs,
81+
) -> CompletionResponse | AsyncGenerator[CompletionChunk, None]:
82+
"""Async completion function."""
83+
84+
api_key = None
85+
if together.api_key:
86+
warnings.warn(API_KEY_WARNING)
87+
api_key = together.api_key
88+
89+
client = together.AsyncTogether(api_key=api_key)
90+
91+
return await client.completions.create(prompt=prompt, **kwargs)

src/together/legacy/embeddings.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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,
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()

src/together/legacy/files.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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()
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+
response = client.files.upload(file=file).model_dump()
57+
58+
response["report_dict"] = report_dict
59+
60+
return response
61+
62+
@classmethod
63+
@deprecated # type: ignore
64+
def delete(
65+
cls,
66+
file_id: str,
67+
) -> Dict[str, Any]:
68+
"""Legacy file delete function."""
69+
70+
api_key = None
71+
if together.api_key:
72+
warnings.warn(API_KEY_WARNING)
73+
api_key = together.api_key
74+
75+
client = together.Together(api_key=api_key)
76+
77+
return client.files.delete(id=file_id).model_dump()
78+
79+
@classmethod
80+
@deprecated # type: ignore
81+
def retrieve(
82+
cls,
83+
file_id: str,
84+
) -> Dict[str, Any]:
85+
"""Legacy file retrieve function."""
86+
87+
api_key = None
88+
if together.api_key:
89+
warnings.warn(API_KEY_WARNING)
90+
api_key = together.api_key
91+
92+
client = together.Together(api_key=api_key)
93+
94+
return client.files.retrieve(id=file_id).model_dump()
95+
96+
@classmethod
97+
@deprecated # type: ignore
98+
def retrieve_content(
99+
cls,
100+
file_id: str,
101+
output: str | None = None,
102+
) -> Dict[str, Any]:
103+
"""Legacy file retrieve content function."""
104+
105+
api_key = None
106+
if together.api_key:
107+
warnings.warn(API_KEY_WARNING)
108+
api_key = together.api_key
109+
110+
client = together.Together(api_key=api_key)
111+
112+
return client.files.retrieve_content(id=file_id, output=output).model_dump()
113+
114+
@classmethod
115+
@deprecated # type: ignore
116+
def save_jsonl(
117+
self, data: Dict[str, str], output_path: str, append: bool = False
118+
) -> None:
119+
"""
120+
Write list of objects to a JSON lines file.
121+
"""
122+
mode = "a+" if append else "w"
123+
with open(output_path, mode, encoding="utf-8") as f:
124+
for line in data:
125+
json_record = json.dumps(line, ensure_ascii=False)
126+
f.write(json_record + "\n")
127+
print("Wrote {} records to {}".format(len(data), output_path))
128+
129+
@classmethod
130+
@deprecated # type: ignore
131+
def load_jsonl(self, input_path: str) -> List[Dict[str, str]]:
132+
"""
133+
Read list of objects from a JSON lines file.
134+
"""
135+
data = []
136+
with open(input_path, "r", encoding="utf-8") as f:
137+
for line in f:
138+
data.append(json.loads(line.rstrip("\n|\r")))
139+
print("Loaded {} records from {}".format(len(data), input_path))
140+
return data

0 commit comments

Comments
 (0)