Skip to content

Commit

Permalink
Bug fixes in LLM connectivity
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Oct 8, 2024
1 parent 5586867 commit 02e8885
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion config/llm.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
],
'openrouter' => [
'providerType' => LLMProviderType::OpenRouter->value,
'apiUrl' => 'https://openrouter.ai/api/v1/',
'apiUrl' => 'https://openrouter.ai/api/v1',
'apiKey' => Env::get('OPENROUTER_API_KEY', ''),
'endpoint' => '/chat/completions',
'defaultModel' => 'qwen/qwen-2.5-72b-instruct', //'microsoft/phi-3.5-mini-128k-instruct',
Expand Down
1 change: 1 addition & 0 deletions evals/LLMModes/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
$connections = [
'azure',
'cohere1',
'cohere2',
'fireworks',
'gemini',
'groq',
Expand Down
9 changes: 5 additions & 4 deletions src/Features/Http/Drivers/GuzzleDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ public function __construct(
if (isset($this->httpClient) && Debug::isEnabled()) {
throw new InvalidArgumentException("Guzzle does not allow to inject debugging stack into existing client. Turn off debug or use default client.");
}
$this->client = match(Debug::isEnabled()) {
false => $httpClient ?? new Client(),
true => new Client(['handler' => $this->addDebugStack(HandlerStack::create())]),
};
// $this->client = match(Debug::isEnabled()) {
// false => $httpClient ?? new Client(),
// true => new Client(['handler' => $this->addDebugStack(HandlerStack::create())]),
// };
$this->client = new Client(['handler' => $this->addDebugStack(HandlerStack::create())]);
}

public function handle(
Expand Down
4 changes: 2 additions & 2 deletions src/Features/LLM/Drivers/CohereV2Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function toLLMResponse(array $data): LLMResponse {
);
}

public function toPartialLLMResponse(array $data) : ?PartialLLMResponse {
public function toPartialLLMResponse(array|null $data) : ?PartialLLMResponse {
if (empty($data)) {
return null;
}
Expand All @@ -80,7 +80,7 @@ public function getData(string $data): string|bool {
}
$data = trim(substr($data, 5));
return match(true) {
$data === '"[DONE]"' => false,
$data === '[DONE]' => false,
default => $data,
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/Features/LLM/Drivers/OpenAIDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public function toLLMResponse(array $data): ?LLMResponse {
);
}

public function toPartialLLMResponse(array $data) : ?PartialLLMResponse {
if (empty($data)) {
public function toPartialLLMResponse(array|null $data) : ?PartialLLMResponse {
if ($data === null || empty($data)) {
return null;
}
return new PartialLLMResponse(
Expand Down
18 changes: 10 additions & 8 deletions src/Features/LLM/InferenceResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,21 @@ public function asPartialLLMResponses() : Generator {
if ($streamEvent === false) {
continue;
}
$result = $this->driver->toPartialLLMResponse(Json::decode($streamEvent));
if ($result === null) {
$data = Json::decode($streamEvent, []);
$partialResponse = $this->driver->toPartialLLMResponse($data);
if ($partialResponse === null) {
continue;
}
if ($result->finishReason !== '') {
$finishReason = $result->finishReason;
if ($partialResponse->finishReason !== '') {
$finishReason = $partialResponse->finishReason;
}
$content .= $result->contentDelta;
$partialResponse = $result
$content .= $partialResponse->contentDelta;
// add accumulated content and last finish reason
$enrichedResponse = $partialResponse
->withContent($content)
->withFinishReason($finishReason);
$this->events->dispatch(new PartialLLMResponseReceived($partialResponse));
yield $partialResponse;
$this->events->dispatch(new PartialLLMResponseReceived($enrichedResponse));
yield $enrichedResponse;
}
}

Expand Down

0 comments on commit 02e8885

Please sign in to comment.