Coder Social home page Coder Social logo

bingchat's Introduction

Socialify Banner

BingChat

.NET API wrapper for Microsoft's new AI-powered Bing Chat.

.NET Version NuGet Version License

Warning This library is unofficial and heavily depends on reverse-engineering. Use at your own risk.

Quick Start

Note Since BingChat's public release, a valid cookie is no longer required. You can use this package without any authentication.

Install this package via NuGet package manager or dotnet CLI:

dotnet add package --prerelease BingChat

Then,

using BingChat;

// Construct the chat client
var client = new BingChatClient(new BingChatClientOptions
{
    // Tone used for conversation
    Tone = BingChatTone.Balanced,
});

var message = "Do you like cats?";
var answer = await client.AskAsync(message);

Console.WriteLine($"Answer: {answer}");

The code above sends a message to the chat AI, and gets the answer from it.

This method creates a one-shot conversation and discards it when completed. If you want to continue chatting in the same context (just like the behavior in the web interface), you need to create a shared conversation:

// Create a conversation, so we can continue chatting in the same context.
var conversation = await client.CreateConversation();

var firstMessage = "Do you like cats?";
var answer = await conversation.AskAsync(firstMessage);
Console.WriteLine($"First answer: {answer}");

await Task.Delay(TimeSpan.FromSeconds(5));

var secondMessage = "What did I just say?";
answer = await conversation.AskAsync(secondMessage);
Console.WriteLine($"Second answer: {answer}");

Interactive Command Line Tool

We also developed an amazing command line tool for you! See the preview below:

CLI Preview

You can install the CLI by executing the following command simply:

dotnet tool install -g --prerelease dotnet-bingchat

Then start the CLI:

dotnet bingchat

Roadmap

  • Implement a command line tool to interact with Bing Chat
  • Provide a way to get the full result, like adaptive cards
  • Add ability to set timeout
  • Publish CLI as dotnet tool
  • Validate that connectivity issues are gone after switching to SignalR
  • Research OptionSet specifics, analyze whether switching to harmonyv3 enables the older model which has better performance
  • Expose rich response object via .SendAsync(..) to allow detailed access and control over the conversation state

Q&A

Set your own cookie

You can set your own cookie to interact with BingChat server, instead of generating a random one.

To get it, you can go to www.bing.com, log in an account which has access, and then open the Developer Tools (F12) > Application Tab > Storage > Cookies, find the cookie named _U, and copy its value. Then pass it to the BingChatClientOptions' constructor.

UnauthorizedRequest Exception

Solution

There are multiple reasons. You may follow these steps to handle the problem.

  1. Refresh the webpage, confirm that the '_U' value is up to date and copied correctly, and retry.

  2. If you are using a proxy (VPN), try setting the global proxy, and retry. The code is as follows:

    HttpClient.DefaultProxy = new WebProxy("127.0.0.1:8807"); //Your proxy address and port
  3. Find another cookie named 'KievRPSSecAuth', set its value, and retry. The code is as follows:

    var client = new BingChatClient(new BingChatClientOptions {
        // The "_U" cookie's value
        CookieU = strU,
        // The "KievRPSSecAuth" cookie's value
        CookieKievRPSSecAuth = strKievRPSSecAuth,
    });
  4. Open the browser menu > Extensions. Search for 'Cookie Editor' and install it. Go to Bing Chat webpage and export all cookies to to a local file in JSON format. Set file path value as follows (The "_U" and "KievRPSSecAuth" value are not needed at this time):

    var client = new BingChatClient(new BingChatClientOptions {
        // The exported cookie file path
        CookieFilePath = strFilePath,
    });

Contributors

Thanks to these contributors for developing or improving this library:

Contributors

If you have any idea on this project, feel free to open a PR and share with us :D

License

This project is licensed under MIT License. We โค open source!

bingchat's People

Contributors

bsdayo avatar neon-sunset avatar caulm avatar awbugl 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.