3 个版本

0.1.2 2024年4月21日
0.1.1 2024年4月8日
0.1.0 2024年3月28日

#178 in 机器人

Download history 133/week @ 2024-04-08 115/week @ 2024-04-15 40/week @ 2024-04-22 8/week @ 2024-04-29 2/week @ 2024-05-20 3/week @ 2024-06-10

每月 161 次下载

Apache-2.0 OR MIT

110KB
2.5K SLoC

Bing Copilot 异步客户端

功能

默认= ["rustls"]

默认使用 rustls 可以简化跨平台的编译。

如果您想使用 native-tls,可以使用 {------, features = ["native-tls"],default-features = false}

您可以使用其中一个枚举来构建一个客户端;cookie 可以通过使用 Cookie 编辑器 或其他浏览器扩展来获取。(您也可以使用 JavaScript 自行获取)

pub enum Cookie {
    // Json means format like this:
    // [
    // {
    //     "domain": ".bing.com",
    //     "expirationDate": 1743827661.986849,
    //     "hostOnly": false,
    //     "httpOnly": false,
    //     "name": "SnrOvr",
    //     "path": "/",
    //     "sameSite": "no_restriction",
    //     "secure": true,
    //     "session": false,
    //     "storeId": null,
    //     "value": "X=rebateson"
    // },
    // ······
    // ]
    JsonPath(String),
    JsonStr(String),
    // Head means format like:
    // SnrOvr=X=rebateson;SRCHUSR=DOB=20240323&T=1712299341000&TPC=1711617907000&POEX=W; ······
    HeadPath(String),
    HeadStr(String),
}

构建不带聊天的 client

use bing_client::BingClient;
#[tokio::main]
async fn main(){
    let client = BingClient::build_with_chats(&Cookie::JsonPath("path to cookie json".to_string()))
}

构建带聊天的 client

let client = BingClient::build(&Cookie::JsonPath("path to cookie json".to_string()))
    .await
    .unwrap();
client.chats.iter().for_each(|chat| {
    println!("{}", chat);
});

序列化和反序列化一个 client

let client = BingClient::build(&Cookie::JsonPath("path to cookie json".to_string())).await.unwrap();
let client_str = serde_json::to_string(&client).unwrap();
let client = serde_json::from_str::<BingClient>(&client_str).unwrap();

获取聊天列表

let chats = client.get_chat_list().await.unwrap();

创建一个新的聊天

let chat = client.create_chat().await.unwrap();

删除一个聊天

client.delete_chat(& chat).await.unwrap();

重命名一个聊天

client..rename_chat(& chat, "new name".to_string()).await.unwrap();

获取聊天消息

let messages = client.get_chat_messages(&mut last_chat).await.unwrap();

在聊天中提问,并只获得字符串(markdown)回复

let client = BingClient::build(&Cookie::JsonPath("path to cookie json".to_string())).await.unwrap();
let mut new_chat = client.create_chat().await.unwrap();
let user_input = UserInput::build(
    // Text question
    "hello".to_string(),
    // Image attachment, uncomment this
    // Some(Image::Path(r"example_image.jpg".to_string())),
    None,
    // Chat Tone
    Tone::Balanced,
    // plugins to use
    vec![Plugin::search()],
    &new_chat,
    &client,
)
.await
.unwrap();

let (mut stream, stop_fn) = client
    .ask_stream_plain(&mut new_chat, user_input)
    .await
    .unwrap();
while let GeneratorState::Yielded(data) = stream.async_resume().await {
    print!("\x1b[2J\x1b[H");
    println!("{data}");
}

在聊天中提问,并获取多种类型的回复

let client = BingClient::build(&Cookie::JsonPath("path to cookie json".to_string())).await.unwrap();
let mut new_chat = client.create_chat().await.unwrap();
let user_input = UserInput::build(
    "hello".to_string(),
    None,
    crate::types::user_input_type::Tone::Balanced,
    vec![Plugin::search()],
    &new_chat,
    &client,
)
.await
.unwrap();
let (mut stream, stop_fn) = client
    .ask_stream(&mut new_chat, user_input)
    .await
    .unwrap();
while let GeneratorState::Yielded(data) = stream.async_resume().await {
    print!("\x1b[2J\x1b[H");
    match data {
        crate::types::bot_easy_resp::BotResp::Text(text) => todo!(),
        crate::types::bot_easy_resp::BotResp::SuggestReply(suggest_replys) => todo!(),
        crate::types::bot_easy_resp::BotResp::Notice(notice) => todo!(),
        crate::types::bot_easy_resp::BotResp::Image(images) => todo!(),
        crate::types::bot_easy_resp::BotResp::Apology(apology) => todo!(),
        crate::types::bot_easy_resp::BotResp::SourceAttribution(sources) => todo!(),
        crate::types::bot_easy_resp::BotResp::Limit(limit) => todo!(),
    }
}

停止回答

let client = BingClient::build(&Cookie::JsonPath("path to cookie json".to_string()))
    .await
    .unwrap();
let mut new_chat = client.create_chat().await.unwrap();
let user_input = UserInput::build(
    "Write a science fiction story.".to_string(),
    None,
    crate::types::user_input_type::Tone::Creative,
    vec![],
    &new_chat,
    &client,
)
.await
.unwrap();
let (mut stream, stop_fn) = client
    .ask_stream_plain(&mut new_chat, user_input)
    .await
    .unwrap();
// For example, stop the client from answering after four answers

let mut times = 0;
while let GeneratorState::Yielded(data) = stream.async_resume().await {
    times += 1;
    if times == 4{
        println!("try stop");
        stop_fn();
    }
    print!("{} ",data.len());
}

绘制图像

let imgs = client.draw_image("a bird").await.unwrap();

依赖项

~13–30MB
~429K SLoC