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

Commit 9abc54f

Browse files
committed
fix objects handler; add support for throttling
1 parent b62b29f commit 9abc54f

File tree

3 files changed

+62
-8
lines changed

3 files changed

+62
-8
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Bing AI client
22

3-
> For now you need to have access to Bing Chat open beta. Or grab the cookie from someone who has access.
4-
53
This is an unofficial Composer package for using Bing AI technology.
64

75
It comes with no warranty of continuous stability.
@@ -50,6 +48,18 @@ $identifiers = $conversation->getIdentifiers();
5048
$conversation = $ai->createChatConversation($cookie, $identifiers, 1);
5149
```
5250

51+
#### Throttling
52+
53+
Bing is limiting messages count per conversations. You can monitor it by calling `getRemainingMessages()` after every interaction.
54+
55+
```php
56+
$remaining = $conversation->getRemainingMessages();
57+
58+
if ($remaining === 0) {
59+
// You reached the limit
60+
}
61+
```
62+
5363
#### Text generation
5464

5565
Note: to prevent answers like "I have already written \[...]", you can disable cache for your prompt with `withoutCache()`.
@@ -68,4 +78,4 @@ $conversation->ask($prompt->withoutCache(), ...)
6878
---------------------------------------
6979

7080
Using Bing AI API outside bing.com may violate Bing AI terms. Use it at your own risk.
71-
Bing is a trademark of Microsoft.
81+
Bing is a trademark of Microsoft.

examples/chat.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
\MaximeRenou\BingAI\Tools::$debug = false; // Set true for verbose
1515

16-
echo "Warning: Bing AI is currently limited to 5 questions per sessions." . PHP_EOL;
1716
echo 'Type "q" to quit' . PHP_EOL;
1817

1918
while (true) {
@@ -42,6 +41,15 @@
4241

4342
// Print final answer
4443
echo "- $text" . PHP_EOL;
44+
45+
$remaining = $conversation->getRemainingMessages();
46+
47+
if ($remaining != 0) {
48+
echo "[$remaining remaining messages]" . PHP_EOL;
49+
} else {
50+
echo "[Limit reached]" . PHP_EOL;
51+
break;
52+
}
4553
}
4654

4755
exit(0);

src/Chat/Conversation.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class Conversation
3636

3737
protected $current_messages;
3838

39+
protected $user_messages_count;
40+
41+
protected $max_messages_count;
42+
3943
//
4044

4145
public function __construct($cookie, $identifiers = null, $invocations = 0)
@@ -72,6 +76,14 @@ public function withLocation($latitude, $longitude, $radius = 1000)
7276
return $this;
7377
}
7478

79+
public function getRemainingMessages()
80+
{
81+
if (is_null($this->max_messages_count))
82+
return 1;
83+
84+
return $this->max_messages_count - $this->user_messages_count;
85+
}
86+
7587
public function createIdentifiers($cookie)
7688
{
7789
$request = curl_init();
@@ -253,8 +265,20 @@ public function handleObject($object, $callback = null)
253265
$messages = [];
254266

255267
foreach ($object['arguments'] as $argument) {
256-
foreach ($argument['messages'] as $messageData) {
257-
$messages[] = Message::fromData($messageData);
268+
if (isset($argument['messages']) && is_array($argument['messages'])) {
269+
foreach ($argument['messages'] as $messageData) {
270+
$messages[] = Message::fromData($messageData);
271+
}
272+
}
273+
274+
if (isset($argument['throttling']) && is_array($argument['throttling'])) {
275+
if (isset($argument['throttling']['maxNumUserMessagesInConversation'])) {
276+
$this->max_messages_count = $argument['throttling']['maxNumUserMessagesInConversation'];
277+
}
278+
279+
if (isset($argument['throttling']['numUserMessagesInConversation'])) {
280+
$this->user_messages_count = $argument['throttling']['numUserMessagesInConversation'];
281+
}
258282
}
259283
}
260284

@@ -278,8 +302,20 @@ public function handleObject($object, $callback = null)
278302
case 2: // Global result
279303
$this->current_messages = [];
280304

281-
foreach ($object['item']['messages'] as $messageData) {
282-
$this->current_messages[] = Message::fromData($messageData);
305+
if (isset($object['item']['messages']) && is_array($object['item']['messages'])) {
306+
foreach ($object['item']['messages'] as $messageData) {
307+
$this->current_messages[] = Message::fromData($messageData);
308+
}
309+
}
310+
311+
if (isset($object['item']['throttling']) && is_array($object['item']['throttling'])) {
312+
if (isset($object['item']['throttling']['maxNumUserMessagesInConversation'])) {
313+
$this->max_messages_count = $object['item']['throttling']['maxNumUserMessagesInConversation'];
314+
}
315+
316+
if (isset($object['item']['throttling']['numUserMessagesInConversation'])) {
317+
$this->user_messages_count = $object['item']['throttling']['numUserMessagesInConversation'];
318+
}
283319
}
284320
break;
285321
case 3: // Answer ended

0 commit comments

Comments
 (0)