Skip to content

Capytara/bevy_openai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

中文

bevy_openai

bevy_openai is an event-driven plugin for Bevy that provides convenient access to the OpenAI API.

Current features:

Installation

Add the crate as a dependency:

[dependencies]
bevy_openai = "0.1.0"

Add the plugin:

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

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

Usage

Set OPENAI_API_KEY to environment variable

$ export OPENAI_API_KEY=sk-xxxxxxx

bevy_openai is event-driven. You can send a prompt to the ChatGPT and read the response from the ChatGPT using events.

Use SendToAiEvent to send your prompt to the ChatGPT.

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

Or use SendToAiWithConfigEvent to send your prompt to the ChatGPT with a custom config.

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"),
    });
}

Use AiResponseEvent to read the response from the ChatGPT.

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

The full example is in the examples directory.

About

bevy_openai is an event-driven plugin for Bevy that provides convenient access to the OpenAI API.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages