64bit / async-openai

Rust library for OpenAI
https://docs.rs/async-openai
MIT License
1.11k stars 166 forks source link

Provide function call example using tool_calls #143

Closed sloganking closed 9 months ago

sloganking commented 10 months ago

https://github.com/64bit/async-openai/blob/main/examples/function-call/src/main.rs

The current function call example uses the now deprecated ChatCompletionResponseMessage::function_call field. It was deprecated in favor of ChatCompletionResponseMessage::tool_calls. Can an example be provided using the new tool_calls method?

SpaghettiFibonacci commented 10 months ago

Here is an example of how to use it meanwhile:

let tool = ChatCompletionToolArgs::default()
        .r#type(async_openai::types::ChatCompletionToolType::Function)
        .function(ChatCompletionFunctions { 
                name: "get_current_weather".to_string(), 
                description: Some("Get weather in location".to_string()), 
                parameters: json!({
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "The city and state, e.g. San Francisco, CA",
                        },
                        "format": {
                            "type": "string",
                            "enum": ["celsius", "fahrenheit"],
                            "description": "The temperature unit to use. Infer this from the users location.",
                        },
                    },
                    "required": ["location", "format"],

                }) 
            }).build()?;

and for the tool choice

        .tool_choice(ChatCompletionToolChoiceOption::Auto)

Though I am not sure it is working right now!

Error: ApiError(ApiError { message: "'$.tool_choice' is invalid. Please check the API reference: https://platform.openai.com/docs/api-reference.", type: Some("invalid_request_error"), param: None, code: None })
sloganking commented 10 months ago

Okay thanks, I will continue using the deprecated option for now and maybe come back to this when it has been tested further. or test it some on my own later.

SpaghettiFibonacci commented 10 months ago

When leaving out

        .tool_choice(ChatCompletionToolChoiceOption::Auto)

it should work

64bit commented 9 months ago

An example was added in #153. And the bug was fixed in #158

Seems like everything is resolved, hence closing the issue.