定制 maximerenou/bing-ai 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

maximerenou/bing-ai

最新稳定版本:1.2

Composer 安装命令:

composer require maximerenou/bing-ai

包简介

Bing AI adapter

README 文档

README

New feature: send images in chat 🔥

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.

❗ Make sure you send a first message to Bing Chat before using your cookie (CAPTCHA bypass)

How to get this cookie?
  1. Navigate to bing.com
  2. Sign in using your Microsoft account
  3. Back on bing.com, go to Bing Chat page and send a message (CAPTCHA verification occurs sometimes)
  4. Then, right-click and select "Inspect" - the browser console appears
  5. Go to "Application" tab
  6. Select "Cookies" > "https://www.bing.com" in the sidebar
  7. Search for "_U" cookie
  8. 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.

🔥 Image analysis

You may attach an image to your message:

$prompt = new Prompt("How cute is this animal?");
$prompt->withImage('/path/to/panda.png');
//or: $prompt->withImage($raw_image_data, true);

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

Try it! Type $image at the end of your message with examples/chat.php.

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.

maximerenou/bing-ai 适用场景与选型建议

maximerenou/bing-ai 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.84k 次下载、GitHub Stars 达 42, 最近一次更新时间为 2023 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 maximerenou/bing-ai 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 maximerenou/bing-ai 我们能提供哪些服务?
定制开发 / 二次开发

基于 maximerenou/bing-ai 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.84k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 42
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 42
  • Watchers: 3
  • Forks: 13
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2023-02-15