| 33 | ] |
| 34 | |
| 35 | class RemoteModel(object): |
| 36 | def __init__(self, model) -> None: |
| 37 | if "gpt" in model: |
| 38 | if "OPENAI_KEY" not in os.environ: |
| 39 | print("Cannot find OPENAI_KEY, please set this variable in your shell.") |
| 40 | exit() |
| 41 | self.apikey = os.environ["OPENAI_KEY"] |
| 42 | self.client = OpenAI(api_key = self.apikey) |
| 43 | self.temperature = 0.7 |
| 44 | self.model = model |
| 45 | |
| 46 | def upload_file(self, filename): |
| 47 | response = self.client.files.create( |
| 48 | file=open(filename, "rb"), |
| 49 | purpose="batch" |
| 50 | ) |
| 51 | print(response) |
| 52 | return response |
| 53 | |
| 54 | def delete_file(self, file_id): |
| 55 | response = self.client.files.delete(file_id) |
| 56 | print(response) |
| 57 | return response |
| 58 | |
| 59 | def download_file(self, batch_id): |
| 60 | response = self.client.batches.retrieve(batch_id) |
| 61 | if response.status != "completed": |
| 62 | raise ValueError("The batch is not completed and its status is {}.".format(response.status)) |
| 63 | output_file = response.output_file_id |
| 64 | content = self.client.files.content(output_file) |
| 65 | contents = content.content.splitlines() |
| 66 | outputs = [] |
| 67 | for line in contents: |
| 68 | outputs.append(json.loads(line)) |
| 69 | return outputs |
| 70 | |
| 71 | def collect_errors(self, batch_id): |
| 72 | response = self.client.batches.retrieve(batch_id) |
| 73 | if response.status != "completed": |
| 74 | raise ValueError("The batch is not completed and its status is {}.".format(response.status)) |
| 75 | error_file = response.error_file_id |
| 76 | if error_file == None: |
| 77 | return [] |
| 78 | content = self.client.files.content(error_file) |
| 79 | contents = content.content.splitlines() |
| 80 | outputs = [] |
| 81 | for line in contents: |
| 82 | outputs.append(json.loads(line)) |
| 83 | return outputs |
| 84 | |
| 85 | def batch_run(self, file_id): |
| 86 | response = self.client.batches.create( |
| 87 | input_file_id=file_id, |
| 88 | endpoint="/v1/chat/completions", |
| 89 | completion_window="24h" |
| 90 | ) |
| 91 | print(response) |
| 92 | return response |
no outgoing calls
no test coverage detected