pub struct ChatGPT {
pub model: String,
pub session_id: String,
pub chat_context: ChatContext,
/* private fields */
}
Expand description
The ChatGPT object
Fields§
§model: String
§session_id: String
§chat_context: ChatContext
Implementations§
Source§impl ChatGPT
impl ChatGPT
Sourcepub fn new(
client: Client,
model: String,
openai_api_token: String,
session_id: String,
chat_context: ChatContext,
) -> Result<ChatGPT>
pub fn new( client: Client, model: String, openai_api_token: String, session_id: String, chat_context: ChatContext, ) -> Result<ChatGPT>
Create a new ChatGPT object
§Arguments
openai_api_token
- The API token from OpenAIchat_context
- The context of the chatbot. Optional. If not provided, it will start a new context with the default modelsession_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::ChatGPTBuilder;
use anyhow::Result;
#[tokio::main]
async fn main() -> Result<()> {
let key = std::env::var("OPENAI_API_KEY").unwrap_or("test".to_string());
let mut gpt = ChatGPTBuilder::new().openai_api_token(key).build()?;
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
Sourcepub async fn completion(&mut self) -> Result<ChatResponse>
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
Sourcepub async fn completion_managed(
&mut self,
content: String,
) -> Result<ChatResponse>
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.
Sourcepub async fn completion_with_message(
&mut self,
message: Message,
) -> Result<ChatResponse>
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
Sourcepub async fn completion_with_user_content(
&mut self,
content: String,
) -> Result<ChatResponse>
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
Sourcepub async fn completion_with_user_content_updating_context(
&mut self,
content: String,
) -> Result<ChatResponse>
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
Sourcepub async fn completion_with_message_updating_context(
&mut self,
message: Message,
) -> Result<ChatResponse>
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
Important: The message received from the AI has to be modified when it is a function This is because when a function is returned the model still says that it is an assistant message. This is a bug in the API. If this is inserted in the context, the next request to the API will fail since it won’t conform with the rules of the model. https://platform.openai.com/docs/api-reference/chat/create#chat/create-messages
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
Sourcepub fn push_message(&mut self, message: Message)
pub fn push_message(&mut self, message: Message)
Sourcepub fn set_messages(&mut self, messages: Vec<Message>)
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
Sourcepub fn push_function(&mut self, function: FunctionSpecification)
pub fn push_function(&mut self, function: FunctionSpecification)
Sourcepub fn set_functions(&mut self, functions: Vec<FunctionSpecification>)
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
Sourcepub fn last_content(&self) -> Option<String>
pub fn last_content(&self) -> Option<String>
This function is used to retrieve the content of the last message in the context
Sourcepub fn last_function(&self) -> Option<(String, String)>
pub fn last_function(&self) -> Option<(String, String)>
This function is used to retrieve the function_call of the last message in the context