Skip to content

Latest commit

 

History

History
63 lines (54 loc) · 1.7 KB

README_CN.md

File metadata and controls

63 lines (54 loc) · 1.7 KB

bevy_openai

bevy_openai是一个由事件驱动的Bevy插件,能够方便的在Bevy中调用OpenAI API

当前功能:

安装

将crate添加为依赖:

[dependencies]
bevy_openai = "0.1.0"

添加插件:

use bevy::prelude::*;
use bevy_openai::OpenAiPlugin;

fn main() {
    App::new()
    .add_plugins((DefaultPlugins, OpenAiPlugin))
}

使用

OPENAI_API_KEY设置为环境变量

$ export OPENAI_API_KEY=sk-xxxxxxx

bevy_openai是事件驱动的。你可以使用事件向ChatGPT发送prompt,也可以使用事件读取来自ChatGPT的响应。

使用SendToAiEvent向ChatGPT发送prompt。

fn send_to_ai(mut event_writer: EventWriter<SendToAiEvent>) {
    event_writer.send(SendToAiEvent("Hello".to_string()));
}

或者使用SendToAiWithConfigEvent来添加自定义的配置信息。

fn send_to_ai(mut config_event_writer: EventWriter<SendToAiWithConfigEvent>, ) {
    // with config
    config_event_writer.send(SendToAiWithConfigEvent {
        prompt: "Hello".to_string(),
        config: ClientConfigBuilder::default()
            .api_endpoint("".to_owned())
            .api_key("".to_owned())
            .build()
            .expect("Failed to build config"),
    });
}

最后,使用AiResponseEvent读取来自ChatGPT的响应。

fn process_ai_response(mut event_reader: EventReader<AiResponseEvent>) {
    for event in event_reader.read() {
        println!("response: {}", event.0);
    }
}

完整的示例在examples目录中。