8个版本 (1个稳定版)

1.0.0 2023年12月18日
0.1.6 2023年12月18日

#573 in 异步

每月38次下载

MIT许可证

15KB
228 代码行

从图像中选择动作

github crates.io docs.rs build status

根据图像和上下文从动作列表中选择最佳动作。

这可以用于其他AI模型,但目前仅实现了ChatGPT-4V的模型。

使用方法

定义一个ImagePath

// Picture of a cute dog
let url = Url::parse("https://www.petlandflorida.com/wp-content/uploads/2022/04/shutterstock_1290320698-1-scaled.jpg").unwrap();
let image_path = ImagePath::Url(&url);

也可以使用文件

let path = PathBuf::from("assets/my_image.jpeg");
let image_path = ImagePath::File(&path);

然后提供选择动作的上下文。这可以是一个问题或陈述。

let context = "What would make them happiest? They haven't eaten."; // Your question

然后定义一个要选择的动作列表。描述有助于AI理解动作是什么以及何时应该选择它。

let actions = vec![
    Action {
        id: "give bone",
        description: "Give a delicious bone to chew on",
    },
    Action {
        id: "give belly rub",
        description: "Give a belly rub",
    },
    Action {
        id: "none",
        description: "Don't do anything",
    },
];

然后创建一个AI实例。目前仅实现了ChatGPT-4V。

dotenv().ok();
let api_key = env::var("OPENAI_API_KEY").expect("API key not found");
let chat_gpt4v = ChatGpt4v { api_key: &api_key };

最后,询问AI关于图像的内容。您将得到一个作为String的最佳动作。

//  Ask the AI about the image
let picked_action = pick_action_from_image(&chat_gpt4v, &context, &actions, &image_path)
    .await
    .unwrap();
println!("Picked action: {}", picked_action);

您可以在仓库的examples文件夹中查看完整的示例。

设置

请确保您已安装Rust

然后您应该设置您的OPENAI_API_KEY环境变量。为此,您可以将.env.example文件复制到.env并填写值。

您可以在您的用户设置中找到您的API密钥。

依赖关系

~7–20MB
~307K SLoC