pub struct FakeProvider { /* private fields */ }Expand description
A programmable fake Provider — a script is a sequence of per-turn
replies, consumed in order by chat / stream_chat.
- Each call consumes one script turn and records the request; once the
script is exhausted, calls return
ProviderError::Api(“script exhausted”) and do not replay — an Agent running an extra turn fails explicitly right away in tests; chatandstream_chatconsume the same script with the same semantics, differing only in how the reply is delivered (an event stream = several increments + oneDone);requestsreturns a snapshot of the received request history for tests to assert what the Agent sent to the model — since a fake is lenient (e.g. if the Agent forgets to feed back tool results, a real API would error but the fake would not), this is the only way to catch such process bugs.
§Examples
use molo::message::Message;
use molo::provider::{ChatRequest, FakeProvider, FakeReply, Provider};
let fake = FakeProvider::new([
FakeReply::Text("hi".into()),
FakeReply::Text("bye".into()),
]);
let r = fake.chat(ChatRequest::default()).await?;
assert_eq!(r.message, Message::assistant("hi"));Implementations§
Source§impl FakeProvider
impl FakeProvider
Sourcepub fn new(replies: impl IntoIterator<Item = FakeReply>) -> Self
pub fn new(replies: impl IntoIterator<Item = FakeReply>) -> Self
Constructs from a script sequence; chat / stream_chat consume it
in order and error once exhausted.
Sourcepub fn push(&self, reply: FakeReply)
pub fn push(&self, reply: FakeReply)
Appends one reply to the end of the script (for staged injection in tests).
Sourcepub fn requests(&self) -> Vec<ChatRequest>
pub fn requests(&self) -> Vec<ChatRequest>
A snapshot of the received request history, for tests to assert what the Agent sent to the model.
§Examples
use molo::message::Message;
use molo::provider::{ChatRequest, FakeProvider, FakeReply, Provider};
let fake = FakeProvider::new([FakeReply::Text("hi".into())]);
fake.chat(ChatRequest {
messages: vec![Message::user("hi")],
..Default::default()
})
.await?;
assert_eq!(fake.requests()[0].messages, vec![Message::user("hi")]);Trait Implementations§
Source§impl Clone for FakeProvider
impl Clone for FakeProvider
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Clones are independent copies: each holds and consumes its own script queue and request history without affecting the other (script calls made before the clone still consume from the start in the clone).
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for FakeProvider
impl Debug for FakeProvider
Source§impl Default for FakeProvider
impl Default for FakeProvider
Source§fn default() -> FakeProvider
fn default() -> FakeProvider
Returns the “default value” for a type. Read more
Source§impl Provider for FakeProvider
impl Provider for FakeProvider
Source§fn chat<'life0, 'async_trait>(
&'life0 self,
request: ChatRequest,
) -> Pin<Box<dyn Future<Output = Result<ChatResponse, ProviderError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn chat<'life0, 'async_trait>(
&'life0 self,
request: ChatRequest,
) -> Pin<Box<dyn Future<Output = Result<ChatResponse, ProviderError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Sends one turn of conversation and returns the model’s reply (text, or
a request to call tools). Read more
Source§fn stream_chat<'life0, 'async_trait>(
&'life0 self,
request: ChatRequest,
) -> Pin<Box<dyn Future<Output = Result<BoxStream<'static, Result<StreamEvent, ProviderError>>, ProviderError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn stream_chat<'life0, 'async_trait>(
&'life0 self,
request: ChatRequest,
) -> Pin<Box<dyn Future<Output = Result<BoxStream<'static, Result<StreamEvent, ProviderError>>, ProviderError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Streams one turn of conversation, returning the Assistant’s reply
incrementally. Read more
Auto Trait Implementations§
impl !Freeze for FakeProvider
impl RefUnwindSafe for FakeProvider
impl Send for FakeProvider
impl Sync for FakeProvider
impl Unpin for FakeProvider
impl UnsafeUnpin for FakeProvider
impl UnwindSafe for FakeProvider
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more