-
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
d8fa401
commit 163b770
Showing
34 changed files
with
435 additions
and
205 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
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
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
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
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
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
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
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 |
---|---|---|
|
@@ -5,5 +5,6 @@ | |
enum HttpClientType : string | ||
{ | ||
case Guzzle = 'guzzle'; | ||
case Symfony = 'symfony'; | ||
case Unknown = 'unknown'; | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace Cognesy\Instructor\Extras\Http\Adapters; | ||
|
||
use Cognesy\Instructor\Extras\Http\Contracts\CanHandleResponse; | ||
use Generator; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
|
||
class PsrResponse implements CanHandleResponse | ||
{ | ||
private ResponseInterface $response; | ||
private StreamInterface $stream; | ||
|
||
public function __construct( | ||
ResponseInterface $response, | ||
StreamInterface $stream, | ||
) { | ||
$this->response = $response; | ||
$this->stream = $stream; | ||
} | ||
|
||
public function getContents(): string { | ||
return $this->response->getBody()->getContents(); | ||
} | ||
|
||
public function streamContents(int $chunkSize = 1): Generator { | ||
while (!$this->stream->eof()) { | ||
yield $this->stream->read($chunkSize); | ||
} | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace Cognesy\Instructor\Extras\Http\Adapters; | ||
|
||
use Cognesy\Instructor\Extras\Http\Contracts\CanHandleResponse; | ||
use Generator; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
class SymfonyResponse implements CanHandleResponse | ||
{ | ||
private ResponseInterface $response; | ||
private HttpClientInterface $client; | ||
|
||
public function __construct( | ||
HttpClientInterface $client, | ||
ResponseInterface $response, | ||
) { | ||
$this->client = $client; | ||
$this->response = $response; | ||
} | ||
|
||
public function getContents(): string { | ||
return $this->response->getContent(); | ||
} | ||
|
||
public function streamContents(int $chunkSize = 1): Generator { | ||
foreach ($this->client->stream($this->response) as $chunk) { | ||
yield $chunk->getContent(); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,7 @@ | ||
<?php | ||
namespace Cognesy\Instructor\Extras\Http\Contracts; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
|
||
interface CanHandleHttp | ||
{ | ||
public function handle(string $url, array $headers, array $body, string $method = 'POST', bool $streaming = false) : ResponseInterface; | ||
public function handle(string $url, array $headers, array $body, string $method = 'POST', bool $streaming = false) : CanHandleResponse; | ||
} |
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,23 @@ | ||
<?php | ||
|
||
namespace Cognesy\Instructor\Extras\Http\Contracts; | ||
|
||
use Generator; | ||
|
||
interface CanHandleResponse | ||
{ | ||
/** | ||
* Get the response | ||
* | ||
* @return int | ||
*/ | ||
public function getContents(): string; | ||
|
||
/** | ||
* Read chunks of the stream | ||
* | ||
* @param int $chunkSize | ||
* @return Generator<string> | ||
*/ | ||
public function streamContents(int $chunkSize = 1): Generator; | ||
} |
Oops, something went wrong.