From 9d75f058c4ad019201e3aaa42cf59c2356aca231 Mon Sep 17 00:00:00 2001 From: Andre Popovitch Date: Thu, 26 Dec 2024 13:50:52 -0600 Subject: [PATCH] docs (#3) --- .github/workflows/ci.yml | 3 +++ Cargo.lock | 2 +- Cargo.toml | 4 ++-- README.md | 4 ++-- src/chatgpt.rs | 3 +++ src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++-- 6 files changed, 53 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 40b1157..7c0b24d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,5 +29,8 @@ jobs: - name: Run tests run: cargo test --verbose + - name: Build docs + run: cargo doc --no-deps + - name: Check semver uses: obi1kenobi/cargo-semver-checks-action@v2 diff --git a/Cargo.lock b/Cargo.lock index 5562677..aca2b15 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1183,7 +1183,7 @@ dependencies = [ [[package]] name = "tysm" -version = "0.2.9" +version = "0.2.10" dependencies = [ "dotenv", "lru", diff --git a/Cargo.toml b/Cargo.toml index e96e85b..629e424 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tysm" -version = "0.2.9" +version = "0.2.10" edition = "2021" description = "Typed OpenAI Chat Completions" license = "MIT" @@ -19,7 +19,7 @@ serde_json = "1.0.107" thiserror = "2.0.9" tynm = "0.1.10" lru = "0.12.5" -schemars = { git = "https://github.com/GREsau/schemars.git", version = "1.0.0-alpha.17", features = [ +schemars = { git = "https://github.com/GREsau/schemars.git", features = [ "preserve_order", ] } diff --git a/README.md b/README.md index 619c01e..9c00736 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ -# tysm +# tysm - Thank You So Much **Typed OpenAI Chat Completions in Rust** ## Table of Contents -- [tysm](#tysm) +- [tysm - Thank You So Much](#tysm---thank-you-so-much) - [Table of Contents](#table-of-contents) - [Usage](#usage) - [Features](#features) diff --git a/src/chatgpt.rs b/src/chatgpt.rs index ce5209d..3634721 100644 --- a/src/chatgpt.rs +++ b/src/chatgpt.rs @@ -245,6 +245,9 @@ impl ChatClient { /// It will also look in the `.env` file for an `OPENAI_API_KEY` variable (using dotenv). /// /// ```rust + /// # use tysm::ChatClient; + /// let client = ChatClient::from_env("gpt-4o").unwrap(); + /// ``` pub fn from_env(model: impl Into) -> Result { Ok(Self::new(api_key()?, model)) } diff --git a/src/lib.rs b/src/lib.rs index 44199ab..bb3527b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,46 @@ -//! A simple library to interact with the ChatGPT API. +//! # `tysm` - Thank You So Much //! -//! It uses the [schemars](https://docs.rs/schemars/latest/schemars/index.html) crate to generate a schema for the desired response type. +//! Typed OpenAI Chat Completions. +//! +//! A strongly-typed Rust client for OpenAI's ChatGPT API that enforces type-safe responses using [Structured Outputs](https://platform.openai.com/docs/guides/structured-outputs). +//! +//! This library uses the [schemars](https://docs.rs/schemars/latest/schemars/index.html) crate to generate a schema for the desired response type. It also uses [serde](https://docs.rs/serde/latest/serde/index.html) to deserialize the response into the desired type. Install these crates like so: +//! +//! 1. `cargo add serde`. +//! 2. `cargo add --git https://github.com/GREsau/schemars.git schemars` +//! +//! ## Usage +//! +//! ```rust,ignore +//! use tysm::ChatClient; +//! +//! // We want names separated into `first` and `last`. +//! #[derive(serde::Deserialize, schemars::JsonSchema)] +//! struct Name { +//! first: String, +//! last: String, +//! } +//! +//! async fn get_president_name() { +//! // Create a client. +//! // `from_env` will look for an API key under the environment +//! // variable "OPENAI_API_KEY" +//! // It will also look inside `.env` if such a file exists. +//! let client = ChatClient::from_env("gpt-4o").unwrap(); +//! +//! // Request a chat completion from OpenAI and +//! // parse the response into our `Name` struct. +//! let name: Name = client +//! .chat("Who was the first US president?") +//! .await +//! .unwrap(); +//! +//! assert_eq!(name.first, "George"); +//! assert_eq!(name.last, "Washington"); +//! } +//! ``` +//! +//! Alternative name: **T**yped **S**chema **M**agic. #![deny(missing_docs)]