From cad584455aa98ba2e54555bc13cfbf76471d6ccb Mon Sep 17 00:00:00 2001 From: yavens <179155341+yavens@users.github.noreply.github.com> Date: Wed, 9 Apr 2025 15:40:57 -0400 Subject: [PATCH] fix: wasm missing generic + fmt --- rig-core/examples/anthropic_streaming.rs | 4 +--- rig-core/examples/gemini_streaming.rs | 5 ++++- rig-core/examples/gemini_streaming_with_tools.rs | 7 +++++-- rig-core/examples/ollama_streaming_with_tools.rs | 2 +- rig-core/examples/openai_streaming.rs | 4 ++-- rig-core/examples/openai_streaming_with_tools.rs | 4 ++-- rig-core/src/providers/gemini/streaming.rs | 1 - rig-core/src/providers/ollama.rs | 2 +- rig-core/src/providers/openai/streaming.rs | 7 +++++-- rig-core/src/streaming.rs | 10 +++++----- 10 files changed, 26 insertions(+), 20 deletions(-) diff --git a/rig-core/examples/anthropic_streaming.rs b/rig-core/examples/anthropic_streaming.rs index 015189a..b61ef96 100644 --- a/rig-core/examples/anthropic_streaming.rs +++ b/rig-core/examples/anthropic_streaming.rs @@ -19,13 +19,11 @@ async fn main() -> Result<(), anyhow::Error> { stream_to_stdout(agent, &mut stream).await?; - if let Some(response) = stream.response { println!("Usage: {:?} tokens", response.usage.output_tokens); }; println!("Message: {:?}", stream.message); - - + Ok(()) } diff --git a/rig-core/examples/gemini_streaming.rs b/rig-core/examples/gemini_streaming.rs index 34f57bd..fc22907 100644 --- a/rig-core/examples/gemini_streaming.rs +++ b/rig-core/examples/gemini_streaming.rs @@ -20,7 +20,10 @@ async fn main() -> Result<(), anyhow::Error> { stream_to_stdout(agent, &mut stream).await?; if let Some(response) = stream.response { - println!("Usage: {:?} tokens", response.usage_metadata.total_token_count); + println!( + "Usage: {:?} tokens", + response.usage_metadata.total_token_count + ); }; println!("Message: {:?}", stream.message); diff --git a/rig-core/examples/gemini_streaming_with_tools.rs b/rig-core/examples/gemini_streaming_with_tools.rs index a5fb1c9..8c9bffe 100644 --- a/rig-core/examples/gemini_streaming_with_tools.rs +++ b/rig-core/examples/gemini_streaming_with_tools.rs @@ -109,10 +109,13 @@ async fn main() -> Result<(), anyhow::Error> { stream_to_stdout(calculator_agent, &mut stream).await?; if let Some(response) = stream.response { - println!("Usage: {:?} tokens", response.usage_metadata.total_token_count); + println!( + "Usage: {:?} tokens", + response.usage_metadata.total_token_count + ); }; println!("Message: {:?}", stream.message); - + Ok(()) } diff --git a/rig-core/examples/ollama_streaming_with_tools.rs b/rig-core/examples/ollama_streaming_with_tools.rs index 4b4427f..37b5854 100644 --- a/rig-core/examples/ollama_streaming_with_tools.rs +++ b/rig-core/examples/ollama_streaming_with_tools.rs @@ -113,6 +113,6 @@ async fn main() -> Result<(), anyhow::Error> { }; println!("Message: {:?}", stream.message); - + Ok(()) } diff --git a/rig-core/examples/openai_streaming.rs b/rig-core/examples/openai_streaming.rs index f668036..40ec569 100644 --- a/rig-core/examples/openai_streaming.rs +++ b/rig-core/examples/openai_streaming.rs @@ -16,11 +16,11 @@ async fn main() -> Result<(), anyhow::Error> { .await?; stream_to_stdout(agent, &mut stream).await?; - + if let Some(response) = stream.response { println!("Usage: {:?}", response.usage) }; - + println!("Message: {:?}", stream.message); Ok(()) diff --git a/rig-core/examples/openai_streaming_with_tools.rs b/rig-core/examples/openai_streaming_with_tools.rs index 72c4aeb..b83a1fa 100644 --- a/rig-core/examples/openai_streaming_with_tools.rs +++ b/rig-core/examples/openai_streaming_with_tools.rs @@ -107,12 +107,12 @@ async fn main() -> Result<(), anyhow::Error> { println!("Calculate 2 - 5"); let mut stream = calculator_agent.stream_prompt("Calculate 2 - 5").await?; stream_to_stdout(calculator_agent, &mut stream).await?; - + if let Some(response) = stream.response { println!("Usage: {:?}", response.usage) }; println!("Message: {:?}", stream.message); - + Ok(()) } diff --git a/rig-core/src/providers/gemini/streaming.rs b/rig-core/src/providers/gemini/streaming.rs index 66a6a82..e48c0b8 100644 --- a/rig-core/src/providers/gemini/streaming.rs +++ b/rig-core/src/providers/gemini/streaming.rs @@ -3,7 +3,6 @@ use futures::StreamExt; use serde::Deserialize; use super::completion::{create_request_body, gemini_api_types::ContentCandidate, CompletionModel}; -use crate::providers::gemini::completion::gemini_api_types::UsageMetadata; use crate::{ completion::{CompletionError, CompletionRequest}, streaming::{self, StreamingCompletionModel}, diff --git a/rig-core/src/providers/ollama.rs b/rig-core/src/providers/ollama.rs index a754fe2..52df410 100644 --- a/rig-core/src/providers/ollama.rs +++ b/rig-core/src/providers/ollama.rs @@ -486,7 +486,7 @@ impl StreamingCompletionModel for CompletionModel { continue; } } - + if response.done { yield Ok(RawStreamingChoice::FinalResponse(StreamingCompletionResponse { total_duration: response.total_duration, diff --git a/rig-core/src/providers/openai/streaming.rs b/rig-core/src/providers/openai/streaming.rs index c42f1a4..c3e83ac 100644 --- a/rig-core/src/providers/openai/streaming.rs +++ b/rig-core/src/providers/openai/streaming.rs @@ -92,7 +92,7 @@ pub async fn send_compatible_streaming_request( prompt_tokens: 0, total_tokens: 0 }; - + let mut partial_data = None; let mut calls: HashMap = HashMap::new(); @@ -183,9 +183,12 @@ pub async fn send_compatible_streaming_request( } } - + if let Some(content) = &choice.delta.content { + yield Ok(streaming::RawStreamingChoice::Message(content.clone())) + } } + if let Some(usage) = data.usage { final_usage = usage.clone(); } diff --git a/rig-core/src/streaming.rs b/rig-core/src/streaming.rs index 5edc2c3..9eff6ab 100644 --- a/rig-core/src/streaming.rs +++ b/rig-core/src/streaming.rs @@ -61,7 +61,8 @@ pub type StreamingResult = Pin, CompletionError>> + Send>>; #[cfg(target_arch = "wasm32")] -pub type StreamingResult = Pin>>>; +pub type StreamingResult = + Pin, CompletionError>>>>; pub struct StreamingCompletionResponse { inner: StreamingResult, @@ -92,14 +93,13 @@ impl Stream for StreamingCompletionResponse { match stream.inner.as_mut().poll_next(cx) { Poll::Pending => Poll::Pending, Poll::Ready(None) => { - let mut content = vec![]; stream.tool_calls.iter().for_each(|(n, d, a)| { content.push(AssistantContent::tool_call(n, d, a.clone())); }); - if content.len() == 0 || stream.text.len() > 0 { + if content.is_empty() || !stream.text.is_empty() { content.insert(0, AssistantContent::text(stream.text.clone())); } @@ -107,9 +107,9 @@ impl Stream for StreamingCompletionResponse { content: OneOrMany::many(content) .expect("There should be at least one assistant message"), }; - + Poll::Ready(None) - }, + } Poll::Ready(Some(Err(err))) => Poll::Ready(Some(Err(err))), Poll::Ready(Some(Ok(choice))) => match choice { RawStreamingChoice::Message(text) => {