|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import List |
| 4 | + |
| 5 | +from together.abstract import api_requestor |
| 6 | +from together.together_response import TogetherResponse |
| 7 | +from together.types import ( |
| 8 | + TogetherClient, |
| 9 | + TogetherRequest, |
| 10 | + BatchJob, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +class Batches: |
| 15 | + def __init__(self, client: TogetherClient) -> None: |
| 16 | + self._client = client |
| 17 | + |
| 18 | + def create_batch(self, file_id: str, endpoint: str) -> BatchJob: |
| 19 | + |
| 20 | + requestor = api_requestor.APIRequestor( |
| 21 | + client=self._client, |
| 22 | + ) |
| 23 | + |
| 24 | + parameter_payload = { |
| 25 | + "input_file_id": file_id, |
| 26 | + "endpoint": endpoint, |
| 27 | + "completion_window": "24h", |
| 28 | + } |
| 29 | + |
| 30 | + response, _, _ = requestor.request( |
| 31 | + options=TogetherRequest( |
| 32 | + method="POST", |
| 33 | + url=f"batches", |
| 34 | + params=parameter_payload, |
| 35 | + ), |
| 36 | + stream=False, |
| 37 | + ) |
| 38 | + |
| 39 | + assert isinstance(response, TogetherResponse) |
| 40 | + response_body = response.data.get("job", {}) |
| 41 | + return BatchJob(**response_body) |
| 42 | + |
| 43 | + def get_batch(self, batch_job_id: str) -> BatchJob: |
| 44 | + requestor = api_requestor.APIRequestor( |
| 45 | + client=self._client, |
| 46 | + ) |
| 47 | + |
| 48 | + response, _, _ = requestor.request( |
| 49 | + options=TogetherRequest( |
| 50 | + method="GET", |
| 51 | + url=f"batches/{batch_job_id}", |
| 52 | + ), |
| 53 | + stream=False, |
| 54 | + ) |
| 55 | + |
| 56 | + assert isinstance(response, TogetherResponse) |
| 57 | + return BatchJob(**response.data) |
| 58 | + |
| 59 | + def list_batches(self) -> List[BatchJob]: |
| 60 | + requestor = api_requestor.APIRequestor( |
| 61 | + client=self._client, |
| 62 | + ) |
| 63 | + |
| 64 | + response, _, _ = requestor.request( |
| 65 | + options=TogetherRequest( |
| 66 | + method="GET", |
| 67 | + url="batches", |
| 68 | + ), |
| 69 | + stream=False, |
| 70 | + ) |
| 71 | + |
| 72 | + assert isinstance(response, TogetherResponse) |
| 73 | + jobs = response.data or [] |
| 74 | + return [BatchJob(**job) for job in jobs] |
| 75 | + |
| 76 | + |
| 77 | +class AsyncBatches: |
| 78 | + def __init__(self, client: TogetherClient) -> None: |
| 79 | + self._client = client |
| 80 | + |
| 81 | + async def create_batch(self, file_id: str, endpoint: str) -> BatchJob: |
| 82 | + requestor = api_requestor.APIRequestor( |
| 83 | + client=self._client, |
| 84 | + ) |
| 85 | + |
| 86 | + parameter_payload = { |
| 87 | + "input_file_id": file_id, |
| 88 | + "endpoint": endpoint, |
| 89 | + "completion_window": "24h", |
| 90 | + } |
| 91 | + |
| 92 | + response, _, _ = await requestor.arequest( |
| 93 | + options=TogetherRequest( |
| 94 | + method="POST", |
| 95 | + url=f"batches", |
| 96 | + params=parameter_payload, |
| 97 | + ), |
| 98 | + stream=False, |
| 99 | + ) |
| 100 | + |
| 101 | + assert isinstance(response, TogetherResponse) |
| 102 | + response_body = response.data.get("job", {}) |
| 103 | + return BatchJob(**response_body) |
| 104 | + |
| 105 | + async def get_batch(self, batch_job_id: str) -> BatchJob: |
| 106 | + requestor = api_requestor.APIRequestor( |
| 107 | + client=self._client, |
| 108 | + ) |
| 109 | + |
| 110 | + response, _, _ = await requestor.arequest( |
| 111 | + options=TogetherRequest( |
| 112 | + method="GET", |
| 113 | + url=f"batches/{batch_job_id}", |
| 114 | + ), |
| 115 | + stream=False, |
| 116 | + ) |
| 117 | + |
| 118 | + assert isinstance(response, TogetherResponse) |
| 119 | + return BatchJob(**response.data) |
| 120 | + |
| 121 | + async def list_batches(self) -> List[BatchJob]: |
| 122 | + requestor = api_requestor.APIRequestor( |
| 123 | + client=self._client, |
| 124 | + ) |
| 125 | + |
| 126 | + response, _, _ = await requestor.arequest( |
| 127 | + options=TogetherRequest( |
| 128 | + method="GET", |
| 129 | + url="batches", |
| 130 | + ), |
| 131 | + stream=False, |
| 132 | + ) |
| 133 | + |
| 134 | + assert isinstance(response, TogetherResponse) |
| 135 | + jobs = response.data or [] |
| 136 | + return [BatchJob(**job) for job in jobs] |
0 commit comments