maximerenou/bing-ai

此包已被废弃,不再维护。未建议替代包。
此包的最新版本(1.2)没有可用的许可信息。

Bing AI适配器

1.2 2023-08-21 16:23 UTC

This package is auto-updated.

Last update: 2024-04-21 17:54:04 UTC


README

新功能:在聊天中发送图片 🔥

Bing + PHP

Bing AI客户端

License Latest Stable Version PHP version cURL extension required

这是一个用于Bing AI的非官方PHP客户端,包括Chat (GPT-4)Image Creator (DALL-E)

安装

composer require maximerenou/bing-ai

演示

此演示程序同时使用Chat和Image Creator。

Demo

源代码:examples/multi.php

用法

首先,您需要在bing.com上登录并获取您的_Ucookie。

❗ 在使用cookie之前,请确保您已向Bing Chat发送了一条第一条消息(绕过CAPTCHA)

如何获取这个cookie?
  1. 导航到bing.com
  2. 使用您的Microsoft账户登录
  3. 回到bing.com,进入Bing Chat页面并发送消息(有时需要通过CAPTCHA验证)
  4. 然后,右键单击并选择“检查” - 浏览器控制台出现
  5. 转到“应用”标签
  6. 在侧边栏中选择“Cookies” > "https://www.bing.com"
  7. 搜索"_U"cookie
  8. 复制其内容
如何检查我的cookie是否正常工作?
use MaximeRenou\BingAI\BingAI;

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

Chat AI

演示:克隆此仓库,编辑并运行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包含与Bing AI交换的所有“消息”。它可以包含文本(提示或答案)、信号、建议、图像生成请求等。查看Message.php以了解其格式。

🔥 图像分析

您可以在消息中附加图像

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

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

试试!examples/chat.php中,在消息末尾输入$image

实时/渐进式回答

您可以将函数作为第二个参数传递以获取实时进度

// $text - Incomplete text version
// $cards - Incomplete messages fleet
list($final_text, $final_cards) = $conversation->ask($prompt, function ($text, $cards) {
    echo $text;
});
区域和位置偏好
$conversation = $ai->createChatConversation()
    ->withLocation($latitude, $longitude, $radius) // Optional
    ->withPreferences('fr-FR', 'fr-FR', 'FR'); // Optional
语气选择
use MaximeRenou\BingAI\Chat\Tone;

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

// Choices:
// Tone::Balanced (default)
// Tone::Creative
// Tone::Precise
恢复对话

如果您想恢复之前的对话,您可以检索其标识符

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

// ...
// Resume conversation with $identifiers parameter, and number of previous questions asked
$conversation = $ai->resumeChatConversation($identifiers, 1);
文本生成
$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(), ...)

为了防止出现“我已经写过了[...]”之类的回答,您可以使用withoutCache()禁用您的提示的缓存。

处理限制和踢出

Bing限制了每个对话的消息数量。您可以在每次交互后通过调用getRemainingMessages()来监控它。

$remaining = $conversation->getRemainingMessages();

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

每次交互后,您还应检查您是否被踢出了对话

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

您可以将这两个检查结合起来使用

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

Image Creator

演示:克隆此仓库,编辑并运行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();
}

消耗完所有“加成”后,图像生成可能会变慢。请查看以下部分以了解您的剩余加成。

检查剩余加成
$creator = $ai->getImageCreator();

$remaining_boosts = $creator->getRemainingBoosts();
异步生成您可以在调用`createImages()`后退出,并稍后使用其ID检查生成情况
$prompt = "A 3D teddy bear";
$creator = $ai->createImages($prompt);
$generation_id = $creator->getGenerationId();

// ...

$creator = $ai->getImageCreator();
$creator->resume($generation_id, $prompt);
手动等待您可以选择自己循环而不是调用`$creator->wait();`
do {
    sleep(1);
} while ($creator->isGenerating());

免责声明

在bing.com之外使用Bing AI可能违反Bing条款。自行承担风险。Bing是Microsoft的商标。