From 3391492f00d6cf2ff1b49ea63a2180c801fa3e30 Mon Sep 17 00:00:00 2001 From: suddenlyAstral Date: Sat, 22 Feb 2025 17:15:55 +0200 Subject: [PATCH] renamed max_tokens -> max_completion_tokens in ChatBody to support reasoning models. added reasoning model test --- README.md | 2 +- src/apis/chat.rs | 33 +++++++++++++++++++++++++++++++-- src/lib.rs | 2 +- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c1979bf..90dacde 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ fn main() { let openai = OpenAI::new(auth, "https://api.openai.com/v1/"); let body = ChatBody { model: "gpt-3.5-turbo".to_string(), - max_tokens: Some(7), + max_completion_tokens: Some(7), temperature: Some(0_f32), top_p: Some(0_f32), n: Some(2), diff --git a/src/apis/chat.rs b/src/apis/chat.rs index e4d1e6e..5d856b6 100644 --- a/src/apis/chat.rs +++ b/src/apis/chat.rs @@ -50,7 +50,7 @@ pub struct ChatBody { /// The total length of input tokens and generated tokens is limited by the model's context length. /// Defaults to inf #[serde(skip_serializing_if = "Option::is_none")] - pub max_tokens: Option, + pub max_completion_tokens: Option, /// Number between -2.0 and 2.0. /// Positive values penalize new tokens based on whether they appear in the text so far, /// increasing the model's likelihood to talk about new topics. @@ -104,7 +104,7 @@ mod tests { let openai = new_test_openai(); let body = ChatBody { model: "gpt-3.5-turbo".to_string(), - max_tokens: Some(7), + max_completion_tokens: Some(7), temperature: Some(0_f32), top_p: Some(0_f32), n: Some(2), @@ -121,4 +121,33 @@ mod tests { let message = &choice[0].message.as_ref().unwrap(); assert!(message.content.contains("Hello")); } + + #[test] + fn test_chat_reasoning_completion() { + // OpenAI's API for reasoning models requires: + // - temperature=1.0 (which is default when None) + // - top_p == None + // - max_completion_tokens includes reasoning tokens, often >200 even for simple requests + // + // The temperature and top_p requirements also cause the test to be non-deterministic + let openai = new_test_openai(); + let body = ChatBody { + model: "o1-mini".to_string(), + max_completion_tokens: Some(500), + temperature: None, + top_p: None, + n: Some(2), + stream: Some(false), + stop: None, + presence_penalty: None, + frequency_penalty: None, + logit_bias: None, + user: None, + messages: vec![Message { role: Role::User, content: "Hello! Don't think deeply, just write 'Hello' then end.".to_string() }], + }; + let rs = openai.chat_completion_create(&body); + let choice = rs.unwrap().choices; + let message = &choice[0].message.as_ref().unwrap(); + assert!(message.content.contains("Hello")); + } } diff --git a/src/lib.rs b/src/lib.rs index 7d050a2..15ef899 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ //! let openai = OpenAI::new(auth, "https://api.openai.com/v1/"); //! let body = ChatBody { //! model: "gpt-3.5-turbo".to_string(), -//! max_tokens: Some(7), +//! max_completion_tokens: Some(7), //! temperature: Some(0_f32), //! top_p: Some(0_f32), //! n: Some(2),