Struct Conversation

Source
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

Source

pub fn new(client: ChatGPT, first_message: String) -> Self

Constructs a new conversation from an API client and the introductory message

Source

pub fn new_with_history(client: ChatGPT, history: Vec<ChatMessage>) -> Self

Constructs a new conversation from a pre-initialized chat history

Source

pub fn rollback(&mut self) -> Option<ChatMessage>

Rollbacks the history by 1 message, removing the last sent and received message.

Source

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

Source

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>>
where S: 'async_recursion + Into<String> + Send + Sync, 'life_self: 'async_recursion,

Sends a message from a specified role to the ChatGPT API and returns the completion response.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub async fn save_history_json<P: AsRef<Path>>(&self, to: P) -> Result<()>

Saves the history to a local JSON file, that can be restored to a conversation at runtime later.

Source

pub async fn save_history_postcard<P: AsRef<Path>>(&self, to: P) -> Result<()>

Saves the history to a local postcard file, that can be restored to a conversation at runtime later.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,