@@ -232,11 +232,9 @@ In an API call, you can describe functions and have the model intelligently choo
232232use openai_dive :: v1 :: api :: Client ;
233233use openai_dive :: v1 :: models :: Gpt4Engine ;
234234use openai_dive :: v1 :: resources :: chat :: {
235- ChatCompletionFunction , ChatCompletionParameters , ChatCompletionTool , ChatCompletionToolChoice ,
236- ChatCompletionToolChoiceFunction , ChatCompletionToolChoiceFunctionName , ChatCompletionToolType ,
237- ChatMessage , Role ,
235+ ChatCompletionFunction , ChatCompletionParameters , ChatCompletionTool , ChatCompletionToolType , ChatMessage ,
236+ ChatMessageContent ,
238237};
239- use openai_dive :: v1 :: resources :: shared :: FinishReason ;
240238use rand :: Rng ;
241239use serde :: {Deserialize , Serialize };
242240use serde_json :: {json, Value };
@@ -247,22 +245,14 @@ async fn main() {
247245
248246 let client = Client :: new (api_key );
249247
250- let mut messages = vec! [ChatMessage {
251- content : ChatMessageContent :: Text (" Give me a random number between 25 and 50 ?" . to_string ()),
248+ let messages = vec! [ChatMessage {
249+ content : ChatMessageContent :: Text (" Give me a random number between 100 and no more than 150 ?" . to_string ()),
252250 .. Default :: default ()
253251 }];
254252
255253 let parameters = ChatCompletionParameters {
256254 model : Gpt4Engine :: Gpt41106Preview . to_string (),
257255 messages : messages . clone (),
258- tool_choice : Some (ChatCompletionToolChoice :: ChatCompletionToolChoiceFunction (
259- ChatCompletionToolChoiceFunction {
260- r#type : Some (ChatCompletionToolType :: Function ),
261- function : ChatCompletionToolChoiceFunctionName {
262- name : " get_random_number" . to_string (),
263- },
264- },
265- )),
266256 tools : Some (vec! [ChatCompletionTool {
267257 r#type : ChatCompletionToolType :: Function ,
268258 function : ChatCompletionFunction {
@@ -273,7 +263,8 @@ async fn main() {
273263 " properties" : {
274264 " min" : {" type" : " integer" , " description" : " Minimum value of the random number." },
275265 " max" : {" type" : " integer" , " description" : " Maximum value of the random number." },
276- }
266+ },
267+ " required" : [" min" , " max" ],
277268 }),
278269 },
279270 }]),
@@ -282,36 +273,22 @@ async fn main() {
282273
283274 let result = client . chat (). create (parameters ). await . unwrap ();
284275
285- for choice in result . choices. iter () {
286- if choice . finish_reason == FinishReason :: StopSequenceReached {
287- if let Some (tool_calls ) = & choice . message. tool_calls {
288- for tool_call in tool_calls . iter () {
289- let random_numbers =
290- serde_json :: from_str (& tool_call . function. arguments). unwrap ();
291-
292- if tool_call . function. name == " get_random_number" {
293- let random_number_result = get_random_number (random_numbers );
294-
295- messages . push (ChatMessage {
296- role : Role :: Function ,
297- content : ChatMessageContent :: Text (
298- serde_json :: to_string (& random_number_result ). unwrap (),
299- ),
300- name : Some (" get_random_number" . to_string ()),
301- .. Default :: default ()
302- });
303-
304- let parameters = ChatCompletionParameters {
305- model : Gpt4Engine :: Gpt41106Preview . to_string (),
306- messages : messages . clone (),
307- .. Default :: default ()
308- };
309-
310- let result = client . chat (). create (parameters ). await . unwrap ();
311-
312- println! (" {:#?}" , result );
313- }
314- }
276+ let message = result . choices[0 ]. message. clone ();
277+
278+ if let Some (tool_calls ) = message . tool_calls {
279+ for tool_call in tool_calls {
280+ let name = tool_call . function. name;
281+ let arguments = tool_call . function. arguments;
282+
283+ if name == " get_random_number" {
284+ let random_numbers : RandomNumber = serde_json :: from_str (& arguments ). unwrap ();
285+
286+ println! (" Min: {:?}" , & random_numbers . min);
287+ println! (" Max: {:?}" , & random_numbers . max);
288+
289+ let random_number_result = get_random_number (random_numbers );
290+
291+ println! (" Random number between those numbers: {:?}" , random_number_result . clone ());
315292 }
316293 }
317294 }
0 commit comments