Maxuss / chatgpt_rs

OpenAI's ChatGPT API wrapper for Rust 🦀
MIT License
148 stars 39 forks source link

gpt_function Macro cant figure out Result Type #73

Open wegfawefgawefg opened 8 months ago

wegfawefgawefg commented 8 months ago

in the following function

pub async fn go_get_remote_response(prompt: &str) -> Result<String, Box<dyn Error>> {
    // send to remote api
    let openai_api_key =
        env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY must be set in .env file");
    let client = ChatGPT::new(openai_api_key)?;
    let mut conversation = client.new_conversation();
    conversation.add_function(read_file());
    conversation.add_function(write_file());

    if let Ok(response) = conversation.send_message_functions(prompt).await {
        let message_choices = response.message_choices;
        loop {
            if let Some(message_choice) = message_choices.first() {
                let message = message_choice.message.clone();
                println!("{:?}: {}", message.role, message.content);
                if message_choice.finish_reason == "stop" {
                    return Ok(message.content);
                }
            }
        }
    }
    Ok("test".to_string())
}

for the function read_file() as follows

/// Read the contents of a file and return them as a string
///
/// * filename - The name of the file to read
/// * Returns - The contents of the file as a string
#[gpt_function]
pub async fn read_file(filename: String) -> Result<String, io::Error> {
    let mut file = File::open(filename).await?;
    let mut contents = String::new();
    file.read_to_string(&mut contents).await?;
    Ok(contents)
}

the first line of the macro /// Read the contents of a file and return them as a string displays the following error:

type annotations needed for `std::result::Result<std::string::String, E>`rustc ...
test.rs(50, 60): consider giving `result` an explicit type, where the type for type parameter `E` is specified: `: std::result::Result<std::string::String, E>`

This error doesnt make sense to me as Result definitely has an explicity type provided.

I suspect that the gpt_function macro is mowing over the stdlib Result type with the chatgpt::Result which wraps it?

wegfawefgawefg commented 8 months ago

Replacing the Result return with Option removes the warning, but shouldn't be necessary.