|
| 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