Skip to content
This repository was archived by the owner on Mar 21, 2024. It is now read-only.

Commit 1cb870d

Browse files
committed
feat: data sharing config
1 parent f1bae75 commit 1cb870d

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ $summary = $conversation->getSummary();
7878

7979
</details>
8080

81+
<details>
82+
<summary>Turn off data sharing</summary>
83+
84+
HuggingChat share your conversations to improve the model. You can turn on/off data sharing:
85+
86+
```php
87+
$conversation->enableSharing(); // on (default)
88+
89+
$conversation->disableSharing(); // off
90+
```
91+
92+
</details>
93+
8194
<details>
8295
<summary>Handle HuggingChat errors</summary>
8396

examples/chat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
$ai = new \MaximeRenou\HuggingChat\Client();
77

8-
$conversation = $ai->createConversation();
8+
$conversation = $ai->createConversation()->disableSharing();
99

1010
echo 'Type "q" to quit' . PHP_EOL;
1111

src/Conversation.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function ask(Prompt $message, $callback = null)
8686
'top_k' => $message->top_k,
8787
'top_p' => $message->top_p,
8888
'truncate' => $message->truncate,
89-
'watermark' => $message->watermark,
89+
'watermark' => $message->watermark,
9090
],
9191
'stream' => true
9292
];
@@ -102,7 +102,7 @@ public function ask(Prompt $message, $callback = null)
102102
CURLOPT_POST => 1,
103103
CURLOPT_POSTFIELDS => json_encode($data)
104104
]);
105-
105+
106106
$es->onMessage(function (Event $event) use ($es, &$callback) {
107107
if ($es === 4) {
108108
$es->abort();
@@ -130,7 +130,7 @@ public function ask(Prompt $message, $callback = null)
130130

131131
$callback($this->current_text, $tokens);
132132
});
133-
133+
134134
@$es->connect();
135135

136136
return $this->current_text;
@@ -155,7 +155,7 @@ protected function handlePacket($raw)
155155
}
156156

157157
$text = $data['token']['special'] ? $data['generated_text'] : $data['token']['text'];
158-
158+
159159
if (($pos = strpos($text, self::END_CHAR)) !== false) {
160160
$text = substr($text, 0, $pos);
161161
}
@@ -184,4 +184,36 @@ public function getSummary()
184184

185185
return trim($data['title'], '"');
186186
}
187+
188+
public function enableSharing()
189+
{
190+
return $this->withSettings([
191+
'shareConversationsWithModelAuthors' => true
192+
]);
193+
}
194+
195+
public function disableSharing()
196+
{
197+
return $this->withSettings([
198+
'shareConversationsWithModelAuthors' => false
199+
]);
200+
}
201+
202+
public function withSettings($settings)
203+
{
204+
$headers = [
205+
'method: PATCH',
206+
'accept: application/json',
207+
"origin: https://huggingface.co",
208+
"referer: https://huggingface.co/chat/privacy",
209+
'content-type: application/json',
210+
"cookie: hf-chat={$this->cookie}"
211+
];
212+
213+
$data = json_encode($settings);
214+
215+
Tools::request("https://huggingface.co/chat/settings", $headers, $data);
216+
217+
return $this;
218+
}
187219
}

0 commit comments

Comments
 (0)