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

Commit 6423306

Browse files
committed
feat: model support
1 parent a8f4193 commit 6423306

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ $conversation = $ai->resumeChatConversation($identifiers);
6464

6565
</details>
6666

67+
<details>
68+
<summary>Use another model</summary>
69+
70+
You can use a specific model:
71+
72+
```php
73+
$conversation = $ai->createConversation("bigcode/starcoder");
74+
```
75+
76+
Default is OpenAssistant.
77+
78+
</details>
79+
6780
<details>
6881
<summary>Generate a conversation's summary</summary>
6982

@@ -110,7 +123,7 @@ The code throws exceptions when it receives an error from HuggingChat. You can t
110123
<details>
111124
<summary>Answers are sometimes malformed (or dumb)</summary>
112125

113-
That's what OpenAssistant's LLaMA model used by HuggingChat generates...
126+
Answers quality depends on the model you're using.
114127

115128
</details>
116129

src/Client.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
class Client
66
{
7-
public function createConversation($identifiers = null)
7+
public function createConversation($model = null)
88
{
9-
return $this->resumeConversation($identifiers);
9+
return new Conversation(null, $model);
1010
}
1111

1212
public function resumeConversation($identifiers)

src/Conversation.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class Conversation
99
{
1010
const END_CHAR = '<|endoftext|>';
11+
const DEFAULT_MODEL = 'OpenAssistant/oasst-sft-6-llama-30b-xor';
1112

1213
// Conversation IDs
1314
public $id;
@@ -17,13 +18,17 @@ class Conversation
1718
protected $current_started;
1819
protected $current_text;
1920

20-
public function __construct($identifiers = null)
21+
public function __construct($identifiers = null, $model = null)
2122
{
23+
if (is_null($model)) {
24+
$model = self::DEFAULT_MODEL;
25+
}
26+
2227
if (is_array($identifiers) && ! empty($identifiers['cookie']))
2328
$this->cookie = $identifiers['cookie'];
2429

2530
if (! is_array($identifiers))
26-
$identifiers = $this->initConversation();
31+
$identifiers = $this->initConversation($model);
2732

2833
$this->id = $identifiers['id'];
2934
$this->cookie = $identifiers['cookie'];
@@ -37,20 +42,26 @@ public function getIdentifiers()
3742
];
3843
}
3944

40-
public function initConversation()
45+
public function initConversation($model)
4146
{
4247
$headers = [
4348
'method: POST',
44-
'accept: application/json',
45-
"referer: https://huggingface.co/chat",
49+
'accept: */*',
50+
'accept-language: en-US,en;q=0.9',
51+
'accept-encoding: gzip, deflate, br',
52+
"referer: https://huggingface.co/chat/",
53+
"origin: https://huggingface.co",
4654
'content-type: application/json',
55+
'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',
4756
];
4857

4958
if (! empty($this->cookie)) {
5059
$headers[] = "cookie: hf-chat={$this->cookie}";
5160
}
5261

53-
list($data, $request, $url, $cookies) = Tools::request("https://huggingface.co/chat/conversation", $headers, '', true);
62+
$data = json_encode(['model' => $model]);
63+
64+
list($data, $request, $url, $cookies) = Tools::request("https://huggingface.co/chat/conversation", $headers, $data, true);
5465
$data = json_decode($data, true);
5566

5667
if (! empty($cookies['hf-chat'])) {

0 commit comments

Comments
 (0)