Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
neowu committed Mar 28, 2024
1 parent 3fcc93f commit 6a46f49
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/openai/chatgpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::error::Error;
use std::fmt;
use std::sync::Arc;

use futures::future::join_all;
use futures::stream::StreamExt;
use reqwest_eventsource::Event;
use reqwest_eventsource::EventSource;
Expand Down Expand Up @@ -76,12 +77,19 @@ impl ChatGPT {
self.messages.push(ChatRequestMessage::new_message(Role::User, message));
let result = self.process(handler).await;
if let Ok(Some(InternalEvent::FunctionCall(calls))) = result {
let mut handles = vec![];

for (_, (id, name, args)) in calls {
let function = Arc::clone(self.function_implementations.get(&name).unwrap());
let result = tokio::spawn(async move { function(json::from_json(&args).unwrap()) }).await?;
let function_message = ChatRequestMessage::new_function_response(id, json::to_json(&result)?);
handles.push(tokio::spawn(async move { (id, function(json::from_json(&args).unwrap())) }));
}
let results = join_all(handles).await;
for result in results {
let result = result?;
let function_message = ChatRequestMessage::new_function_response(result.0, json::to_json(&result.1)?);
self.messages.push(function_message);
}

self.process(handler).await?;
}
Ok(())
Expand Down

0 comments on commit 6a46f49

Please sign in to comment.