#[non_exhaustive]pub struct InvokeModelStage;Expand description
The model call (InvokeModelStage): rate-limit, cache, route or transform model inputs.
Hooks fire outside the chain: BeforeModelCallEvent before it, AfterModelCallEvent after,
once per call even when a handler short-circuits or retries. Model state is not on the
context; the agent commits the provider’s writes after the whole chain succeeds.
use strands::Agent;
use strands::middleware::InvokeModelStage;
use strands::testing::MockModel;
let model = MockModel::new().with_turn("Hello");
let mut agent = Agent::builder().model(model.clone()).printer(false).build()?;
agent.add_middleware(InvokeModelStage::input(|mut context| async move {
context.system_prompt = Some("Answer in French.".into());
Ok(context)
}));
agent.invoke("Hi").await?;
assert_eq!(model.requests()[0].system_prompt, Some("Answer in French.".into()));Implementations§
Source§impl InvokeModelStage
impl InvokeModelStage
Sourcepub fn input<F, Fut>(handler: F) -> Middleware<Self>where
F: Fn(<Self as MiddlewareStage>::Context) -> Fut + Send + Sync + 'static,
Fut: Future<Output = StageResult<<Self as MiddlewareStage>::Context>> + Send + 'static,
pub fn input<F, Fut>(handler: F) -> Middleware<Self>where
F: Fn(<Self as MiddlewareStage>::Context) -> Fut + Send + Sync + 'static,
Fut: Future<Output = StageResult<<Self as MiddlewareStage>::Context>> + Send + 'static,
An Input handler (.Input): transforms the context before the stage runs.
Sourcepub fn output<F, Fut>(handler: F) -> Middleware<Self>where
F: Fn(<Self as MiddlewareStage>::Result) -> Fut + Send + Sync + 'static,
Fut: Future<Output = StageResult<<Self as MiddlewareStage>::Result>> + Send + 'static,
pub fn output<F, Fut>(handler: F) -> Middleware<Self>where
F: Fn(<Self as MiddlewareStage>::Result) -> Fut + Send + Sync + 'static,
Fut: Future<Output = StageResult<<Self as MiddlewareStage>::Result>> + Send + 'static,
An Output handler (.Output): transforms the result after the stage runs.
Sourcepub fn wrap<F>(handler: F) -> Middleware<Self>where
F: Fn(<Self as MiddlewareStage>::Context, MiddlewareNext<Self>) -> StageStream<Self> + Send + Sync + 'static,
pub fn wrap<F>(handler: F) -> Middleware<Self>where
F: Fn(<Self as MiddlewareStage>::Context, MiddlewareNext<Self>) -> StageStream<Self> + Send + Sync + 'static,
A Wrap handler (the bare stage or .Wrap): gets the context and next.
Trait Implementations§
Source§impl Clone for InvokeModelStage
impl Clone for InvokeModelStage
impl Copy for InvokeModelStage
Source§impl Debug for InvokeModelStage
impl Debug for InvokeModelStage
Source§impl MiddlewareStage for InvokeModelStage
impl MiddlewareStage for InvokeModelStage
Auto Trait Implementations§
impl Freeze for InvokeModelStage
impl RefUnwindSafe for InvokeModelStage
impl Send for InvokeModelStage
impl Sync for InvokeModelStage
impl Unpin for InvokeModelStage
impl UnsafeUnpin for InvokeModelStage
impl UnwindSafe for InvokeModelStage
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.