Crates.io | chat-gpt-lib-rs |
lib.rs | chat-gpt-lib-rs |
version | 0.5.1 |
source | src |
created_at | 2023-03-15 16:58:48.016649 |
updated_at | 2024-05-22 16:04:21.208225 |
description | A Rust library for interacting with OpenAI's ChatGPT API, providing a simple interface to make API requests and handle responses. |
homepage | |
repository | https://github.com/Arend-Jan/chat-gpt-lib-rs |
max_upload_size | |
id | 810856 |
size | 102,471 |
A Rust library for interacting with OpenAI's ChatGPT API. This library simplifies the process of making requests to the ChatGPT API and parsing responses.
Utilizes Rustls for the TLS layer, eliminating the need for OpenSSL and enabling seamless native execution on Linux with musl.
Add the following line to your 'Cargo.toml' file under the '[dependencies]' section:
chat-gpt-lib-rs = "<put here the latest and greatest version number>"
Then, run cargo build to download and compile the dependencies.
First, import the necessary components:
use chat_gpt_lib_rs::{ChatGPTClient, ChatInput, Message, Model, Role};
Next, create a new client with your API key:
let api_key = "your_api_key_here";
let base_url = "https://api.openai.com";
let client = ChatGPTClient::new(api_key, base_url);
To send a chat message, create a ChatInput structure and call the chat method:
let chat_input = ChatInput {
model: Model::Gpt_4o,
messages: vec![
Message {
role: Role::System,
content: "You are a helpful assistant.".to_string(),
},
Message {
role: Role::User,
content: "Who won the world series in 2020?".to_string(),
},
],
..Default::default()
};
let response = client.chat(chat_input).await.unwrap();
The response will be a 'ChatResponse' structure containing the API response data.
Two example CLI chat applications are provided in the examples folder:
The cli-simple-chat-example.rs demonstrates how to use the chat-gpt-lib-rs library to interact with an AI model based on the GPT-3 architecture through a command-line interface. To run the example, first set your OPENAI_API_KEY in the .env file or as an environment variable, and then execute the following command:
cargo run --example cli-simple-chat-example
The example will prompt the user to enter a question, and the AI chatbot will respond with an answer. The conversation will continue until the user exits the program.
Optionally, you can provide initial user input as a command-line argument:
cargo run --example cli-simple-chat-example "Hello, computer!"
The cli-chat-example.rs demonstrates how to use the chat-gpt-lib-rs library to create an interactive AI chatbot with a command-line interface. To run the example, first set your OPENAI_API_KEY in the .env file or as an environment variable, and then execute the following command:
cargo run --example cli-chat-example
The example will prompt the user to enter a message, and the AI chatbot will respond with an answer. The conversation will continue until the user exits the program.
Optionally, you can provide initial user input as a command-line argument:
cargo run --example cli-chat-example "Hello, computer!"
For an enhanced experience with icons, use a terminal that supports Nerd Fonts. To enable this feature set you USE_ICONS=true in the .env file or as en environment variable.
For more details about the request parameters and response structure, refer to the OpenAI API documentation.
We welcome contributions to the chat-gpt-lib-rs
project! Whether it's reporting bugs, proposing new features, improving documentation, or contributing code, your help is greatly appreciated. Here's how you can contribute:
chat-gpt-lib-rs
repository to your own GitHub account. This will create a copy of the repository that you can modify without affecting the original project.chat-gpt-lib-rs
repository. In your pull request, describe the changes you made and why you think they should be included in the project.Remember, contributions to open source projects like chat-gpt-lib-rs
are a collaborative effort. Be respectful and patient with other contributors, and remember that everyone is working together to improve the project.
Thank you for your interest in contributing to chat-gpt-lib-rs
!
There is an interesting project teachlead now utilizing this project.
This project is licensed under the Apache License 2.0. See the LICENSE file for details.