Skip to content

Commit

Permalink
fix: psalm
Browse files Browse the repository at this point in the history
  • Loading branch information
mcharytoniuk committed Jan 16, 2024
1 parent 0df6f49 commit 82119d4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/Command/OllamaCompletion.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ protected function executeInCoroutine(InputInterface $input, OutputInterface $ou
prompt: $prompt,
);

foreach ($this->ollamaClient->generateCompletion($completionRequest) as $chunk) {
$output->write($chunk);
foreach ($this->ollamaClient->generateCompletion($completionRequest) as $token) {
$output->write($token);
}

return Command::SUCCESS;
Expand Down
31 changes: 20 additions & 11 deletions src/OllamaClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public function __destruct()
public function generateCompletion(OllamaCompletionRequest $request): Generator
{
$channel = new Channel(1);
$data = json_encode($request);
$requestData = json_encode($request);

$cid = go(function () use ($channel, $data) {
$cid = go(function () use ($channel, $requestData) {
try {
curl_setopt($this->ch, CURLOPT_URL, $this->ollamaLinkBuilder->build('/api/generate'));
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $requestData);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($this->ch, CURLOPT_WRITEFUNCTION, function (CurlHandle $ch, string $data) use ($channel) {
if (!empty($data)) {
Expand Down Expand Up @@ -73,21 +73,27 @@ public function generateCompletion(OllamaCompletionRequest $request): Generator
throw new RuntimeException('Unable to start a coroutine');
}

foreach (new SwooleChannelIterator($channel) as $data) {
if ($data) {
yield $data->response;
}
/**
* @var SwooleChannelIterator<object{ response: string }>
*/
$swooleChannelIterator = new SwooleChannelIterator($channel);

foreach ($swooleChannelIterator as $token) {
yield $token->response;
}
}

public function generateEmbedding(OllamaEmbeddingRequest $request): OllamaEmbeddingResponse
{
$data = json_encode($request);
$requestData = json_encode($request);

curl_setopt($this->ch, CURLOPT_URL, $this->ollamaLinkBuilder->build('/api/embeddings'));
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $requestData);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);

/**
* @var false|string $responseContent
*/
$responseContent = curl_exec($this->ch);

if (false === $responseContent) {
Expand All @@ -96,12 +102,15 @@ public function generateEmbedding(OllamaEmbeddingRequest $request): OllamaEmbedd

$this->assertStatusCode(200);

$data = $this
/**
* @var object{ embedding: array<float> } $responseData
*/
$responseData = $this
->jsonSerializer
->unserialize($responseContent)
;

return new OllamaEmbeddingResponse($data->embedding);
return new OllamaEmbeddingResponse($responseData->embedding);
}

private function assertStatusCode(int $expectedStatusCode): void
Expand Down
5 changes: 1 addition & 4 deletions src/OllamaRequestOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ public function jsonSerialize(): array
{
$ret = [];

if (isset($this->stopDelimiter)) {
$ret['stop'] = $this->stopDelimiter;
}

$ret['stop'] = $this->stopDelimiter;
$ret['temperature'] = $this->temperature;

return $ret;
Expand Down
7 changes: 4 additions & 3 deletions src/SwooleChannelIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ public function __construct(private Channel $channel) {}
public function getIterator(): Generator
{
do {
/**
* @var mixed $data explicitly mixed for typechecks
*/
$data = $this->channel->pop();

if (false === $data) {
switch ($this->channel->errCode) {
case SWOOLE_CHANNEL_CLOSED:
return;
case SWOOLE_CHANNEL_OK:
yield $data;

break;
throw new RuntimeException('Using "false" is ambiguous in channels');
case SWOOLE_CHANNEL_TIMEOUT:
throw new RuntimeException('Swoole channel timed out');
}
Expand Down

0 comments on commit 82119d4

Please sign in to comment.