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

Commit 3da93fa

Browse files
committed
fix: handle mandatory ethics modal
1 parent 0d0b3cc commit 3da93fa

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
This is an unofficial PHP client for **HuggingChat** (OpenAssistant's LLaMA model).
1111

12+
> HuggingChat API [is evolving fast](https://huggingface.co/spaces/huggingchat/chat-ui/commits/main) with recurring breaking changes. I try to keep up with it, but it may not always work as expected. Feel free to open an issue if you encounter any problem.
13+
1214
## Installation
1315

1416
composer require maximerenou/hugging-chat
@@ -92,14 +94,14 @@ $summary = $conversation->getSummary();
9294
</details>
9395

9496
<details>
95-
<summary>Turn off data sharing</summary>
97+
<summary>Turn on/off data sharing</summary>
9698

9799
HuggingChat share your conversations to improve the model. You can turn on/off data sharing:
98100

99101
```php
100-
$conversation->enableSharing(); // on (default)
102+
$conversation->enableSharing(); // on
101103

102-
$conversation->disableSharing(); // off
104+
$conversation->disableSharing(); // off (module default)
103105
```
104106

105107
</details>

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()->disableSharing();
8+
$conversation = $ai->createConversation();
99

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

src/Conversation.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,43 @@ public function initConversation($model)
4646
{
4747
$headers = [
4848
'method: POST',
49-
'accept: */*',
49+
'accept: application/json',
5050
'accept-language: en-US,en;q=0.9',
5151
'accept-encoding: gzip, deflate, br',
5252
"referer: https://huggingface.co/chat/",
5353
"origin: https://huggingface.co",
54-
'content-type: application/json',
5554
'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
5655
];
5756

5857
if (! empty($this->cookie)) {
5958
$headers[] = "cookie: hf-chat={$this->cookie}";
59+
} else {
60+
list($data, $request, $url, $cookies) = Tools::request("https://huggingface.co/chat", [], '', true);
61+
62+
if (! empty($cookies['hf-chat'])) {
63+
$this->cookie = $cookies['hf-chat'];
64+
$headers[] = "cookie: hf-chat={$this->cookie}";
65+
}
66+
67+
// Ethics modal
68+
$data = [
69+
'ethicsModalAccepted' => true,
70+
'ethicsModalAcceptedAt' => null,
71+
'shareConversationsWithModelAuthors' => false,
72+
'activeModel' => $model
73+
];
74+
75+
Tools::request("https://huggingface.co/chat/settings", array_merge($headers, [
76+
'x-sveltekit-action: true',
77+
'content-type: multipart/form-data'
78+
]), $data);
6079
}
6180

81+
// Init conversation
6282
$data = json_encode(['model' => $model]);
63-
64-
list($data, $request, $url, $cookies) = Tools::request("https://huggingface.co/chat/conversation", $headers, $data, true);
83+
$data = Tools::request("https://huggingface.co/chat/conversation", array_merge($headers, ['content-type: application/json']), $data);
6584
$data = json_decode($data, true);
6685

67-
if (! empty($cookies['hf-chat'])) {
68-
$this->cookie = $cookies['hf-chat'];
69-
}
70-
7186
if (! is_array($data) || empty($data['conversationId']))
7287
throw new \Exception("Failed to init conversation");
7388

0 commit comments

Comments
 (0)