pub struct ChatGPT {
    pub session_id: String,
    pub chat_context: ChatContext,
    /* private fields */
}
Expand description

The ChatGPT object

Fields§

§session_id: String§chat_context: ChatContext

Implementations§

source§

impl ChatGPT

source

pub fn new( openai_api_token: String, chat_context: Option<ChatContext>, session_id: Option<String> ) -> Result<ChatGPT>

Create a new ChatGPT object

Arguments
  • openai_api_token - The API token from OpenAI
  • chat_context - The context of the chatbot. Optional. If not provided, it will start a new context with the default model
  • session_id - The session ID of the chatbot. Optional. If not provided, it will generate a new session ID. This will be useful to track the conversation history
Example
use chatgpt_functions::chat_gpt::ChatGPT;
use anyhow::Result;

#[tokio::main]
async fn main() -> Result<()> {
    let key = std::env::var("OPENAI_API_KEY")?;
    let mut gpt = ChatGPT::new(key, None, None)?;
    Ok(())
}
Errors

It returns an error if the API token is not valid

Panics

It panics if the API token is not provided

Remarks

The API token can be found on the OpenAI API keys

source

pub async fn completion(&mut self) -> Result<ChatResponse>

Calls the OpenAI API to get a response using the current context

Arguments
  • message - The message to send to the AI
Errors

It returns an error if the API token is not valid It returns an error if the response from the API is not valid or if the content of the response is not valid

Panics

It panics if the API token is not provided

Remarks

The context is updated with the response from the AI

source

pub async fn completion_managed( &mut self, content: String ) -> Result<ChatResponse>

Calls the OpenAI API to get a response using the current context, adding the content provided by the user This is the preferred function to use for chat completions that work with context.

This is a fully managed function, it does update the context with the message provided, and it does update the context with the response from the AI. It calls completion_with_user_content_updating_context internally, it’s for convenience.

Arguments
  • content - The content of the message
Errors

It returns an error if the API token is not valid It returns an error if the response from the API is not valid or if the content of the response is not valid

Panics

It panics if the API token is not provided

Remarks

This is a fully managed function, it does update the context with the message provided, and it does update the context with the response from the AI.

source

pub async fn completion_with_message( &mut self, message: Message ) -> Result<ChatResponse>

This function is used to call the openai API, using a Message already prepared. It requires a Message object as an argument, so access to some internal work of the library. This gives more flexibility to the user, but it is not recommended to use it directly. It returns the response from the AI It does update the context with the message provided, but it does not update the context with the response from the AI

Arguments
  • message - The message to send to the AI
Errors

It returns an error if the API token is not valid It returns an error if the response from the API is not valid or if the content of the response is not valid

Remarks

The context is updated with the message provided The context is not updated with the response from the AI This function is used by the other functions of the library It is not recommended to use it directly

source

pub async fn completion_with_user_content( &mut self, content: String ) -> Result<ChatResponse>

This function is used to call the openai API, using a String as the content of the message. It returns the response from the AI It does update the context with the message provided, but it does not update the context with the response from the AI

Arguments
  • content - The content of the message
Errors

It returns an error if the API token is not valid It returns an error if the response from the API is not valid or if the content of the response is not valid

Remarks

The context is updated with the message provided The context is not updated with the response from the AI This function is used by the other functions of the library It is not recommended to use it directly

source

pub async fn completion_with_user_content_updating_context( &mut self, content: String ) -> Result<ChatResponse>

This function is used to call the openai API, using content as the content of the message. It returns the response from the AI It does update the context with the message provided and the response from the AI

Arguments
  • content - The content of the message
Errors

It returns an error if the API token is not valid It returns an error if the response from the API is not valid or if the content of the response is not valid

Remarks

The context is updated with the message provided The context is updated with the response from the AI This function is used by the other functions of the library It assumes that there will only be one choice in the response It returns the response from the AI

source

pub async fn completion_with_message_updating_context( &mut self, message: Message ) -> Result<ChatResponse>

This function is used to update the context with the response from the AI It assumes that there will only be one choice in the response It returns the response from the AI It does update the context with the response from the AI

Arguments
  • message - The message to send to the AI
Errors

It returns an error if the API token is not valid It returns an error if the response from the API is not valid or if the content of the response is not valid

Remarks

The context is updated with the response from the AI This function is used by the other functions of the library It assumes that there will only be one choice in the response It panics if there is more than one choice in the response

source

pub fn push_message(&mut self, message: Message)

This function is used to push a message to the context This is a low level function, it is not recommended to use it directly

Arguments
  • message - The message to push to the context
Remarks

This function is used by the other functions of the library

source

pub fn set_messages(&mut self, messages: Vec<Message>)

This function is used to set all the messages in the context This will override the current messages in the context This is a low level function, it is not recommended to use it directly

Arguments
  • messages - The messages to set in the context
Remarks

This function is used by the other functions of the library

source

pub fn push_function(&mut self, function: FunctionSpecification)

This function is used to push a function to the context This is a low level function, it is not recommended to use it directly

Arguments
  • function - The function to push to the context
Remarks

This function is used by the other functions of the library

source

pub fn set_functions(&mut self, functions: Vec<FunctionSpecification>)

This function is used to set all the functions in the context This will override the current functions in the context This is a low level function, it is not recommended to use it directly

Arguments
  • functions - The vec of functions to set in the context
Remarks

This function is used by the other functions of the library

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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