A single request within a batch submission.
| 27 | |
| 28 | @dataclass |
| 29 | class BatchRequest: |
| 30 | """A single request within a batch submission.""" |
| 31 | |
| 32 | custom_id: str |
| 33 | """Unique identifier for mapping responses back to requests.""" |
| 34 | |
| 35 | model: str |
| 36 | """The OpenAI model to use (e.g., 'gpt-4o-mini').""" |
| 37 | |
| 38 | messages: List[Dict[str, str]] |
| 39 | """The chat messages for this request.""" |
| 40 | |
| 41 | temperature: float = 0.0 |
| 42 | """Sampling temperature.""" |
| 43 | |
| 44 | max_tokens: Optional[int] = None |
| 45 | """Maximum tokens in the response.""" |
| 46 | |
| 47 | response_format: Optional[Dict[str, str]] = None |
| 48 | """Optional response format (e.g., {"type": "json_object"}).""" |
| 49 | |
| 50 | def to_jsonl_line(self) -> str: |
| 51 | """Convert to a JSONL line for the Batch API input file.""" |
| 52 | body = { |
| 53 | "model": self.model, |
| 54 | "messages": self.messages, |
| 55 | "temperature": self.temperature, |
| 56 | } |
| 57 | if self.max_tokens is not None: |
| 58 | body["max_tokens"] = self.max_tokens |
| 59 | if self.response_format is not None: |
| 60 | body["response_format"] = self.response_format |
| 61 | |
| 62 | return json.dumps({ |
| 63 | "custom_id": self.custom_id, |
| 64 | "method": "POST", |
| 65 | "url": "/v1/chat/completions", |
| 66 | "body": body, |
| 67 | }) |
| 68 | |
| 69 | |
| 70 | @dataclass |
no outgoing calls