Skip to content

Commit

Permalink
Fixes in LLM clients
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Sep 26, 2024
1 parent 3e33c72 commit 7946bbd
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion evals/LLMModes/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
'together'
];

$streamingModes = [false, true];
$streamingModes = [true];
$modes = [
Mode::Text,
Mode::MdJson,
Expand Down
6 changes: 5 additions & 1 deletion src/Extras/LLM/Drivers/AnthropicDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function toApiResponse(array $data): ApiResponse {

public function toPartialApiResponse(array $data) : PartialApiResponse {
return new PartialApiResponse(
delta: $data['delta']['text'] ?? $data['delta']['partial_json'] ?? '',
delta: $this->makeDelta($data),
responseData: $data,
toolName: $data['content_block']['name'] ?? '',
toolArgs: $data['delta']['partial_json'] ?? '',
Expand Down Expand Up @@ -203,4 +203,8 @@ private function mapToolsData(array $data) : array {
private function makeContent(array $data) : string {
return $data['content'][0]['text'] ?? Json::encode($data['content'][0]['input']) ?? '';
}

private function makeDelta(array $data) : string {
return $data['delta']['text'] ?? $data['delta']['partial_json'] ?? '';
}
}
8 changes: 6 additions & 2 deletions src/Extras/LLM/Drivers/CohereDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function toApiResponse(array $data): ApiResponse {

public function toPartialApiResponse(array $data) : PartialApiResponse {
return new PartialApiResponse(
delta: $data['text'] ?? $data['tool_calls'][0]['parameters'] ?? '',
delta: $this->makeDelta($data),
responseData: $data,
toolName: $data['tool_calls'][0]['name'] ?? '',
toolArgs: Json::encode($data['tool_calls'][0]['parameters'] ?? []),
Expand Down Expand Up @@ -182,4 +182,8 @@ private function makeContent(array $data) : string {
: ''
);
}
}

private function makeDelta(array $data) : string {
return $data['text'] ?? $data['tool_calls'][0]['parameters'] ?? '';
}
}
10 changes: 7 additions & 3 deletions src/Extras/LLM/Drivers/GeminiDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ public function toApiResponse(array $data): ApiResponse {

public function toPartialApiResponse(array $data) : PartialApiResponse {
return new PartialApiResponse(
delta: $data['candidates'][0]['content']['parts'][0]['text']
?? Json::encode($data['candidates'][0]['content']['parts'][0]['functionCall']['args'] ?? [])
?? '',
delta: $this->makeDelta($data),
responseData: $data,
toolName: $data['candidates'][0]['content']['parts'][0]['functionCall']['name'] ?? '',
toolArgs: Json::encode($data['candidates'][0]['content']['parts'][0]['functionCall']['args'] ?? []),
Expand Down Expand Up @@ -248,4 +246,10 @@ private function makeContent(array $data) : string {
?? Json::encode($data['candidates'][0]['content']['parts'][0]['functionCall']['args'] ?? [])
?? '';
}

private function makeDelta(array $data): string {
return $data['candidates'][0]['content']['parts'][0]['text']
?? Json::encode($data['candidates'][0]['content']['parts'][0]['functionCall']['args'] ?? [])
?? '';
}
}
8 changes: 4 additions & 4 deletions src/Extras/LLM/Drivers/OpenAIDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function toApiResponse(array $data): ApiResponse {

public function toPartialApiResponse(array $data) : PartialApiResponse {
return new PartialApiResponse(
delta: $this->getDelta($data),
delta: $this->makeDelta($data),
responseData: $data,
toolName: $data['choices'][0]['delta']['tool_calls'][0]['function']['name'] ?? '',
toolArgs: $data['choices'][0]['delta']['tool_calls'][0]['function']['arguments'] ?? '',
Expand Down Expand Up @@ -174,12 +174,12 @@ private function makeContent(array $data): string {
};
}

private function getDelta(array $data): string {
private function makeDelta(array $data): string {
$deltaContent = $data['choices'][0]['delta']['content'] ?? '';
$deltaFnArgs = $data['choices'][0]['delta']['tool_calls'][0]['function']['arguments'] ?? '';
return match(true) {
!empty($deltaContent) => $deltaContent,
!empty($deltaFnArgs) => $deltaFnArgs,
('' !== $deltaContent) => $deltaContent,
('' !== $deltaFnArgs) => $deltaFnArgs,
default => ''
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/Extras/LLM/InferenceResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ protected function streamIterator(StreamInterface $stream): Generator {
}

while (!$stream->eof()) {
if (($line = trim($this->readLine($stream))) === '') {
if ('' === ($line = trim($this->readLine($stream)))) {
continue;
}
if (($data = $this->driver->getData($line)) === false) {
if (false === ($data = $this->driver->getData($line))) {
break;
}
yield $data;
Expand All @@ -112,7 +112,7 @@ protected function readLine(StreamInterface $stream): string {
return $buffer;
}
$buffer .= $byte;
if ($byte === "\n") {
if ("\n" === $byte) {
break;
}
}
Expand Down

0 comments on commit 7946bbd

Please sign in to comment.