Skip to content

Commit 68219a7

Browse files
committed
chore: add Response wrapper
1 parent 0ca7dd5 commit 68219a7

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/Support/GrokResponse.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace GrokPHP\Laravel\Support;
4+
5+
class GrokResponse implements \JsonSerializable
6+
{
7+
private array $response;
8+
9+
public function __construct(array $response)
10+
{
11+
$this->response = $response;
12+
}
13+
14+
/**
15+
* Get the full API response.
16+
*/
17+
public function full(): array
18+
{
19+
return $this->response;
20+
}
21+
22+
/**
23+
* Get only the assistant's message content.
24+
*/
25+
public function content(): string
26+
{
27+
return $this->response['choices'][0]['message']['content'] ?? 'No response received.';
28+
}
29+
30+
/**
31+
* Get the usage statistics for the API response.
32+
*/
33+
public function usage(): array
34+
{
35+
return $this->response['usage'] ?? [];
36+
}
37+
38+
/**
39+
* Allow the GrokResponse object to be serialized to JSON.
40+
* @return array
41+
*/
42+
public function jsonSerialize(): array
43+
{
44+
return $this->response;
45+
}
46+
}

0 commit comments

Comments
 (0)