pub struct Conversation {
pub history: Vec<ChatMessage>,
pub always_send_functions: bool,
/* private fields */
}
Expand description
Stores a single conversation session, and automatically saves message history
Fields§
§history: Vec<ChatMessage>
All the messages sent and received, starting with the beginning system message
always_send_functions: bool
Set to true
if you want to automatically send all functions to API with each message.
Functions are counted as tokens internally, so it is set to false
by default.
Implementations§
Source§impl Conversation
impl Conversation
Sourcepub fn new(client: ChatGPT, first_message: String) -> Self
pub fn new(client: ChatGPT, first_message: String) -> Self
Constructs a new conversation from an API client and the introductory message
Sourcepub fn new_with_history(client: ChatGPT, history: Vec<ChatMessage>) -> Self
pub fn new_with_history(client: ChatGPT, history: Vec<ChatMessage>) -> Self
Constructs a new conversation from a pre-initialized chat history
Sourcepub fn rollback(&mut self) -> Option<ChatMessage>
pub fn rollback(&mut self) -> Option<ChatMessage>
Rollbacks the history by 1 message, removing the last sent and received message.
Sourcepub fn add_function<A: FunctionArgument + Send + Sync + 'static, C: CallableAsyncFunction<A> + Send + Sync + 'static>(
&mut self,
prebuilt: GptFunction<A, C>,
) -> Result<()>
pub fn add_function<A: FunctionArgument + Send + Sync + 'static, C: CallableAsyncFunction<A> + Send + Sync + 'static>( &mut self, prebuilt: GptFunction<A, C>, ) -> Result<()>
Adds a function that can later be called by ChatGPT
Sourcepub fn send_role_message<'life_self, 'async_recursion, S>(
&'life_self mut self,
role: Role,
message: S,
) -> Pin<Box<dyn Future<Output = Result<CompletionResponse>> + Send + 'async_recursion>>
pub fn send_role_message<'life_self, 'async_recursion, S>( &'life_self mut self, role: Role, message: S, ) -> Pin<Box<dyn Future<Output = Result<CompletionResponse>> + Send + 'async_recursion>>
Sends a message from a specified role to the ChatGPT API and returns the completion response.
Sourcepub async fn send_message<S: Into<String> + Send + Sync>(
&mut self,
message: S,
) -> Result<CompletionResponse>
pub async fn send_message<S: Into<String> + Send + Sync>( &mut self, message: S, ) -> Result<CompletionResponse>
Sends the message to the ChatGPT API and returns the completion response.
Execution speed depends on API response times.
Sourcepub async fn send_message_functions<S: Into<String>>(
&mut self,
message: S,
) -> Result<CompletionResponse>
pub async fn send_message_functions<S: Into<String>>( &mut self, message: S, ) -> Result<CompletionResponse>
Sends a message with all functions to the ChatGPT API and returns the completion response.
NOTE: Functions are counted as tokens internally.
Sourcepub async fn send_role_message_streaming<S: Into<String>>(
&mut self,
role: Role,
message: S,
) -> Result<impl Stream<Item = Result<ResponseChunk>>>
pub async fn send_role_message_streaming<S: Into<String>>( &mut self, role: Role, message: S, ) -> Result<impl Stream<Item = Result<ResponseChunk>>>
Sends a message with specified role to the ChatGPT API and returns the completion response as stream.
Note, that this method will not automatically save the received message to history, as it is returned in streamed chunks. You will have to collect them into chat message yourself.
You can use ChatMessage::from_response_chunks
for this
Requires the streams
crate feature.
Sourcepub async fn send_message_streaming<S: Into<String>>(
&mut self,
message: S,
) -> Result<impl Stream<Item = Result<ResponseChunk>>>
pub async fn send_message_streaming<S: Into<String>>( &mut self, message: S, ) -> Result<impl Stream<Item = Result<ResponseChunk>>>
Sends the message to the ChatGPT API and returns the completion response as stream.
Note, that this method will not automatically save the received message to history, as it is returned in streamed chunks. You will have to collect them into chat message yourself.
You can use ChatMessage::from_response_chunks
for this
Requires the streams
crate feature.
Auto Trait Implementations§
impl Freeze for Conversation
impl !RefUnwindSafe for Conversation
impl Send for Conversation
impl Sync for Conversation
impl Unpin for Conversation
impl !UnwindSafe for Conversation
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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>
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>
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