Skip to content

Commit

Permalink
Cleanup events
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Oct 9, 2024
1 parent 146708b commit 4e80b35
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 174 deletions.
18 changes: 18 additions & 0 deletions src/Events/Inference/StreamDataParsed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Cognesy\Instructor\Events\Inference;

use Cognesy\Instructor\Events\Event;

class StreamDataParsed extends Event
{
public function __construct(
public string $content,
) {
parent::__construct();
}

public function __toString(): string {
return $this->content;
}
}
5 changes: 2 additions & 3 deletions src/Events/Inference/StreamDataReceived.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
class StreamDataReceived extends Event
{
public function __construct(
public string $raw,
public string $parsed,
public string $content,
) {
parent::__construct();
}

public function __toString(): string {
return $this->raw;
return $this->content;
}
}
28 changes: 0 additions & 28 deletions src/Events/LLMClient/LLMRequestErrorRaised.php

This file was deleted.

22 changes: 0 additions & 22 deletions src/Events/LLMClient/LLMRequestInitiated.php

This file was deleted.

29 changes: 0 additions & 29 deletions src/Events/LLMClient/LLMRequestSent.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/Events/LLMClient/LLMStreamConnected.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/Events/LLMClient/LLMStreamRequestInitiated.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/Events/LLMClient/LLMStreamRequestSent.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Events/LLMClient/LLMStreamResponseReceived.php

This file was deleted.

18 changes: 0 additions & 18 deletions src/Events/LLMClient/LLMStreamUpdateReceived.php

This file was deleted.

19 changes: 0 additions & 19 deletions src/Events/LLMClient/RequestBodyCompiled.php

This file was deleted.

4 changes: 3 additions & 1 deletion src/Features/LLM/EventStreamReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Cognesy\Instructor\Events\EventDispatcher;
use Cognesy\Instructor\Events\Inference\StreamDataParsed;
use Cognesy\Instructor\Events\Inference\StreamDataReceived;
use Cognesy\Instructor\Utils\Debug\Debug;
use Generator;
Expand All @@ -27,8 +28,10 @@ public function __construct(
*/
public function eventsFrom(Generator $stream): Generator {
foreach ($this->readLines($stream) as $line) {
$this->events->dispatch(new StreamDataReceived($line));
$processedData = $this->processLine($line);
if ($processedData !== null) {
$this->events->dispatch(new StreamDataParsed($processedData));
yield $processedData;
}
}
Expand Down Expand Up @@ -60,7 +63,6 @@ protected function processLine(string $line): ?string {
if (false === ($data = $this->parse($line))) {
return null;
}
$this->events->dispatch(new StreamDataReceived($line, $data));
return $data;
}

Expand Down
13 changes: 4 additions & 9 deletions src/Features/LLM/InferenceResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function stream() : InferenceStream {
return new InferenceStream($this->response, $this->driver, $this->config, $this->events);
}

// AS API RESPONSE OBJECTS //////////////////////////////////
// AS API RESPONSE OBJECT ///////////////////////////////////

public function response() : LLMResponse {
$response = match($this->isStreamed) {
Expand All @@ -72,20 +72,15 @@ public function response() : LLMResponse {
// INTERNAL /////////////////////////////////////////////////

private function makeLLMResponse() : LLMResponse {
$response = $this->driver->toLLMResponse($this->getResponseData());
$content = $this->getResponseContent();
$data = Json::decode($content) ?? [];
$response = $this->driver->toLLMResponse($data);
$this->events->dispatch(new LLMResponseReceived($response));
return $response;
}

// PRIVATE /////////////////////////////////////////////////

/**
* @return array<string, mixed>
*/
private function getResponseData() : array {
return Json::decode($this->getResponseContent()) ?? [];
}

private function getResponseContent() : string {
if (empty($this->responseContent)) {
$this->responseContent = $this->readFromResponse();
Expand Down
5 changes: 2 additions & 3 deletions src/Features/LLM/InferenceStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Cognesy\Instructor\Features\Http\Contracts\CanAccessResponse;
use Cognesy\Instructor\Features\LLM\Contracts\CanHandleInference;
use Cognesy\Instructor\Features\LLM\Data\LLMConfig;
use Cognesy\Instructor\Features\LLM\Data\LLMResponse;
use Cognesy\Instructor\Features\LLM\Data\PartialLLMResponse;
use Cognesy\Instructor\Utils\Json\Json;
use Generator;
Expand Down Expand Up @@ -97,7 +96,7 @@ private function makePartialLLMResponses(Generator $stream) : Generator {
$content = '';
$finishReason = '';
foreach ($this->getEventStream($stream) as $streamEvent) {
if ($streamEvent === false) {
if ($streamEvent === null || $streamEvent === '') {
continue;
}
$data = Json::decode($streamEvent, []);
Expand All @@ -123,7 +122,7 @@ private function makePartialLLMResponse(array $data) : ?PartialLLMResponse {
}

/**
* @return Generator<string>
* @return Generator<string|null>
*/
private function getEventStream(Generator $stream) : Generator {
if (!$this->streamReceived) {
Expand Down

0 comments on commit 4e80b35

Please sign in to comment.