Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

how to use functions feature within continuous conversation #70

Open
For-December opened this issue Oct 20, 2023 · 3 comments
Open

how to use functions feature within continuous conversation #70

For-December opened this issue Oct 20, 2023 · 3 comments

Comments

@For-December
Copy link

Thank you for developing this powerful lib!
It always works well, but when I try to use functions feature, I' m in trouble.
I want to fulfill a continuous conversation with functions feature, for instance:

  • ask chatGPT what' s the time
  • chatGPT call my function to get current time
  • then chatGPT reply to me with the time that my function returns

Just like the example: https://openai.com/blog/function-calling-and-other-api-updates
image

Here is my test code:

  // build conversation and send to openAI
  conversation.add_function(get_current_time()).unwrap();
  let res = conversation
      .send_message_functions(ask)  // ask is "what' s the time now?"
      .await
      .unwrap()
      .message_choices;

about get_current_time:

/// get current time
///
/// * time - the time that user asks
#[gpt_function]
async fn get_current_time(time: String) -> Value {
    println!("AI uses param: {time}");
    return Ok(json!({
        "time":"10:30"
    }));
}

my function ( get_current_time ) was called successfully, but I could' t get a expected reply about current time.

message_functions' length is 1, content is empty str
called message().content also return empty str

history like this:
image

maybe need this?( I guess...)
image
and when I called send_message_functions again, error occur:
image

I tried again and again, but couldn't be able to solve it, hope to get help

@For-December
Copy link
Author

and src\api\chatgpt:95: 14 is here
image

@rsmeowry
Copy link
Owner

Could you check if the issue still persists with version from the function-invocation-fix branch?

@For-December
Copy link
Author

For-December commented Oct 23, 2023

@Maxuss
I write a testing code just now, the issue still persists.
But I got a warning while running, do I use functions feature correctly?
Here are all about testing code:

  • main logic
  // some config code for client......

   let mut conversation = client.new_conversation();
    conversation.add_function(get_current_time()).unwrap();

    let res = conversation
        .send_message_functions("Please tell the current time.")
        .await
        .unwrap()
        .message_choices;

    println!("{}", res.len());
    for ele in res {
        println!("ele is: {}", ele.message.content);
    }
    println!("\nhistory is:{:#?}", conversation.history);
  • gpt_function
/// get current time
///
/// * time - the time that user asks
#[gpt_function]
async fn get_current_time(time: String) -> Result<Value> {
    println!("AI uses param: {time}");
    let result: serde_json::Value = serde_json::json!({
        "time":"10:30"
    });
    result
}
  • Cargo.toml
[package]
name = "test_gpt"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chatgpt_rs = { git = "https://github.com/Maxuss/chatgpt_rs", features = [
    "functions",
], branch = "function-invocation-fix" }
reqwest = { version = "0.11", features = ["json", "blocking"] }
tokio = { version = "1", features = ["full"] }
serde_json = { version = "1.0", features = ["raw_value"] }
serde = { version = "1.0", features = ["derive"] }
  • All output
    image

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants