Skip to content

Commit

Permalink
Merge pull request #22 from dongri/endpoint-from-env
Browse files Browse the repository at this point in the history
Endpoint from env
  • Loading branch information
Dongri Jin authored Aug 3, 2023
2 parents e34bde5 + 1011c12 commit 70ab6c2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "openai-api-rs"
version = "0.1.11"
version = "0.1.12"
edition = "2021"
authors = ["Dongri Jin <[email protected]>"]
license = "MIT"
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# OpenAI API client library for Rust (unofficial)
The OpenAI API client Rust library provides convenient access to the OpenAI API from Rust applications.

Check out the [docs.rs](https://docs.rs/openai-api-rs/0.1.11/openai_api_rs/v1/index.html).
Check out the [docs.rs](https://docs.rs/openai-api-rs/).

## Installation:
Cargo.toml
```toml
[dependencies]
openai-api-rs = "0.1.11"
openai-api-rs = "0.1"
```

## Usage
Expand All @@ -18,6 +18,11 @@ The library needs to be configured with your account's secret key, which is avai
$ export OPENAI_API_KEY=sk-xxxxxxx
```

### Set OPENAI_API_ENDPOINT to environment variable (optional)
```bash
$ export OPENAI_API_ENDPOINT=https://api.openai.com/v1
```

### Create client
```rust
use openai_api_rs::v1::api::Client;
Expand Down
4 changes: 3 additions & 1 deletion src/v1/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ pub struct Client {

impl Client {
pub fn new(api_key: String) -> Self {
Self::new_with_endpoint(API_URL_V1.to_owned(), api_key)
let endpoint =
std::env::var("OPENAI_API_ENDPOINT").unwrap_or_else(|_| API_URL_V1.to_owned());
Self::new_with_endpoint(endpoint, api_key)
}

pub fn new_with_endpoint(api_endpoint: String, api_key: String) -> Self {
Expand Down

0 comments on commit 70ab6c2

Please sign in to comment.