Skip to content

Commit

Permalink
adding rustls support (#67)
Browse files Browse the repository at this point in the history
* adding rustls support
  • Loading branch information
Arend-Jan authored May 22, 2024
1 parent 4ced92f commit 0b71ca1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
59 changes: 55 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,34 @@ jobs:
- uses: actions/checkout@v4
with:
submodules: true
- name: Install nasm
if: runner.os == 'Windows'
run: choco install nasm -y
- name: Verify NASM Installation
if: runner.os == 'Windows'
shell: powershell
run: |
$env:Path += ";C:\Program Files\NASM"
[System.Environment]::SetEnvironmentVariable("Path", $env:Path, [System.EnvironmentVariableTarget]::Process)
Write-Host "Checking NASM version..."
nasm -v
- name: Install stable
uses: dtolnay/rust-toolchain@stable
- name: cargo generate-lockfile
- name: cargo generate-lockfile
if: hashFiles('Cargo.lock') == ''
run: cargo generate-lockfile
- name: cargo test
run: cargo test --locked --all-features --all-targets
run: |
cargo generate-lockfile
- name: cargo test - Windows
if: runner.os == 'Windows'
run: |
$env:Path += ";C:\Program Files\NASM"
[System.Environment]::SetEnvironmentVariable("Path", $env:Path, [System.EnvironmentVariableTarget]::Process)
cargo test --locked --all-features --all-targets
- name: cargo test - Mac
if: runner.os == 'macOS'
run: |
cargo test --locked --all-features --all-targets
coverage:
runs-on: ubuntu-latest
name: ubuntu / stable / coverage
Expand All @@ -67,3 +88,33 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
alpine-test:
runs-on: ubuntu-latest
name: alpine / stable
steps:
- uses: actions/checkout@v4
with:
submodules: true
- name: Set up Docker container
run: |
docker pull alpine:latest
docker run -d --name alpine-container -v ${{ github.workspace }}:/workspace -w /workspace alpine:latest tail -f /dev/null
- name: Install dependencies
run: |
docker exec alpine-container sh -c "
apk update &&
apk add --no-cache musl-dev gcc rust cargo rustup &&
rustup-init -y &&
source /root/.cargo/env &&
rustup toolchain install stable-x86_64-unknown-linux-musl &&
rustup default stable-x86_64-unknown-linux-musl
"
- name: cargo generate-lockfile
if: hashFiles('Cargo.lock') == ''
run: docker exec alpine-container sh -c "source /root/.cargo/env && cargo generate-lockfile"
- name: cargo test --locked
run: docker exec alpine-container sh -c "source /root/.cargo/env && cargo test --locked --all-features --all-targets"
- name: Clean up Docker container
if: always()
run: docker rm -f alpine-container
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "chat-gpt-lib-rs"
version = "0.4.1"
version = "0.5.0"
edition = "2021"
description = "A Rust library for interacting with OpenAI's ChatGPT API, providing a simple interface to make API requests and handle responses."
license = "Apache-2.0"
Expand All @@ -18,7 +18,8 @@ path = "examples/cli-chat-example.rs"
[dependencies]
env_logger = "0.11"
log = "0.4"
reqwest = { version = "0.12", features = ["json"] }
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
rustls = "0.23"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1.37", features = ["full"] }
Expand All @@ -27,3 +28,4 @@ tokio = { version = "1.37", features = ["full"] }
dotenvy = "0.15"
console = "0.15"
indicatif = "0.17"

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ A Rust library for interacting with OpenAI's ChatGPT API. This library simplifie
* An example CLI chat application that demonstrates library usage
* An token estimation functionality

Utilizes Rustls for the TLS layer, eliminating the need for OpenSSL and enabling seamless native execution on Linux with musl.

## Installation
Add the following line to your 'Cargo.toml' file under the '[dependencies]' section:
```toml
Expand Down
4 changes: 3 additions & 1 deletion examples/cli-chat-example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize the message history with a system message
let mut messages = vec![Message {
role: Role::System,
content: "Behave and talk like the computer from star trek.".to_string(),
content:
"Be a helpfull pair programmer, who want to show solutions and examples in code blocks"
.to_string(),
}];

// Check if any command line arguments are provided
Expand Down
7 changes: 6 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,15 @@ impl ChatGPTClient {
/// * `api_key` - The API key for the ChatGPT API.
/// * `base_url` - The base URL for the ChatGPT API.
pub fn new(api_key: &str, base_url: &str) -> Self {
let client = Client::builder()
.use_rustls_tls()
.build()
.expect("New client");

Self {
base_url: base_url.to_string(),
api_key: api_key.to_string(),
client: Client::new(),
client,
}
}

Expand Down

0 comments on commit 0b71ca1

Please sign in to comment.