Code Monkey home page Code Monkey logo

php-bing-ai's Introduction

Bing + PHP

Bing AI client

License Latest Stable Version PHP version cURL extension required

This is an unofficial PHP client for Bing AI, including Chat (GPT-4) and Image Creator (DALL-E).

Installation

composer require maximerenou/bing-ai

Demo

This demo program uses both Chat and Image Creator:

Demo

Source: examples/multi.php.

Usage

First, you need to sign in on bing.com and get your _U cookie.

How to get this cookie?
  1. Navigate to bing.com
  2. Sign in using your Microsoft account
  3. Back on bing.com, right-click and select "Inspect" - the browser console appears
  4. Go to "Application" tab
  5. Select "Cookies" > "https://www.bing.com" in the sidebar
  6. Search for "_U" cookie
  7. Copy its content
How to check if my cookie is working properly?
use MaximeRenou\BingAI\BingAI;

// $cookie - your "_U" cookie from bing.com
$ai = new BingAI($cookie);
$valid = $ai->checkCookie();

Chat AI

Demo: clone this repo, edit and run examples/chat.php.

use MaximeRenou\BingAI\BingAI;
use MaximeRenou\BingAI\Chat\Prompt;

// $cookie - your "_U" cookie from bing.com
$ai = new BingAI($cookie);

$conversation = $ai->createChatConversation();

// $text - Text-only version of Bing's answer
// $cards - Message objects array
list($text, $cards) = $conversation->ask(new Prompt("Hello World"));

$cards contains all "messages" exchanged with Bing AI. It can be text (prompt or answer), signals, suggestions, image generation requests, etc. Check Message.php to learn more about its format.

Real-time / progressive answer

You may pass a function as second argument to get real-time progression:

// $text - Incomplete text version
// $cards - Incomplete messages fleet
list($final_text, $final_cards) = $conversation->ask($prompt, function ($text, $cards) {
    echo $text;
});
Locale and location preferences
$conversation = $ai->createChatConversation()
    ->withLocation($latitude, $longitude, $radius) // Optional
    ->withPreferences('fr-FR', 'fr-FR', 'FR'); // Optional
Tone choice
use MaximeRenou\BingAI\Chat\Tone;

$conversation = $ai->createChatConversation()
    ->withTone(Tone::Creative); // Optional

// Choices:
// Tone::Balanced (default)
// Tone::Creative
// Tone::Precise
Resume a conversation

If you want to resume a previous conversation, you can retrieve its identifiers:

// Get current identifiers
$identifiers = $conversation->getIdentifiers();

// ...
// Resume conversation with $identifiers parameter, and number of previous questions asked
$conversation = $ai->resumeChatConversation($identifiers, 1);
Text generation
$subject = "Internet memes";
$tone = 'funny';
$type = 'blog post';
$length = 'short';

$prompt = new Prompt("Please write a *$length* *$type* in a *$tone* style about `$subject`. Please wrap the $type in a markdown codeblock.");

$conversation->ask($prompt->withoutCache(), ...)

To prevent answers like "I have already written [...]", you can disable cache for your prompt with withoutCache().

Handle throttling and kicks

Bing is limiting messages count per conversations. You can monitor it by calling getRemainingMessages() after every interaction.

$remaining = $conversation->getRemainingMessages();

if ($remaining === 0) {
    // You reached the limit
}

After every interaction, you should also check if you have been kicked from the conversation:

if ($conversation->kicked()) {
    // You have been kicked, you should start a new conversation
}

You may combine both checks with:

if ($conversation->ended()) {
    // You reached the limit or have been kicked
}

Image Creator

Demo: clone this repo, edit and run examples/images.php.

use MaximeRenou\BingAI\BingAI;

// $cookie - your "_U" cookie from bing.com
$ai = new BingAI($cookie);

$creator = $ai->createImages("A 3D teddy bear");

$creator->wait();

// Finally, get images URLs
if (! $creator->hasFailed()) {
    $images = $creator->getImages();
}

Image generation can become slower after consuming all of your "boosts". Check the section below to stay aware of your remaining boosts.

Check remaining boosts
$creator = $ai->getImageCreator();

$remaining_boosts = $creator->getRemainingBoosts();
Asynchronous generation You may quit after calling `createImages()` and check generation later using its ID:
$prompt = "A 3D teddy bear";
$creator = $ai->createImages($prompt);
$generation_id = $creator->getGenerationId();

// ...

$creator = $ai->getImageCreator();
$creator->resume($generation_id, $prompt);
Manually wait Instead of calling `$creator->wait();` you can loop by yourself:
do {
    sleep(1);
} while ($creator->isGenerating());

Disclaimer

Using Bing AI outside bing.com may violate Bing terms. Use it at your own risk. Bing is a trademark of Microsoft.

php-bing-ai's People

Contributors

maximerenou avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.