Skip to content

Commit 0ca7dd5

Browse files
committed
chore: add GrokAI Service
1 parent 5452280 commit 0ca7dd5

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

src/Facades/GrokAI.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
namespace GrokPHP\Laravel\Facades;
44

5+
use GrokPHP\Client\Clients\Vision;
56
use Illuminate\Support\Facades\Facade;
67

78
/**
8-
* @method static array chat(array $messages, \GrokPHP\Client\Config\ChatOptions $options)
9+
* @method static array chat(array $messages, ?\GrokPHP\Client\Config\ChatOptions $options = null)
10+
* @method static Vision vision()
911
*/
1012
class GrokAI extends Facade
1113
{
1214
protected static function getFacadeAccessor(): string
1315
{
14-
return 'grok-ai';
16+
return \GrokPHP\Laravel\Services\GrokAI::class;
1517
}
1618
}

src/Providers/GrokServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace GrokPHP\Laravel\Providers;
44

55
use GrokPHP\Laravel\Commands\InstallGrokCommand;
6+
use GrokPHP\Laravel\Services\GrokAI;
67
use Illuminate\Support\ServiceProvider;
78
use GrokPHP\Client\Clients\GrokClient;
89
use GrokPHP\Client\Config\GrokConfig;
@@ -26,6 +27,10 @@ public function register(): void
2627
return new GrokClient($app->make(GrokConfig::class));
2728
});
2829

30+
$this->app->singleton(GrokAI::class, function ($app) {
31+
return new GrokAI($app->make(GrokClient::class));
32+
});
33+
2934
$this->app->alias(GrokClient::class, 'grok-ai');
3035
}
3136

src/Services/GrokAI.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace GrokPHP\Laravel\Services;
4+
5+
use GrokPHP\Client\Clients\GrokClient;
6+
use GrokPHP\Client\Config\ChatOptions;
7+
use GrokPHP\Client\Enums\Model;
8+
use GrokPHP\Client\Exceptions\GrokException;
9+
use GrokPHP\Laravel\Support\GrokResponse;
10+
11+
class GrokAI
12+
{
13+
protected GrokClient $client;
14+
15+
public function __construct(GrokClient $client)
16+
{
17+
$this->client = $client;
18+
}
19+
20+
/**
21+
* Send a chat completion request to Grok API.
22+
* @param array $messages
23+
* @param ChatOptions|null $options
24+
* @return GrokResponse
25+
* @throws GrokException
26+
*/
27+
public function chat(array $messages, ?ChatOptions $options = null): GrokResponse
28+
{
29+
$options = $options ?? new ChatOptions(
30+
model: Model::GROK_2,
31+
temperature: 0.7,
32+
stream: false
33+
);
34+
35+
$response = $this->client->chat($messages, $options);
36+
37+
return new GrokResponse($response);
38+
}
39+
40+
/**
41+
* Analyze an image using Grok Vision models.
42+
* @return GrokVision
43+
*/
44+
public function vision(): GrokVision
45+
{
46+
return new GrokVision($this->client);
47+
}
48+
}

0 commit comments

Comments
 (0)