This library has now been rewritten to use official OpenAI's ChatGPT API, instead of other unofficial workarounds.
If you have previously used this library, older versions (0.6.0 and below) are deprecated, unsupported, and will produce errors.
You should use the latest version, which is 1.0.0
.
Here is a simple usage of the API, getting completion for a single message.
You can see more practical examples in the examples
directory.
use chatgpt::prelude::*;
#[tokio::main]
async fn main() -> Result<()> {
// Getting the API key here
let key = args().nth(1).unwrap();
/// Creating a new ChatGPT client.
/// Note that it requires an API key, and uses
/// tokens from your OpenAI API account balance.
let client = ChatGPT::new(key)?;
/// Sending a message and getting the completion
let response: CompletionResponse = client
.send_message("Describe in five words the Rust programming language.")
.await?;
println!("Response: {}", response.message().content);
Ok(())
}
Conversations are the threads in which ChatGPT can analyze previous messages and chain it's thoughts. They also automatically store all the message history.
Here is an example:
// Creating a new conversation
let mut conversation: Conversation = client.new_conversation();
// Sending messages to the conversation
let response_a: CompletionResponse = conversation
.send_message("Could you describe the Rust programming language in 5 words?")
.await?;
let response_b: CompletionResponse = conversation
.send_message("Now could you do the same, but for Kotlin?")
.await?;
// You can also access the message history itself
for message in &conversation.history {
println!("{message:#?}")
}
This way of creating a conversation creates it with the default introductory message, which roughly is:
You are ChatGPT, an AI model developed by OpenAI. Answer as concisely as possible. Today is: {today's date}
.
However, you can specify the introductory message yourself this way:
let mut conversation: Conversation = client.new_conversation_directed("You are RustGPT, when answering any questions, you always shift the topic of the conversation to the Rust programming language.");
// Continue with the new conversation
You can currently store the conversation's message in two formats: JSON or postcard.
They can be toggled on or off using the json
and postcard
features respectively.
Since the ChatMessage
struct derives serde's Serialize
and Deserialize
traits, you can also use any serde-compatible serialization library,
as the history
field and the Conversation::new_with_history()
method are public in the Conversation
struct.
Requires the json
feature (enabled by default)
// Create a new conversation here
let mut conversation: Conversation = ...;
// ... send messages to the conversation ...
// Saving the conversation
conversation.save_history_json("my-conversation.json").await?;
// You can later read this conversation history again
let mut restored = client
.restore_conversation_json("my-conversation.json")
.await?;
Requires the postcard
feature (disabled by default)
// Create a new conversation here
let mut conversation: Conversation = ...;
// ... send messages to the conversation ...
// Saving the conversation
conversation.save_history_postcard("my-conversation.bin").await?;
// You can later read this conversation history again
let mut restored = client
.restore_conversation_postcard("my-conversation.bin")
.await?;