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

conversation function call failing: error missing name #75

Open
philipp1992 opened this issue Dec 12, 2023 · 2 comments
Open

conversation function call failing: error missing name #75

philipp1992 opened this issue Dec 12, 2023 · 2 comments

Comments

@philipp1992
Copy link

Hi,
im trying to get the function feature running and it works more or less.

When i send a new message after calling a function is get this error:
BackendError { message: "Missing parameter 'name': messages with role 'function' must have a 'name'.", error_type: "invalid_request_error"

Ive already tested the fix function branch.

Kind regards
Philipp

/// returns a list of links for a search query on google
///
/// * search_query - the search query
#[gpt_function]
async fn search_google(search_query: String) -> Result<Value, chatgpt::err::Error> {
    let google_api = crate::google_api::GoogleApi::new(
        "xxx".to_string(),
        "xxx".to_string(),
    );
    let links = google_api.search(search_query).await.unwrap();
    println!("Links: {:?}", links);
    return Ok(format!("{:?}", links).into());
}
#[tokio::test]
async fn test_gpt_function_search_google() {
    let api_key = "sk-x".to_string(); 
    let gpt_api = GptApi::new(api_key).unwrap();
    let mut conversation = gpt_api.client.new_conversation();
    conversation.add_function(search_google()).unwrap();
    conversation.always_send_functions = true;
    let response = conversation
        .send_message(
            "search google to find the best links for changing a tire",
        )
        .await
        .unwrap();
    println!("Response: {:?}", response);
    let response = conversation
        .send_message("whats the result?")
        .await
        .unwrap();
    println!("Response: {:?}", response);
}

running 1 test
Links: [LinkResult { title: "How to Change a Flat Tire", link: "https://www.bridgestonetire.com/learn/maintenance/how-to-change-a-flat-tire/", snippet: "Apr 1, 2021 ... How to Change a Flat Tire · 1. FIND A SAFE LOCATION. As soon as you realize you have a flat tire, do not abruptly brake or turn. · 2. TURN ON\u{a0}..." }, LinkResult { title: "How to Change a Flat Tire | Driving-Tests.org", link: "https://driving-tests.org/beginner-drivers/how-to-change-tires/", snippet: "How To Change Tires · 1) Pull off the road as soon as possible · 2) Turn on your hazard lights · 3) Apply the parking brake · 4) Apply wheel wedges / large\u{a0}..." }, LinkResult { title: "How to Change a Tire: Swap Your Flat Out Like a Pro", link: "https://www.wikihow.com/Change-a-Tire", snippet: "Steps · Pull over and put your hazards on. · Remove your spare tire and the jack. · Assess the issue with your vehicle. · Elevate your vehicle with a jack." }, LinkResult { title: "Stuck with a flat tire? here's How to Change a Tire in 10 steps | Miller ...", link: "https://www.millerautoplaza.com/stuck-with-a-flat-tire-heres-how-to-change-a-tire-in-10-steps/", snippet: "Sep 22, 2017 ... Stuck with a flat tire? here's How to Change a Tire in 10 steps · 1. Find a Safe Place to Pull Over · 3. Check for Materials · 4. Loosen the Lug\u{a0}..." }, LinkResult { title: "How to Change a Tire - The Home Depot", link: "https://www.homedepot.com/c/ah/how-to-change-a-tire/9ba683603be9fa5395fab908e21cabb", snippet: "Tools to Change a Tire · A manual car jack designed to raise your vehicle high enough to remove the flat tire. · A spare tire. · A lug wrench or torque wrench." }, LinkResult { title: "How to Change a Car Tire | Flat Tire - Consumer Reports", link: "https://www.consumerreports.org/cars/tire-buying-maintenance/how-to-change-a-car-tire-a2760414554/", snippet: "Aug 11, 2023 ... How to Change a Car Tire · The car needs to be on level, solid ground in order for you to safely use the jack. · Turn on your hazard lights." }, LinkResult { title: "EMSK: How to change a flat tire!! : r/everymanshouldknow", link: "https://www.reddit.com/r/everymanshouldknow/comments/1icjyx/emsk_how_to_change_a_flat_tire/", snippet: "Jul 15, 2013 ... EMSK: How to change a flat tire!! · Apply parking brake · Loosen bolts on tire before jacking up · Locate jack and lift points. · Use jack to\u{a0}..." }, LinkResult { title: "How to Change a Tire | Change a flat car tire step by step - YouTube", link: "https://www.youtube.com/watch?v=joBmbh0AGSQ", snippet: "Jan 30, 2008 ... Nothing takes the joy out of a road trip like a flat tire. Do you know how to change it? We didn't, but we've learned from Allan Stanley of\u{a0}..." }, LinkResult { title: "Changing tires :: BeamNG.drive General Discussions", link: "https://steamcommunity.com/app/284160/discussions/0/2217311444328764498/", snippet: "Jul 14, 2017 ... In most cars those are located under the 'Suspension' group. Once you find, you will see that you can further expand the 'wheels' group. On the\u{a0}..." }, LinkResult { title: "60 Percent of People Can't Change a Flat Tire - But Most Can ...", link: "https://www.nbcnews.com/business/consumer/draft-60-percent-people-can-t-change-flat-tire-most-n655501", snippet: "Sep 27, 2016 ... 60 Percent of People Can't Change a Flat Tire - But Most Can Google It." }]
Response: CompletionResponse { message_id: Some("chatcmpl-8V47LSJIYREzPSkEpnxMcN5o9yjVe"), created_timestamp: Some(1702414687), model: "gpt-4-0613", usage: TokenUsage { prompt_tokens: 90, completion_tokens: 20, total_tokens: 110 }, message_choices: [MessageChoice { message: ChatMessage { role: Assistant, content: "", function_call: Some(FunctionCall { name: "search_google", arguments: "{\n  \"search_query\": \"how to change a tire\"\n}" }) }, finish_reason: "function_call", index: 0 }] }
thread 'gpt_api::test_gpt_function_search_google' panicked at src/gpt_api.rs:149:10:
called `Result::unwrap()` on an `Err` value: BackendError { message: "Missing parameter 'name': messages with role 'function' must have a 'name'.", error_type: "invalid_request_error" }
stack backtrace:
   0: rust_begin_unwind
             at /rustc/0e2dac8375950a12812ec65868e42b43ed214ef9/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/0e2dac8375950a12812ec65868e42b43ed214ef9/library/core/src/panicking.rs:72:14
   2: core::result::unwrap_failed
             at /rustc/0e2dac8375950a12812ec65868e42b43ed214ef9/library/core/src/result.rs:1649:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/0e2dac8375950a12812ec65868e42b43ed214ef9/library/core/src/result.rs:1073:23
   4: cc_blog_gpt::gpt_api::test_gpt_function_search_google::{{closure}}
             at ./src/gpt_api.rs:146:20
   5: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/0e2dac8375950a12812ec65868e42b43ed214ef9/library/core/src/future/future.rs:125:9
   6: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/0e2dac8375950a12812ec65868e42b43ed214ef9/library/core/src/future/future.rs:125:9
   7: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}::{{closure}}
             at /Users/philipp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.0/src/runtime/scheduler/current_thread/mod.rs:665:57
   8: tokio::runtime::coop::with_budget
             at /Users/philipp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.0/src/runtime/coop.rs:107:5
   9: tokio::runtime::coop::budget
             at /Users/philipp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.0/src/runtime/coop.rs:73:5
  10: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}::{{closure}}
             at /Users/philipp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.0/src/runtime/scheduler/current_thread/mod.rs:665:25
  11: tokio::runtime::scheduler::current_thread::Context::enter
             at /Users/philipp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.0/src/runtime/scheduler/current_thread/mod.rs:410:19
  12: tokio::runtime::scheduler::current_thread::CoreGuard::block_on::{{closure}}
             at /Users/philipp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.0/src/runtime/scheduler/current_thread/mod.rs:664:36
  13: tokio::runtime::scheduler::current_thread::CoreGuard::enter::{{closure}}
             at /Users/philipp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.0/src/runtime/scheduler/current_thread/mod.rs:743:68
  14: tokio::runtime::context::scoped::Scoped<T>::set
             at /Users/philipp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.0/src/runtime/context/scoped.rs:40:9
  15: tokio::runtime::context::set_scheduler::{{closure}}
             at /Users/philipp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.0/src/runtime/context.rs:176:26
  16: std::thread::local::LocalKey<T>::try_with
             at /rustc/0e2dac8375950a12812ec65868e42b43ed214ef9/library/std/src/thread/local.rs:270:16
  17: std::thread::local::LocalKey<T>::with
             at /rustc/0e2dac8375950a12812ec65868e42b43ed214ef9/library/std/src/thread/local.rs:246:9
  18: tokio::runtime::context::set_scheduler
             at /Users/philipp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.0/src/runtime/context.rs:176:9
  19: tokio::runtime::scheduler::current_thread::CoreGuard::enter
             at /Users/philipp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.0/src/runtime/scheduler/current_thread/mod.rs:743:27
  20: tokio::runtime::scheduler::current_thread::CoreGuard::block_on
             at /Users/philipp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.0/src/runtime/scheduler/current_thread/mod.rs:652:19
  21: tokio::runtime::scheduler::current_thread::CurrentThread::block_on::{{closure}}
             at /Users/philipp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.0/src/runtime/scheduler/current_thread/mod.rs:175:28
  22: tokio::runtime::context::runtime::enter_runtime
             at /Users/philipp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.0/src/runtime/context/runtime.rs:65:16
  23: tokio::runtime::scheduler::current_thread::CurrentThread::block_on
             at /Users/philipp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.0/src/runtime/scheduler/current_thread/mod.rs:167:9
  24: tokio::runtime::runtime::Runtime::block_on
             at /Users/philipp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.35.0/src/runtime/runtime.rs:348:47
  25: cc_blog_gpt::gpt_api::test_gpt_function_search_google
             at ./src/gpt_api.rs:150:5
  26: cc_blog_gpt::gpt_api::test_gpt_function_search_google::{{closure}}
             at ./src/gpt_api.rs:133:43
  27: core::ops::function::FnOnce::call_once
             at /rustc/0e2dac8375950a12812ec65868e42b43ed214ef9/library/core/src/ops/function.rs:250:5
  28: core::ops::function::FnOnce::call_once
             at /rustc/0e2dac8375950a12812ec65868e42b43ed214ef9/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
test gpt_api::test_gpt_function_search_google ... FAILED
@spennythug
Copy link

I believe this is the same issue as #70

The OpenAI API needs the name of the function called if a ChatMessage has the role of function. This occurs because function results are automatically added to the chat history but without the function name. When you send a follow up message with the function result in the chat history, the API refuses

I was able to fix this for myself by adding an optional name to ChatMessage and patching in the function name where appropriate.

@Maxuss I did this pretty quickly so there may be flaws I'm not seeing. That said, it works for me so, let me know if you'd be interested in a pull request.

@spennythug
Copy link

After testing this a bit more, I've noticed functions calls are being added to the chat history twice. I don't know if this is something I caused or something that was already there but this additional commit fixes the issue for me.

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