From 00071e7ca0d943c77159c03b1c86a65bfe84c21e Mon Sep 17 00:00:00 2001 From: Aleksandr Date: Fri, 7 Feb 2025 13:14:16 +0700 Subject: [PATCH 1/2] Added Function Calling (tools) --- src/Contracts/DeepseekClientContract.php | 9 ++++- src/DeepSeekClient.php | 45 ++++++++++++++++++++---- src/Enums/Queries/QueryRoles.php | 2 ++ src/Enums/Requests/QueryFlags.php | 1 + 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/Contracts/DeepseekClientContract.php b/src/Contracts/DeepseekClientContract.php index 7ea06b9..f0e290f 100644 --- a/src/Contracts/DeepseekClientContract.php +++ b/src/Contracts/DeepseekClientContract.php @@ -6,7 +6,14 @@ interface DeepseekClientContract { public static function build(string $apiKey): self; public function run(): string; - public function query(string $content, ?string $role = "user"): self; + public function query( + ?string $content = null, + ?string $role = "user", + ?string $toolCallId = null, + ?array $toolCalls = null, + ): self; public function withModel(?string $model = null): self; public function withStream(bool $stream = true): self; + public function setTemperature(float $temperature): self; + public function setTools(array $tools): self; } diff --git a/src/DeepSeekClient.php b/src/DeepSeekClient.php index 882a8ec..d73a933 100644 --- a/src/DeepSeekClient.php +++ b/src/DeepSeekClient.php @@ -47,6 +47,8 @@ class DeepSeekClient implements DeepseekClientContract protected float $temperature; + protected array $tools = []; + /** * response result contract * @var ResultContract @@ -74,6 +76,9 @@ public function run(): string QueryFlags::STREAM->value => $this->stream, QueryFlags::TEMPERATURE->value => $this->temperature, ]; + if (count($this->tools)) { + $requestData[QueryFlags::TOOLS->value] = $this->tools; + } // Clear queries after sending $this->queries = []; $this->setResult((new Resource($this->httpClient))->sendRequest($requestData)); @@ -102,13 +107,20 @@ public static function build(string $apiKey, ?string $baseUrl = null, ?int $time /** * Add a query to the accumulated queries list. * - * @param string $content + * @param string|null $content * @param string|null $role + * @param string|null $toolCallId + * @param array|null $toolCalls * @return self The current instance for method chaining. */ - public function query(string $content, ?string $role = null): self + public function query( + ?string $content = null, + ?string $role = null, + ?string $toolCallId = null, + ?array $toolCalls = null, + ): self { - $this->queries[] = $this->buildQuery($content, $role); + $this->queries[] = $this->buildQuery($content, $role, $toolCallId, $toolCalls); return $this; } @@ -142,12 +154,33 @@ public function setTemperature(float $temperature): self return $this; } - protected function buildQuery(string $content, ?string $role = null): array + public function setTools(array $tools): self + { + $this->tools = $tools; + return $this; + } + + protected function buildQuery( + ?string $content = null, + ?string $role = null, + ?string $toolCallId = null, + ?array $toolCalls = null, + ): array { - return [ + $query = [ 'role' => $role ?: QueryRoles::USER->value, - 'content' => $content ]; + if ($content !== null) { + $query['content'] = $content; + } + if ($toolCallId !== null) { + $query['tool_call_id'] = $toolCallId; + } + if ($toolCalls !== null) { + $query['tool_calls'] = $toolCalls; + } + + return $query; } /** diff --git a/src/Enums/Queries/QueryRoles.php b/src/Enums/Queries/QueryRoles.php index 4659084..a23925c 100644 --- a/src/Enums/Queries/QueryRoles.php +++ b/src/Enums/Queries/QueryRoles.php @@ -6,4 +6,6 @@ enum QueryRoles: string { case USER = 'user'; case SYSTEM = 'system'; + case ASSISTANT = 'assistant'; + case TOOL = 'tool'; } diff --git a/src/Enums/Requests/QueryFlags.php b/src/Enums/Requests/QueryFlags.php index 19c1cdd..0e2b49a 100644 --- a/src/Enums/Requests/QueryFlags.php +++ b/src/Enums/Requests/QueryFlags.php @@ -8,4 +8,5 @@ enum QueryFlags: string case MODEL = 'model'; case STREAM = 'stream'; case TEMPERATURE = 'temperature'; + case TOOLS = 'tools'; } From 13633989fe6b1571fe843f97d0875265357de3ae Mon Sep 17 00:00:00 2001 From: Aleksandr Date: Fri, 7 Feb 2025 13:41:36 +0700 Subject: [PATCH 2/2] Added example of using Function Calling usage example to README.md --- README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/README.md b/README.md index b63d2e0..2a0a3ec 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ - **Enterprise Ready**: PSR-18 compliant HTTP client integration - **Model Flexibility**: Support for multiple DeepSeek models (Coder, Chat, etc.) - **Streaming Ready**: Built-in support for real-time response handling +- **Function Calling**: Allows the model to call external tools to enhance its capabilities - **Framework Friendly**: Laravel & Symfony packages available --- @@ -93,6 +94,38 @@ $response = DeepSeekClient::build('your-api-key') echo 'API Response:'.$response; ``` +### Function Calling + +```php +use DeepSeek\DeepSeekClient; +use DeepSeek\Enums\Models; + +$response = DeepSeekClient::build('your-api-key') + ->withModel(Models::CHAT) + ->withTools([ + [ + 'type' => 'function', + 'function' => [ + 'name' => 'get_weather', + 'description' => 'Get weather of an location, the user shoud supply a location first', + 'parameters' => [ + 'type' => 'object', + 'properties' => [ + 'location' => [ + 'type' => 'string', + 'description' => 'The city and state, e.g. San Francisco, CA', + ], + ], + 'required' => ['location'], + ], + ], + ], + ]) + ->run(); + +echo 'API Response:'.$response; +``` + ### 🛠 Framework Integration ### [Laravel Deepseek Package](https://github.com/deepseek-php/deepseek-laravel)