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

Commit e6e2a1f

Browse files
committed
feat: image creator boosts
1 parent 56be97f commit e6e2a1f

File tree

5 files changed

+73
-23
lines changed

5 files changed

+73
-23
lines changed

README.md

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ Edit and run `examples/chat.php` to test it.
4040
use MaximeRenou\BingAI\BingAI;
4141
use MaximeRenou\BingAI\Chat\Prompt;
4242

43-
$ai = new BingAI;
4443
// $cookie - your "_U" cookie from bing.com
45-
$conversation = $ai->createChatConversation($cookie);
44+
$ai = new BingAI($cookie);
45+
46+
$conversation = $ai->createChatConversation();
4647

4748
// $text - Text-only version of Bing's answer
4849
// $messages - Message objects array
@@ -70,7 +71,7 @@ list($final_text, $final_messages) = $conversation->ask($prompt, function ($text
7071
<summary>Locale and location preferences</summary>
7172

7273
```php
73-
$conversation = $ai->createChatConversation($cookie)
74+
$conversation = $ai->createChatConversation()
7475
->withLocation($latitude, $longitude, $radius) // Optional
7576
->withPreferences('fr-FR', 'fr-FR', 'FR'); // Optional
7677
```
@@ -87,8 +88,8 @@ If you want to resume a previous conversation, you can retrieve its identifiers:
8788
$identifiers = $conversation->getIdentifiers();
8889

8990
// ...
90-
// Resume conversation with $identifiers parameter, and number of previous questions
91-
$conversation = $ai->createChatConversation($cookie, $identifiers, 1);
91+
// Resume conversation with $identifiers parameter, and number of previous questions asked
92+
$conversation = $ai->resumeChatConversation($identifiers, 1);
9293
```
9394

9495
</details>
@@ -132,14 +133,13 @@ if ($remaining === 0) {
132133

133134
Edit and run `examples/images.php` to test it.
134135

135-
> Image generation becomes slower after using it a few times. Bing limits the number of images generated fast per user.
136-
137136
```php
138137
use MaximeRenou\BingAI\BingAI;
139138

140-
$ai = new BingAI;
141139
// $cookie - your "_U" cookie from bing.com
142-
$creator = $ai->createImages($cookie, "A 3D teddy bear");
140+
$ai = new BingAI($cookie);
141+
142+
$creator = $ai->createImages("A 3D teddy bear");
143143

144144
$creator->wait();
145145

@@ -149,20 +149,34 @@ if (! $creator->hasFailed()) {
149149
}
150150
```
151151

152+
> Image generation can become slower after consuming all of your "boosts". Check the section below to stay aware of your remaining boosts.
153+
154+
<details>
155+
<summary>Check remaining boosts</summary>
156+
157+
```php
158+
$creator = $ai->getImageCreator();
159+
160+
$remaining_boosts = $creator->getRemainingBoosts();
161+
```
162+
163+
</details>
164+
152165
<details>
153166
<summary>Asynchronous generation</summary>
154167
You may quit after calling `createImages()` and check generation later using its ID:
155168

156169
```php
157170
$prompt = "A 3D teddy bear";
158-
$creator = $ai->createImages($cookie, $prompt);
171+
$creator = $ai->createImages($prompt);
159172
$generation_id = $creator->getGenerationId();
160173

161174
// ...
162175

163-
$creator = new ImageCreator($cookie);
176+
$creator = $ai->getImageCreator();
164177
$creator->resume($generation_id, $prompt);
165178
```
179+
166180
</details>
167181

168182
<details>
@@ -174,6 +188,7 @@ do {
174188
sleep(1);
175189
} while ($creator->isGenerating());
176190
```
191+
177192
</details>
178193

179194
---------------------------------------

examples/chat.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66

77
date_default_timezone_set("Europe/Paris");
88

9-
$ai = new \MaximeRenou\BingAI\BingAI();
9+
$ai = new \MaximeRenou\BingAI\BingAI($cookie);
1010

11-
$conversation = $ai->createChatConversation($cookie)
12-
->withPreferences('fr-FR', 'fr-FR', 'FR');
11+
$conversation = $ai->createChatConversation();
1312

1413
\MaximeRenou\BingAI\Tools::$debug = false; // Set true for verbose
1514

examples/images.php

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

55
require __DIR__ . '/../vendor/autoload.php';
66

7-
$ai = new \MaximeRenou\BingAI\BingAI();
7+
$ai = new \MaximeRenou\BingAI\BingAI($cookie);
88

99
\MaximeRenou\BingAI\Tools::$debug = false; // Set true for verbose
1010

11+
$boosts = $ai->getImageCreator()->getRemainingBoosts();
12+
13+
echo "You have $boosts remaining boosts." . PHP_EOL;
14+
1115
echo 'Type "q" to quit' . PHP_EOL;
1216

1317
echo PHP_EOL . "> ";
@@ -16,7 +20,7 @@
1620
if ($text == 'q')
1721
exit(0);
1822

19-
$creator = $ai->createImages($cookie, $text);
23+
$creator = $ai->createImages($text);
2024

2125
echo 'Generating...' . PHP_EOL;
2226

src/BingAI.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,30 @@
77

88
class BingAI
99
{
10-
public function __construct()
10+
protected $cookie;
11+
12+
public function __construct($cookie)
13+
{
14+
$this->cookie = $cookie;
15+
}
16+
17+
public function createChatConversation($data = null)
1118
{
12-
//
19+
return new Conversation($this->cookie, $data, 0);
1320
}
1421

15-
public function createChatConversation($cookie, $data = null, $invocations = 0)
22+
public function resumeChatConversation($data = null, $invocations = 1)
1623
{
17-
return new Conversation($cookie, $data, $invocations);
24+
return new Conversation($this->cookie, $data, $invocations);
1825
}
1926

20-
public function createImages($cookie, $prompt)
27+
public function getImageCreator()
2128
{
22-
$creator = new ImageCreator($cookie);
29+
return new ImageCreator($this->cookie);
30+
}
2331

24-
return $creator->create($prompt);
32+
public function createImages($prompt)
33+
{
34+
return $this->getImageCreator()->create($prompt);
2535
}
2636
}

src/Images/ImageCreator.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,28 @@ public function isGenerating()
134134
return $this->generating;
135135
}
136136

137+
public function getRemainingBoosts()
138+
{
139+
$request = curl_init();
140+
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
141+
curl_setopt($request, CURLOPT_FOLLOWLOCATION, true);
142+
curl_setopt($request, CURLOPT_URL, "https://www.bing.com/images/create");
143+
curl_setopt($request, CURLOPT_HTTPHEADER, [
144+
'cookie: _U=' . $this->cookie,
145+
'method: GET',
146+
'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
147+
"accept-language: en;q=0.9",
148+
]);
149+
150+
$data = curl_exec($request);
151+
curl_close($request);
152+
153+
if (! is_string($data) || ! preg_match('/<div id="token_bal" aria-label="[^"]+">([0-9]+)<\/div>/', $data, $matches))
154+
return 0;
155+
156+
return intval($matches[1]);
157+
}
158+
137159
public function wait()
138160
{
139161
while (! $this->hasFailed()) {

0 commit comments

Comments
 (0)