pub struct Chat<A> { /* private fields */ }Expand description
A conversation with an agent: every send carries the turns
before it, and the conversation keeps its own State.
use gemini_adk_rs::llm::{LlmResponse, MockLlm};
use gemini_adk_rs::text::{LlmTextAgent, TextAgent};
let llm = MockLlm::script([
LlmResponse::from_text("Nice to meet you, Ada."),
LlmResponse::from_text("Your name is Ada."),
]);
let agent = LlmTextAgent::new("assistant", llm.clone());
let mut chat = agent.chat();
chat.send("Hi, I'm Ada.").await.unwrap();
assert_eq!(chat.send("What's my name?").await.unwrap(), "Your name is Ada.");
// The second request carried the whole conversation.
assert_eq!(llm.last_request().unwrap().contents.len(), 3);
assert_eq!(chat.history().len(), 4);agent.chat() borrows the agent; Chat::new(agent) takes it, for a
conversation that outlives the scope that built the agent.
Implementations§
Source§impl<A: TextAgent> Chat<A>
impl<A: TextAgent> Chat<A>
Sourcepub fn new(agent: A) -> Self
pub fn new(agent: A) -> Self
A new conversation with agent, with empty history and fresh state.
Sourcepub fn with_state(agent: A, state: State) -> Self
pub fn with_state(agent: A, state: State) -> Self
A new conversation that reads and writes state.
Sourcepub async fn send(
&mut self,
message: impl Into<String>,
) -> Result<String, AgentError>
pub async fn send( &mut self, message: impl Into<String>, ) -> Result<String, AgentError>
Send a message and get the reply. The turn is added to the history only when it succeeds.
Sourcepub async fn send_request(
&mut self,
request: RunRequest,
) -> Result<RunResult, AgentError>
pub async fn send_request( &mut self, request: RunRequest, ) -> Result<RunResult, AgentError>
Send a request (media, a response schema) and get the full result. The request’s own history is replaced by the conversation’s.
Sourcepub fn send_stream(
&mut self,
message: impl Into<String>,
) -> BoxStream<'_, Result<RunEvent, AgentError>>
pub fn send_stream( &mut self, message: impl Into<String>, ) -> BoxStream<'_, Result<RunEvent, AgentError>>
Send a message and stream the reply; the turn joins the history when
the stream delivers RunEvent::Finished.
Sourcepub fn usage(&self) -> TokenUsage
pub fn usage(&self) -> TokenUsage
Tokens used by the conversation so far.
Auto Trait Implementations§
impl<A> Freeze for Chat<A>where
A: Freeze,
impl<A> !RefUnwindSafe for Chat<A>
impl<A> Send for Chat<A>where
A: Send,
impl<A> Sync for Chat<A>where
A: Sync,
impl<A> Unpin for Chat<A>where
A: Unpin,
impl<A> !UnwindSafe for Chat<A>
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<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>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> 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 more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Applies the layer to a service and wraps it in [
Layered].