|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OneOffTech\LibrarianClient\Requests\StructuredExtraction; |
| 4 | + |
| 5 | +use OneOffTech\LibrarianClient\Dto\Document; |
| 6 | +use OneOffTech\LibrarianClient\Dto\Extraction; |
| 7 | +use OneOffTech\LibrarianClient\Dto\ResponseSchema; |
| 8 | +use Saloon\Contracts\Body\HasBody; |
| 9 | +use Saloon\Enums\Method; |
| 10 | +use Saloon\Http\Request; |
| 11 | +use Saloon\Http\Response; |
| 12 | +use Saloon\Traits\Body\HasJsonBody; |
| 13 | + |
| 14 | +class ExtractRequest extends Request implements HasBody |
| 15 | +{ |
| 16 | + use HasJsonBody; |
| 17 | + |
| 18 | + protected Method $method = Method::POST; |
| 19 | + |
| 20 | + public function __construct( |
| 21 | + protected readonly string $library_id, |
| 22 | + protected readonly Document $document, |
| 23 | + protected readonly ResponseSchema $responseSchema, |
| 24 | + protected readonly ?array $sections = null, |
| 25 | + protected readonly ?string $instructions = null, |
| 26 | + ) { |
| 27 | + // |
| 28 | + } |
| 29 | + |
| 30 | + public function resolveEndpoint(): string |
| 31 | + { |
| 32 | + return "/library/{$this->library_id}/structured_extract"; |
| 33 | + } |
| 34 | + |
| 35 | + protected function defaultBody(): array |
| 36 | + { |
| 37 | + return [ |
| 38 | + 'doc' => $this->document->data, |
| 39 | + 'response_model' => $this->responseSchema->schema, |
| 40 | + 'instructions' => $this->instructions ?? '', |
| 41 | + 'sections' => $this->sections, |
| 42 | + ]; |
| 43 | + } |
| 44 | + |
| 45 | + public function createDtoFromResponse(Response $response): Extraction |
| 46 | + { |
| 47 | + $data = $response->json(); |
| 48 | + |
| 49 | + return (new Extraction( |
| 50 | + content: $data, |
| 51 | + ))->setResponse($response); |
| 52 | + } |
| 53 | +} |
0 commit comments