Skip to content

Commit acf3271

Browse files
authored
Merge pull request #52 from deepseek-php/feat-support-response-format-option
feat / support response format option
2 parents 4aedcae + 0dc68a2 commit acf3271

File tree

7 files changed

+125
-8
lines changed

7 files changed

+125
-8
lines changed

README-AR.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@
2222

2323
[الإنجليزية](README.md) | [الصينية](README-CN.md)
2424

25-
</p>
26-
2725
## فهرس المحتويات
2826
- [✨ المميزات](#-المميزات)
2927
- [📦 التثبيت](#-التثبيت)
3028
- [🚀 البداية السريعة](#-البداية-السريعة)
3129
- [الاستخدام الأساسي](#الاستخدام-الأساسي)
3230
- [التكوين المتقدم](#التكوين-المتقدم)
31+
- [تحذير هام عند استخدام وضع JSON](#-متطلب-وضع-json-في-deepseek)
3332
- [الاستخدام مع عميل HTTP من Symfony](#الاستخدام-مع-عميل-http-من-symfony)
3433
- [الحصول على قائمة النماذج](#الحصول-على-قائمة-النماذج)
3534
- [استدعاء الدوال](#استدعاء-الدوال)
@@ -101,12 +100,48 @@ $response = $client
101100
->withStream()
102101
->withTemperature(1.2)
103102
->setMaxTokens(8192)
103+
->setResponseFormat('text')
104104
->query('Explain quantum computing in simple terms')
105105
->run();
106106

107107
echo 'API Response:'.$response;
108108
```
109109

110+
111+
## ⚠️ متطلب وضع JSON في DeepSeek
112+
113+
عند استخدام:
114+
115+
```php
116+
->setResponseFormat('json_object')
117+
```
118+
119+
يجب أن يحتوي الـ برومبت على **كلمة "json"** بشكل واضح.
120+
121+
وإلا سيتم رفض الطلب من قبل وترجع رسالة الخطأ التالية:
122+
123+
> `"Prompt must contain the word 'json' in some form to use 'response_format' of type 'json_object'"`
124+
125+
---
126+
127+
### 🚫 استخدام غير صحيح
128+
129+
```php
130+
->setResponseFormat('json_object')
131+
->query('اشرح الحوسبة الكمومية بطريقة مبسطة')
132+
```
133+
134+
### ✅ استخدام صحيح
135+
136+
```php
137+
->setResponseFormat('json_object')
138+
->query('أجب بصيغة JSON صحيحة. اشرح الحوسبة الكمومية بطريقة مبسطة.')
139+
```
140+
141+
> **نصيحة**: للحصول على أفضل النتائج، قم أيضًا بإعطاء مثال على صيغة JSON في الرسالة.
142+
143+
---
144+
110145
### الاستخدام مع عميل HTTP من Symfony
111146
الحزمة مبنية مسبقاً مع `symfony Http client`، فإذا كنت بحاجة إلى استخدامها مع عميل HTTP الخاص بـ Symfony، فيمكن تحقيق ذلك بسهولة عن طريق تمرير `clientType:'symfony'` إلى دالة `build`.
112147

README-CN.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@
2222

2323
[EN](README.md) | [AR](README-AR.md)
2424

25-
</p>
26-
2725
## 目录
2826
- [✨ 特性](#-特性)
2927
- [📦 安装](#-安装)
3028
- [🚀 快速入门](#-快速入门)
3129
- [基本用法](#基本用法)
3230
- [高级配置](#高级配置)
31+
- [使用 JSON 模式的重要警告](#-deepseek-json-模式使用要求)
3332
- [使用 Symfony HttpClient](#使用-symfony-httpclient)
3433
- [获取模型列表](#获取模型列表)
3534
- [函数调用](#函数调用)
@@ -100,12 +99,47 @@ $response = $client
10099
->withStream()
101100
->withTemperature(1.2)
102101
->setMaxTokens(8192)
102+
->setResponseFormat('text')
103103
->query('Explain quantum computing in simple terms')
104104
->run();
105105

106106
echo 'API Response:'.$response;
107107
```
108108

109+
110+
## ⚠️ DeepSeek JSON 模式使用要求
111+
112+
当使用:
113+
114+
```php
115+
->setResponseFormat('json_object')
116+
```
117+
118+
你的提示语(prompt)**必须包含 "json" 这个词**,否则 API 会返回以下错误:
119+
120+
> `"Prompt must contain the word 'json' in some form to use 'response_format' of type 'json_object'"`
121+
122+
---
123+
124+
### 🚫 错误示例
125+
126+
```php
127+
->setResponseFormat('json_object')
128+
->query('用简单的语言解释量子计算')
129+
```
130+
131+
### ✅ 正确示例
132+
133+
```php
134+
->setResponseFormat('json_object')
135+
->query('请以有效的 JSON 格式回答,并用简单语言解释量子计算。')
136+
```
137+
138+
> **建议**:为了获得更好的结果,最好也在提示中提供一个 JSON 示例,并强调 “只返回 JSON”。
139+
140+
141+
---
142+
109143
### Use with Symfony HttpClient
110144
the package already built with `symfony Http client`, if you need to use package with `symfony` Http Client , it is easy to achieve that, just pass `clientType:'symfony'` with `build` function.
111145

README.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
<a href="https://github.com/deepseek-php/deepseek-php-client/stargazers">
1919
<img src="https://img.shields.io/github/stars/deepseek-php/deepseek-php-client?style=social" alt="GitHub Stars">
2020
</a>
21-
22-
[AR](README-AR.md) | [CN](README-CN.md)
21+
</p>
2322

24-
</p>
23+
[AR](README-AR.md) | [CN](README-CN.md)
2524

2625
## Table of Contents
2726
- [✨ Features](#-features)
2827
- [📦 Installation](#-installation)
2928
- [🚀 Quick Start](#-quick-start)
3029
- [Basic Usage](#basic-usage)
3130
- [Advanced Configuration](#advanced-configuration)
31+
- [important warning with json mode](#-deepseek-json-mode-requirement)
3232
- [Use with Symfony HttpClient](#use-with-symfony-httpclient)
3333
- [Get Models List](#get-models-list)
3434
- [Function Calling](#function-calling)
@@ -100,12 +100,47 @@ $response = $client
100100
->withStream()
101101
->setTemperature(1.2)
102102
->setMaxTokens(8192)
103+
->setResponseFormat('text') // or "json_object" with careful .
103104
->query('Explain quantum computing in simple terms')
104105
->run();
105106

106107
echo 'API Response:'.$response;
107108
```
108109

110+
## ⚠️ DeepSeek JSON Mode Requirement
111+
112+
When using:
113+
114+
```php
115+
->setResponseFormat('json_object')
116+
```
117+
118+
Your prompt **must contain the word `"json"`** in some form. Otherwise, the API will reject the request with the following error:
119+
120+
> `"Prompt must contain the word 'json' in some form to use 'response_format' of type 'json_object'"`
121+
122+
---
123+
124+
### 🚫 Incorrect Usage
125+
126+
```php
127+
->setResponseFormat('json_object')
128+
->query('Explain quantum computing in simple terms')
129+
```
130+
131+
### ✅ Correct Usage
132+
133+
```php
134+
->setResponseFormat('json_object')
135+
->query('Respond in valid JSON format. Explain quantum computing in simple terms.')
136+
```
137+
138+
> **Tip**: For best results, also provide a JSON example or explicitly say:
139+
> *"Respond only in valid JSON."*
140+
141+
142+
---
143+
109144
### Use with Symfony HttpClient
110145
the package already built with `symfony Http client`, if you need to use package with `symfony` Http Client , it is easy to achieve that, just pass `clientType:'symfony'` with `build` function.
111146

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"role": "creator"
5050
}
5151
],
52-
"version": "2.0.5",
52+
"version": "2.0.6",
5353
"require": {
5454
"php": "^8.1.0",
5555
"nyholm/psr7": "^1.8",

src/DeepSeekClient.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class DeepSeekClient implements ClientContract
5050

5151
protected float $temperature;
5252
protected int $maxTokens;
53+
protected string $responseFormatType;
5354

5455
/**
5556
* response result contract
@@ -81,6 +82,7 @@ public function __construct(ClientInterface $httpClient)
8182
$this->endpointSuffixes = EndpointSuffixes::CHAT->value;
8283
$this->temperature = (float) TemperatureValues::GENERAL_CONVERSATION->value;
8384
$this->maxTokens = (int) TemperatureValues::MAX_TOKENS->value;
85+
$this->responseFormatType = TemperatureValues::RESPONSE_FORMAT_TYPE->value;
8486
$this->tools = null;
8587
}
8688

@@ -93,6 +95,9 @@ public function run(): string
9395
QueryFlags::TEMPERATURE->value => $this->temperature,
9496
QueryFlags::MAX_TOKENS->value => $this->maxTokens,
9597
QueryFlags::TOOLS->value => $this->tools,
98+
QueryFlags::RESPONSE_FORMAT->value => [
99+
'type' => $this->responseFormatType
100+
],
96101
];
97102

98103
$this->setResult((new Resource($this->httpClient, $this->endpointSuffixes))->sendRequest($requestData, $this->requestMethod));
@@ -192,6 +197,12 @@ public function setMaxTokens(int $maxTokens): self
192197
return $this;
193198
}
194199

200+
public function setResponseFormat(string $type): self
201+
{
202+
$this->responseFormatType = $type;
203+
return $this;
204+
}
205+
195206
public function buildQuery(string $content, ?string $role = null): array
196207
{
197208
return [

src/Enums/Configs/TemperatureValues.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ enum TemperatureValues: string
1313
case CREATIVE_WRITING = "1.5";
1414
case POETRY = "1.6";
1515
case MAX_TOKENS = "4096";
16+
case RESPONSE_FORMAT_TYPE = "text";
1617
}

src/Enums/Requests/QueryFlags.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ enum QueryFlags: string
1010
case TEMPERATURE = 'temperature';
1111
case MAX_TOKENS = 'max_tokens';
1212
case TOOLS = 'tools';
13+
case RESPONSE_FORMAT = 'response_format';
1314
}

0 commit comments

Comments
 (0)