pub struct MockModel { /* private fields */ }Expand description
A scripted model for tests.
With one turn, every call replays it. With several, each call takes the next one, and a call
after the last fails with All turns have been consumed. Every request is recorded. A call
takes its turn and records its request when its stream is first polled, not when
Model::stream returns.
use futures::StreamExt;
use strands::Model;
use strands::models::AggregatedEvent;
use strands::testing::{MockModel, MockTurn};
let model = MockModel::new();
model.add_turn("Hello").add_turn(MockTurn::throttled("slow down"));
futures::executor::block_on(async {
let mut stream = model.stream_aggregated(Default::default());
let mut text = String::new();
while let Some(item) = stream.next().await {
if let AggregatedEvent::Done(result) = item.unwrap() {
text = result.message.text();
}
}
assert_eq!(text, "Hello");
});
assert_eq!(model.call_count(), 1);Implementations§
Source§impl MockModel
impl MockModel
Sourcepub fn from_turns(turns: impl IntoIterator<Item = impl Into<MockTurn>>) -> Self
pub fn from_turns(turns: impl IntoIterator<Item = impl Into<MockTurn>>) -> Self
A model scripted with turns.
Sourcepub fn with_turn(self, turn: impl Into<MockTurn>) -> Self
pub fn with_turn(self, turn: impl Into<MockTurn>) -> Self
Adds a turn to the script, by value, for building a model in one expression.
Sourcepub fn add_turn(&self, turn: impl Into<MockTurn>) -> &Self
pub fn add_turn(&self, turn: impl Into<MockTurn>) -> &Self
Adds a turn to the script of a model that is already shared.
Sourcepub fn call_count(&self) -> usize
pub fn call_count(&self) -> usize
The number of multi-turn calls made so far; a single-turn script does not advance it.
Sourcepub fn requests(&self) -> Vec<ModelRequest>
pub fn requests(&self) -> Vec<ModelRequest>
Every request received, in order.
Trait Implementations§
Source§impl Clone for MockModel
Clones share the script and the recorded requests, so a test can keep one clone to inspect
while the agent holds another.
impl Clone for MockModel
Clones share the script and the recorded requests, so a test can keep one clone to inspect while the agent holds another.
Source§impl Model for MockModel
impl Model for MockModel
Source§fn stream(
&self,
request: ModelRequest,
) -> BoxStream<'static, Result<ModelStreamEvent>>
fn stream( &self, request: ModelRequest, ) -> BoxStream<'static, Result<ModelStreamEvent>>
Streams the model’s response to
request as raw events. Read moreSource§fn get_config(&self) -> BaseModelConfig
fn get_config(&self) -> BaseModelConfig
The current configuration.
Source§fn update_config(&self, config: BaseModelConfig) -> Result<()>
fn update_config(&self, config: BaseModelConfig) -> Result<()>
Merges
config into the configuration: every field it sets replaces the current value
(BaseModelConfig::merge). Read moreSource§fn stream_aggregated(
&self,
request: ModelRequest,
) -> BoxStream<'static, Result<AggregatedEvent>>
fn stream_aggregated( &self, request: ModelRequest, ) -> BoxStream<'static, Result<AggregatedEvent>>
Streams the response with content blocks and the final message assembled. Read more
Source§fn model_info(&self) -> ModelInfo
fn model_info(&self) -> ModelInfo
Everything the SDK knows about the configured model, resolved on each call. Read more
Source§fn stateful(&self) -> bool
fn stateful(&self) -> bool
Whether the model keeps conversation state server-side (
stateful), from
model_info().capabilities.stateful; false when unknown. Read moreSource§fn count_tokens<'life0, 'life1, 'async_trait>(
&'life0 self,
messages: &'life1 [Message],
options: CountTokensOptions,
) -> Pin<Box<dyn Future<Output = Result<u64>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn count_tokens<'life0, 'life1, 'async_trait>(
&'life0 self,
messages: &'life1 [Message],
options: CountTokensOptions,
) -> Pin<Box<dyn Future<Output = Result<u64>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Estimates the input tokens of a request before sending it. Read more
Auto Trait Implementations§
impl !Freeze for MockModel
impl RefUnwindSafe for MockModel
impl Send for MockModel
impl Sync for MockModel
impl Unpin for MockModel
impl UnsafeUnpin for MockModel
impl UnwindSafe for MockModel
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreCreates a shared type from an unshared type.