DeepseekClient.php
4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace DeepseekPhp;
use DeepseekPhp\Contracts\DeepseekClientContract;
use DeepseekPhp\Contracts\Models\ResultContract;
use DeepseekPhp\Resources\Resource;
use Psr\Http\Client\ClientInterface;
use DeepseekPhp\Factories\ApiFactory;
use DeepseekPhp\Enums\Queries\QueryRoles;
use DeepseekPhp\Enums\Requests\QueryFlags;
use DeepseekPhp\Enums\Requests\HeaderFlags;
use DeepseekPhp\Enums\Configs\TemperatureValues;
use DeepseekPhp\Traits\Resources\{HasChat, HasCoder};
class DeepseekClient implements DeepseekClientContract
{
use HasChat, HasCoder;
/**
* PSR-18 HTTP client for making requests.
*
* @var ClientInterface
*/
protected ClientInterface $httpClient;
/**
* Array to store accumulated queries.
*
* @var array
*/
protected array $queries = [];
/**
* The model being used for API requests.
*
* @var string|null
*/
protected ?string $model;
/**
* Indicates whether to enable streaming for API responses.
*
* @var bool
*/
protected bool $stream;
protected float $temperature;
/**
* response result contract
* @var ResultContract
*/
protected ResultContract $result;
/**
* Initialize the DeepseekClient with a PSR-compliant HTTP client.
*
* @param ClientInterface $httpClient The HTTP client used for making API requests.
*/
public function __construct(ClientInterface $httpClient)
{
$this->httpClient = $httpClient;
$this->model = null;
$this->stream = false;
$this->temperature = (float) TemperatureValues::GENERAL_CONVERSATION->value;
}
public function run(): string
{
$requestData = [
QueryFlags::MESSAGES->value => $this->queries,
QueryFlags::MODEL->value => $this->model,
QueryFlags::STREAM->value => $this->stream,
QueryFlags::TEMPERATURE->value => $this->temperature,
];
// Clear queries after sending
$this->queries = [];
$this->result = (new Resource($this->httpClient))->sendRequest($requestData);
return $this->getResult()->getContent();
}
/**
* Create a new DeepseekClient instance with the given API key.
*
* @param string $apiKey The API key for authentication.
* @param string|null $baseUrl The base URL for the API (optional).
* @param int|null $timeout The timeout duration for requests in seconds (optional).
* @return self A new instance of the DeepseekClient.
*/
public static function build(string $apiKey, ?string $baseUrl = null, ?int $timeout = null): self
{
$httpClient = ApiFactory::build()
->setBaseUri($baseUrl)
->setTimeout($timeout)
->setKey($apiKey)
->run();
return new self($httpClient);
}
/**
* Add a query to the accumulated queries list.
*
* @param string $content
* @param string|null $role
* @return self The current instance for method chaining.
*/
public function query(string $content, ?string $role = null): self
{
$this->queries[] = $this->buildQuery($content, $role);
return $this;
}
/**
* Set the model to be used for API requests.
*
* @param string|null $model The model name (optional).
* @return self The current instance for method chaining.
*/
public function withModel(?string $model = null): self
{
$this->model = $model;
return $this;
}
/**
* Enable or disable streaming for API responses.
*
* @param bool $stream Whether to enable streaming (default: true).
* @return self The current instance for method chaining.
*/
public function withStream(bool $stream = true): self
{
$this->stream = $stream;
return $this;
}
public function setTemperature(float $temperature): self
{
$this->temperature = $temperature;
return $this;
}
protected function buildQuery(string $content, ?string $role = null): array
{
return [
'role' => $role ?: QueryRoles::USER->value,
'content' => $content
];
}
/**
* response result model
* @return \DeepseekPhp\Contracts\Models\ResultContract
*/
public function getResult(): ResultContract
{
return $this->result;
}
}