-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c033c89
commit d8fa401
Showing
3 changed files
with
86 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Cognesy\Instructor; | ||
|
||
use Cognesy\Instructor\Core\RequestHandler; | ||
use Cognesy\Instructor\Data\Request; | ||
use Cognesy\Instructor\Events\EventDispatcher; | ||
use Cognesy\Instructor\Events\Instructor\InstructorDone; | ||
use Exception; | ||
|
||
class InstructorResponse | ||
{ | ||
private RequestHandler $requestHandler; | ||
private EventDispatcher $events; | ||
private Request $request; | ||
|
||
public function __construct( | ||
Request $request, | ||
RequestHandler $requestHandler, | ||
EventDispatcher $events, | ||
) { | ||
$this->events = $events; | ||
$this->requestHandler = $requestHandler; | ||
$this->request = $request; | ||
} | ||
|
||
/** | ||
* Executes the request and returns the response | ||
*/ | ||
public function get() : mixed { | ||
if ($this->request->isStream()) { | ||
return $this->stream()->final(); | ||
} | ||
$result = $this->requestHandler->responseFor($this->request); | ||
$this->events->dispatch(new InstructorDone(['result' => $result])); | ||
return $result->value(); | ||
} | ||
|
||
/** | ||
* Executes the request and returns the response stream | ||
*/ | ||
public function stream() : Stream { | ||
// TODO: do we need this? cannot we just turn streaming on? | ||
if (!$this->request->isStream()) { | ||
throw new Exception('Instructor::stream() method requires response streaming: set "stream" = true in the request options.'); | ||
} | ||
$stream = $this->requestHandler->streamResponseFor($this->request); | ||
return new Stream($stream, $this->events); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters